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 { public class MessageEventManagerTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/** /**
* Constructor for MessageEventManagerTest. * Constructor for MessageEventManagerTest.
* @param name * @param name
@ -83,44 +89,24 @@ public class MessageEventManagerTest extends TestCase {
* occurs: offline, composing, displayed or delivered * occurs: offline, composing, displayed or delivered
*/ */
public void testSendMessageEventRequest() { public void testSendMessageEventRequest() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost"; // 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);
XMPPConnection conn1 = null; // Send the message that contains the notifications request
try {
try { chat1.sendMessage(msg);
// Connect to the server and log in the users } catch (Exception e) {
conn1 = new XMPPConnection(host); fail("An error occured sending the message");
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();
}
}
/** /**
* High level API test. * High level API test.
@ -132,68 +118,48 @@ public class MessageEventManagerTest extends TestCase {
* 3. User_1 will display any notification that receives * 3. User_1 will display any notification that receives
*/ */
public void testSendMessageEventRequestAndDisplayNotifications() { public void testSendMessageEventRequestAndDisplayNotifications() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost"; MessageEventManager messageEventManager = new MessageEventManager(conn1);
messageEventManager
XMPPConnection conn1 = null; .addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) {
try { System.out.println("From: " + from + " PacketID: " + packetID + "(delivered)");
// 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");
} }
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) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message");
} 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 * 5. User_2 will simulate that he/she has cancelled the reply
*/ */
public void testRequestsAndNotifications() { 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(); final ArrayList results = new ArrayList();
ArrayList resultsExpected = new ArrayList(); ArrayList resultsExpected = new ArrayList();
resultsExpected.add("deliveredNotificationRequested"); resultsExpected.add("deliveredNotificationRequested");
@ -230,106 +184,136 @@ public class MessageEventManagerTest extends TestCase {
resultsExpected.add("composingNotification"); resultsExpected.add("composingNotification");
resultsExpected.add("cancelledNotification"); resultsExpected.add("cancelledNotification");
try { // Create a chat for each connection
// Connect to the server and log in the users Chat chat1 = conn1.createChat(user2);
conn1 = new XMPPConnection(host);
conn1.login(server_user1, pass1);
conn2 = new XMPPConnection(host);
conn2.login(server_user2, pass2);
// Create a chat for each connection MessageEventManager messageEventManager1 = new MessageEventManager(conn1);
Chat chat1 = conn1.createChat(user2); messageEventManager1
.addMessageEventNotificationListener(new MessageEventNotificationListener() {
MessageEventManager messageEventManager1 = new MessageEventManager(conn1); public void deliveredNotification(String from, String packetID) {
messageEventManager1 results.add("deliveredNotification");
.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");
} }
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) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message");
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
} }
} }
/*
* @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();
}
} }

View file

@ -58,7 +58,6 @@ import junit.framework.TestCase;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
/** /**
* *
* Test the Roster Exchange extension using the high level API * Test the Roster Exchange extension using the high level API
@ -67,6 +66,16 @@ import org.jivesoftware.smack.*;
*/ */
public class RosterExchangeManagerTest extends TestCase { 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 entriesSent;
private int entriesReceived; private int entriesReceived;
@ -84,38 +93,14 @@ public class RosterExchangeManagerTest extends TestCase {
* 1. User_1 will send his/her roster to user_2 * 1. User_1 will send his/her roster to user_2
*/ */
public void testSendRoster() { public void testSendRoster() {
String host = "localhost"; // Send user1's roster to user2
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try { try {
// Connect to the server and log in the users RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1);
conn1 = new XMPPConnection(host); rosterExchangeManager.send(conn1.getRoster(), user2);
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) { } catch (Exception e) {
fail(e.toString()); e.printStackTrace();
} finally { fail("An error occured sending the roster");
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 * 1. User_1 will send his/her RosterGroups to user_2
*/ */
public void testSendRosterGroup() { public void testSendRosterGroup() {
String host = "localhost"; // Send user1's RosterGroups to user2
String server_user1 = "gato3";
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost";
XMPPConnection conn1 = null;
try { try {
// Connect to the server and log in the users RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1);
conn1 = new XMPPConnection(host); for (Iterator it = conn1.getRoster().getGroups(); it.hasNext();)
conn1.login(server_user1, pass1); rosterExchangeManager.send((RosterGroup) it.next(), user2);
// 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");
}
} catch (Exception e) { } catch (Exception e) {
fail(e.toString()); e.printStackTrace();
} finally { fail("An error occured sending the roster");
if (conn1 != null)
conn1.close();
} }
} }
/** /**
@ -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 * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/ */
public void testSendAndReceiveRoster() { public void testSendAndReceiveRoster() {
String host = "localhost"; RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1);
String server_user1 = "gato3"; RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a RosterExchangeListener that will iterate over the received roster entries
String user2 = "gato4@localhost"; RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
String pass2 = "gato4"; public void entriesReceived(String from, Iterator remoteRosterEntries) {
int received = 0;
XMPPConnection conn1 = null; assertNotNull("From is null", from);
XMPPConnection conn2 = null; assertNotNull("rosterEntries is null", remoteRosterEntries);
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
try { while (remoteRosterEntries.hasNext()) {
// Connect to the server and log in the users received++;
conn1 = new XMPPConnection(host); RemoteRosterEntry remoteEntry = (RemoteRosterEntry) remoteRosterEntries.next();
conn1.login(server_user1, pass1); System.out.println(remoteEntry);
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;
} }
}; 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");
} }
// Wait for 2 seconds };
Thread.sleep(2000); rosterExchangeManager2.addRosterListener(rosterExchangeListener);
assertEquals("Number of sent and received entries does not match", entriesSent, entriesReceived);
// 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) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with the roster");
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
} }
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 * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/ */
public void testSendAndAcceptRoster() { public void testSendAndAcceptRoster() {
String host = "localhost"; RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1);
String server_user1 = "gato3"; RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a RosterExchangeListener that will accept all the received roster entries
String user2 = "gato4@localhost"; RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
String pass2 = "gato4"; public void entriesReceived(String from, Iterator remoteRosterEntries) {
int received = 0;
XMPPConnection conn1 = null; assertNotNull("From is null", from);
XMPPConnection conn2 = null; assertNotNull("remoteRosterEntries is null", remoteRosterEntries);
assertTrue("Roster without entries", remoteRosterEntries.hasNext());
try { while (remoteRosterEntries.hasNext()) {
// Connect to the server and log in the users received++;
conn1 = new XMPPConnection(host); try {
conn1.login(server_user1, pass1); RemoteRosterEntry remoteRosterEntry =
conn2 = new XMPPConnection(host); (RemoteRosterEntry) remoteRosterEntries.next();
conn2.login(server_user2, pass2); conn2.getRoster().createEntry(
final Roster user2_roster = conn2.getRoster(); remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1); remoteRosterEntry.getGroupArrayNames());
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2); } catch (Exception 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();
user2_roster.createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
}
catch (XMPPException e) {
fail(e.toString());
}
} }
entriesReceived = received;
} }
}; 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");
} }
// Wait for 2 seconds };
Thread.sleep(2000); rosterExchangeManager2.addRosterListener(rosterExchangeListener);
assertEquals("Number of sent and received entries does not match", entriesSent, entriesReceived);
// 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) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with the roster");
} finally { }
if (conn1 != null) assertEquals(
conn1.close(); "Number of sent and received entries does not match",
if (conn2 != null) entriesSent,
conn2.close(); 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();
}
} }

