From 45fbd2e4b71d5748c43f58ca8a5c435bd248e65d Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Wed, 15 Oct 2003 14:55:19 +0000 Subject: [PATCH] Added #setUp and #tearDown git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2147 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smackx/MessageEventManagerTest.java | 396 ++++++++-------- .../smackx/RosterExchangeManagerTest.java | 321 ++++++------- .../jivesoftware/smackx/XHTMLManagerTest.java | 423 ++++++++---------- .../smackx/packet/MessageEventTest.java | 198 ++++---- .../smackx/packet/RosterExchangeTest.java | 390 ++++++++-------- .../smackx/packet/XHTMLExtensionTest.java | 369 +++++++-------- 6 files changed, 998 insertions(+), 1099 deletions(-) diff --git a/test/org/jivesoftware/smackx/MessageEventManagerTest.java b/test/org/jivesoftware/smackx/MessageEventManagerTest.java index ab62b78fc..1e25f379e 100644 --- a/test/org/jivesoftware/smackx/MessageEventManagerTest.java +++ b/test/org/jivesoftware/smackx/MessageEventManagerTest.java @@ -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,44 +89,24 @@ 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); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("An interesting body comes here..."); - // Add to the message all the notifications requests (offline, delivered, displayed, - // composing) - MessageEventManager.addNotificationsRequests(msg, true, true, true, true); - - // Send the message that contains the notifications request - try { - chat1.sendMessage(msg); - } catch (Exception e) { - fail("An error occured sending the message"); - } - } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - } - } + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("An interesting body comes here..."); + // Add to the message all the notifications requests (offline, delivered, displayed, + // composing) + MessageEventManager.addNotificationsRequests(msg, true, true, true, true); + + // Send the message that contains the notifications request + try { + chat1.sendMessage(msg); + } catch (Exception e) { + fail("An error occured sending the message"); + } + } /** * High level API test. @@ -132,68 +118,48 @@ 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); - - MessageEventManager messageEventManager = new MessageEventManager(conn1); - messageEventManager - .addMessageEventNotificationListener(new MessageEventNotificationListener() { - public void deliveredNotification(String from, String packetID) { - System.out.println("From: " + from + " PacketID: " + packetID + "(delivered)"); - } - - public void displayedNotification(String from, String packetID) { - System.out.println("From: " + from + " PacketID: " + packetID + "(displayed)"); - } - - public void composingNotification(String from, String packetID) { - System.out.println("From: " + from + " PacketID: " + packetID + "(composing)"); - } - - public void offlineNotification(String from, String packetID) { - System.out.println("From: " + from + " PacketID: " + packetID + "(offline)"); - } - - public void cancelledNotification(String from, String packetID) { - System.out.println("From: " + from + " PacketID: " + packetID + "(cancelled)"); - } - }); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("An interesting body comes here..."); - // Add to the message all the notifications requests (offline, delivered, displayed, - // composing) - MessageEventManager.addNotificationsRequests(msg, true, true, true, true); - - // Send the message that contains the notifications request - try { - chat1.sendMessage(msg); - // Wait a few seconds so that the XMPP client can send any event - Thread.sleep(5000); - } catch (Exception e) { - fail("An error occured sending the message"); + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + + MessageEventManager messageEventManager = new MessageEventManager(conn1); + messageEventManager + .addMessageEventNotificationListener(new MessageEventNotificationListener() { + public void deliveredNotification(String from, String packetID) { + System.out.println("From: " + from + " PacketID: " + packetID + "(delivered)"); } + + public void displayedNotification(String from, String packetID) { + System.out.println("From: " + from + " PacketID: " + packetID + "(displayed)"); + } + + public void composingNotification(String from, String packetID) { + System.out.println("From: " + from + " PacketID: " + packetID + "(composing)"); + } + + public void offlineNotification(String from, String packetID) { + System.out.println("From: " + from + " PacketID: " + packetID + "(offline)"); + } + + public void cancelledNotification(String from, String packetID) { + System.out.println("From: " + from + " PacketID: " + packetID + "(cancelled)"); + } + }); + + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("An interesting body comes here..."); + // Add to the message all the notifications requests (offline, delivered, displayed, + // composing) + MessageEventManager.addNotificationsRequests(msg, true, true, true, true); + + // Send the message that contains the notifications request + try { + chat1.sendMessage(msg); + // Wait a few seconds so that the XMPP client can send any event + Thread.sleep(200); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + fail("An error occured sending the message"); } } @@ -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,106 +184,136 @@ 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); - // Create a chat for each connection - Chat chat1 = conn1.createChat(user2); - - MessageEventManager messageEventManager1 = new MessageEventManager(conn1); - messageEventManager1 - .addMessageEventNotificationListener(new MessageEventNotificationListener() { - public void deliveredNotification(String from, String packetID) { - results.add("deliveredNotification"); - } - - public void displayedNotification(String from, String packetID) { - results.add("displayedNotification"); - } - - public void composingNotification(String from, String packetID) { - results.add("composingNotification"); - } - - public void offlineNotification(String from, String packetID) { - results.add("offlineNotification"); - } - - public void cancelledNotification(String from, String packetID) { - results.add("cancelledNotification"); - } - }); - - MessageEventManager messageEventManager2 = new MessageEventManager(conn2); - messageEventManager2 - .addMessageEventRequestListener(new DefaultMessageEventRequestListener() { - public void deliveredNotificationRequested( - String from, - String packetID, - MessageEventManager messageEventManager) { - super.deliveredNotificationRequested(from, packetID, messageEventManager); - results.add("deliveredNotificationRequested"); - } - - public void displayedNotificationRequested( - String from, - String packetID, - MessageEventManager messageEventManager) { - super.displayedNotificationRequested(from, packetID, messageEventManager); - results.add("displayedNotificationRequested"); - } - - public void composingNotificationRequested( - String from, - String packetID, - MessageEventManager messageEventManager) { - super.composingNotificationRequested(from, packetID, messageEventManager); - results.add("composingNotificationRequested"); - } - - public void offlineNotificationRequested( - String from, - String packetID, - MessageEventManager messageEventManager) { - super.offlineNotificationRequested(from, packetID, messageEventManager); - results.add("offlineNotificationRequested"); - } - }); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("An interesting body comes here..."); - // Add to the message all the notifications requests (offline, delivered, displayed, - // composing) - MessageEventManager.addNotificationsRequests(msg, true, true, true, true); - - // Send the message that contains the notifications request - try { - chat1.sendMessage(msg); - messageEventManager2.sendDisplayedNotification(user1, msg.getPacketID()); - messageEventManager2.sendComposingNotification(user1, msg.getPacketID()); - 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)); - - } catch (Exception e) { - fail("An error occured sending the message"); + MessageEventManager messageEventManager1 = new MessageEventManager(conn1); + messageEventManager1 + .addMessageEventNotificationListener(new MessageEventNotificationListener() { + public void deliveredNotification(String from, String packetID) { + results.add("deliveredNotification"); } + + public void displayedNotification(String from, String packetID) { + results.add("displayedNotification"); + } + + public void composingNotification(String from, String packetID) { + results.add("composingNotification"); + } + + public void offlineNotification(String from, String packetID) { + results.add("offlineNotification"); + } + + public void cancelledNotification(String from, String packetID) { + results.add("cancelledNotification"); + } + }); + + MessageEventManager messageEventManager2 = new MessageEventManager(conn2); + messageEventManager2 + .addMessageEventRequestListener(new DefaultMessageEventRequestListener() { + public void deliveredNotificationRequested( + String from, + String packetID, + MessageEventManager messageEventManager) { + super.deliveredNotificationRequested(from, packetID, messageEventManager); + results.add("deliveredNotificationRequested"); + } + + public void displayedNotificationRequested( + String from, + String packetID, + MessageEventManager messageEventManager) { + super.displayedNotificationRequested(from, packetID, messageEventManager); + results.add("displayedNotificationRequested"); + } + + public void composingNotificationRequested( + String from, + String packetID, + MessageEventManager messageEventManager) { + super.composingNotificationRequested(from, packetID, messageEventManager); + results.add("composingNotificationRequested"); + } + + public void offlineNotificationRequested( + String from, + String packetID, + MessageEventManager messageEventManager) { + super.offlineNotificationRequested(from, packetID, messageEventManager); + results.add("offlineNotificationRequested"); + } + }); + + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("An interesting body comes here..."); + // Add to the message all the notifications requests (offline, delivered, displayed, + // composing) + MessageEventManager.addNotificationsRequests(msg, true, true, true, true); + + // Send the message that contains the notifications request + try { + chat1.sendMessage(msg); + messageEventManager2.sendDisplayedNotification(user1, msg.getPacketID()); + messageEventManager2.sendComposingNotification(user1, msg.getPacketID()); + 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)); + } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - if (conn2 != null) - conn2.close(); + 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.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(); + } } diff --git a/test/org/jivesoftware/smackx/RosterExchangeManagerTest.java b/test/org/jivesoftware/smackx/RosterExchangeManagerTest.java index 52b1fdc3e..507a589b6 100644 --- a/test/org/jivesoftware/smackx/RosterExchangeManagerTest.java +++ b/test/org/jivesoftware/smackx/RosterExchangeManagerTest.java @@ -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; - + // Send user1's roster to user2 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"); - } - + RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1); + rosterExchangeManager.send(conn1.getRoster(), user2); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + e.printStackTrace(); + fail("An error occured sending the roster"); } - } /** @@ -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; - + // Send user1's RosterGroups to user2 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); - } catch (Exception e) { - e.printStackTrace(); - fail("An error occured sending the roster"); - } - + RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1); + for (Iterator it = conn1.getRoster().getGroups(); it.hasNext();) + rosterExchangeManager.send((RosterGroup) it.next(), user2); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + e.printStackTrace(); + fail("An error occured sending the roster"); } - } /** @@ -166,64 +127,40 @@ 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"; + RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1); + RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2); - 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); - - // Create a RosterExchangeListener that will iterate over the received roster entries - RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { - 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()); - while (remoteRosterEntries.hasNext()) { - received++; - RemoteRosterEntry remoteEntry = (RemoteRosterEntry) remoteRosterEntries.next(); - System.out.println(remoteEntry); - } - entriesReceived = received; + // Create a RosterExchangeListener that will iterate over the received roster entries + RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { + 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()); + while (remoteRosterEntries.hasNext()) { + received++; + RemoteRosterEntry remoteEntry = (RemoteRosterEntry) remoteRosterEntries.next(); + System.out.println(remoteEntry); } - }; - rosterExchangeManager2.addRosterListener(rosterExchangeListener); - - // Send user1's roster to user2 - try { - entriesSent = conn1.getRoster().getEntryCount(); - entriesReceived = 0; - rosterExchangeManager1.send(conn1.getRoster(), user2); - } catch (Exception e) { - fail("An error occured sending the message with the roster"); + entriesReceived = received; } - // Wait for 2 seconds - Thread.sleep(2000); - assertEquals("Number of sent and received entries does not match", entriesSent, entriesReceived); + }; + rosterExchangeManager2.addRosterListener(rosterExchangeListener); + + // Send user1's roster to user2 + try { + entriesSent = conn1.getRoster().getEntryCount(); + entriesReceived = 0; + rosterExchangeManager1.send(conn1.getRoster(), user2); + // Wait for 1 second + Thread.sleep(300); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - if (conn2 != null) - conn2.close(); + fail("An error occured sending the message with the roster"); } + assertEquals( + "Number of sent and received entries does not match", + entriesSent, + entriesReceived); } /** @@ -233,73 +170,109 @@ 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"; + RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1); + RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2); - 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); - - // Create a RosterExchangeListener that will accept all the received roster entries - RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { - 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()); - while (remoteRosterEntries.hasNext()) { - received++; - try { - RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) remoteRosterEntries.next(); - user2_roster.createEntry( - remoteRosterEntry.getUser(), - remoteRosterEntry.getName(), - remoteRosterEntry.getGroupArrayNames()); - } - catch (XMPPException e) { - fail(e.toString()); - } + // Create a RosterExchangeListener that will accept all the received roster entries + RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { + 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()); + while (remoteRosterEntries.hasNext()) { + received++; + try { + RemoteRosterEntry remoteRosterEntry = + (RemoteRosterEntry) remoteRosterEntries.next(); + conn2.getRoster().createEntry( + remoteRosterEntry.getUser(), + remoteRosterEntry.getName(), + remoteRosterEntry.getGroupArrayNames()); + } catch (Exception e) { + fail(e.toString()); } - entriesReceived = received; } - }; - rosterExchangeManager2.addRosterListener(rosterExchangeListener); - - // Send user1's roster to user2 - try { - entriesSent = conn1.getRoster().getEntryCount(); - entriesReceived = 0; - rosterExchangeManager1.send(conn1.getRoster(), user2); - } catch (Exception e) { - fail("An error occured sending the message with the roster"); + entriesReceived = received; } - // Wait for 2 seconds - Thread.sleep(2000); - assertEquals("Number of sent and received entries does not match", entriesSent, entriesReceived); + }; + rosterExchangeManager2.addRosterListener(rosterExchangeListener); + + // Send user1's roster to user2 + try { + entriesSent = conn1.getRoster().getEntryCount(); + entriesReceived = 0; + rosterExchangeManager1.send(conn1.getRoster(), user2); + // Wait for 1 seconds + Thread.sleep(400); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - if (conn2 != null) - conn2.close(); + fail("An error occured sending the message with the roster"); + } + 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.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(); + } } diff --git a/test/org/jivesoftware/smackx/XHTMLManagerTest.java b/test/org/jivesoftware/smackx/XHTMLManagerTest.java index 290729b3d..ddf5a51e1 100644 --- a/test/org/jivesoftware/smackx/XHTMLManagerTest.java +++ b/test/org/jivesoftware/smackx/XHTMLManagerTest.java @@ -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,55 +89,34 @@ 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"; + // User1 creates a chat with user2 + Chat chat1 = conn1.createChat(user2); - String user2 = "gato4@localhost"; + // User1 creates a message to send to user2 + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("Hey John, this is my new green!!!!"); - XMPPConnection conn1 = null; + // Create an XHTMLText to send with the message + XHTMLText xhtmlText = new XHTMLText(null, null); + xhtmlText.appendOpenParagraphTag("font-size:large"); + xhtmlText.append("Hey John, this is my new "); + xhtmlText.appendOpenSpanTag("color:green"); + xhtmlText.append("green"); + xhtmlText.appendCloseSpanTag(); + xhtmlText.appendOpenEmTag(); + xhtmlText.append("!!!!"); + xhtmlText.appendCloseEmTag(); + xhtmlText.appendCloseParagraphTag(); + // Add the XHTML text to the message + XHTMLManager.addBody(msg, xhtmlText.toString()); + // User1 sends the message that contains the XHTML to user2 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); - - // User1 creates a message to send to user2 - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("Hey John, this is my new green!!!!"); - - // Create an XHTMLText to send with the message - XHTMLText xhtmlText = new XHTMLText(null, null); - xhtmlText.appendOpenParagraphTag("font-size:large"); - xhtmlText.append("Hey John, this is my new "); - xhtmlText.appendOpenSpanTag("color:green"); - xhtmlText.append("green"); - xhtmlText.appendCloseSpanTag(); - xhtmlText.appendOpenEmTag(); - xhtmlText.append("!!!!"); - xhtmlText.appendCloseEmTag(); - xhtmlText.appendCloseParagraphTag(); - // Add the XHTML text to the message - XHTMLManager.addBody(msg, xhtmlText.toString()); - - // User1 sends the message that contains the XHTML to user2 - try { - chat1.sendMessage(msg); - Thread.sleep(250); - } catch (Exception e) { - fail("An error occured sending the message with XHTML"); - } + chat1.sendMessage(msg); + Thread.sleep(200); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + fail("An error occured sending the message with XHTML"); } } @@ -144,95 +129,66 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); - 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()); - - // Create a listener for the chat that will check if the received message includes - // an XHTML extension. Answer an ACK if everything is ok - PacketListener packetListener = new PacketListener() { - public void processPacket(Packet packet) { - Message message = (Message) packet; + // Create a listener for the chat that will check if the received message includes + // an XHTML extension. Answer an ACK if everything is ok + PacketListener packetListener = new PacketListener() { + public void processPacket(Packet packet) { + Message message = (Message) packet; + assertTrue( + "The received message is not an XHTML Message", + XHTMLManager.isXHTMLMessage(message)); + try { assertTrue( - "The received message is not an XHTML Message", - XHTMLManager.isXHTMLMessage(message)); - try { - assertTrue( - "Message without XHTML bodies", - XHTMLManager.getBodies(message).hasNext()); - for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) { - String body = (String) it.next(); - System.out.println(body); - } - } catch (ClassCastException e) { - fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); - } - try { - chat2.sendMessage("ok"); - } catch (Exception e) { - fail("An error occured sending ack " + e.getMessage()); + "Message without XHTML bodies", + XHTMLManager.getBodies(message).hasNext()); + for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) { + String body = (String) it.next(); + System.out.println(body); } + } catch (ClassCastException e) { + fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); + } + try { + chat2.sendMessage("ok"); + } catch (Exception e) { + fail("An error occured sending ack " + e.getMessage()); } - }; - chat2.addMessageListener(packetListener); - - // User1 creates a message to send to user2 - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("Hey John, this is my new green!!!!"); - - // Create an XHTMLText to send with the message - XHTMLText xhtmlText = new XHTMLText(null, null); - xhtmlText.appendOpenParagraphTag("font-size:large"); - xhtmlText.append("Hey John, this is my new "); - xhtmlText.appendOpenSpanTag("color:green"); - xhtmlText.append("green"); - xhtmlText.appendCloseSpanTag(); - xhtmlText.appendOpenEmTag(); - xhtmlText.append("!!!!"); - xhtmlText.appendCloseEmTag(); - xhtmlText.appendCloseParagraphTag(); - // Add the XHTML text to the message - XHTMLManager.addBody(msg, xhtmlText.toString()); - - // User1 sends the message that contains the XHTML to user2 - try { - chat1.sendMessage(msg); - } catch (Exception e) { - fail("An error occured sending the message with XHTML"); } - // Wait for 2 seconds for a reply - msg = chat1.nextMessage(1000); - assertNotNull("No reply received", msg); + }; + chat2.addMessageListener(packetListener); + + // User1 creates a message to send to user2 + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("Hey John, this is my new green!!!!"); + + // Create an XHTMLText to send with the message + XHTMLText xhtmlText = new XHTMLText(null, null); + xhtmlText.appendOpenParagraphTag("font-size:large"); + xhtmlText.append("Hey John, this is my new "); + xhtmlText.appendOpenSpanTag("color:green"); + xhtmlText.append("green"); + xhtmlText.appendCloseSpanTag(); + xhtmlText.appendOpenEmTag(); + xhtmlText.append("!!!!"); + xhtmlText.appendCloseEmTag(); + xhtmlText.appendCloseParagraphTag(); + // Add the XHTML text to the message + XHTMLManager.addBody(msg, xhtmlText.toString()); + + // User1 sends the message that contains the XHTML to user2 + try { + chat1.sendMessage(msg); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - if (conn2 != null) - conn2.close(); + fail("An error occured sending the message with XHTML"); } + // Wait for 1 second for a reply + msg = chat1.nextMessage(1000); + assertNotNull("No reply received", msg); } /** @@ -244,112 +200,129 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); - 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()); - - // Create a listener for the chat that will check if the received message includes - // an XHTML extension. Answer an ACK if everything is ok - PacketListener packetListener = new PacketListener() { - public void processPacket(Packet packet) { - int received = 0; - Message message = (Message) packet; + // Create a listener for the chat that will check if the received message includes + // an XHTML extension. Answer an ACK if everything is ok + PacketListener packetListener = new PacketListener() { + public void processPacket(Packet packet) { + int received = 0; + Message message = (Message) packet; + assertTrue( + "The received message is not an XHTML Message", + XHTMLManager.isXHTMLMessage(message)); + try { assertTrue( - "The received message is not an XHTML Message", - XHTMLManager.isXHTMLMessage(message)); - try { - assertTrue( - "Message without XHTML bodies", - XHTMLManager.getBodies(message).hasNext()); - for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) { - received++; - String body = (String) it.next(); - System.out.println(body); - } - bodiesReceived = received; - } catch (ClassCastException e) { - fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); + "Message without XHTML bodies", + XHTMLManager.getBodies(message).hasNext()); + for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) { + received++; + String body = (String) it.next(); + System.out.println(body); } + bodiesReceived = received; + } catch (ClassCastException e) { + fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); } - }; - chat2.addMessageListener(packetListener); - - // 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."); - - // Create an XHTMLText to send with the message (in Spanish) - XHTMLText xhtmlText = new XHTMLText(null, "es-ES"); - xhtmlText.appendOpenHeaderTag(1, null); - xhtmlText.append("impresionante!"); - xhtmlText.appendCloseHeaderTag(1); - xhtmlText.appendOpenParagraphTag(null); - xhtmlText.append("Como Emerson dijo una vez:"); - xhtmlText.appendCloseParagraphTag(); - xhtmlText.appendOpenBlockQuoteTag(null); - xhtmlText.appendOpenParagraphTag(null); - xhtmlText.append("Una consistencia ridícula es el espantajo de mentes pequeñas."); - xhtmlText.appendCloseParagraphTag(); - xhtmlText.appendCloseBlockQuoteTag(); - // Add the XHTML text to the message - XHTMLManager.addBody(msg, xhtmlText.toString()); - - // Create an XHTMLText to send with the message (in English) - xhtmlText = new XHTMLText(null, "en-US"); - xhtmlText.appendOpenHeaderTag(1, null); - xhtmlText.append("awesome!"); - xhtmlText.appendCloseHeaderTag(1); - xhtmlText.appendOpenParagraphTag(null); - xhtmlText.append("As Emerson once said:"); - xhtmlText.appendCloseParagraphTag(); - xhtmlText.appendOpenBlockQuoteTag(null); - xhtmlText.appendOpenParagraphTag(null); - xhtmlText.append("A foolish consistency is the hobgoblin of little minds."); - xhtmlText.appendCloseParagraphTag(); - xhtmlText.appendCloseBlockQuoteTag(); - // Add the XHTML text to the message - XHTMLManager.addBody(msg, xhtmlText.toString()); - - // User1 sends the message that contains the XHTML to user2 - try { - bodiesSent = 2; - bodiesReceived = 0; - chat1.sendMessage(msg); - } catch (Exception e) { - fail("An error occured sending the message with XHTML"); } + }; + chat2.addMessageListener(packetListener); + + // 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."); + + // Create an XHTMLText to send with the message (in Spanish) + XHTMLText xhtmlText = new XHTMLText(null, "es-ES"); + xhtmlText.appendOpenHeaderTag(1, null); + xhtmlText.append("impresionante!"); + xhtmlText.appendCloseHeaderTag(1); + xhtmlText.appendOpenParagraphTag(null); + xhtmlText.append("Como Emerson dijo una vez:"); + xhtmlText.appendCloseParagraphTag(); + xhtmlText.appendOpenBlockQuoteTag(null); + xhtmlText.appendOpenParagraphTag(null); + xhtmlText.append("Una consistencia ridícula es el espantajo de mentes pequeñas."); + xhtmlText.appendCloseParagraphTag(); + xhtmlText.appendCloseBlockQuoteTag(); + // Add the XHTML text to the message + XHTMLManager.addBody(msg, xhtmlText.toString()); + + // Create an XHTMLText to send with the message (in English) + xhtmlText = new XHTMLText(null, "en-US"); + xhtmlText.appendOpenHeaderTag(1, null); + xhtmlText.append("awesome!"); + xhtmlText.appendCloseHeaderTag(1); + xhtmlText.appendOpenParagraphTag(null); + xhtmlText.append("As Emerson once said:"); + xhtmlText.appendCloseParagraphTag(); + xhtmlText.appendOpenBlockQuoteTag(null); + xhtmlText.appendOpenParagraphTag(null); + xhtmlText.append("A foolish consistency is the hobgoblin of little minds."); + xhtmlText.appendCloseParagraphTag(); + xhtmlText.appendCloseBlockQuoteTag(); + // Add the XHTML text to the message + XHTMLManager.addBody(msg, xhtmlText.toString()); + + // User1 sends the message that contains the XHTML to user2 + try { + bodiesSent = 2; + bodiesReceived = 0; + chat1.sendMessage(msg); // 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); + Thread.sleep(300); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - if (conn2 != null) - conn2.close(); + fail("An error occured sending the message with XHTML"); + } + 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.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(); + } + } diff --git a/test/org/jivesoftware/smackx/packet/MessageEventTest.java b/test/org/jivesoftware/smackx/packet/MessageEventTest.java index 742f527a3..2aac7a495 100644 --- a/test/org/jivesoftware/smackx/packet/MessageEventTest.java +++ b/test/org/jivesoftware/smackx/packet/MessageEventTest.java @@ -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,48 +88,28 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); - String user2 = "gato4@localhost"; - - XMPPConnection conn1 = null; + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("An interesting body comes here..."); + // Create a MessageEvent Package and add it to the message + MessageEvent messageEvent = new MessageEvent(); + messageEvent.setComposing(true); + messageEvent.setDelivered(true); + messageEvent.setDisplayed(true); + messageEvent.setOffline(true); + msg.addExtension(messageEvent); + // Send the message that contains the notifications request 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); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("An interesting body comes here..."); - // Create a MessageEvent Package and add it to the message - MessageEvent messageEvent = new MessageEvent(); - messageEvent.setComposing(true); - messageEvent.setDelivered(true); - messageEvent.setDisplayed(true); - messageEvent.setOffline(true); - msg.addExtension(messageEvent); - - // Send the message that contains the notifications request - try { - chat1.sendMessage(msg); - // Wait half second so that the complete test can run - Thread.sleep(500); - } catch (Exception e) { - fail("An error occured sending the message"); - } + chat1.sendMessage(msg); + // Wait half second so that the complete test can run + Thread.sleep(200); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + fail("An error occured sending the message"); } } @@ -137,67 +123,91 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); - 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); - - // Create a Listener that listens for Messages with the extension "jabber:x:roster" - // This listener will listen on the conn2 and answer an ACK if everything is ok - PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event"); - PacketListener packetListener = new PacketListener() { - public void processPacket(Packet packet) { - Message message = (Message) packet; - try { - 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()); - System.out.println(messageEvent.toXML()); - } catch (ClassCastException e) { - fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); - } + // Create a Listener that listens for Messages with the extension "jabber:x:roster" + // This listener will listen on the conn2 and answer an ACK if everything is ok + PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event"); + PacketListener packetListener = new PacketListener() { + public void processPacket(Packet packet) { + Message message = (Message) packet; + try { + 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()); + System.out.println(messageEvent.toXML()); + } catch (ClassCastException e) { + fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); } - }; - conn1.addPacketListener(packetListener, packetFilter); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("An interesting body comes here..."); - // Create a MessageEvent Package and add it to the message - MessageEvent messageEvent = new MessageEvent(); - messageEvent.setComposing(true); - messageEvent.setDelivered(true); - messageEvent.setDisplayed(true); - messageEvent.setOffline(true); - msg.addExtension(messageEvent); - - // Send the message that contains the notifications request - try { - chat1.sendMessage(msg); - // Wait half second so that the complete test can run - Thread.sleep(500); - } catch (Exception e) { - fail("An error occured sending the message"); } + }; + conn1.addPacketListener(packetListener, packetFilter); + + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("An interesting body comes here..."); + // Create a MessageEvent Package and add it to the message + MessageEvent messageEvent = new MessageEvent(); + messageEvent.setComposing(true); + messageEvent.setDelivered(true); + messageEvent.setDisplayed(true); + messageEvent.setOffline(true); + msg.addExtension(messageEvent); + + // Send the message that contains the notifications request + try { + chat1.sendMessage(msg); + // Wait half second so that the complete test can run + Thread.sleep(200); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + 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.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(); + } } diff --git a/test/org/jivesoftware/smackx/packet/RosterExchangeTest.java b/test/org/jivesoftware/smackx/packet/RosterExchangeTest.java index abb590a6b..69a73a0b5 100644 --- a/test/org/jivesoftware/smackx/packet/RosterExchangeTest.java +++ b/test/org/jivesoftware/smackx/packet/RosterExchangeTest.java @@ -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,135 +45,84 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); - String user2 = "gato4@localhost"; - - XMPPConnection conn1 = null; + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("This message contains roster items."); + // Create a RosterExchange Package and add it to the message + assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); + RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); + msg.addExtension(rosterExchange); + // Send the message that contains the roster 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); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("This message contains roster items."); - // Create a RosterExchange Package and add it to the message - assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); - RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); - msg.addExtension(rosterExchange); - - // Send the message that contains the roster - try { - chat1.sendMessage(msg); - } catch (Exception e) { - fail("An error occured sending the message with the roster"); - } + chat1.sendMessage(msg); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + fail("An error occured sending the message with the roster"); } - } - /** - * Low level API test. - * 1. User_1 will send his/her roster entries to user_2 - * 2. User_2 will receive the entries and iterate over them to check if everything is fine - * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong - */ + /** + * Low level API test. + * 1. User_1 will send his/her roster entries to user_2 + * 2. User_2 will receive the entries and iterate over them to check if everything is fine + * 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); - 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()); - - // Create a Listener that listens for Messages with the extension "jabber:x:roster" - // This listener will listen on the conn2 and answer an ACK if everything is ok - PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster"); - PacketListener packetListener = new PacketListener() { - public void processPacket(Packet packet) { - Message message = (Message) packet; - assertNotNull("Body is null", message.getBody()); - try { - RosterExchange rosterExchange = - (RosterExchange) message.getExtension("x", "jabber:x:roster"); - assertNotNull( - "Message without extension \"jabber:x:roster\"", - rosterExchange); - assertTrue( - "Roster without entries", - rosterExchange.getRosterEntries().hasNext()); - for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) { - RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next(); - } - } catch (ClassCastException e) { - fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); - } - try { - chat2.sendMessage("ok"); - } catch (Exception e) { - fail("An error occured sending ack " + e.getMessage()); + // Create a Listener that listens for Messages with the extension "jabber:x:roster" + // This listener will listen on the conn2 and answer an ACK if everything is ok + PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster"); + PacketListener packetListener = new PacketListener() { + public void processPacket(Packet packet) { + Message message = (Message) packet; + assertNotNull("Body is null", message.getBody()); + try { + RosterExchange rosterExchange = + (RosterExchange) message.getExtension("x", "jabber:x:roster"); + assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange); + assertTrue( + "Roster without entries", + rosterExchange.getRosterEntries().hasNext()); + for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) { + RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next(); } + } catch (ClassCastException e) { + fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); + } + try { + chat2.sendMessage("ok"); + } catch (Exception e) { + fail("An error occured sending ack " + e.getMessage()); } - }; - conn2.addPacketListener(packetListener, packetFilter); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("This message contains roster items."); - // Create a RosterExchange Package and add it to the message - assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); - RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); - msg.addExtension(rosterExchange); - - // Send the message that contains the roster - try { - chat1.sendMessage(msg); - } catch (Exception e) { - fail("An error occured sending the message with the roster"); } - // 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(); - } + }; + conn2.addPacketListener(packetListener, packetFilter); + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("This message contains roster items."); + // Create a RosterExchange Package and add it to the message + assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); + RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); + msg.addExtension(rosterExchange); + + // Send the message that contains the roster + try { + chat1.sendMessage(msg); + } catch (Exception e) { + fail("An error occured sending the message with the roster"); + } + // Wait for 2 seconds for a reply + msg = chat1.nextMessage(2000); + assertNotNull("No reply received", msg); } /** @@ -173,95 +132,126 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); - 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()); - - // Create a Listener that listens for Messages with the extension "jabber:x:roster" - // This listener will listen on the conn2, save the roster entries and answer an ACK if everything is ok - PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster"); - PacketListener packetListener = new PacketListener() { - public void processPacket(Packet packet) { - Message message = (Message) packet; - assertNotNull("Body is null", message.getBody()); - try { - RosterExchange rosterExchange = - (RosterExchange) message.getExtension("x", "jabber:x:roster"); - 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( - remoteRosterEntry.getUser(), - remoteRosterEntry.getName(), - remoteRosterEntry.getGroupArrayNames()); - } - } catch (ClassCastException e) { - fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); - } catch (Exception e) { - fail(e.toString()); - } - try { - chat2.sendMessage("ok"); - } catch (Exception e) { - fail("An error occured sending ack " + e.getMessage()); + // Create a Listener that listens for Messages with the extension "jabber:x:roster" + // This listener will listen on the conn2, save the roster entries and answer an ACK if everything is ok + PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster"); + PacketListener packetListener = new PacketListener() { + public void processPacket(Packet packet) { + Message message = (Message) packet; + assertNotNull("Body is null", message.getBody()); + try { + RosterExchange rosterExchange = + (RosterExchange) message.getExtension("x", "jabber:x:roster"); + 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(); + conn2.getRoster().createEntry( + remoteRosterEntry.getUser(), + remoteRosterEntry.getName(), + remoteRosterEntry.getGroupArrayNames()); } + } catch (ClassCastException e) { + fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); + } catch (Exception e) { + fail(e.toString()); + } + try { + chat2.sendMessage("ok"); + } catch (Exception e) { + fail("An error occured sending ack " + e.getMessage()); } - }; - conn2.addPacketListener(packetListener, packetFilter); - - // Create the message to send with the roster - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("This message contains roster items."); - // Create a RosterExchange Package and add it to the message - assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); - RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); - msg.addExtension(rosterExchange); - - // Send the message that contains the roster - try { - chat1.sendMessage(msg); - } catch (Exception e) { - fail("An error occured sending the message with the roster"); } - // 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(); - } + }; + conn2.addPacketListener(packetListener, packetFilter); + // Create the message to send with the roster + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("This message contains roster items."); + // Create a RosterExchange Package and add it to the message + assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); + RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); + msg.addExtension(rosterExchange); + + // Send the message that contains the roster + try { + chat1.sendMessage(msg); + } catch (Exception e) { + fail("An error occured sending the message with the roster"); + } + // Wait for 10 seconds for a reply + msg = chat1.nextMessage(5000); + assertNotNull("No reply received", msg); + 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(); } } diff --git a/test/org/jivesoftware/smackx/packet/XHTMLExtensionTest.java b/test/org/jivesoftware/smackx/packet/XHTMLExtensionTest.java index 500006bd8..9de6eab42 100644 --- a/test/org/jivesoftware/smackx/packet/XHTMLExtensionTest.java +++ b/test/org/jivesoftware/smackx/packet/XHTMLExtensionTest.java @@ -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,46 +90,25 @@ 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"; + // User1 creates a chat with user2 + Chat chat1 = conn1.createChat(user2); - String user2 = "gato4@localhost"; - - XMPPConnection conn1 = null; + // User1 creates a message to send to user2 + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("Hey John, this is my new green!!!!"); + // Create a XHTMLExtension Package and add it to the message + XHTMLExtension xhtmlExtension = new XHTMLExtension(); + xhtmlExtension.addBody( + "

