Delete accounts created by LowLevelIntegrationTest performCheck()

those where created but not deleted.
This commit is contained in:
Florian Schmaus 2015-04-24 21:26:07 +02:00
parent fcc62ad131
commit 221b81a627
3 changed files with 51 additions and 36 deletions

View File

@ -60,7 +60,7 @@ public abstract class AbstractSmackLowLevelIntegrationTest extends AbstractSmack
try { try {
callback.connectionCallback(connection); callback.connectionCallback(connection);
} finally { } finally {
connection.disconnect(); IntTestUtil.disconnectAndMaybeDelete(connection, true);
} }
} }

View File

@ -16,19 +16,27 @@
*/ */
package org.igniterealtime.smack.inttest; package org.igniterealtime.smack.inttest;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; 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.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.iqregister.AccountManager; import org.jivesoftware.smackx.iqregister.AccountManager;
public class IntTestUtil { public class IntTestUtil {
private static final Logger LOGGER = Logger.getLogger(IntTestUtil.class.getName());
public static UsernameAndPassword registerAccount(XMPPConnection connection) public static UsernameAndPassword registerAccount(XMPPConnection connection)
throws NoResponseException, XMPPErrorException, NotConnectedException, throws NoResponseException, XMPPErrorException, NotConnectedException,
InterruptedException { InterruptedException {
@ -64,4 +72,42 @@ public class IntTestUtil {
this.password = password; 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();
}
}
} }

View File

@ -51,7 +51,6 @@ import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.Builder; 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 // We have checked before that every parameter, if any, is of type XMPPTCPConnection
final int numberOfConnections = testMethod.getParameterTypes().length; final int numberOfConnections = testMethod.getParameterTypes().length;
XMPPTCPConnection[] connections = null; XMPPTCPConnection[] connections = null;
@ -451,43 +450,13 @@ public class SmackIntegrationTestFramework {
} }
finally { finally {
for (int i = 0; i < numberOfConnections; ++i) { for (int i = 0; i < numberOfConnections; ++i) {
try { IntTestUtil.disconnectAndMaybeDelete(connections[i], true);
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();
} }
} }
} }
protected void disconnectAndMaybeDelete(XMPPTCPConnection connection) protected void disconnectAndMaybeDelete(XMPPTCPConnection connection) throws InterruptedException {
throws InterruptedException, XMPPException, SmackException, IOException { IntTestUtil.disconnectAndMaybeDelete(connection, config.registerAccounts);
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 SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException, protected SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException,