1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-26 16:22:06 +01:00

Added #setUp and #tearDown

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2147 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2003-10-15 14:55:19 +00:00 committed by gdombiak
parent 627c46b51d
commit 45fbd2e4b7
6 changed files with 998 additions and 1099 deletions

View file

@ -67,6 +67,12 @@ import junit.framework.TestCase;
*/
public class MessageEventManagerTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/**
* Constructor for MessageEventManagerTest.
* @param name
@ -83,20 +89,6 @@ public class MessageEventManagerTest extends TestCase {
* occurs: offline, composing, displayed or delivered
*/
public void testSendMessageEventRequest() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
@ -114,12 +106,6 @@ public class MessageEventManagerTest extends TestCase {
} catch (Exception e) {
fail("An error occured sending the message");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -132,20 +118,6 @@ public class MessageEventManagerTest extends TestCase {
* 3. User_1 will display any notification that receives
*/
public void testSendMessageEventRequestAndDisplayNotifications() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
@ -185,16 +157,10 @@ public class MessageEventManagerTest extends TestCase {
try {
chat1.sendMessage(msg);
// Wait a few seconds so that the XMPP client can send any event
Thread.sleep(5000);
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -207,18 +173,6 @@ public class MessageEventManagerTest extends TestCase {
* 5. User_2 will simulate that he/she has cancelled the reply
*/
public void testRequestsAndNotifications() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
final ArrayList results = new ArrayList();
ArrayList resultsExpected = new ArrayList();
resultsExpected.add("deliveredNotificationRequested");
@ -230,13 +184,6 @@ public class MessageEventManagerTest extends TestCase {
resultsExpected.add("composingNotification");
resultsExpected.add("cancelledNotification");
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
@ -316,20 +263,57 @@ public class MessageEventManagerTest extends TestCase {
messageEventManager2.sendCancelledNotification(user1, msg.getPacketID());
// Wait half second so that the complete test can run
Thread.sleep(500);
assertTrue("Test failed due to bad results (1)" + resultsExpected, resultsExpected.containsAll(results));
assertTrue("Test failed due to bad results (2)" + results, results.containsAll(resultsExpected));
assertTrue(
"Test failed due to bad results (1)" + resultsExpected,
resultsExpected.containsAll(results));
assertTrue(
"Test failed due to bad results (2)" + results,
results.containsAll(resultsExpected));
} catch (Exception e) {
fail("An error occured sending the message");
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
}
}

View file

@ -58,7 +58,6 @@ import junit.framework.TestCase;
import org.jivesoftware.smack.*;
/**
*
* Test the Roster Exchange extension using the high level API
@ -67,6 +66,16 @@ import org.jivesoftware.smack.*;
*/
public class RosterExchangeManagerTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private XMPPConnection conn3 = null;
private XMPPConnection conn4 = null;
private String user1 = null;
private String user2 = null;
private String user3 = null;
private String user4 = null;
private int entriesSent;
private int entriesReceived;
@ -84,38 +93,14 @@ public class RosterExchangeManagerTest extends TestCase {
* 1. User_1 will send his/her roster to user_2
*/
public void testSendRoster() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Send user1's roster to user2
try {
RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1);
rosterExchangeManager.send(conn1.getRoster(), user2);
// Wait half second so that the complete test can run
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
fail("An error occured sending the roster");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -124,39 +109,15 @@ public class RosterExchangeManagerTest extends TestCase {
* 1. User_1 will send his/her RosterGroups to user_2
*/
public void testSendRosterGroup() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Send user1's RosterGroups to user2
try {
RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1);
for (Iterator it = conn1.getRoster().getGroups(); it.hasNext(); )
rosterExchangeManager.send((RosterGroup)it.next(), user2);
// Wait half second so that the complete test can run
Thread.sleep(500);
for (Iterator it = conn1.getRoster().getGroups(); it.hasNext();)
rosterExchangeManager.send((RosterGroup) it.next(), user2);
} catch (Exception e) {
e.printStackTrace();
fail("An error occured sending the roster");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -166,25 +127,6 @@ public class RosterExchangeManagerTest extends TestCase {
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/
public void testSendAndReceiveRoster() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1);
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2);
@ -193,8 +135,8 @@ public class RosterExchangeManagerTest extends TestCase {
public void entriesReceived(String from, Iterator remoteRosterEntries) {
int received = 0;
assertNotNull("From is null", from);
assertNotNull("rosterEntries is null",remoteRosterEntries);
assertTrue("Roster without entries",remoteRosterEntries.hasNext());
assertNotNull("rosterEntries is null", remoteRosterEntries);
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
while (remoteRosterEntries.hasNext()) {
received++;
RemoteRosterEntry remoteEntry = (RemoteRosterEntry) remoteRosterEntries.next();
@ -210,20 +152,15 @@ public class RosterExchangeManagerTest extends TestCase {
entriesSent = conn1.getRoster().getEntryCount();
entriesReceived = 0;
rosterExchangeManager1.send(conn1.getRoster(), user2);
// Wait for 1 second
Thread.sleep(300);
} catch (Exception e) {
fail("An error occured sending the message with the roster");
}
// Wait for 2 seconds
Thread.sleep(2000);
assertEquals("Number of sent and received entries does not match", entriesSent, entriesReceived);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
}
assertEquals(
"Number of sent and received entries does not match",
entriesSent,
entriesReceived);
}
/**
@ -233,26 +170,6 @@ public class RosterExchangeManagerTest extends TestCase {
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/
public void testSendAndAcceptRoster() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
final Roster user2_roster = conn2.getRoster();
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1);
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2);
@ -261,18 +178,18 @@ public class RosterExchangeManagerTest extends TestCase {
public void entriesReceived(String from, Iterator remoteRosterEntries) {
int received = 0;
assertNotNull("From is null", from);
assertNotNull("remoteRosterEntries is null",remoteRosterEntries);
assertTrue("Roster without entries",remoteRosterEntries.hasNext());
assertNotNull("remoteRosterEntries is null", remoteRosterEntries);
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
while (remoteRosterEntries.hasNext()) {
received++;
try {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) remoteRosterEntries.next();
user2_roster.createEntry(
RemoteRosterEntry remoteRosterEntry =
(RemoteRosterEntry) remoteRosterEntries.next();
conn2.getRoster().createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
}
catch (XMPPException e) {
} catch (Exception e) {
fail(e.toString());
}
}
@ -286,20 +203,76 @@ public class RosterExchangeManagerTest extends TestCase {
entriesSent = conn1.getRoster().getEntryCount();
entriesReceived = 0;
rosterExchangeManager1.send(conn1.getRoster(), user2);
// Wait for 1 seconds
Thread.sleep(400);
} catch (Exception e) {
fail("An error occured sending the message with the roster");
}
// Wait for 2 seconds
Thread.sleep(2000);
assertEquals("Number of sent and received entries does not match", entriesSent, entriesReceived);
assertEquals(
"Number of sent and received entries does not match",
entriesSent,
entriesReceived);
assertTrue("Roster2 has no entries", conn2.getRoster().getEntryCount() > 0);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
conn3 = new XMPPConnection("localhost");
conn4 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
conn3.getAccountManager().createAccount("gato5", "gato5");
conn4.getAccountManager().createAccount("gato6", "gato6");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
conn3.login("gato5", "gato5");
conn4.login("gato6", "gato6");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
user3 = "gato5@" + conn3.getHost();
user4 = "gato6@" + conn4.getHost();
conn1.getRoster().createEntry(
"gato5@" + conn3.getHost(),
"gato5",
new String[] { "Friends, Coworker" });
conn1.getRoster().createEntry("gato6@" + conn4.getHost(), "gato6", null);
Thread.sleep(100);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
conn3.getAccountManager().deleteAccount();
conn4.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
conn3.close();
conn4.close();
}
}

View file

@ -66,6 +66,12 @@ import junit.framework.TestCase;
*/
public class XHTMLManagerTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
private int bodiesSent;
private int bodiesReceived;
@ -83,21 +89,6 @@ public class XHTMLManagerTest extends TestCase {
* 1. User_1 will send a message with formatted text (XHTML) to user_2
*/
public void testSendSimpleXHTMLMessage() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server
conn1 = new XMPPConnection(host);
// User1 logs in
conn1.login(server_user1, pass1);
// User1 creates a chat with user2
Chat chat1 = conn1.createChat(user2);
@ -123,16 +114,10 @@ public class XHTMLManagerTest extends TestCase {
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
Thread.sleep(250);
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -144,27 +129,6 @@ public class XHTMLManagerTest extends TestCase {
* something is wrong
*/
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Wait a few milliseconds between each login
Thread.sleep(250);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
@ -222,17 +186,9 @@ public class XHTMLManagerTest extends TestCase {
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
// Wait for 2 seconds for a reply
// Wait for 1 second for a reply
msg = chat1.nextMessage(1000);
assertNotNull("No reply received", msg);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
}
}
/**
@ -244,27 +200,6 @@ public class XHTMLManagerTest extends TestCase {
* something is wrong
*/
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Wait a few milliseconds between each login
Thread.sleep(250);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
@ -298,7 +233,8 @@ public class XHTMLManagerTest extends TestCase {
// User1 creates a message to send to user2
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want");
msg.setBody("awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
msg.setBody(
"awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
// Create an XHTMLText to send with the message (in Spanish)
XHTMLText xhtmlText = new XHTMLText(null, "es-ES");
@ -337,19 +273,56 @@ public class XHTMLManagerTest extends TestCase {
bodiesSent = 2;
bodiesReceived = 0;
chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(300);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
// Wait half second so that the complete test can run
Thread.sleep(500);
assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
assertEquals(
"Number of sent and received XHTMP bodies does not match",
bodiesSent,
bodiesReceived);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
if (conn2 != null)
conn2.close();
}
}
}

View file

@ -66,6 +66,12 @@ import junit.framework.TestCase;
*/
public class MessageEventTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/**
* Constructor for MessageEventTest.
* @param name
@ -82,20 +88,6 @@ public class MessageEventTest extends TestCase {
* occurs: offline, composing, displayed or delivered
*/
public void testSendMessageEventRequest() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
@ -115,16 +107,10 @@ public class MessageEventTest extends TestCase {
try {
chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(500);
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -137,20 +123,6 @@ public class MessageEventTest extends TestCase {
* 3. User_1 will display any notification that receives
*/
public void testSendMessageEventRequestAndDisplayNotifications() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
@ -161,9 +133,12 @@ public class MessageEventTest extends TestCase {
public void processPacket(Packet packet) {
Message message = (Message) packet;
try {
MessageEvent messageEvent = (MessageEvent) message.getExtension("x", "jabber:x:event");
MessageEvent messageEvent =
(MessageEvent) message.getExtension("x", "jabber:x:event");
assertNotNull("Message without extension \"jabber:x:event\"", messageEvent);
assertTrue("Message event is a request not a notification", !messageEvent.isMessageEventRequest());
assertTrue(
"Message event is a request not a notification",
!messageEvent.isMessageEventRequest());
System.out.println(messageEvent.toXML());
} catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
@ -188,16 +163,51 @@ public class MessageEventTest extends TestCase {
try {
chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(500);
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message");
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
}
}

View file

@ -21,6 +21,16 @@ import junit.framework.TestCase;
*/
public class RosterExchangeTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private XMPPConnection conn3 = null;
private XMPPConnection conn4 = null;
private String user1 = null;
private String user2 = null;
private String user3 = null;
private String user4 = null;
/**
* Constructor for RosterExchangeTest.
* @param arg0
@ -35,20 +45,6 @@ public class RosterExchangeTest extends TestCase {
* 1. User_1 will send his/her roster entries to user_2
*/
public void testSendRosterEntries() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
@ -67,13 +63,6 @@ public class RosterExchangeTest extends TestCase {
} catch (Exception e) {
fail("An error occured sending the message with the roster");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -83,25 +72,6 @@ public class RosterExchangeTest extends TestCase {
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/
public void testSendAndReceiveRosterEntries() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
@ -116,9 +86,7 @@ public class RosterExchangeTest extends TestCase {
try {
RosterExchange rosterExchange =
(RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull(
"Message without extension \"jabber:x:roster\"",
rosterExchange);
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
@ -155,15 +123,6 @@ public class RosterExchangeTest extends TestCase {
// Wait for 2 seconds for a reply
msg = chat1.nextMessage(2000);
assertNotNull("No reply received", msg);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
}
}
/**
@ -173,26 +132,6 @@ public class RosterExchangeTest extends TestCase {
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/
public void testSendAndAcceptRosterEntries() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
final Roster user2_roster = conn2.getRoster();
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
@ -207,16 +146,14 @@ public class RosterExchangeTest extends TestCase {
try {
RosterExchange rosterExchange =
(RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull(
"Message without extension \"jabber:x:roster\"",
rosterExchange);
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
// Add the roster entries to user2's roster
for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
user2_roster.createEntry(
conn2.getRoster().createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
@ -253,15 +190,68 @@ public class RosterExchangeTest extends TestCase {
// Wait for 10 seconds for a reply
msg = chat1.nextMessage(5000);
assertNotNull("No reply received", msg);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
assertTrue("Roster2 has no entries", conn2.getRoster().getEntryCount() > 0);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
conn3 = new XMPPConnection("localhost");
conn4 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
conn3.getAccountManager().createAccount("gato5", "gato5");
conn4.getAccountManager().createAccount("gato6", "gato6");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
conn3.login("gato5", "gato5");
conn4.login("gato6", "gato6");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
user3 = "gato5@" + conn3.getHost();
user4 = "gato6@" + conn4.getHost();
conn1.getRoster().createEntry(
"gato5@" + conn3.getHost(),
"gato5",
new String[] { "Friends, Coworker" });
conn1.getRoster().createEntry("gato6@" + conn4.getHost(), "gato6", null);
Thread.sleep(300);
} catch (Exception e) {
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
conn3.getAccountManager().deleteAccount();
conn4.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
conn3.close();
conn4.close();
}
}

View file

@ -67,6 +67,12 @@ import junit.framework.TestCase;
*/
public class XHTMLExtensionTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
private int bodiesSent;
private int bodiesReceived;
@ -84,21 +90,6 @@ public class XHTMLExtensionTest extends TestCase {
* 1. User_1 will send a message with formatted text (XHTML) to user_2
*/
public void testSendSimpleXHTMLMessage() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try {
// Connect to the server
conn1 = new XMPPConnection(host);
// User1 logs in
conn1.login(server_user1, pass1);
// User1 creates a chat with user2
Chat chat1 = conn1.createChat(user2);
@ -115,16 +106,10 @@ public class XHTMLExtensionTest extends TestCase {
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
Thread.sleep(250);
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
}
}
/**
@ -136,27 +121,6 @@ public class XHTMLExtensionTest extends TestCase {
* something is wrong
*/
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Wait a few milliseconds between each login
Thread.sleep(250);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
@ -178,9 +142,7 @@ public class XHTMLExtensionTest extends TestCase {
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue(
"Message without XHTML bodies",
xhtmlExtension.getBodiesCount() > 0);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
String body = (String) it.next();
System.out.println(body);
@ -216,14 +178,6 @@ public class XHTMLExtensionTest extends TestCase {
// Wait for 2 seconds for a reply
msg = chat1.nextMessage(1000);
assertNotNull("No reply received", msg);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
}
}
/**
@ -235,27 +189,6 @@ public class XHTMLExtensionTest extends TestCase {
* something is wrong
*/
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost";
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4";
String user2 = "gato4@localhost";
String pass2 = "gato4";
XMPPConnection conn1 = null;
XMPPConnection conn2 = null;
try {
// Connect to the server and log in the users
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
// Wait a few milliseconds between each login
Thread.sleep(250);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
@ -278,9 +211,7 @@ public class XHTMLExtensionTest extends TestCase {
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue(
"Message without XHTML bodies",
xhtmlExtension.getBodiesCount() > 0);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
received++;
System.out.println((String) it.next());
@ -296,7 +227,8 @@ public class XHTMLExtensionTest extends TestCase {
// User1 creates a message to send to user2
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want");
msg.setBody("awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
msg.setBody(
"awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
// Create an XHTMLExtension and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody(
@ -310,20 +242,57 @@ public class XHTMLExtensionTest extends TestCase {
bodiesSent = xhtmlExtension.getBodiesCount();
bodiesReceived = 0;
chat1.sendMessage(msg);
Thread.sleep(300);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
// Wait half second so that the complete test can run
Thread.sleep(500);
assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
assertEquals(
"Number of sent and received XHTMP bodies does not match",
bodiesSent,
bodiesReceived);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
}
}