AbstractSmackSpecificLowLevelIntegrationTest.java

  1. /**
  2.  *
  3.  * Copyright 2018-2021 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.util.ArrayList;
  19. import java.util.List;

  20. import org.jivesoftware.smack.AbstractXMPPConnection;
  21. import org.jivesoftware.smack.ConnectionConfiguration;
  22. import org.jivesoftware.smack.SmackException.NoResponseException;
  23. import org.jivesoftware.smack.SmackException.NotConnectedException;
  24. import org.jivesoftware.smack.XMPPException.XMPPErrorException;

  25. public abstract class AbstractSmackSpecificLowLevelIntegrationTest<C extends AbstractXMPPConnection>
  26.         extends AbstractSmackLowLevelIntegrationTest {

  27.     private final SmackIntegrationTestEnvironment environment;

  28.     private final XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor;

  29.     public AbstractSmackSpecificLowLevelIntegrationTest(SmackIntegrationTestEnvironment environment,
  30.             Class<C> connectionClass) {
  31.         super(environment);
  32.         this.environment = environment;

  33.         connectionDescriptor = environment.connectionManager.getConnectionDescriptorFor(connectionClass);
  34.         if (connectionDescriptor == null) {
  35.             throw new IllegalStateException("No connection descriptor for " + connectionClass + " known");
  36.         }
  37.     }

  38.     public XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> getConnectionDescriptor() {
  39.         return connectionDescriptor;
  40.     }

  41.     protected C getSpecificUnconnectedConnection() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  42.         return environment.connectionManager.constructConnection(connectionDescriptor);
  43.     }

  44.     protected List<C> getSpecificUnconnectedConnections(int count)
  45.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  46.         List<C> connections = new ArrayList<>(count);
  47.         for (int i = 0; i < count; i++) {
  48.             C connection = getSpecificUnconnectedConnection();
  49.             connections.add(connection);
  50.         }
  51.         return connections;
  52.     }
  53. }