View file

@ -66,6 +66,12 @@ import junit.framework.TestCase;
*/ */
public class XHTMLManagerTest extends 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 bodiesSent;
private int bodiesReceived; 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 * 1. User_1 will send a message with formatted text (XHTML) to user_2
*/ */
public void testSendSimpleXHTMLMessage() { public void testSendSimpleXHTMLMessage() {
String host = "localhost"; // User1 creates a chat with user2
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
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 { try {
// Connect to the server chat1.sendMessage(msg);
conn1 = new XMPPConnection(host); Thread.sleep(200);
// 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");
}
} catch (Exception e) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with XHTML");
} finally {
if (conn1 != null)
conn1.close();
} }
} }
@ -144,95 +129,66 @@ public class XHTMLManagerTest extends TestCase {
* something is wrong * something is wrong
*/ */
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost"; final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a listener for the chat that will check if the received message includes
String user2 = "gato4@localhost"; // an XHTML extension. Answer an ACK if everything is ok
String pass2 = "gato4"; PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
XMPPConnection conn1 = null; Message message = (Message) packet;
XMPPConnection conn2 = null; assertTrue(
"The received message is not an XHTML Message",
try { XHTMLManager.isXHTMLMessage(message));
// Connect to the server and log in the users try {
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;
assertTrue( assertTrue(
"The received message is not an XHTML Message", "Message without XHTML bodies",
XHTMLManager.isXHTMLMessage(message)); XHTMLManager.getBodies(message).hasNext());
try { for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) {
assertTrue( String body = (String) it.next();
"Message without XHTML bodies", System.out.println(body);
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());
} }
} 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); chat2.addMessageListener(packetListener);
assertNotNull("No reply received", msg);
// 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) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with XHTML");
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
} }
// 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 * something is wrong
*/ */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost"; final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a listener for the chat that will check if the received message includes
String user2 = "gato4@localhost"; // an XHTML extension. Answer an ACK if everything is ok
String pass2 = "gato4"; PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
XMPPConnection conn1 = null; int received = 0;
XMPPConnection conn2 = null; Message message = (Message) packet;
assertTrue(
try { "The received message is not an XHTML Message",
// Connect to the server and log in the users XHTMLManager.isXHTMLMessage(message));
conn1 = new XMPPConnection(host); try {
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;
assertTrue( assertTrue(
"The received message is not an XHTML Message", "Message without XHTML bodies",
XHTMLManager.isXHTMLMessage(message)); XHTMLManager.getBodies(message).hasNext());
try { for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) {
assertTrue( received++;
"Message without XHTML bodies", String body = (String) it.next();
XHTMLManager.getBodies(message).hasNext()); System.out.println(body);
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");
} }
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 // Wait half second so that the complete test can run
Thread.sleep(500); Thread.sleep(300);
assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
} catch (Exception e) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with XHTML");
} finally { }
if (conn1 != null) assertEquals(
conn1.close(); "Number of sent and received XHTMP bodies does not match",
if (conn2 != null) bodiesSent,
conn2.close(); 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();
}
} }

