From 221b81a627f3bb8509b6d916ebaa0c8b75ddb97f Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 24 Apr 2015 21:26:07 +0200 Subject: [PATCH] Delete accounts created by LowLevelIntegrationTest performCheck() those where created but not deleted. --- .../AbstractSmackLowLevelIntegrationTest.java | 2 +- .../smack/inttest/IntTestUtil.java | 46 +++++++++++++++++++ .../SmackIntegrationTestFramework.java | 39 ++-------------- 3 files changed, 51 insertions(+), 36 deletions(-) diff --git a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackLowLevelIntegrationTest.java b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackLowLevelIntegrationTest.java index ff33a2f3d..c5ce1673d 100644 --- a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackLowLevelIntegrationTest.java +++ b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/AbstractSmackLowLevelIntegrationTest.java @@ -60,7 +60,7 @@ public abstract class AbstractSmackLowLevelIntegrationTest extends AbstractSmack try { callback.connectionCallback(connection); } finally { - connection.disconnect(); + IntTestUtil.disconnectAndMaybeDelete(connection, true); } } diff --git a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/IntTestUtil.java b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/IntTestUtil.java index 35a4d8516..185b763e9 100644 --- a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/IntTestUtil.java +++ b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/IntTestUtil.java @@ -16,19 +16,27 @@ */ package org.igniterealtime.smack.inttest; +import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; +import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.iqregister.AccountManager; public class IntTestUtil { + private static final Logger LOGGER = Logger.getLogger(IntTestUtil.class.getName()); + public static UsernameAndPassword registerAccount(XMPPConnection connection) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { @@ -64,4 +72,42 @@ public class IntTestUtil { this.password = password; } } + + public static void disconnectAndMaybeDelete(XMPPTCPConnection connection, boolean delete) + throws InterruptedException { + try { + if (delete) { + final int maxAttempts = 3; + AccountManager am = AccountManager.getInstance(connection); + int attempts; + for (attempts = 0; attempts < maxAttempts; attempts++) { + try { + am.deleteAccount(); + } + catch (XMPPErrorException | NoResponseException e) { + LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e); + continue; + } + catch (NotConnectedException e) { + LOGGER.log(Level.WARNING, "Exception deleting account for " + connection, e); + try { + connection.connect().login(); + } + catch (XMPPException | SmackException | IOException e2) { + LOGGER.log(Level.WARNING, "Exception while trying to re-connect " + connection, e); + } + continue; + } + LOGGER.info("Successfully deleted account of " + connection); + break; + } + if (attempts > maxAttempts) { + LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection); + } + } + } + finally { + connection.disconnect(); + } + } } diff --git a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java index 23f68a1a9..4883f0dc8 100644 --- a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java +++ b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java @@ -51,7 +51,6 @@ import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.XMPPException; -import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder; @@ -425,7 +424,7 @@ public class SmackIntegrationTestFramework { } } - private void invokeLowLevel(Method testMethod, AbstractSmackIntTest test) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { + 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; @@ -451,43 +450,13 @@ public class SmackIntegrationTestFramework { } finally { for (int i = 0; i < numberOfConnections; ++i) { - try { - AccountManager.getInstance(connections[i]).deleteAccount(); - LOGGER.info("Successfully deleted account of" + connections[i]); - } - catch (NoResponseException | XMPPErrorException | NotConnectedException - | InterruptedException e) { - LOGGER.log(Level.SEVERE, "Could not delete account of " + connections[i], e); - } - connections[i].disconnect(); + IntTestUtil.disconnectAndMaybeDelete(connections[i], true); } } } - protected void disconnectAndMaybeDelete(XMPPTCPConnection connection) - throws InterruptedException, XMPPException, SmackException, IOException { - if (config.registerAccounts) { - final int maxAttempts = 3; - AccountManager am = AccountManager.getInstance(connection); - int attempts; - for (attempts = 0; attempts < maxAttempts; attempts++) { - try { - am.deleteAccount(); - } catch (NoResponseException | InterruptedException e) { - LOGGER.log(Level.WARNING, "Exception deleting account for " + connection , e); - continue; - } catch (NotConnectedException e) { - LOGGER.log(Level.WARNING, "Exception deleting account for " + connection , e); - connection.connect().login(); - continue; - } - break; - } - if (attempts > maxAttempts) { - LOGGER.log(Level.SEVERE, "Could not delete account for connection: " + connection); - } - } - connection.disconnect(); + protected void disconnectAndMaybeDelete(XMPPTCPConnection connection) throws InterruptedException { + IntTestUtil.disconnectAndMaybeDelete(connection, config.registerAccounts); } protected SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException,