AbstractSmackIntTest.java

  1. /**
  2.  *
  3.  * Copyright 2015-2024 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.igniterealtime.smack.inttest;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.net.HttpURLConnection;
  21. import java.net.URL;
  22. import java.util.List;
  23. import java.util.Random;
  24. import java.util.concurrent.TimeoutException;
  25. import java.util.logging.Logger;

  26. import javax.net.ssl.HttpsURLConnection;

  27. import org.jivesoftware.smack.SmackException.NoResponseException;
  28. import org.jivesoftware.smack.SmackException.NotConnectedException;
  29. import org.jivesoftware.smack.StanzaCollector;
  30. import org.jivesoftware.smack.XMPPConnection;
  31. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  32. import org.jivesoftware.smack.filter.StanzaFilter;

  33. import org.igniterealtime.smack.inttest.util.MultiResultSyncPoint;
  34. import org.igniterealtime.smack.inttest.util.ResultSyncPoint;

  35. import org.opentest4j.AssertionFailedError;

  36. public abstract class AbstractSmackIntTest {

  37.     protected static final Logger LOGGER = Logger.getLogger(AbstractSmackIntTest.class.getName());

  38.     protected static final Random INSECURE_RANDOM = new Random();

  39.     protected final String testRunId;

  40.     protected final long timeout;

  41.     protected final Configuration sinttestConfiguration;

  42.     protected AbstractSmackIntTest(SmackIntegrationTestEnvironment environment) {
  43.         this.testRunId = environment.testRunId;
  44.         this.sinttestConfiguration = environment.configuration;
  45.         this.timeout = environment.configuration.replyTimeout;
  46.     }

  47.     protected void performActionAndWaitUntilStanzaReceived(Runnable action, XMPPConnection connection, StanzaFilter filter)
  48.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  49.         StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter(
  50.                         filter).setSize(1);
  51.         try (StanzaCollector collector = connection.createStanzaCollector(configuration)) {
  52.             action.run();
  53.             collector.nextResultOrThrow(timeout);
  54.         }
  55.     }

  56.     protected void waitUntilTrue(Condition condition) throws TimeoutException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  57.         final long deadline = System.currentTimeMillis() + timeout;
  58.         do {
  59.             if (condition.evaluate()) {
  60.                 return;
  61.             }
  62.             Thread.sleep(15);
  63.         } while (System.currentTimeMillis() <= deadline);
  64.         throw new TimeoutException("Timeout waiting for condition to become true. Timeout was " + timeout + " ms.");
  65.     }

  66.     protected interface Condition {
  67.         boolean evaluate() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException;
  68.     }

  69.     protected File createNewTempFile() throws IOException {
  70.         File file = File.createTempFile("smack-integration-test-" + testRunId + "-temp-file", null);
  71.         file.deleteOnExit();
  72.         return file;
  73.     }

  74.     protected HttpURLConnection getHttpUrlConnectionFor(URL url) throws IOException {
  75.         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  76.         if (sinttestConfiguration.sslContextFactory != null && urlConnection instanceof HttpsURLConnection) {
  77.             HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
  78.             httpsUrlConnection.setSSLSocketFactory(sinttestConfiguration.sslContextFactory.createSslContext().getSocketFactory());
  79.         }
  80.         return urlConnection;
  81.     }

  82.     public <R> R assertResult(ResultSyncPoint<R, ?> syncPoint, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
  83.         return assertResult(syncPoint, timeout, message);
  84.     }

  85.     public static <R> R assertResult(ResultSyncPoint<R, ?> syncPoint, long timeout, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
  86.         try {
  87.             return syncPoint.waitForResult(timeout, message);
  88.         } catch (InterruptedException | TimeoutException e) {
  89.             throw e;
  90.         } catch (Exception e) {
  91.             throw new AssertionFailedError(message, e);
  92.         }
  93.     }

  94.     public <R> List<R> assertResult(MultiResultSyncPoint<R, ?> syncPoint, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
  95.         return assertResult(syncPoint, timeout, message);
  96.     }

  97.     public static <R> List<R> assertResult(MultiResultSyncPoint<R, ?> syncPoint, long timeout, String message) throws InterruptedException, TimeoutException, AssertionFailedError {
  98.         try {
  99.             return syncPoint.waitForResults(timeout, message);
  100.         } catch (InterruptedException | TimeoutException e) {
  101.             throw e;
  102.         } catch (Exception e) {
  103.             throw new AssertionFailedError(message, e);
  104.         }
  105.     }
  106. }