View file

@ -66,6 +66,12 @@ import junit.framework.TestCase;
*/ */
public class MessageEventTest extends 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. * Constructor for MessageEventTest.
* @param name * @param name
@ -82,48 +88,28 @@ public class MessageEventTest extends TestCase {
* occurs: offline, composing, displayed or delivered * occurs: offline, composing, displayed or delivered
*/ */
public void testSendMessageEventRequest() { public void testSendMessageEventRequest() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost"; // Create the message to send with the roster
Message msg = chat1.createMessage();
XMPPConnection conn1 = null; 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 { try {
// Connect to the server and log in the users chat1.sendMessage(msg);
conn1 = new XMPPConnection(host); // Wait half second so that the complete test can run
conn1.login(server_user1, pass1); Thread.sleep(200);
// 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");
}
} catch (Exception e) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message");
} finally {
if (conn1 != null)
conn1.close();
} }
} }
@ -137,67 +123,91 @@ public class MessageEventTest extends TestCase {
* 3. User_1 will display any notification that receives * 3. User_1 will display any notification that receives
*/ */
public void testSendMessageEventRequestAndDisplayNotifications() { public void testSendMessageEventRequestAndDisplayNotifications() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost"; // 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
XMPPConnection conn1 = null; PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event");
PacketListener packetListener = new PacketListener() {
try { public void processPacket(Packet packet) {
// Connect to the server and log in the users Message message = (Message) packet;
conn1 = new XMPPConnection(host); try {
conn1.login(server_user1, pass1); MessageEvent messageEvent =
(MessageEvent) message.getExtension("x", "jabber:x:event");
// Create a chat for each connection assertNotNull("Message without extension \"jabber:x:event\"", messageEvent);
Chat chat1 = conn1.createChat(user2); assertTrue(
"Message event is a request not a notification",
// Create a Listener that listens for Messages with the extension "jabber:x:roster" !messageEvent.isMessageEventRequest());
// This listener will listen on the conn2 and answer an ACK if everything is ok System.out.println(messageEvent.toXML());
PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event"); } catch (ClassCastException e) {
PacketListener packetListener = new PacketListener() { fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
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) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message");
} finally {
if (conn1 != null)
conn1.close();
} }
} }
/*
* @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();
}
} }

View file

@ -21,6 +21,16 @@ import junit.framework.TestCase;
*/ */
public class RosterExchangeTest extends 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. * Constructor for RosterExchangeTest.
* @param arg0 * @param arg0
@ -35,135 +45,84 @@ public class RosterExchangeTest extends TestCase {
* 1. User_1 will send his/her roster entries to user_2 * 1. User_1 will send his/her roster entries to user_2
*/ */
public void testSendRosterEntries() { public void testSendRosterEntries() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost"; // Create the message to send with the roster
Message msg = chat1.createMessage();
XMPPConnection conn1 = null; 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 { try {
// Connect to the server and log in the users chat1.sendMessage(msg);
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");
}
} catch (Exception e) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with the roster");
} finally {
if (conn1 != null)
conn1.close();
} }
} }
/** /**
* Low level API test. * Low level API test.
* 1. User_1 will send his/her roster entries to user_2 * 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 * 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 * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/ */
public void testSendAndReceiveRosterEntries() { public void testSendAndReceiveRosterEntries() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost"; final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a Listener that listens for Messages with the extension "jabber:x:roster"
String user2 = "gato4@localhost"; // This listener will listen on the conn2 and answer an ACK if everything is ok
String pass2 = "gato4"; PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster");
PacketListener packetListener = new PacketListener() {
XMPPConnection conn1 = null; public void processPacket(Packet packet) {
XMPPConnection conn2 = null; Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try { try {
// Connect to the server and log in the users RosterExchange rosterExchange =
conn1 = new XMPPConnection(host); (RosterExchange) message.getExtension("x", "jabber:x:roster");
conn1.login(server_user1, pass1); assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
conn2 = new XMPPConnection(host); assertTrue(
conn2.login(server_user2, pass2); "Roster without entries",
rosterExchange.getRosterEntries().hasNext());
// Create a chat for each connection for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
Chat chat1 = conn1.createChat(user2); RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
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());
} }
} 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); conn2.addPacketListener(packetListener, packetFilter);
assertNotNull("No reply received", msg);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
}
// 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 * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong
*/ */
public void testSendAndAcceptRosterEntries() { public void testSendAndAcceptRosterEntries() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost"; final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a Listener that listens for Messages with the extension "jabber:x:roster"
String user2 = "gato4@localhost"; // This listener will listen on the conn2, save the roster entries and answer an ACK if everything is ok
String pass2 = "gato4"; PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster");
PacketListener packetListener = new PacketListener() {
XMPPConnection conn1 = null; public void processPacket(Packet packet) {
XMPPConnection conn2 = null; Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try { try {
// Connect to the server and log in the users RosterExchange rosterExchange =
conn1 = new XMPPConnection(host); (RosterExchange) message.getExtension("x", "jabber:x:roster");
conn1.login(server_user1, pass1); assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
conn2 = new XMPPConnection(host); assertTrue(
conn2.login(server_user2, pass2); "Roster without entries",
final Roster user2_roster = conn2.getRoster(); rosterExchange.getRosterEntries().hasNext());
// Add the roster entries to user2's roster
// Create a chat for each connection for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
Chat chat1 = conn1.createChat(user2); RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); conn2.getRoster().createEntry(
remoteRosterEntry.getUser(),
// Create a Listener that listens for Messages with the extension "jabber:x:roster" remoteRosterEntry.getName(),
// This listener will listen on the conn2, save the roster entries and answer an ACK if everything is ok remoteRosterEntry.getGroupArrayNames());
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());
} }
} 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); conn2.addPacketListener(packetListener, packetFilter);
assertNotNull("No reply received", msg);
} catch (Exception e) {
fail(e.toString());
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
}
// 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();
} }
} }