Hey John, this is my new green!!!!

"); + msg.addExtension(xhtmlExtension); + // User1 sends the message that contains the XHTML to user2 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); - - // User1 creates a message to send to user2 - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("Hey John, this is my new green!!!!"); - // Create a XHTMLExtension Package and add it to the message - XHTMLExtension xhtmlExtension = new XHTMLExtension(); - xhtmlExtension.addBody( - "

Hey John, this is my new green!!!!

"); - msg.addExtension(xhtmlExtension); - - // User1 sends the message that contains the XHTML to user2 - try { - chat1.sendMessage(msg); - Thread.sleep(250); - } catch (Exception e) { - fail("An error occured sending the message with XHTML"); - } + chat1.sendMessage(msg); + Thread.sleep(200); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); + fail("An error occured sending the message with XHTML"); } } @@ -136,94 +121,63 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); - 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()); - - // Create a Listener that listens for Messages with the extension - //"http://jabber.org/protocol/xhtml-im" - // This listener will listen on the conn2 and answer an ACK if everything is ok - PacketFilter packetFilter = - new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im"); - PacketListener packetListener = new PacketListener() { - public void processPacket(Packet packet) { - Message message = (Message) packet; - assertNotNull("Body is null", message.getBody()); - try { - XHTMLExtension xhtmlExtension = - (XHTMLExtension) message.getExtension( - "html", - "http://jabber.org/protocol/xhtml-im"); - assertNotNull( - "Message without extension \"http://jabber.org/protocol/xhtml-im\"", + // Create a Listener that listens for Messages with the extension + //"http://jabber.org/protocol/xhtml-im" + // This listener will listen on the conn2 and answer an ACK if everything is ok + PacketFilter packetFilter = + new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im"); + PacketListener packetListener = new PacketListener() { + public void processPacket(Packet packet) { + Message message = (Message) packet; + assertNotNull("Body is null", message.getBody()); + try { + XHTMLExtension xhtmlExtension = + (XHTMLExtension) message.getExtension( + "html", + "http://jabber.org/protocol/xhtml-im"); + assertNotNull( + "Message without extension \"http://jabber.org/protocol/xhtml-im\"", xhtmlExtension); - assertTrue( - "Message without XHTML bodies", - xhtmlExtension.getBodiesCount() > 0); - for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) { - String body = (String) it.next(); - System.out.println(body); - } - } catch (ClassCastException e) { - fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); - } - try { - chat2.sendMessage("ok"); - } catch (Exception e) { - fail("An error occured sending ack " + e.getMessage()); + assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0); + for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) { + String body = (String) it.next(); + System.out.println(body); } + } catch (ClassCastException e) { + fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); + } + try { + chat2.sendMessage("ok"); + } catch (Exception e) { + fail("An error occured sending ack " + e.getMessage()); } - }; - conn2.addPacketListener(packetListener, packetFilter); - - // User1 creates a message to send to user2 - Message msg = chat1.createMessage(); - msg.setSubject("Any subject you want"); - msg.setBody("Hey John, this is my new green!!!!"); - // Create a XHTMLExtension Package and add it to the message - XHTMLExtension xhtmlExtension = new XHTMLExtension(); - xhtmlExtension.addBody( - "

