Search in sources :

Example 1 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project Smack by igniterealtime.

the class IoT method iotScenario.

public static void iotScenario(String dataThingJidString, String dataThingPassword, String readingThingJidString, String readingThingPassword, IotScenario scenario) throws TimeoutException, Exception {
    final EntityBareJid dataThingJid = JidCreate.entityBareFrom(dataThingJidString);
    final EntityBareJid readingThingJid = JidCreate.entityBareFrom(readingThingJidString);
    final XMPPTCPConnectionConfiguration dataThingConnectionConfiguration = XMPPTCPConnectionConfiguration.builder().setUsernameAndPassword(dataThingJid.getLocalpart(), dataThingPassword).setXmppDomain(dataThingJid.asDomainBareJid()).setSecurityMode(SecurityMode.disabled).setDebuggerEnabled(true).build();
    final XMPPTCPConnectionConfiguration readingThingConnectionConfiguration = XMPPTCPConnectionConfiguration.builder().setUsernameAndPassword(readingThingJid.getLocalpart(), readingThingPassword).setXmppDomain(readingThingJid.asDomainBareJid()).setSecurityMode(SecurityMode.disabled).setDebuggerEnabled(true).build();
    final XMPPTCPConnection dataThingConnection = new XMPPTCPConnection(dataThingConnectionConfiguration);
    final XMPPTCPConnection readingThingConnection = new XMPPTCPConnection(readingThingConnectionConfiguration);
    dataThingConnection.setReplyTimeout(TIMEOUT);
    readingThingConnection.setReplyTimeout(TIMEOUT);
    dataThingConnection.setUseStreamManagement(false);
    readingThingConnection.setUseStreamManagement(false);
    try {
        dataThingConnection.connect().login();
        readingThingConnection.connect().login();
        scenario.iotScenario(dataThingConnection, readingThingConnection);
    } finally {
        dataThingConnection.disconnect();
        readingThingConnection.disconnect();
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) EntityBareJid(org.jxmpp.jid.EntityBareJid)

Example 2 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project Smack by igniterealtime.

the class SmackIntegrationTestFramework method runTests.

@SuppressWarnings({ "unchecked", "Finally" })
private void runTests(Set<Class<? extends AbstractSmackIntTest>> classes) throws NoResponseException, InterruptedException {
    for (Class<? extends AbstractSmackIntTest> testClass : classes) {
        final String testClassName = testClass.getName();
        if (config.enabledTests != null && !isInSet(testClass, config.enabledTests)) {
            LOGGER.info("Skipping test class " + testClassName + " because it is not enabled");
            continue;
        }
        if (isInSet(testClass, config.disabledTests)) {
            LOGGER.info("Skipping test class " + testClassName + " because it is disalbed");
            continue;
        }
        TestType testType;
        if (AbstractSmackLowLevelIntegrationTest.class.isAssignableFrom(testClass)) {
            testType = TestType.LowLevel;
        } else if (AbstractSmackIntegrationTest.class.isAssignableFrom(testClass)) {
            testType = TestType.Normal;
        } else {
            throw new AssertionError();
        }
        List<Method> smackIntegrationTestMethods = new LinkedList<>();
        for (Method method : testClass.getMethods()) {
            if (!method.isAnnotationPresent(SmackIntegrationTest.class)) {
                continue;
            }
            Class<?> retClass = method.getReturnType();
            if (!(retClass.equals(Void.TYPE))) {
                LOGGER.warning("SmackIntegrationTest annotation on method that does not return void");
                continue;
            }
            final Class<?>[] parameterTypes = method.getParameterTypes();
            switch(testType) {
                case Normal:
                    if (method.getParameterTypes().length > 0) {
                        LOGGER.warning("SmackIntegrationTest annotaton on method that takes arguments ");
                        continue;
                    }
                    break;
                case LowLevel:
                    for (Class<?> parameterType : parameterTypes) {
                        if (!parameterType.isAssignableFrom(XMPPTCPConnection.class)) {
                            LOGGER.warning("SmackIntegrationTest low-level test method declares parameter that is not of type XMPPTCPConnection");
                        }
                    }
                    break;
            }
            smackIntegrationTestMethods.add(method);
        }
        if (smackIntegrationTestMethods.isEmpty()) {
            LOGGER.warning("No integration test methods found");
            continue;
        }
        Iterator<Method> it = smackIntegrationTestMethods.iterator();
        while (it.hasNext()) {
            final Method method = it.next();
            final String methodName = method.getName();
            if (config.enabledTests != null && !(config.enabledTests.contains(methodName) || isInSet(testClass, config.enabledTests))) {
                LOGGER.fine("Skipping test method " + methodName + " because it is not enabled");
                it.remove();
                continue;
            }
            if (config.disabledTests != null && config.disabledTests.contains(methodName)) {
                LOGGER.info("Skipping test method " + methodName + " because it is disabled");
                it.remove();
                continue;
            }
        }
        if (smackIntegrationTestMethods.isEmpty()) {
            LOGGER.info("All tests in " + testClassName + " are disabled");
            continue;
        }
        final int detectedTestMethodsCount = smackIntegrationTestMethods.size();
        testRunResult.numberOfAvailableTests.addAndGet(detectedTestMethodsCount);
        testRunResult.numberOfPossibleTests.addAndGet(detectedTestMethodsCount);
        AbstractSmackIntTest test;
        switch(testType) {
            case Normal:
                {
                    Constructor<? extends AbstractSmackIntegrationTest> cons;
                    try {
                        cons = ((Class<? extends AbstractSmackIntegrationTest>) testClass).getConstructor(SmackIntegrationTestEnvironment.class);
                    } catch (NoSuchMethodException | SecurityException e) {
                        LOGGER.log(Level.WARNING, "Smack Integration Test class could not get constructed (public Con)structor(SmackIntegrationTestEnvironment) missing?)", e);
                        continue;
                    }
                    try {
                        test = cons.newInstance(environment);
                    } catch (InvocationTargetException e) {
                        Throwable cause = e.getCause();
                        if (cause instanceof TestNotPossibleException) {
                            testRunResult.impossibleTestClasses.put(testClass, cause.getMessage());
                            testRunResult.numberOfPossibleTests.addAndGet(-detectedTestMethodsCount);
                        } else {
                            throwFatalException(cause);
                            LOGGER.log(Level.WARNING, "Could not construct test class", e);
                        }
                        continue;
                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException e) {
                        LOGGER.log(Level.WARNING, "todo", e);
                        continue;
                    }
                }
                break;
            case LowLevel:
                {
                    Constructor<? extends AbstractSmackLowLevelIntegrationTest> cons;
                    try {
                        cons = ((Class<? extends AbstractSmackLowLevelIntegrationTest>) testClass).getConstructor(SmackIntegrationTestEnvironment.class);
                    } catch (NoSuchMethodException | SecurityException e) {
                        LOGGER.log(Level.WARNING, "Smack Integration Test class could not get constructed (public Con)structor(SmackIntegrationTestEnvironment) missing?)", e);
                        continue;
                    }
                    try {
                        test = cons.newInstance(environment);
                    } catch (InvocationTargetException e) {
                        Throwable cause = e.getCause();
                        if (cause instanceof TestNotPossibleException) {
                            testRunResult.impossibleTestClasses.put(testClass, cause.getMessage());
                            testRunResult.numberOfPossibleTests.addAndGet(-detectedTestMethodsCount);
                        } else {
                            throwFatalException(cause);
                            LOGGER.log(Level.WARNING, "Could not construct test class", e);
                        }
                        continue;
                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException e) {
                        LOGGER.log(Level.WARNING, "todo", e);
                        continue;
                    }
                }
                break;
            default:
                throw new AssertionError();
        }
        try {
            // Run the @BeforeClass methods (if any)
            Set<Method> beforeClassMethods = getAllMethods(testClass, withAnnotation(BeforeClass.class), withReturnType(Void.TYPE), withParametersCount(0), withModifier(Modifier.PUBLIC));
            // See if there are any methods that have the @BeforeClassAnnotation but a wrong signature
            Set<Method> allBeforeClassMethods = getAllMethods(testClass, withAnnotation(BeforeClass.class));
            allBeforeClassMethods.removeAll(beforeClassMethods);
            if (!allBeforeClassMethods.isEmpty()) {
                throw new IllegalArgumentException("@BeforeClass methods with wrong signature found");
            }
            if (beforeClassMethods.size() == 1) {
                Method beforeClassMethod = beforeClassMethods.iterator().next();
                LOGGER.info("Executing @BeforeClass method of " + testClass);
                try {
                    beforeClassMethod.invoke(test);
                } catch (InvocationTargetException | IllegalAccessException e) {
                    LOGGER.log(Level.SEVERE, "Exception executing @BeforeClass method", e);
                } catch (IllegalArgumentException e) {
                    throw new AssertionError(e);
                }
            } else if (beforeClassMethods.size() > 1) {
                throw new IllegalArgumentException("Only one @BeforeClass method allowed");
            }
            for (Method testMethod : smackIntegrationTestMethods) {
                final String testPrefix = testClass.getSimpleName() + '.' + testMethod.getName() + " (" + testType + "): ";
                // Invoke all test methods on the test instance
                LOGGER.info(testPrefix + "Start");
                long testStart = System.currentTimeMillis();
                try {
                    switch(testType) {
                        case Normal:
                            testMethod.invoke(test);
                            break;
                        case LowLevel:
                            invokeLowLevel(testMethod, test);
                            break;
                    }
                    LOGGER.info(testPrefix + "Success");
                    long testEnd = System.currentTimeMillis();
                    testRunResult.successfulTests.add(new SuccessfulTest(testMethod, testStart, testEnd, null));
                } catch (InvocationTargetException e) {
                    long testEnd = System.currentTimeMillis();
                    Throwable cause = e.getCause();
                    if (cause instanceof TestNotPossibleException) {
                        LOGGER.info(testPrefix + "Not possible");
                        testRunResult.impossibleTestMethods.add(new TestNotPossible(testMethod, testStart, testEnd, null, (TestNotPossibleException) cause));
                        continue;
                    }
                    Throwable nonFatalFailureReason;
                    // thrown up, as it would be done by throwFatalException()
                    if (cause instanceof AssertionError) {
                        nonFatalFailureReason = cause;
                    } else {
                        nonFatalFailureReason = throwFatalException(cause);
                    }
                    // An integration test failed
                    testRunResult.failedIntegrationTests.add(new FailedTest(testMethod, testStart, testEnd, null, nonFatalFailureReason));
                    LOGGER.log(Level.SEVERE, testPrefix + "Failed", e);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new AssertionError(e);
                }
            }
        } finally {
            // Run the @AfterClass method (if any)
            Set<Method> afterClassMethods = getAllMethods(testClass, withAnnotation(AfterClass.class), withReturnType(Void.TYPE), withParametersCount(0), withModifier(Modifier.PUBLIC));
            // See if there are any methods that have the @AfterClassAnnotation but a wrong signature
            Set<Method> allAfterClassMethods = getAllMethods(testClass, withAnnotation(AfterClass.class));
            allAfterClassMethods.removeAll(afterClassMethods);
            if (!allAfterClassMethods.isEmpty()) {
                throw new IllegalArgumentException("@AfterClass methods with wrong signature found");
            }
            if (afterClassMethods.size() == 1) {
                Method afterClassMethod = afterClassMethods.iterator().next();
                LOGGER.info("Executing @AfterClass method of " + testClass);
                try {
                    afterClassMethod.invoke(test);
                } catch (InvocationTargetException | IllegalAccessException e) {
                    LOGGER.log(Level.SEVERE, "Exception executing @AfterClass method", e);
                } catch (IllegalArgumentException e) {
                    throw new AssertionError(e);
                }
            } else if (afterClassMethods.size() > 1) {
                throw new IllegalArgumentException("Only one @AfterClass method allowed");
            }
        }
    }
}
Also used : BeforeClass(org.junit.BeforeClass) XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass)

