1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-13 02:59:39 +02:00

Added reconnection support. SMACK-172

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@5367 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2006-09-14 19:16:40 +00:00 committed by gato
parent 96e4201f61
commit 3af86fd462
22 changed files with 501 additions and 194 deletions

View file

@ -32,10 +32,11 @@ public class LoginTest extends SmackTestCase {
public void testInvalidLogin() {
try {
XMPPConnection connection = new XMPPConnection(getHost(), getPort());
connection.connect();
try {
// Login with an invalid user
connection.login("invaliduser" , "invalidpass");
connection.close();
connection.disconnect();
fail("Invalid user was able to log into the server");
}
catch (XMPPException e) {
@ -60,6 +61,8 @@ public class LoginTest extends SmackTestCase {
try {
XMPPConnection conn1 = new XMPPConnection(getHost(), getPort());
XMPPConnection conn2 = new XMPPConnection(getHost(), getPort());
conn1.connect();
conn2.connect();
try {
// Try to login anonymously
conn1.loginAnonymously();
@ -76,8 +79,8 @@ public class LoginTest extends SmackTestCase {
fail(e.getMessage());
}
// Close the connection
conn1.close();
conn2.close();
conn1.disconnect();
conn2.disconnect();
}
catch (Exception e) {
e.printStackTrace();
@ -93,10 +96,13 @@ public class LoginTest extends SmackTestCase {
ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
config.setSASLAuthenticationEnabled(false);
XMPPConnection conn1 = new XMPPConnection(config);
conn1.connect();
config = new ConnectionConfiguration(getHost(), getPort());
config.setSASLAuthenticationEnabled(false);
XMPPConnection conn2 = new XMPPConnection(config);
conn2.connect();
try {
// Try to login anonymously
conn1.loginAnonymously();
@ -113,8 +119,8 @@ public class LoginTest extends SmackTestCase {
fail(e.getMessage());
}
// Close the connection
conn1.close();
conn2.close();
conn1.disconnect();
conn2.disconnect();
}
catch (Exception e) {
e.printStackTrace();
@ -128,6 +134,7 @@ public class LoginTest extends SmackTestCase {
public void testLoginWithNoResource() {
try {
XMPPConnection conn = new XMPPConnection(getHost(), getPort());
conn.connect();
try {
conn.getAccountManager().createAccount("user_1", "user_1");
} catch (XMPPException e) {
@ -142,7 +149,7 @@ public class LoginTest extends SmackTestCase {
assertNotNull("JID assigned by server is missing", conn.getUser());
assertNotNull("JID assigned by server does not have a resource",
StringUtils.parseResource(conn.getUser()));
conn.close();
conn.disconnect();
}
else {
fail("User with no resource was able to log into the server");

View file

@ -86,6 +86,7 @@ public class MessengerLoginTest extends TestCase {
try {
XMPPConnection con = new XMPPConnection(host, port);
con.connect();
con.login(username, password, resource);
}
catch (XMPPException e) {

View file

@ -37,6 +37,7 @@ public class PresenceTest extends SmackTestCase {
try {
// User_1 will log in again using another resource
conn = new XMPPConnection(getHost(), getPort());
conn.connect();
conn.login(getUsername(1), getUsername(1), "OtherPlace");
// Change the presence priorities of User_1
getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 1,
@ -71,8 +72,7 @@ public class PresenceTest extends SmackTestCase {
chat2.nextMessage(1000));
// User_1 closes his connection
chat2 = null;
conn.close();
conn.disconnect();
Thread.sleep(150);
// Test delivery of message to the unique presence of the user_1
@ -85,6 +85,7 @@ public class PresenceTest extends SmackTestCase {
// User_1 will log in again using another resource
conn = new XMPPConnection(getHost(), getPort());
conn.connect();
conn.login(getUsername(1), getUsername(1), "OtherPlace");
conn.sendPacket(new Presence(Presence.Type.available, null, 1,
Presence.Mode.available));
@ -119,7 +120,7 @@ public class PresenceTest extends SmackTestCase {
}
finally {
if (conn != null) {
conn.close();
conn.disconnect();
}
}
}
@ -128,6 +129,7 @@ public class PresenceTest extends SmackTestCase {
* User1 logs from 2 resources but only one is available. User0 sends a message
* to the full JID of the unavailable resource. User1 in the not available resource
* should receive the message.
* TODO Fix this in Wildfire but before check if XMPP spec requests this feature
*/
public void testNotAvailablePresence() throws XMPPException {
// Change the presence to unavailable of User_1
@ -135,6 +137,7 @@ public class PresenceTest extends SmackTestCase {
// User_1 will log in again using another resource (that is going to be available)
XMPPConnection conn = new XMPPConnection(getHost(), getPort());
conn.connect();
conn.login(getUsername(1), getUsername(1), "OtherPlace");
// Create chats between participants
@ -158,6 +161,7 @@ public class PresenceTest extends SmackTestCase {
public void testMultipleResources() throws Exception {
// Create another connection for the same user of connection 1
XMPPConnection conn4 = new XMPPConnection(getServiceName());
conn4.connect();
conn4.login(getUsername(1), getUsername(1), "Home");
// Add a new roster entry
@ -188,7 +192,7 @@ public class PresenceTest extends SmackTestCase {
assertTrue("Only one presence was found for user1", presences.hasNext());
// User1 logs out from one resource
conn4.close();
conn4.disconnect();
// Wait up to 1 second
Thread.sleep(700);

View file

@ -367,6 +367,7 @@ public class RosterTest extends SmackTestCase {
// Log in from another resource so we can test the roster
XMPPConnection con2 = new XMPPConnection(getHost(), getPort());
con2.connect();
con2.login(getUsername(0), getUsername(0), "MyNewResource");
Roster roster2 = con2.getRoster();
@ -386,7 +387,7 @@ public class RosterTest extends SmackTestCase {
assertTrue("NewGroup group was not found", groupNames.contains("NewGroup"));
// Close the new connection
con2.close();
con2.disconnect();
Thread.sleep(500);
cleanUpRoster();
@ -480,6 +481,7 @@ public class RosterTest extends SmackTestCase {
// Create another connection for the same user of connection 1
XMPPConnection conn4 = new XMPPConnection(getServiceName());
conn4.connect();
conn4.login(getUsername(1), getUsername(1), "Home");
// Add a new roster entry
@ -506,6 +508,7 @@ public class RosterTest extends SmackTestCase {
// Check that the right presence is returned for a user+resource
presence = roster.getPresenceResource(getFullJID(1));
assertNotNull("Presence not found for user " + getFullJID(1), presence);
assertEquals(
"Returned the wrong Presence",
StringUtils.parseResource(presence.getFrom()),
@ -525,7 +528,7 @@ public class RosterTest extends SmackTestCase {
assertEquals("Wrong number of returned presences", count, 2);
// Close the connection so one presence must go
conn4.close();
conn4.disconnect();
// Check that the returned presences are correct
presences = roster.getPresences(getBareJID(1));
@ -548,6 +551,7 @@ public class RosterTest extends SmackTestCase {
public void testMultipleResources() throws Exception {
// Create another connection for the same user of connection 1
XMPPConnection conn4 = new XMPPConnection(getServiceName());
conn4.connect();
conn4.login(getUsername(1), getUsername(1), "Home");
// Add a new roster entry
@ -605,6 +609,7 @@ public class RosterTest extends SmackTestCase {
// Create another connection for the same user of connection 0
XMPPConnection conn2 = new XMPPConnection(getServiceName());
conn2.connect();
conn2.login(getUsername(0), getUsername(0), "Home");
// Retrieve roster and verify that new contact is there and nickname is correct
@ -677,6 +682,46 @@ public class RosterTest extends SmackTestCase {
getConnection(2).getRoster().getGroupCount());
}
/**
* Tests the creation of a roster and then simulates abrupt termination. Cached presences
* must go offline. At reconnection, presences must go back to online.
* <ol>
* <li> Create some entries
* <li> Breack the connection
* <li> Check offline presences
* <li> Whait for automatic reconnection
* <li> Check online presences
* </ol>
*/
public void testOfflinePresencesAfterDisconnection() throws Exception {
// Add a new roster entry
Roster roster = getConnection(0).getRoster();
roster.createEntry(getBareJID(1), "gato11", null);
roster.createEntry(getBareJID(2), "gato12", null);
// Wait up to 2 seconds to let the server process presence subscriptions
long initial = System.currentTimeMillis();
while (System.currentTimeMillis() - initial < 2000 && (
roster.getPresence(getBareJID(1)) == null ||
roster.getPresence(getBareJID(2)) == null)) {
Thread.sleep(100);
}
Thread.sleep(200);
// Brakes the connection
getConnection(0).packetReader.notifyConnectionError(new Exception("Simulated Error"));
Presence presence = roster.getPresence(getBareJID(1));
assertNull("Presence should be offline after a connection termination", presence);
// Reconnection should occur in 10 seconds
Thread.sleep(12200);
presence = roster.getPresence(getBareJID(1));
assertNotNull("Presence not found for user", presence);
assertEquals("Presence should be online after a connection reconnection",
Presence.Type.available, presence.getType());
}
protected int getMaxConnections() {
return 3;
}

View file

@ -1,7 +1,7 @@
/**
* $RCSfile$
* $Revision$
* $Date: $
* $Date$
*
* Copyright 2003-2005 Jive Software.
*
@ -19,20 +19,18 @@
*/
package org.jivesoftware.smack.test;
import junit.framework.TestCase;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.xmlpull.mxp1.MXParser;
import org.xmlpull.v1.XmlPullParser;
import javax.net.SocketFactory;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import javax.net.SocketFactory;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.xmlpull.v1.*;
import org.xmlpull.mxp1.MXParser;
import junit.framework.TestCase;
/**
* Base class for all the test cases which provides a pre-configured execution context. This
* means that any test case that subclassifies this base class will have access to a pool of
@ -196,6 +194,7 @@ public abstract class SmackTestCase extends TestCase {
else {
connections[i] = new XMPPConnection(config, getSocketFactory());
}
connections[i].connect();
}
// Use the host name that the server reports. This is a good idea in most
// cases, but could fail if the user set a hostname in their XMPP server
@ -232,11 +231,12 @@ public abstract class SmackTestCase extends TestCase {
super.tearDown();
for (int i = 0; i < getMaxConnections(); i++) {
if (getConnection(i).isConnected()) {
// Delete the created account for the test
getConnection(i).getAccountManager().deleteAccount();
// Close the connection
getConnection(i).close();
getConnection(i).disconnect();
}
}
}

View file

@ -20,11 +20,10 @@
package org.jivesoftware.smackx;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.test.SmackTestCase;
/**
* Ensure that stream compression (JEP-138) is correctly supported by Smack.
@ -50,6 +49,7 @@ public class CompressionTest extends SmackTestCase {
config.setSASLAuthenticationEnabled(true);
XMPPConnection connection = new XMPPConnection(config);
connection.connect();
// Login with the test account
connection.login("user0", "user0");
@ -57,7 +57,7 @@ public class CompressionTest extends SmackTestCase {
assertTrue("Connection is not using stream compression", connection.isUsingCompression());
// Close connection
connection.close();
connection.disconnect();
}
protected int getMaxConnections() {
@ -70,6 +70,7 @@ public class CompressionTest extends SmackTestCase {
protected void setUp() throws Exception {
super.setUp();
XMPPConnection setupConnection = new XMPPConnection(getHost(), getPort());
setupConnection.connect();
if (!setupConnection.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
@ -90,11 +91,12 @@ public class CompressionTest extends SmackTestCase {
protected void tearDown() throws Exception {
super.tearDown();
XMPPConnection setupConnection = new XMPPConnection(getHost(), getPort());
setupConnection.connect();
setupConnection.login("user0", "user0");
// Delete the created account for the test
setupConnection.getAccountManager().deleteAccount();
// Close the setupConnection
setupConnection.close();
setupConnection.disconnect();
}
}

View file

@ -250,6 +250,7 @@ public class MultiUserChatTest extends SmackTestCase {
try {
// Anonymous user joins the new room
XMPPConnection anonConnection = new XMPPConnection(getHost(), getPort());
anonConnection.connect();
anonConnection.loginAnonymously();
MultiUserChat muc2 = new MultiUserChat(anonConnection, room);
muc2.join("testbot2");
@ -265,7 +266,7 @@ public class MultiUserChatTest extends SmackTestCase {
// Anonymous user leaves the room
muc2.leave();
anonConnection.close();
anonConnection.disconnect();
Thread.sleep(250);
// User1 checks the presence of Anonymous user in the room
presence = muc.getOccupantPresence(room + "/testbot2");
@ -1766,6 +1767,7 @@ public class MultiUserChatTest extends SmackTestCase {
XMPPConnection[] conns = new XMPPConnection[20];
for (int i = 0; i < conns.length; i++) {
conns[i] = new XMPPConnection(getServiceName());
conns[i].connect();
conns[i].login(getUsername(1), getUsername(1), "resource-" + i);
}
@ -1788,7 +1790,7 @@ public class MultiUserChatTest extends SmackTestCase {
// Each connection leaves the room and closes the connection
for (int i = 0; i < mucs.length; i++) {
mucs[i].leave();
conns[i].close();
conns[i].disconnect();
}
} catch (Exception e) {