Hey John, this is my new green!!!!

"); - msg.addExtension(xhtmlExtension); - - // User1 sends the message that contains the XHTML to user2 - try { - chat1.sendMessage(msg); - } catch (Exception e) { - fail("An error occured sending the message with XHTML"); } - // Wait for 2 seconds for a reply - msg = chat1.nextMessage(1000); - assertNotNull("No reply received", msg); + }; + conn2.addPacketListener(packetListener, packetFilter); + + // User1 creates a message to send to user2 + Message msg = chat1.createMessage(); + msg.setSubject("Any subject you want"); + msg.setBody("Hey John, this is my new green!!!!"); + // Create a XHTMLExtension Package and add it to the message + XHTMLExtension xhtmlExtension = new XHTMLExtension(); + xhtmlExtension.addBody( + "

Hey John, this is my new green!!!!

"); + msg.addExtension(xhtmlExtension); + + // User1 sends the message that contains the XHTML to user2 + try { + chat1.sendMessage(msg); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - if (conn2 != null) - conn2.close(); + fail("An error occured sending the message with XHTML"); } + // Wait for 2 seconds for a reply + msg = chat1.nextMessage(1000); + assertNotNull("No reply received", msg); } /** @@ -235,95 +189,110 @@ 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"; + // Create a chat for each connection + Chat chat1 = conn1.createChat(user2); + final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); - 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()); - - // Create a Listener that listens for Messages with the extension - //"http://jabber.org/protocol/xhtml-im" - // This listener will listen on the conn2 and answer an ACK if everything is ok - PacketFilter packetFilter = - new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im"); - PacketListener packetListener = new PacketListener() { - public void processPacket(Packet packet) { - int received = 0; - Message message = (Message) packet; - assertNotNull("Body is null", message.getBody()); - try { - XHTMLExtension xhtmlExtension = - (XHTMLExtension) message.getExtension( - "html", - "http://jabber.org/protocol/xhtml-im"); - assertNotNull( - "Message without extension \"http://jabber.org/protocol/xhtml-im\"", - xhtmlExtension); - assertTrue( - "Message without XHTML bodies", - xhtmlExtension.getBodiesCount() > 0); - for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) { - received++; - System.out.println((String) it.next()); - } - bodiesReceived = received; - } catch (ClassCastException e) { - fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); + // Create a Listener that listens for Messages with the extension + //"http://jabber.org/protocol/xhtml-im" + // This listener will listen on the conn2 and answer an ACK if everything is ok + PacketFilter packetFilter = + new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im"); + PacketListener packetListener = new PacketListener() { + public void processPacket(Packet packet) { + int received = 0; + Message message = (Message) packet; + assertNotNull("Body is null", message.getBody()); + try { + XHTMLExtension xhtmlExtension = + (XHTMLExtension) message.getExtension( + "html", + "http://jabber.org/protocol/xhtml-im"); + assertNotNull( + "Message without extension \"http://jabber.org/protocol/xhtml-im\"", + xhtmlExtension); + assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0); + for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) { + received++; + System.out.println((String) it.next()); } + bodiesReceived = received; + } catch (ClassCastException e) { + fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); } - }; - conn2.addPacketListener(packetListener, packetFilter); - - // 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."); - // Create an XHTMLExtension and add it to the message - XHTMLExtension xhtmlExtension = new XHTMLExtension(); - xhtmlExtension.addBody( - "