Example 3 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project Smack by igniterealtime.

the class SmackIntegrationTestFramework method prepareEnvironment.

protected SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException, IOException, XMPPException, InterruptedException, KeyManagementException, NoSuchAlgorithmException {
    XMPPTCPConnection conOne = null;
    XMPPTCPConnection conTwo = null;
    XMPPTCPConnection conThree = null;
    try {
        conOne = getConnectedConnectionFor(AccountNum.One);
        conTwo = getConnectedConnectionFor(AccountNum.Two);
        conThree = getConnectedConnectionFor(AccountNum.Three);
    } catch (Exception e) {
        // TODO Reverse the order, i.e. conThree should be disconnected first.
        if (conOne != null) {
            conOne.disconnect();
        }
        if (conTwo != null) {
            conTwo.disconnect();
        }
        if (conThree != null) {
            conThree.disconnect();
        }
        throw e;
    }
    return new SmackIntegrationTestEnvironment(conOne, conTwo, conThree, testRunResult.testRunId, config);
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SmackException(org.jivesoftware.smack.SmackException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException)

Example 4 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project Smack by igniterealtime.

the class SmackIntegrationTestFramework method getConnectedConnection.

static XMPPTCPConnection getConnectedConnection(SmackIntegrationTestEnvironment environment, int connectionId) throws KeyManagementException, NoSuchAlgorithmException, InterruptedException, SmackException, IOException, XMPPException {
    Configuration config = environment.configuration;
    XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
    if (config.tlsContext != null) {
        builder.setCustomSSLContext(config.tlsContext);
    }
    builder.setSecurityMode(config.securityMode);
    builder.setXmppDomain(config.service);
    XMPPTCPConnection connection = new XMPPTCPConnection(builder.build());
    connection.connect();
    UsernameAndPassword uap = IntTestUtil.registerAccount(connection, environment, connectionId);
    connection.login(uap.username, uap.password);
    return connection;
}
Also used : SmackConfiguration(org.jivesoftware.smack.SmackConfiguration) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration) XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) Builder(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder) UsernameAndPassword(org.igniterealtime.smack.inttest.IntTestUtil.UsernameAndPassword) XMPPTCPConnectionConfiguration(org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration)