View file

@ -67,6 +67,12 @@ import junit.framework.TestCase;
*/ */
public class XHTMLExtensionTest extends 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 bodiesSent;
private int bodiesReceived; 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 * 1. User_1 will send a message with formatted text (XHTML) to user_2
*/ */
public void testSendSimpleXHTMLMessage() { public void testSendSimpleXHTMLMessage() {
String host = "localhost"; // User1 creates a chat with user2
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost";
String pass1 = "gato3";
String user2 = "gato4@localhost"; // User1 creates a message to send to user2
Message msg = chat1.createMessage();
XMPPConnection conn1 = null; 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(
"<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try { try {
// Connect to the server chat1.sendMessage(msg);
conn1 = new XMPPConnection(host); Thread.sleep(200);
// 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(
"<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
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");
}
} catch (Exception e) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with XHTML");
} finally {
if (conn1 != null)
conn1.close();
} }
} }
@ -136,94 +121,63 @@ public class XHTMLExtensionTest extends TestCase {
* something is wrong * something is wrong
*/ */
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost"; final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a Listener that listens for Messages with the extension
String user2 = "gato4@localhost"; //"http://jabber.org/protocol/xhtml-im"
String pass2 = "gato4"; // This listener will listen on the conn2 and answer an ACK if everything is ok
PacketFilter packetFilter =
XMPPConnection conn1 = null; new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
XMPPConnection conn2 = null; PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
try { Message message = (Message) packet;
// Connect to the server and log in the users assertNotNull("Body is null", message.getBody());
conn1 = new XMPPConnection(host); try {
conn1.login(server_user1, pass1); XHTMLExtension xhtmlExtension =
// Wait a few milliseconds between each login (XHTMLExtension) message.getExtension(
Thread.sleep(250); "html",
conn2 = new XMPPConnection(host); "http://jabber.org/protocol/xhtml-im");
conn2.login(server_user2, pass2); assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
// 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\"",
xhtmlExtension); xhtmlExtension);
assertTrue( assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
"Message without XHTML bodies", for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
xhtmlExtension.getBodiesCount() > 0); String body = (String) it.next();
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) { System.out.println(body);
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());
} }
} 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(
"<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
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); conn2.addPacketListener(packetListener, packetFilter);
assertNotNull("No reply received", msg);
// 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(
"<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
} catch (Exception e) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with XHTML");
} finally {
if (conn1 != null)
conn1.close();
if (conn2 != null)
conn2.close();
} }
// 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 * something is wrong
*/ */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
String host = "localhost"; // Create a chat for each connection
String server_user1 = "gato3"; Chat chat1 = conn1.createChat(user2);
String user1 = "gato3@localhost"; final Chat chat2 = new Chat(conn2, user1, chat1.getChatID());
String pass1 = "gato3";
String server_user2 = "gato4"; // Create a Listener that listens for Messages with the extension
String user2 = "gato4@localhost"; //"http://jabber.org/protocol/xhtml-im"
String pass2 = "gato4"; // This listener will listen on the conn2 and answer an ACK if everything is ok
PacketFilter packetFilter =
XMPPConnection conn1 = null; new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
XMPPConnection conn2 = null; PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
try { int received = 0;
// Connect to the server and log in the users Message message = (Message) packet;
conn1 = new XMPPConnection(host); assertNotNull("Body is null", message.getBody());
conn1.login(server_user1, pass1); try {
// Wait a few milliseconds between each login XHTMLExtension xhtmlExtension =
Thread.sleep(250); (XHTMLExtension) message.getExtension(
conn2 = new XMPPConnection(host); "html",
conn2.login(server_user2, pass2); "http://jabber.org/protocol/xhtml-im");
assertNotNull(
// Create a chat for each connection "Message without extension \"http://jabber.org/protocol/xhtml-im\"",
Chat chat1 = conn1.createChat(user2); xhtmlExtension);
final Chat chat2 = new Chat(conn2, user1, chat1.getChatID()); assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
// Create a Listener that listens for Messages with the extension received++;
//"http://jabber.org/protocol/xhtml-im" System.out.println((String) it.next());
// 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");
} }
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(
"<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridícula es el espantajo de mentes pequeñas.</p></blockquote></body>");
xhtmlExtension.addBody(
"<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
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); conn2.addPacketListener(packetListener, packetFilter);
assertEquals("Number of sent and received XHTMP bodies does not match", bodiesSent, bodiesReceived);
// 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(
"<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridícula es el espantajo de mentes pequeñas.</p></blockquote></body>");
xhtmlExtension.addBody(
"<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
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) { } catch (Exception e) {
fail(e.toString()); fail("An error occured sending the message with XHTML");
} finally { }
if (conn1 != null) // Wait half second so that the complete test can run
conn1.close(); assertEquals(
if (conn2 != null) "Number of sent and received XHTMP bodies does not match",
conn2.close(); 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();
}
} }