impresionante!

Como Emerson dijo una vez:

Una consistencia ridícula es el espantajo de mentes pequeñas.

"); - xhtmlExtension.addBody( - "

awesome!

As Emerson once said:

A foolish consistency is the hobgoblin of little minds.

"); - msg.addExtension(xhtmlExtension); - - // User1 sends the message that contains the XHTML to user2 - try { - bodiesSent = xhtmlExtension.getBodiesCount(); - bodiesReceived = 0; - chat1.sendMessage(msg); - } 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); + }; + conn2.addPacketListener(packetListener, packetFilter); + + // 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."); + // Create an XHTMLExtension and add it to the message + XHTMLExtension xhtmlExtension = new XHTMLExtension(); + xhtmlExtension.addBody( + "

impresionante!

Como Emerson dijo una vez:

Una consistencia ridícula es el espantajo de mentes pequeñas.

"); + xhtmlExtension.addBody( + "

awesome!

As Emerson once said:

A foolish consistency is the hobgoblin of little minds.

"); + msg.addExtension(xhtmlExtension); + + // User1 sends the message that contains the XHTML to user2 + try { + bodiesSent = xhtmlExtension.getBodiesCount(); + bodiesReceived = 0; + chat1.sendMessage(msg); + Thread.sleep(300); } catch (Exception e) { - fail(e.toString()); - } finally { - if (conn1 != null) - conn1.close(); - if (conn2 != null) - conn2.close(); + fail("An error occured sending the message with XHTML"); + } + // Wait half second so that the complete test can run + 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.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(); + } + }