Example 5 with XMPPTCPConnection

use of org.jivesoftware.smack.tcp.XMPPTCPConnection in project Smack by igniterealtime.

the class SmackIntegrationTestFramework method invokeLowLevel.

private void invokeLowLevel(Method testMethod, AbstractSmackIntTest test) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InterruptedException {
    // We have checked before that every parameter, if any, is of type XMPPTCPConnection
    final int numberOfConnections = testMethod.getParameterTypes().length;
    XMPPTCPConnection[] connections = null;
    try {
        if (numberOfConnections > 0 && !config.isAccountRegistrationPossible()) {
            throw new TestNotPossibleException("Must create accounts for this test, but it's not enabled");
        }
        connections = new XMPPTCPConnection[numberOfConnections];
        for (int i = 0; i < numberOfConnections; ++i) {
            connections[i] = getConnectedConnection(environment, i);
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        // Behave like this was an InvocationTargetException
        throw new InvocationTargetException(e);
    }
    try {
        testMethod.invoke(test, (Object[]) connections);
    } finally {
        for (int i = 0; i < numberOfConnections; ++i) {
            IntTestUtil.disconnectAndMaybeDelete(connections[i], config);
        }
    }
}
Also used : XMPPTCPConnection(org.jivesoftware.smack.tcp.XMPPTCPConnection) SmackException(org.jivesoftware.smack.SmackException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)13 XMPPTCPConnectionConfiguration (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration)6 KeyManagementException (java.security.KeyManagementException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 SmackException (org.jivesoftware.smack.SmackException)4 XMPPException (org.jivesoftware.smack.XMPPException)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 SSLContext (javax.net.ssl.SSLContext)3 UsernameAndPassword (org.igniterealtime.smack.inttest.IntTestUtil.UsernameAndPassword)2 ConnectionConfiguration (org.jivesoftware.smack.ConnectionConfiguration)2 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)2 Builder (org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder)2 AccountRosterListener (com.xabber.android.data.roster.AccountRosterListener)1 MemorizingTrustManager (de.duenndns.ssl.MemorizingTrustManager)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 SmackIntegrationTest (org.igniterealtime.smack.inttest.SmackIntegrationTest)1 SmackConfiguration (org.jivesoftware.smack.SmackConfiguration)1