Initial cut of constructor re-factor (SMACK-184).
[Smack.git] / test / org / jivesoftware / smack / ReconnectionTest.java
blob87cbb76dbf67ad5f67dd87442e52256b4335ae6b
1 package org.jivesoftware.smack;
3 import org.jivesoftware.smack.test.SmackTestCase;
5 /**
6 * Tests the connection and reconnection mechanism
8 * @author Francisco Vives
9 */
11 public class ReconnectionTest extends SmackTestCase {
13 public ReconnectionTest(String arg0) {
14 super(arg0);
17 /**
18 * Tests an automatic reconnection.
19 * Simulates a connection error and then waits until gets reconnected.
22 public void testAutomaticReconnection() throws Exception {
23 XMPPConnection connection = getConnection(0);
24 XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
25 connection.addConnectionListener(listener);
27 // Simulates an error in the connection
28 connection.packetReader.notifyConnectionError(new Exception("Simulated Error"));
29 Thread.sleep(12000);
30 // After 10 seconds, the reconnection manager must reestablishes the connection
31 assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
32 true, listener.reconnected);
33 assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
34 listener.attemptsNotifications);
35 assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
36 listener.remainingSeconds);
38 // Executes some server interaction testing the connection
39 executeSomeServerInteraction(connection);
42 public void testAutomaticReconnectionWithCompression() throws Exception {
43 // Create the configuration for this new connection
44 ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
45 config.setTLSEnabled(true);
46 config.setCompressionEnabled(true);
47 config.setSASLAuthenticationEnabled(true);
49 XMPPConnection connection = new XMPPConnection(config);
50 // Connect to the server
51 connection.connect();
52 // Log into the server
53 connection.login(getUsername(0), getUsername(0), "MyOtherResource");
55 assertTrue("Failed to use compression", connection.isUsingCompression());
57 XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
58 connection.addConnectionListener(listener);
60 // Simulates an error in the connection
61 connection.packetReader.notifyConnectionError(new Exception("Simulated Error"));
62 Thread.sleep(12000);
63 // After 10 seconds, the reconnection manager must reestablishes the connection
64 assertEquals("The ConnectionListener.connectionStablished() notification was not fired",
65 true, listener.reconnected);
66 assertEquals("The ConnectionListener.reconnectingIn() notification was not fired", 10,
67 listener.attemptsNotifications);
68 assertEquals("The ReconnectionManager algorithm has reconnected without waiting until 0", 0,
69 listener.remainingSeconds);
71 // Executes some server interaction testing the connection
72 executeSomeServerInteraction(connection);
75 /**
76 * Tests a manual reconnection.
77 * Simulates a connection error, disables the reconnection mechanism and then reconnects.
79 public void testManualReconnectionWithCancelation() throws Exception {
80 XMPPConnection connection = getConnection(0);
81 XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
82 connection.addConnectionListener(listener);
84 // Produces a connection error
85 connection.packetReader.notifyConnectionError(new Exception("Simulated Error"));
86 assertEquals(
87 "An error occurs but the ConnectionListener.connectionClosedOnError(e) was not notified",
88 true, listener.connectionClosedOnError);
89 Thread.sleep(1000);
90 // Cancels the automatic reconnection
91 connection.getConfiguration().setReconnectionAllowed(false);
92 // Waits for a reconnection that must not happened.
93 Thread.sleep(10500);
94 // Cancels the automatic reconnection
95 assertEquals("The connection was stablished but it was not allowed to", false,
96 listener.reconnected);
98 // Makes a manual reconnection from an error terminated connection without reconnection
99 connection.connect();
101 // Executes some server interaction testing the connection
102 executeSomeServerInteraction(connection);
106 * Tests a manual reconnection after a login.
107 * Closes the connection and then reconnects.
109 public void testCloseAndManualReconnection() throws Exception {
110 XMPPConnection connection = getConnection(0);
111 String username = connection.getConfiguration().getUsername();
112 String password = connection.getConfiguration().getPassword();
113 XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
114 connection.addConnectionListener(listener);
116 // Produces a normal disconnection
117 connection.disconnect();
118 assertEquals("ConnectionListener.connectionClosed() was not notified",
119 true, listener.connectionClosed);
120 // Waits 10 seconds waiting for a reconnection that must not happened.
121 Thread.sleep(12200);
122 assertEquals("The connection was stablished but it was not allowed to", false,
123 listener.reconnected);
125 // Makes a manual reconnection
126 connection.connect();
127 connection.login(username, password);
129 // Executes some server interaction testing the connection
130 executeSomeServerInteraction(connection);
134 * Tests a reconnection in a anonymously logged connection.
135 * Closes the connection and then reconnects.
137 public void testAnonymousReconnection() throws Exception {
138 XMPPConnection connection = createConnection();
139 connection.connect();
140 XMPPConnectionTestListener listener = new XMPPConnectionTestListener();
141 connection.addConnectionListener(listener);
143 // Makes the anounymous login
144 connection.loginAnonymously();
146 // Produces a normal disconnection
147 connection.disconnect();
148 assertEquals("ConnectionListener.connectionClosed() was not notified",
149 true, listener.connectionClosed);
150 // Makes a manual reconnection
151 connection.connect();
152 connection.loginAnonymously();
153 assertEquals("Failed the manual connection", true, connection.isAnonymous());
156 private XMPPConnection createXMPPConnection() throws Exception {
157 XMPPConnection connection;
158 // Create the configuration
159 ConnectionConfiguration config = new ConnectionConfiguration(getHost(), getPort());
160 config.setTLSEnabled(true);
161 config.setCompressionEnabled(Boolean.getBoolean("test.compressionEnabled"));
162 config.setSASLAuthenticationEnabled(true);
163 connection = new XMPPConnection(config);
165 return connection;
169 * Execute some server interaction in order to test that the regenerated connection works fine.
171 private void executeSomeServerInteraction(XMPPConnection connection) throws XMPPException {
172 PrivacyListManager privacyManager = PrivacyListManager.getInstanceFor(connection);
173 privacyManager.getPrivacyLists();
176 protected int getMaxConnections() {
177 return 1;
180 private class XMPPConnectionTestListener implements ConnectionListener {
182 // Variables to support listener notifications verification
183 private boolean connectionClosed = false;
184 private boolean connectionClosedOnError = false;
185 private boolean reconnected = false;
186 private boolean reconnectionFailed = false;
187 private int remainingSeconds = 0;
188 private int attemptsNotifications = 0;
189 private boolean reconnectionCanceled = false;
192 * Methods to test the listener.
194 public void connectionClosed() {
195 connectionClosed = true;
198 public void connectionClosedOnError(Exception e) {
199 connectionClosedOnError = true;
202 public void reconnectionCanceled() {
203 reconnectionCanceled = true;
206 public void reconnectingIn(int seconds) {
207 attemptsNotifications = attemptsNotifications + 1;
208 remainingSeconds = seconds;
212 public void reconnectionSuccessful() {
213 reconnected = true;
216 public void reconnectionFailed(Exception error) {
217 reconnectionFailed = true;