From 091015aa4bc8ff22d0af54d1efb526a4da3b284d Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 14 Feb 2013 11:43:41 +0000 Subject: [PATCH] SMACK-358: Support account creation parameters for LoginTest.testLoginWithNoResource() git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@13479 b35dd754-fafc-0310-a699-88a17e54d16e --- test/config/test-case.xml | 8 ++++++ test/org/jivesoftware/smack/LoginTest.java | 24 ++++++++++++----- .../smack/test/SmackTestCase.java | 27 ++++++++++++++++++- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/test/config/test-case.xml b/test/config/test-case.xml index c50ed47b2..0b4624609 100644 --- a/test/config/test-case.xml +++ b/test/config/test-case.xml @@ -13,4 +13,12 @@ chat conference + + false + + + + diff --git a/test/org/jivesoftware/smack/LoginTest.java b/test/org/jivesoftware/smack/LoginTest.java index 21a6ecd35..39f30862d 100644 --- a/test/org/jivesoftware/smack/LoginTest.java +++ b/test/org/jivesoftware/smack/LoginTest.java @@ -67,6 +67,8 @@ public class LoginTest extends SmackTestCase { * Check that the server handles anonymous users correctly. */ public void testSASLAnonymousLogin() { + if (!isTestAnonymousLogin()) return; + try { XMPPConnection conn1 = createConnection(); XMPPConnection conn2 = createConnection(); @@ -84,8 +86,7 @@ public class LoginTest extends SmackTestCase { assertNotNull("Username is null", StringUtils.parseName(conn2.getUser())); } catch (XMPPException e) { - e.printStackTrace(); - //fail(e.getMessage()); + fail(e.getMessage()); } finally { // Close the connection @@ -103,6 +104,8 @@ public class LoginTest extends SmackTestCase { * Check that the server handles anonymous users correctly. */ public void testNonSASLAnonymousLogin() { + if (!isTestAnonymousLogin()) return; + try { ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort()); config.setSASLAuthenticationEnabled(false); @@ -126,7 +129,6 @@ public class LoginTest extends SmackTestCase { assertNotNull("Username is null", StringUtils.parseName(conn2.getUser())); } catch (XMPPException e) { - e.printStackTrace(); fail(e.getMessage()); } // Close the connection @@ -147,12 +149,16 @@ public class LoginTest extends SmackTestCase { XMPPConnection conn = createConnection(); conn.connect(); try { - conn.getAccountManager().createAccount("user_1", "user_1"); + conn.getAccountManager().createAccount("user_1", "user_1", getAccountCreationParameters()); } catch (XMPPException e) { - // Do nothing if the accout already exists + // Do nothing if the account already exists if (e.getXMPPError().getCode() != 409) { throw e; } + // Else recreate the connection, ins case the server closed it as + // a result of the error, so we can login. + conn = createConnection(); + conn.connect(); } conn.login("user_1", "user_1", (String) null); if (conn.getSASLAuthentication().isAuthenticated()) { @@ -163,11 +169,15 @@ public class LoginTest extends SmackTestCase { conn.disconnect(); } else { - fail("User with no resource was able to log into the server"); + fail("User with no resource was not able to log into the server"); } } catch (XMPPException e) { - assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode()); + if (e.getXMPPError() != null) { + assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode()); + } else { + fail(e.getMessage()); + } } } diff --git a/test/org/jivesoftware/smack/test/SmackTestCase.java b/test/org/jivesoftware/smack/test/SmackTestCase.java index 6587a666a..d28aee83f 100644 --- a/test/org/jivesoftware/smack/test/SmackTestCase.java +++ b/test/org/jivesoftware/smack/test/SmackTestCase.java @@ -23,7 +23,9 @@ import java.io.InputStream; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.net.SocketFactory; @@ -58,6 +60,8 @@ public abstract class SmackTestCase extends TestCase { private int port = 5222; private String usernamePrefix = "user"; private String passwordPrefix; + private boolean testAnonymousLogin = false; + private Map accountCreationParameters = new HashMap(); private boolean samePassword; private List createdUserIdx = new ArrayList(); @@ -435,9 +439,23 @@ public abstract class SmackTestCase extends TestCase { usernamePrefix = parser.nextText(); } else if (parser.getName().equals("password")) { - samePassword = "true".equals(parser.getAttributeValue(0)); + samePassword = "true".equals(parser.getAttributeValue(0)); passwordPrefix = parser.nextText(); } + else if (parser.getName().equals("testAnonymousLogin")) { + testAnonymousLogin = "true".equals(parser.nextText()); + } + else if (parser.getName().equals("accountCreationParameters")) { + int numAttributes = parser.getAttributeCount(); + String key = null; + String value = null; + + for (int i = 0; i < numAttributes; i++) { + key = parser.getAttributeName(i); + value = parser.getAttributeValue(i); + accountCreationParameters.put(key, value); + } + } } eventType = parser.next(); } @@ -498,4 +516,11 @@ public abstract class SmackTestCase extends TestCase { } } + public boolean isTestAnonymousLogin() { + return testAnonymousLogin; + } + + public Map getAccountCreationParameters() { + return accountCreationParameters; + } }