1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 23:42:06 +01:00

Changes superclass to SmackTestCase.SMACK-142

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2339 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2004-07-12 13:36:13 +00:00 committed by gdombiak
parent 3886ed0736
commit d471d42fbf
14 changed files with 490 additions and 992 deletions

View file

@ -55,21 +55,15 @@ package org.jivesoftware.smack;
import java.util.Date; import java.util.Date;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
/** /**
* Tests the chat functionality. * Tests the chat functionality.
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class ChatTest extends TestCase { public class ChatTest extends SmackTestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/** /**
* Constructor for ChatTest. * Constructor for ChatTest.
@ -79,10 +73,10 @@ public class ChatTest extends TestCase {
super(arg0); super(arg0);
} }
public void testChat() { public void testProperties() {
try { try {
Chat newChat = conn1.createChat(user2); Chat newChat = getConnection(0).createChat(getFullJID(1));
Chat newChat2 = new Chat(conn2, user1, newChat.getThreadID()); Chat newChat2 = new Chat(getConnection(1), getFullJID(0), newChat.getThreadID());
Message msg = newChat.createMessage(); Message msg = newChat.createMessage();
@ -126,48 +120,12 @@ public class ChatTest extends TestCase {
msg2.getProperty("birthdate")); msg2.getProperty("birthdate"));
} }
catch (XMPPException e) { catch (XMPPException e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
}
}
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("gato10", "gato10");
conn2.getAccountManager().createAccount("gato11", "gato11");
// Login with the test accounts
conn1.login("gato10", "gato10");
conn2.login("gato11", "gato11");
user1 = "gato10@" + conn1.getHost() + "/Smack";
user2 = "gato11@" + conn2.getHost() + "/Smack";
}
catch (Exception e) {
fail(e.getMessage()); fail(e.getMessage());
e.printStackTrace(); }
} }
} protected int getMaxConnections() {
return 2;
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

@ -54,16 +54,10 @@ package org.jivesoftware.smack;
import org.jivesoftware.smack.filter.*; import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
public class PacketReaderTest extends TestCase { public class PacketReaderTest extends SmackTestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/** /**
* Constructor for PacketReaderTest. * Constructor for PacketReaderTest.
@ -86,13 +80,13 @@ public class PacketReaderTest extends TestCase {
return "<query xmlns=\"my:ns:test\"/>"; return "<query xmlns=\"my:ns:test\"/>";
} }
}; };
iqPacket.setTo(user2); iqPacket.setTo(getBareJID(1));
iqPacket.setType(IQ.Type.GET); iqPacket.setType(IQ.Type.GET);
// Send the IQ and wait for the answer // Send the IQ and wait for the answer
PacketCollector collector = conn1.createPacketCollector( PacketCollector collector = getConnection(0).createPacketCollector(
new PacketIDFilter(iqPacket.getPacketID())); new PacketIDFilter(iqPacket.getPacketID()));
conn1.sendPacket(iqPacket); getConnection(0).sendPacket(iqPacket);
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
if (response == null) { if (response == null) {
fail("No response from the other user."); fail("No response from the other user.");
@ -102,45 +96,7 @@ public class PacketReaderTest extends TestCase {
collector.cancel(); collector.cancel();
} }
/* protected int getMaxConnections() {
* @see TestCase#setUp() return 2;
*/
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

@ -55,20 +55,15 @@ package org.jivesoftware.smack;
import java.util.Iterator; import java.util.Iterator;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import junit.framework.TestCase;
/** /**
* Tests the Roster functionality by creating and removing roster entries. * Tests the Roster functionality by creating and removing roster entries.
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class RosterTest extends TestCase { public class RosterTest extends SmackTestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private XMPPConnection conn3 = null;
/** /**
* Constructor for RosterTest. * Constructor for RosterTest.
@ -86,15 +81,16 @@ public class RosterTest extends TestCase {
public void testDeleteAllRosterGroupEntries() { public void testDeleteAllRosterGroupEntries() {
try { try {
// Add a new roster entry // Add a new roster entry
conn1.getRoster().createEntry("gato11@" + conn1.getHost(), "gato11", new String[] {"Friends", "Family"}); Roster roster = getConnection(0).getRoster();
conn1.getRoster().createEntry("gato12@" + conn1.getHost(), "gato12", new String[] {"Family"}); roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends", "Family" });
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
// Wait until the server confirms the new entries // Wait until the server confirms the new entries
while (conn1.getRoster().getEntryCount() != 2) { while (roster.getEntryCount() != 2) {
Thread.sleep(50); Thread.sleep(50);
} }
Iterator it = conn1.getRoster().getEntries(); Iterator it = roster.getEntries();
while (it.hasNext()) { while (it.hasNext()) {
RosterEntry entry = (RosterEntry) it.next(); RosterEntry entry = (RosterEntry) it.next();
Iterator groups = entry.getGroups(); Iterator groups = entry.getGroups();
@ -105,14 +101,32 @@ public class RosterTest extends TestCase {
} }
Thread.sleep(750); Thread.sleep(750);
assertEquals("The number of entries in conn2 should be 1", 1, conn2.getRoster().getEntryCount()); assertEquals(
assertEquals("The number of groups in conn2 should be 0", 0, conn2.getRoster().getGroupCount()); "The number of entries in connection 1 should be 1",
1,
getConnection(1).getRoster().getEntryCount());
assertEquals(
"The number of groups in connection 1 should be 0",
0,
getConnection(1).getRoster().getGroupCount());
assertEquals("The number of entries in conn3 should be 1", 1, conn3.getRoster().getEntryCount()); assertEquals(
assertEquals("The number of groups in conn3 should be 0", 0, conn3.getRoster().getGroupCount()); "The number of entries in connection 2 should be 1",
1,
getConnection(2).getRoster().getEntryCount());
assertEquals(
"The number of groups in connection 2 should be 0",
0,
getConnection(2).getRoster().getGroupCount());
assertEquals("The number of entries in conn1 should be 2", 2, conn1.getRoster().getEntryCount()); assertEquals(
assertEquals("The number of groups in conn1 should be 0", 0, conn1.getRoster().getGroupCount()); "The number of entries in connection 0 should be 2",
2,
roster.getEntryCount());
assertEquals(
"The number of groups in connection 0 should be 0",
0,
roster.getGroupCount());
cleanUpRoster(); cleanUpRoster();
} }
@ -129,23 +143,30 @@ public class RosterTest extends TestCase {
public void testDeleteAllRosterEntries() { public void testDeleteAllRosterEntries() {
try { try {
// Add a new roster entry // Add a new roster entry
conn1.getRoster().createEntry("gato11@" + conn1.getHost(), "gato11", new String[] {"Friends"}); Roster roster = getConnection(0).getRoster();
conn1.getRoster().createEntry("gato12@" + conn1.getHost(), "gato12", new String[] {"Family"}); roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
Thread.sleep(200); Thread.sleep(200);
Iterator it = conn1.getRoster().getEntries(); Iterator it = roster.getEntries();
while (it.hasNext()) { while (it.hasNext()) {
RosterEntry entry = (RosterEntry) it.next(); RosterEntry entry = (RosterEntry) it.next();
conn1.getRoster().removeEntry(entry); roster.removeEntry(entry);
Thread.sleep(250); Thread.sleep(250);
} }
assertEquals("The number of entries in conn1 should be 0", 0, conn1.getRoster().getEntryCount()); assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
assertEquals("The number of groups in conn1 should be 0", 0, conn1.getRoster().getGroupCount()); assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount());
assertEquals("The number of entries in conn2 should be 0", 0, conn2.getRoster().getEntryCount()); assertEquals(
assertEquals("The number of groups in conn2 should be 0", 0, conn2.getRoster().getGroupCount()); "Wrong number of entries in connection 1",
0,
getConnection(1).getRoster().getEntryCount());
assertEquals(
"Wrong number of groups in connection 1",
0,
getConnection(1).getRoster().getGroupCount());
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage()); fail(e.getMessage());
@ -160,23 +181,30 @@ public class RosterTest extends TestCase {
public void testDeleteAllUnfiledRosterEntries() { public void testDeleteAllUnfiledRosterEntries() {
try { try {
// Add a new roster entry // Add a new roster entry
conn1.getRoster().createEntry("gato11@" + conn1.getHost(), "gato11", null); Roster roster = getConnection(0).getRoster();
conn1.getRoster().createEntry("gato12@" + conn1.getHost(), "gato12", null); roster.createEntry(getBareJID(1), "gato11", null);
roster.createEntry(getBareJID(2), "gato12", null);
Thread.sleep(200); Thread.sleep(200);
Iterator it = conn1.getRoster().getEntries(); Iterator it = roster.getEntries();
while (it.hasNext()) { while (it.hasNext()) {
RosterEntry entry = (RosterEntry) it.next(); RosterEntry entry = (RosterEntry) it.next();
conn1.getRoster().removeEntry(entry); roster.removeEntry(entry);
Thread.sleep(250); Thread.sleep(250);
} }
assertEquals("The number of entries in conn1 should be 0", 0, conn1.getRoster().getEntryCount()); assertEquals("Wrong number of entries in connection 0", 0, roster.getEntryCount());
assertEquals("The number of groups in conn1 should be 0", 0, conn1.getRoster().getGroupCount()); assertEquals("Wrong number of groups in connection 0", 0, roster.getGroupCount());
assertEquals("The number of entries in conn2 should be 0", 0, conn2.getRoster().getEntryCount()); assertEquals(
assertEquals("The number of groups in conn2 should be 0", 0, conn2.getRoster().getGroupCount()); "Wrong number of entries in connection 1",
0,
getConnection(1).getRoster().getEntryCount());
assertEquals(
"Wrong number of groups in connection 1",
0,
getConnection(1).getRoster().getGroupCount());
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage()); fail(e.getMessage());
@ -193,21 +221,22 @@ public class RosterTest extends TestCase {
public void testChangeNameToUnfiledEntry() { public void testChangeNameToUnfiledEntry() {
try { try {
// Add a new roster entry // Add a new roster entry
conn1.getRoster().createEntry("gato11@" + conn1.getHost(), null, null); Roster roster = getConnection(0).getRoster();
roster.createEntry(getBareJID(1), null, null);
Thread.sleep(200); Thread.sleep(200);
// Change the roster entry name and check if the change was made // Change the roster entry name and check if the change was made
Iterator it = conn1.getRoster().getEntries(); Iterator it = roster.getEntries();
while (it.hasNext()) { while (it.hasNext()) {
RosterEntry entry = (RosterEntry) it.next(); RosterEntry entry = (RosterEntry) it.next();
entry.setName("gato11"); entry.setName("gato11");
assertEquals("gato11", entry.getName()); assertEquals("gato11", entry.getName());
} }
// Reload the roster and check the name again // Reload the roster and check the name again
conn1.getRoster().reload(); roster.reload();
Thread.sleep(2000); Thread.sleep(2000);
it = conn1.getRoster().getEntries(); it = roster.getEntries();
while (it.hasNext()) { while (it.hasNext()) {
RosterEntry entry = (RosterEntry) it.next(); RosterEntry entry = (RosterEntry) it.next();
assertEquals("gato11", entry.getName()); assertEquals("gato11", entry.getName());
@ -227,23 +256,29 @@ public class RosterTest extends TestCase {
public void testRenameRosterGroup() { public void testRenameRosterGroup() {
try { try {
// Add a new roster entry // Add a new roster entry
conn1.getRoster().createEntry("gato11@" + conn1.getHost(), "gato11", new String[] {"Friends"}); Roster roster = getConnection(0).getRoster();
conn1.getRoster().createEntry("gato12@" + conn1.getHost(), "gato12", new String[] {"Friends"}); roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends" });
roster.createEntry(getBareJID(2), "gato12", new String[] { "Friends" });
Thread.sleep(200); Thread.sleep(200);
conn1.getRoster().getGroup("Friends").setName("Amigos"); roster.getGroup("Friends").setName("Amigos");
Thread.sleep(200); Thread.sleep(200);
assertNull("The group Friends still exists", conn1.getRoster().getGroup("Friends")); assertNull("The group Friends still exists", roster.getGroup("Friends"));
assertNotNull("The group Amigos does not exist", conn1.getRoster().getGroup("Amigos")); assertNotNull("The group Amigos does not exist", roster.getGroup("Amigos"));
assertEquals("Wrong number of entries in the group Amigos ", 2, conn1.getRoster().getGroup("Amigos").getEntryCount()); assertEquals(
"Wrong number of entries in the group Amigos",
2,
roster.getGroup("Amigos").getEntryCount());
roster.getGroup("Amigos").setName("");
conn1.getRoster().getGroup("Amigos").setName("");
Thread.sleep(200); Thread.sleep(200);
assertNull("The group Amigos still exists", conn1.getRoster().getGroup("Amigos")); assertNull("The group Amigos still exists", roster.getGroup("Amigos"));
assertNotNull("The group with no name does not exist", conn1.getRoster().getGroup("")); assertNotNull("The group with no name does not exist", roster.getGroup(""));
assertEquals("Wrong number of entries in the group \"\" ", 2, conn1.getRoster().getGroup("").getEntryCount()); assertEquals(
"Wrong number of entries in the group \"\" ",
2,
roster.getGroup("").getEntryCount());
cleanUpRoster(); cleanUpRoster();
Thread.sleep(200); Thread.sleep(200);
@ -260,32 +295,40 @@ public class RosterTest extends TestCase {
try { try {
Presence presence = null; Presence presence = null;
XMPPConnection conn4 = new XMPPConnection("localhost"); // Create another connection for the same user of connection 1
conn4.login("gato11", "gato11", "Home"); XMPPConnection conn4 = new XMPPConnection(getHost());
conn4.login(getUsername(1), getUsername(1), "Home");
// Add a new roster entry // Add a new roster entry
conn1.getRoster().createEntry("gato11@" + conn1.getHost(), "gato11", null); Roster roster = getConnection(0).getRoster();
roster.createEntry(getBareJID(1), "gato11", null);
Thread.sleep(200); Thread.sleep(250);
// Check that a presence is returned for a user // Check that a presence is returned for a user
presence = conn1.getRoster().getPresence("gato11@" + conn1.getHost()); presence = roster.getPresence(getBareJID(1));
assertNotNull("Returned a null Presence for an existing user", presence); assertNotNull("Returned a null Presence for an existing user", presence);
// Check that the right presence is returned for a user+resource // Check that the right presence is returned for a user+resource
presence = conn1.getRoster().getPresenceResource("gato11@" + conn1.getHost() + "/Home"); presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getHost() + "/Home");
assertEquals("Returned the wrong Presence", StringUtils.parseResource(presence.getFrom()), "Home"); assertEquals(
"Returned the wrong Presence",
StringUtils.parseResource(presence.getFrom()),
"Home");
// Check that the right presence is returned for a user+resource // Check that the right presence is returned for a user+resource
presence = conn1.getRoster().getPresenceResource("gato11@" + conn1.getHost() + "/Smack"); presence = roster.getPresenceResource(getFullJID(1));
assertEquals("Returned the wrong Presence", StringUtils.parseResource(presence.getFrom()), "Smack"); assertEquals(
"Returned the wrong Presence",
StringUtils.parseResource(presence.getFrom()),
"Smack");
// Check that the no presence is returned for a non-existent user+resource // Check that the no presence is returned for a non-existent user+resource
presence = conn1.getRoster().getPresenceResource("gato15@" + conn1.getHost() + "/Smack"); presence = roster.getPresenceResource("noname@" + getHost() + "/Smack");
assertNull("Returned a Presence for a non-existing user", presence); assertNull("Returned a Presence for a non-existing user", presence);
// Check that the returned presences are correct // Check that the returned presences are correct
Iterator presences = conn1.getRoster().getPresences("gato11@" + conn1.getHost()); Iterator presences = roster.getPresences(getBareJID(1));
int count = 0; int count = 0;
while (presences.hasNext()) { while (presences.hasNext()) {
count++; count++;
@ -297,7 +340,7 @@ public class RosterTest extends TestCase {
conn4.close(); conn4.close();
// Check that the returned presences are correct // Check that the returned presences are correct
presences = conn1.getRoster().getPresences("gato11@" + conn1.getHost()); presences = roster.getPresences(getBareJID(1));
count = 0; count = 0;
while (presences.hasNext()) { while (presences.hasNext()) {
count++; count++;
@ -319,74 +362,46 @@ public class RosterTest extends TestCase {
*/ */
private void cleanUpRoster() { private void cleanUpRoster() {
// Delete all the entries from the roster // Delete all the entries from the roster
Iterator it = conn1.getRoster().getEntries(); Iterator it = getConnection(0).getRoster().getEntries();
while (it.hasNext()) { while (it.hasNext()) {
RosterEntry entry = (RosterEntry) it.next(); RosterEntry entry = (RosterEntry) it.next();
conn1.getRoster().removeEntry(entry); getConnection(0).getRoster().removeEntry(entry);
} }
try { try {
Thread.sleep(700); Thread.sleep(700);
} }
catch (Exception e) {}
assertEquals("The number of entries in conn1 should be 0", 0, conn1.getRoster().getEntryCount());
assertEquals("The number of groups in conn1 should be 0", 0, conn1.getRoster().getGroupCount());
assertEquals("The number of entries in conn2 should be 0", 0, conn2.getRoster().getEntryCount());
assertEquals("The number of groups in conn2 should be 0", 0, conn2.getRoster().getGroupCount());
assertEquals("The number of entries in conn3 should be 0", 0, conn3.getRoster().getEntryCount());
assertEquals("The number of groups in conn3 should be 0", 0, conn3.getRoster().getGroupCount());
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
// Use a second connection to create and delete the entry that will be added and
// deleted from the roster
conn2 = new XMPPConnection("localhost");
// Use a third connection to create and delete the entry that will be added and
// deleted from the roster
conn3 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato10", "gato10");
conn2.getAccountManager().createAccount("gato11", "gato11");
conn3.getAccountManager().createAccount("gato12", "gato12");
// Login with the test accounts
conn1.login("gato10", "gato10");
conn2.login("gato11", "gato11");
conn3.login("gato12", "gato12");
}
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
} }
assertEquals(
"Wrong number of entries in connection 0",
0,
getConnection(0).getRoster().getEntryCount());
assertEquals(
"Wrong number of groups in connection 0",
0,
getConnection(0).getRoster().getGroupCount());
assertEquals(
"Wrong number of entries in connection 1",
0,
getConnection(1).getRoster().getEntryCount());
assertEquals(
"Wrong number of groups in connection 1",
0,
getConnection(1).getRoster().getGroupCount());
assertEquals(
"Wrong number of entries in connection 2",
0,
getConnection(2).getRoster().getEntryCount());
assertEquals(
"Wrong number of groups in connection 2",
0,
getConnection(2).getRoster().getGroupCount());
} }
/* protected int getMaxConnections() {
* @see TestCase#tearDown() return 3;
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
conn3.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
conn3.close();
} }
} }

View file

@ -53,24 +53,16 @@
package org.jivesoftware.smackx; package org.jivesoftware.smackx;
import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
/** /**
* Tests the DataForms extensions. * Tests the DataForms extensions.
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class FormTest extends TestCase { public class FormTest extends SmackTestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/** /**
* Constructor for FormTest. * Constructor for FormTest.
@ -109,10 +101,20 @@ public class FormTest extends TestCase {
field.setLabel("Enter a description"); field.setLabel("Enter a description");
field.setType(FormField.TYPE_TEXT_MULTI); field.setType(FormField.TYPE_TEXT_MULTI);
formToSend.addField(field); formToSend.addField(field);
// Add a boolean variable
field = new FormField("time");
field.setLabel("Is this your first case?");
field.setType(FormField.TYPE_BOOLEAN);
formToSend.addField(field);
// Add a text variable where an int value is expected
field = new FormField("age");
field.setLabel("How old are you?");
field.setType(FormField.TYPE_TEXT_SINGLE);
formToSend.addField(field);
// Create the chats between the two participants // Create the chats between the two participants
Chat chat = conn1.createChat(user2); Chat chat = getConnection(0).createChat(getBareJID(1));
Chat chat2 = new Chat(conn2, user1, chat.getThreadID()); Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat.getThreadID());
Message msg = chat.createMessage(); Message msg = chat.createMessage();
msg.setBody("To enter a case please fill out this form and send it back to me"); msg.setBody("To enter a case please fill out this form and send it back to me");
@ -132,12 +134,23 @@ public class FormTest extends TestCase {
// Obtain the form to send with the replies // Obtain the form to send with the replies
Form completedForm = formToRespond.createAnswerForm(); Form completedForm = formToRespond.createAnswerForm();
assertNotNull(completedForm.getField("hidden_var")); assertNotNull(completedForm.getField("hidden_var"));
// Check that a field of type String does not accept booleans
try {
completedForm.setAnswer("name", true);
fail("A boolean value was set to a field of type String");
}
catch (IllegalArgumentException e) {
}
completedForm.setAnswer("name", "Credit card number invalid"); completedForm.setAnswer("name", "Credit card number invalid");
completedForm.setAnswer( completedForm.setAnswer(
"description", "description",
"The ATM says that my credit card number is invalid. What's going on?"); "The ATM says that my credit card number is invalid. What's going on?");
completedForm.setAnswer("time", true);
completedForm.setAnswer("age", 20);
// Create a new message to send with the completed form
msg2 = chat2.createMessage(); msg2 = chat2.createMessage();
msg2.setBody("To enter a case please fill out this form and send it back to me"); msg2.setBody("To enter a case please fill out this form and send it back to me");
// Add the completed form to the message
msg2.addExtension(completedForm.getDataFormToSend()); msg2.addExtension(completedForm.getDataFormToSend());
// Send the message with the completed form // Send the message with the completed form
chat2.sendMessage(msg2); chat2.sendMessage(msg2);
@ -152,6 +165,9 @@ public class FormTest extends TestCase {
assertEquals( assertEquals(
completedForm.getField("name").getValues().next(), completedForm.getField("name").getValues().next(),
"Credit card number invalid"); "Credit card number invalid");
assertNotNull(completedForm.getField("time"));
assertNotNull(completedForm.getField("age"));
assertEquals("The age is bad", "20", completedForm.getField("age").getValues().next());
} }
catch (XMPPException ex) { catch (XMPPException ex) {
@ -159,41 +175,8 @@ public class FormTest extends TestCase {
} }
} }
protected void setUp() throws Exception { protected int getMaxConnections() {
super.setUp(); return 2;
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());
}
}
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

@ -54,28 +54,34 @@ package org.jivesoftware.smackx;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter; import org.jivesoftware.smack.filter.PacketExtensionFilter;
import junit.framework.TestCase;
/** /**
* *
* *
* @author Matt Tucker * @author Matt Tucker
*/ */
public class GroupChatInvitationTest extends TestCase { public class GroupChatInvitationTest extends SmackTestCase {
private XMPPConnection con1 = null;
private XMPPConnection con2 = null;
private PacketCollector collector = null; private PacketCollector collector = null;
/**
* Constructor for GroupChatInvitationTest.
* @param arg0
*/
public GroupChatInvitationTest(String arg0) {
super(arg0);
}
public void testInvitation() { public void testInvitation() {
try { try {
GroupChatInvitation invitation = new GroupChatInvitation("test@chat.localhost"); GroupChatInvitation invitation = new GroupChatInvitation("test@" + getChatDomain());
Message message = new Message("test2@" + con1.getHost()); Message message = new Message(getBareJID(1));
message.setBody("Group chat invitation!"); message.setBody("Group chat invitation!");
message.addExtension(invitation); message.addExtension(invitation);
con1.sendPacket(message); getConnection(0).sendPacket(message);
Thread.sleep(250); Thread.sleep(250);
@ -85,7 +91,7 @@ public class GroupChatInvitationTest extends TestCase {
GroupChatInvitation resultInvite = (GroupChatInvitation)result.getExtension("x", GroupChatInvitation resultInvite = (GroupChatInvitation)result.getExtension("x",
"jabber:x:conference"); "jabber:x:conference");
assertEquals("Invitation not to correct room", "test@chat.localhost", assertEquals("Invitation not to correct room", "test@" + getChatDomain(),
resultInvite.getRoomAddress()); resultInvite.getRoomAddress());
} }
catch (Exception e) { catch (Exception e) {
@ -95,42 +101,19 @@ public class GroupChatInvitationTest extends TestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
try {
con1 = new XMPPConnection("localhost");
con2 = new XMPPConnection("localhost");
// Create the test accounts
if (!con1.getAccountManager().supportsAccountCreation()) {
fail("Server does not support account creation");
}
con1.getAccountManager().createAccount("test1", "test1");
con2.getAccountManager().createAccount("test2", "test2");
// Login with the test accounts
con1.login("test1", "test1");
con2.login("test2", "test2");
// Register listener for groupchat invitations. // Register listener for groupchat invitations.
PacketFilter filter = new PacketExtensionFilter("x", "jabber:x:conference"); PacketFilter filter = new PacketExtensionFilter("x", "jabber:x:conference");
collector = con2.createPacketCollector(filter); collector = getConnection(1).createPacketCollector(filter);
}
catch (Exception e) {
fail(e.getMessage());
}
} }
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
// Cancel the packet collector so that no more results are queued up
collector.cancel();
super.tearDown(); super.tearDown();
}
// Delete the created accounts for the test protected int getMaxConnections() {
con1.getAccountManager().deleteAccount(); return 2;
con2.getAccountManager().deleteAccount();
// Close all the connections
con1.close();
con2.close();
} }
} }

View file

@ -56,8 +56,7 @@ import java.util.ArrayList;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
/** /**
* *
@ -65,13 +64,7 @@ import junit.framework.TestCase;
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class MessageEventManagerTest extends TestCase { public class MessageEventManagerTest extends SmackTestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/** /**
* Constructor for MessageEventManagerTest. * Constructor for MessageEventManagerTest.
@ -90,7 +83,7 @@ public class MessageEventManagerTest extends TestCase {
*/ */
public void testSendMessageEventRequest() { public void testSendMessageEventRequest() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
@ -119,9 +112,9 @@ public class MessageEventManagerTest extends TestCase {
*/ */
public void testSendMessageEventRequestAndDisplayNotifications() { public void testSendMessageEventRequestAndDisplayNotifications() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
MessageEventManager messageEventManager = new MessageEventManager(conn1); MessageEventManager messageEventManager = new MessageEventManager(getConnection(0));
messageEventManager messageEventManager
.addMessageEventNotificationListener(new MessageEventNotificationListener() { .addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) { public void deliveredNotification(String from, String packetID) {
@ -185,9 +178,9 @@ public class MessageEventManagerTest extends TestCase {
resultsExpected.add("cancelledNotification"); resultsExpected.add("cancelledNotification");
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
MessageEventManager messageEventManager1 = new MessageEventManager(conn1); MessageEventManager messageEventManager1 = new MessageEventManager(getConnection(0));
messageEventManager1 messageEventManager1
.addMessageEventNotificationListener(new MessageEventNotificationListener() { .addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) { public void deliveredNotification(String from, String packetID) {
@ -211,7 +204,7 @@ public class MessageEventManagerTest extends TestCase {
} }
}); });
MessageEventManager messageEventManager2 = new MessageEventManager(conn2); MessageEventManager messageEventManager2 = new MessageEventManager(getConnection(1));
messageEventManager2 messageEventManager2
.addMessageEventRequestListener(new DefaultMessageEventRequestListener() { .addMessageEventRequestListener(new DefaultMessageEventRequestListener() {
public void deliveredNotificationRequested( public void deliveredNotificationRequested(
@ -258,9 +251,9 @@ public class MessageEventManagerTest extends TestCase {
// Send the message that contains the notifications request // Send the message that contains the notifications request
try { try {
chat1.sendMessage(msg); chat1.sendMessage(msg);
messageEventManager2.sendDisplayedNotification(user1, msg.getPacketID()); messageEventManager2.sendDisplayedNotification(getBareJID(0), msg.getPacketID());
messageEventManager2.sendComposingNotification(user1, msg.getPacketID()); messageEventManager2.sendComposingNotification(getBareJID(0), msg.getPacketID());
messageEventManager2.sendCancelledNotification(user1, msg.getPacketID()); messageEventManager2.sendCancelledNotification(getBareJID(0), msg.getPacketID());
// 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(500);
assertTrue( assertTrue(
@ -275,45 +268,7 @@ public class MessageEventManagerTest extends TestCase {
} }
} }
/* protected int getMaxConnections() {
* @see TestCase#setUp() return 2;
*/
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

@ -54,9 +54,8 @@ package org.jivesoftware.smackx;
import java.util.Iterator; import java.util.Iterator;
import junit.framework.TestCase;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.test.SmackTestCase;
/** /**
* *
@ -64,17 +63,7 @@ import org.jivesoftware.smack.*;
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class RosterExchangeManagerTest extends TestCase { public class RosterExchangeManagerTest extends SmackTestCase {
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;
@ -89,15 +78,18 @@ public class RosterExchangeManagerTest extends TestCase {
/** /**
* High level API test. * High level API test.
* This is a simple test to use with a XMPP client and check if the client receives user1's roster * This is a simple test to use with a XMPP client and check if the client receives user1's
* roster
* 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() {
// Send user1's roster to user2 // Send user1's roster to user2
try { try {
RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1); RosterExchangeManager rosterExchangeManager =
rosterExchangeManager.send(conn1.getRoster(), user2); new RosterExchangeManager(getConnection(0));
} catch (Exception e) { rosterExchangeManager.send(getConnection(0).getRoster(), getBareJID(1));
}
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail("An error occured sending the roster"); fail("An error occured sending the roster");
} }
@ -105,16 +97,19 @@ public class RosterExchangeManagerTest extends TestCase {
/** /**
* High level API test. * High level API test.
* This is a simple test to use with a XMPP client and check if the client receives user1's roster groups * This is a simple test to use with a XMPP client and check if the client receives user1's
* roster groups
* 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() {
// Send user1's RosterGroups to user2 // Send user1's RosterGroups to user2
try { try {
RosterExchangeManager rosterExchangeManager = new RosterExchangeManager(conn1); RosterExchangeManager rosterExchangeManager =
for (Iterator it = conn1.getRoster().getGroups(); it.hasNext();) new RosterExchangeManager(getConnection(0));
rosterExchangeManager.send((RosterGroup) it.next(), user2); for (Iterator it = getConnection(0).getRoster().getGroups(); it.hasNext();)
} catch (Exception e) { rosterExchangeManager.send((RosterGroup) it.next(), getBareJID(1));
}
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail("An error occured sending the roster"); fail("An error occured sending the roster");
} }
@ -124,11 +119,12 @@ public class RosterExchangeManagerTest extends TestCase {
* High level API test. * High level API test.
* 1. User_1 will send his/her roster to user_2 * 1. User_1 will send his/her roster 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 testSendAndReceiveRoster() { public void testSendAndReceiveRoster() {
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1); RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2); RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
// Create a RosterExchangeListener that will iterate over the received roster entries // Create a RosterExchangeListener that will iterate over the received roster entries
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
@ -149,12 +145,13 @@ public class RosterExchangeManagerTest extends TestCase {
// Send user1's roster to user2 // Send user1's roster to user2
try { try {
entriesSent = conn1.getRoster().getEntryCount(); entriesSent = getConnection(0).getRoster().getEntryCount();
entriesReceived = 0; entriesReceived = 0;
rosterExchangeManager1.send(conn1.getRoster(), user2); rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
// Wait for 1 second // Wait for 1 second
Thread.sleep(300); Thread.sleep(300);
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending the message with the roster"); fail("An error occured sending the message with the roster");
} }
assertEquals( assertEquals(
@ -166,12 +163,14 @@ public class RosterExchangeManagerTest extends TestCase {
/** /**
* High level API test. * High level API test.
* 1. User_1 will send his/her roster to user_2 * 1. User_1 will send his/her roster to user_2
* 2. User_2 will automatically add the entries that receives to his/her roster in the corresponding group * 2. User_2 will automatically add the entries that receives to his/her roster in the
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then something is wrong * corresponding group
* 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() {
RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(conn1); RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(conn2); RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));
// Create a RosterExchangeListener that will accept all the received roster entries // Create a RosterExchangeListener that will accept all the received roster entries
RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() { RosterExchangeListener rosterExchangeListener = new RosterExchangeListener() {
@ -185,11 +184,12 @@ public class RosterExchangeManagerTest extends TestCase {
try { try {
RemoteRosterEntry remoteRosterEntry = RemoteRosterEntry remoteRosterEntry =
(RemoteRosterEntry) remoteRosterEntries.next(); (RemoteRosterEntry) remoteRosterEntries.next();
conn2.getRoster().createEntry( getConnection(1).getRoster().createEntry(
remoteRosterEntry.getUser(), remoteRosterEntry.getUser(),
remoteRosterEntry.getName(), remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames()); remoteRosterEntry.getGroupArrayNames());
} catch (Exception e) { }
catch (Exception e) {
fail(e.toString()); fail(e.toString());
} }
} }
@ -200,79 +200,39 @@ public class RosterExchangeManagerTest extends TestCase {
// Send user1's roster to user2 // Send user1's roster to user2
try { try {
entriesSent = conn1.getRoster().getEntryCount(); entriesSent = getConnection(0).getRoster().getEntryCount();
entriesReceived = 0; entriesReceived = 0;
rosterExchangeManager1.send(conn1.getRoster(), user2); rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
// Wait for 1 seconds // Wait for 1 seconds
Thread.sleep(400); Thread.sleep(600);
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending the message with the roster"); fail("An error occured sending the message with the roster");
} }
assertEquals( assertEquals(
"Number of sent and received entries does not match", "Number of sent and received entries does not match",
entriesSent, entriesSent,
entriesReceived); entriesReceived);
assertTrue("Roster2 has no entries", conn2.getRoster().getEntryCount() > 0); assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
} }
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
try { try {
// Connect to the server getConnection(0).getRoster().createEntry(
conn1 = new XMPPConnection("localhost"); getBareJID(2),
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", "gato5",
new String[] { "Friends, Coworker" }); new String[] { "Friends, Coworker" });
conn1.getRoster().createEntry("gato6@" + conn4.getHost(), "gato6", null); getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
Thread.sleep(100); Thread.sleep(100);
} catch (Exception e) { }
catch (Exception e) {
fail(e.getMessage()); fail(e.getMessage());
} }
} }
/* protected int getMaxConnections() {
* @see TestCase#tearDown() return 4;
*/
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

@ -52,20 +52,16 @@
package org.jivesoftware.smackx; package org.jivesoftware.smackx;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.DiscoverItems; import org.jivesoftware.smackx.packet.DiscoverItems;
import junit.framework.TestCase;
/** /**
* Tests the service discovery functionality. * Tests the service discovery functionality.
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class ServiceDiscoveryManagerTest extends TestCase { public class ServiceDiscoveryManagerTest extends SmackTestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
/** /**
* Constructor for ServiceDiscoveryManagerTest. * Constructor for ServiceDiscoveryManagerTest.
@ -81,18 +77,18 @@ public class ServiceDiscoveryManagerTest extends TestCase {
public void testXHTMLFeature() { public void testXHTMLFeature() {
// Check for local XHTML service support // Check for local XHTML service support
// By default the XHTML service support is enabled in all the connections // By default the XHTML service support is enabled in all the connections
assertTrue(XHTMLManager.isServiceEnabled(conn1)); assertTrue(XHTMLManager.isServiceEnabled(getConnection(0)));
assertTrue(XHTMLManager.isServiceEnabled(conn2)); assertTrue(XHTMLManager.isServiceEnabled(getConnection(1)));
// Check for XHTML support in connection1 from connection2 // Check for XHTML support in connection1 from connection2
assertTrue(XHTMLManager.isServiceEnabled(conn2, "gato10@" + conn1.getHost())); assertTrue(XHTMLManager.isServiceEnabled(getConnection(1), getBareJID(0)));
// Disable the XHTML Message support in connection1 // Disable the XHTML Message support in connection1
XHTMLManager.setServiceEnabled(conn1, false); XHTMLManager.setServiceEnabled(getConnection(0), false);
// Check for local XHTML service support // Check for local XHTML service support
assertFalse(XHTMLManager.isServiceEnabled(conn1)); assertFalse(XHTMLManager.isServiceEnabled(getConnection(0)));
assertTrue(XHTMLManager.isServiceEnabled(conn2)); assertTrue(XHTMLManager.isServiceEnabled(getConnection(1)));
// Check for XHTML support in connection1 from connection2 // Check for XHTML support in connection1 from connection2
assertFalse(XHTMLManager.isServiceEnabled(conn2, "gato10@" + conn1.getHost())); assertFalse(XHTMLManager.isServiceEnabled(getConnection(1), getFullJID(0)));
} }
/** /**
@ -101,7 +97,7 @@ public class ServiceDiscoveryManagerTest extends TestCase {
/*public void testPublishItems() { /*public void testPublishItems() {
// TODO Remove this line when the "additional services for extensions" are // TODO Remove this line when the "additional services for extensions" are
// implemented // implemented
new ServiceDiscoveryManager(conn1); new ServiceDiscoveryManager(getConnection(0));
DiscoverItems itemsToPublish = new DiscoverItems(); DiscoverItems itemsToPublish = new DiscoverItems();
DiscoverItems.Item itemToPublish = new DiscoverItems.Item("pubsub.shakespeare.lit"); DiscoverItems.Item itemToPublish = new DiscoverItems.Item("pubsub.shakespeare.lit");
@ -111,7 +107,7 @@ public class ServiceDiscoveryManagerTest extends TestCase {
itemsToPublish.addItem(itemToPublish); itemsToPublish.addItem(itemToPublish);
try { try {
ServiceDiscoveryManager.getInstanceFor(conn1).publishItems("host", itemsToPublish); ServiceDiscoveryManager.getInstanceFor(getConnection(0)).publishItems("host", itemsToPublish);
} }
catch (XMPPException e) { catch (XMPPException e) {
fail(e.getMessage()); fail(e.getMessage());
@ -119,42 +115,7 @@ public class ServiceDiscoveryManagerTest extends TestCase {
}*/ }*/
/* protected int getMaxConnections() {
* @see TestCase#setUp() return 2;
*/
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("gato10", "gato10");
conn2.getAccountManager().createAccount("gato11", "gato11");
// Login with the test accounts
conn1.login("gato10", "gato10");
conn2.login("gato11", "gato11");
} 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

@ -56,21 +56,14 @@ import java.util.Iterator;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
/** /**
* Test the XHTML extension using the high level API * Test the XHTML extension using the high level API
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class XHTMLManagerTest extends TestCase { public class XHTMLManagerTest extends SmackTestCase {
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;
@ -90,7 +83,7 @@ public class XHTMLManagerTest extends TestCase {
*/ */
public void testSendSimpleXHTMLMessage() { public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2 // User1 creates a chat with user2
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
@ -130,8 +123,8 @@ public class XHTMLManagerTest extends TestCase {
*/ */
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID()); final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// Create a listener for the chat that will check if the received message includes // Create a listener for the chat that will check if the received message includes
// an XHTML extension. Answer an ACK if everything is ok // an XHTML extension. Answer an ACK if everything is ok
@ -201,8 +194,8 @@ public class XHTMLManagerTest extends TestCase {
*/ */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID()); final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// Create a listener for the chat that will check if the received message includes // Create a listener for the chat that will check if the received message includes
// an XHTML extension. Answer an ACK if everything is ok // an XHTML extension. Answer an ACK if everything is ok
@ -283,46 +276,9 @@ public class XHTMLManagerTest extends TestCase {
bodiesSent, bodiesSent,
bodiesReceived); 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 protected int getMaxConnections() {
if (!conn1.getAccountManager().supportsAccountCreation()) return 2;
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

@ -54,25 +54,17 @@ package org.jivesoftware.smackx.muc;
import java.util.*; import java.util.*;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.*; import org.jivesoftware.smackx.*;
import junit.framework.TestCase;
/** /**
* Tests creating new MUC rooms. * Tests creating new MUC rooms.
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class MultiUserChatCreationTest extends TestCase { public class MultiUserChatCreationTest extends SmackTestCase {
private String host = "gatoux"; private String room = "fruta124@" + getMUCDomain();
private String room = "fruta124@conference." + host;
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/** /**
* Constructor for MultiUserChatCreationTest. * Constructor for MultiUserChatCreationTest.
@ -86,7 +78,7 @@ public class MultiUserChatCreationTest extends TestCase {
* Tests creating a new "Reserved Room". * Tests creating a new "Reserved Room".
*/ */
public void testCreateReservedRoom() { public void testCreateReservedRoom() {
MultiUserChat muc = new MultiUserChat(conn1, room); MultiUserChat muc = new MultiUserChat(getConnection(0), room);
try { try {
// Create the room // Create the room
@ -127,7 +119,7 @@ public class MultiUserChatCreationTest extends TestCase {
* Tests creating a new "Instant Room". * Tests creating a new "Instant Room".
*/ */
public void testCreateInstantRoom() { public void testCreateInstantRoom() {
MultiUserChat muc = new MultiUserChat(conn1, room); MultiUserChat muc = new MultiUserChat(getConnection(0), room);
try { try {
// Create the room // Create the room
@ -145,40 +137,7 @@ public class MultiUserChatCreationTest extends TestCase {
} }
} }
protected void setUp() throws Exception { protected int getMaxConnections() {
super.setUp(); return 2;
try {
// Connect to the server
conn1 = new XMPPConnection(host);
conn2 = new XMPPConnection(host);
// 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());
}
}
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

@ -57,27 +57,17 @@ import java.util.Iterator;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*; import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.Form; import org.jivesoftware.smackx.Form;
import junit.framework.TestCase;
/** /**
* Tests the new MUC functionalities. * Tests the new MUC functionalities.
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class MultiUserChatTest extends TestCase { public class MultiUserChatTest extends SmackTestCase {
private String host = "gatoux"; private String room = "fruta124@" + getMUCDomain();
private String room = "fruta124@conference." + host;
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private XMPPConnection conn3 = null;
private String user1 = null;
private String user2 = null;
private String user3 = null;
private MultiUserChat muc; private MultiUserChat muc;
@ -92,7 +82,7 @@ public class MultiUserChatTest extends TestCase {
/*public void testDiscussionHistory() { /*public void testDiscussionHistory() {
try { try {
// User2 joins the room // User2 joins the room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 sends some messages to the room // User2 sends some messages to the room
muc2.sendMessage("Message 1"); muc2.sendMessage("Message 1");
@ -100,7 +90,7 @@ public class MultiUserChatTest extends TestCase {
muc2.sendMessage("Message 3"); muc2.sendMessage("Message 3");
// User3 joins the room requesting to receive the last 2 messages. // User3 joins the room requesting to receive the last 2 messages.
MultiUserChat muc3 = new MultiUserChat(conn2, room); MultiUserChat muc3 = new MultiUserChat(getConnection(1), room);
DiscussionHistory history = new DiscussionHistory(); DiscussionHistory history = new DiscussionHistory();
history.setMaxStanzas(2); history.setMaxStanzas(2);
muc3.join("testbot3", null, history, SmackConfiguration.getPacketReplyTimeout()); muc3.join("testbot3", null, history, SmackConfiguration.getPacketReplyTimeout());
@ -122,15 +112,15 @@ public class MultiUserChatTest extends TestCase {
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
}*/ }*/
public void testParticipantPresence() { public void testParticipantPresence() {
try { try {
// User2 joins the new room // User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
Thread.sleep(300); Thread.sleep(300);
@ -176,8 +166,8 @@ public class MultiUserChatTest extends TestCase {
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -185,11 +175,11 @@ public class MultiUserChatTest extends TestCase {
final String[] answer = new String[2]; final String[] answer = new String[2];
try { try {
// User2 joins the new room // User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User3 is listening to MUC invitations // User3 is listening to MUC invitations
MultiUserChat.addInvitationListener(conn3, new InvitationListener() { MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {
public void invitationReceived( public void invitationReceived(
XMPPConnection conn, XMPPConnection conn,
String room, String room,
@ -212,7 +202,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User2 invites user3 to join to the room // User2 invites user3 to join to the room
muc2.invite(user3, "Meet me in this excellent room"); muc2.invite(getFullJID(2), "Meet me in this excellent room");
Thread.sleep(350); Thread.sleep(350);
assertEquals( assertEquals(
@ -228,53 +218,50 @@ public class MultiUserChatTest extends TestCase {
muc2.leave(); muc2.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
public void testDiscoverJoinedRooms() { public void testDiscoverJoinedRooms() {
try { try {
// Check that user1 has joined only to one room // Check that user1 has joined only to one room
Iterator joinedRooms = MultiUserChat.getJoinedRooms(conn2, user1); Iterator joinedRooms = MultiUserChat.getJoinedRooms(getConnection(1), getFullJID(0));
assertTrue("Joined rooms shouldn't be empty", joinedRooms.hasNext()); assertTrue("Joined rooms shouldn't be empty", joinedRooms.hasNext());
assertEquals( assertEquals("Joined room is incorrect", joinedRooms.next(), room);
"Joined room is incorrect",
joinedRooms.next(),
room);
assertFalse("User has joined more than one room", joinedRooms.hasNext()); assertFalse("User has joined more than one room", joinedRooms.hasNext());
// Leave the new room // Leave the new room
muc.leave(); muc.leave();
// Check that user1 is not currently join any room // Check that user1 is not currently join any room
joinedRooms = MultiUserChat.getJoinedRooms(conn2, user1); joinedRooms = MultiUserChat.getJoinedRooms(getConnection(1), getFullJID(0));
assertFalse("Joined rooms should be empty", joinedRooms.hasNext()); assertFalse("Joined rooms should be empty", joinedRooms.hasNext());
muc.join("testbot"); muc.join("testbot");
} }
catch (XMPPException e) { catch (XMPPException e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
public void testDiscoverMUCSupport() { public void testDiscoverMUCSupport() {
// Discover user1 support of MUC // Discover user1 support of MUC
boolean supports = MultiUserChat.isServiceEnabled(conn2, user1); boolean supports = MultiUserChat.isServiceEnabled(getConnection(1), getFullJID(0));
assertTrue("Couldn't detect that user1 supports MUC", supports); assertTrue("Couldn't detect that user1 supports MUC", supports);
} }
public void testPrivateChat() { public void testPrivateChat() {
try { try {
// User2 joins the new room // User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
conn1.addPacketListener(new PacketListener() { getConnection(0).addPacketListener(new PacketListener() {
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
Message message = (Message) packet; Message message = (Message) packet;
Chat chat2 = new Chat(conn1, message.getFrom(), message.getThread()); Chat chat2 = new Chat(getConnection(0), message.getFrom(), message.getThread());
assertEquals( assertEquals(
"Sender of chat is incorrect", "Sender of chat is incorrect",
room + "/testbot2", room + "/testbot2",
@ -289,30 +276,29 @@ public class MultiUserChatTest extends TestCase {
}, },
new AndFilter( new AndFilter(
new MessageTypeFilter(Message.Type.CHAT), new MessageTypeFilter(Message.Type.CHAT),
new PacketTypeFilter(Message.class)) new PacketTypeFilter(Message.class)));
);
// Start a private chat with another participant // Start a private chat with another participant
Chat chat = muc2.createPrivateChat(room + "/testbot"); Chat chat = muc2.createPrivateChat(room + "/testbot");
chat.sendMessage("Hello there"); chat.sendMessage("Hello there");
Message response = chat.nextMessage(2000); Message response = chat.nextMessage(2000);
assertEquals("Sender of response is incorrect",room + "/testbot", response.getFrom()); assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
assertEquals("Body of response is incorrect","ACK", response.getBody()); assertEquals("Body of response is incorrect", "ACK", response.getBody());
// User2 leaves the room // User2 leaves the room
muc2.leave(); muc2.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
// TODO This test is commented because jabberd2 doesn't support discovering reserved nicknames // TODO This test is commented because jabberd2 doesn't support discovering reserved nicknames
/*public void testReservedNickname() { /*public void testReservedNickname() {
// User2 joins the new room // User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
String reservedNickname = muc2.getReservedNickname(); String reservedNickname = muc2.getReservedNickname();
assertNull("Reserved nickname is not null", reservedNickname); assertNull("Reserved nickname is not null", reservedNickname);
@ -325,11 +311,11 @@ public class MultiUserChatTest extends TestCase {
muc.changeSubject("Initial Subject"); muc.changeSubject("Initial Subject");
// User2 joins the new room // User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User3 joins the new room // User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 wants to be notified every time the room's subject is changed. // User3 wants to be notified every time the room's subject is changed.
@ -382,8 +368,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -391,7 +377,7 @@ public class MultiUserChatTest extends TestCase {
final String[] answer = new String[3]; final String[] answer = new String[3];
try { try {
// User2 joins the new room // User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 will lister for his own "kicking" // User2 will lister for his own "kicking"
muc2.addUserStatusListener(new DefaultUserStatusListener() { muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -403,7 +389,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User3 joins the new room // User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 will lister for user2's "kicking" // User3 will lister for user2's "kicking"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -420,9 +406,7 @@ public class MultiUserChatTest extends TestCase {
} }
catch (XMPPException e) { catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError(); XMPPError xmppError = e.getXMPPError();
assertNotNull( assertNotNull("No XMPPError was received when kicking a room owner", xmppError);
"No XMPPError was received when kicking a room owner",
xmppError);
assertEquals( assertEquals(
"A simple participant was able to kick another participant from the room", "A simple participant was able to kick another participant from the room",
403, 403,
@ -433,14 +417,16 @@ public class MultiUserChatTest extends TestCase {
muc.kickParticipant("testbot2", "Because I'm the owner"); muc.kickParticipant("testbot2", "Because I'm the owner");
Thread.sleep(300); Thread.sleep(300);
assertNull("User2 wasn't kicked from the room", muc.getParticipantPresence(room + "/testbot2")); assertNull(
"User2 wasn't kicked from the room",
muc.getParticipantPresence(room + "/testbot2"));
assertFalse("User2 thinks that he's still in the room", muc2.isJoined()); assertFalse("User2 thinks that he's still in the room", muc2.isJoined());
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
assertEquals( assertEquals(
"User2 didn't receive the correct initiator of the kick", "User2 didn't receive the correct initiator of the kick",
"gato3@" + conn1.getHost(), getBareJID(0),
answer[0]); answer[0]);
assertEquals( assertEquals(
"User2 didn't receive the correct reason for the kick", "User2 didn't receive the correct reason for the kick",
@ -459,8 +445,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -468,7 +454,7 @@ public class MultiUserChatTest extends TestCase {
final String[] answer = new String[3]; final String[] answer = new String[3];
try { try {
// User2 joins the new room // User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 will lister for his own "banning" // User2 will lister for his own "banning"
muc2.addUserStatusListener(new DefaultUserStatusListener() { muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -480,7 +466,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User3 joins the new room // User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 will lister for user2's "banning" // User3 will lister for user2's "banning"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -492,14 +478,12 @@ public class MultiUserChatTest extends TestCase {
try { try {
// Check whether a simple participant can ban a room owner or not // Check whether a simple participant can ban a room owner or not
muc2.banUser("gato3@" + conn2.getHost(), "Because I'm bad"); muc2.banUser(getBareJID(0), "Because I'm bad");
fail("User2 was able to ban a room owner"); fail("User2 was able to ban a room owner");
} }
catch (XMPPException e) { catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError(); XMPPError xmppError = e.getXMPPError();
assertNotNull( assertNotNull("No XMPPError was received when banning a room owner", xmppError);
"No XMPPError was received when banning a room owner",
xmppError);
assertEquals( assertEquals(
"A simple participant was able to ban another participant from the room", "A simple participant was able to ban another participant from the room",
403, 403,
@ -507,17 +491,19 @@ public class MultiUserChatTest extends TestCase {
} }
// Check that the room's owner can ban a simple participant // Check that the room's owner can ban a simple participant
muc.banUser("gato4@" + conn2.getHost(), "Because I'm the owner"); muc.banUser(getBareJID(1), "Because I'm the owner");
Thread.sleep(300); Thread.sleep(300);
assertNull("User2 wasn't banned from the room", muc.getParticipantPresence(room + "/testbot2")); assertNull(
"User2 wasn't banned from the room",
muc.getParticipantPresence(room + "/testbot2"));
assertFalse("User2 thinks that he's still in the room", muc2.isJoined()); assertFalse("User2 thinks that he's still in the room", muc2.isJoined());
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
assertEquals( assertEquals(
"User2 didn't receive the correct initiator of the ban", "User2 didn't receive the correct initiator of the ban",
"gato3@" + conn1.getHost(), getBareJID(0),
answer[0]); answer[0]);
assertEquals( assertEquals(
"User2 didn't receive the correct reason for the banning", "User2 didn't receive the correct reason for the banning",
@ -536,8 +522,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -548,7 +534,7 @@ public class MultiUserChatTest extends TestCase {
makeRoomModerated(); makeRoomModerated();
// User2 joins the new room (as a visitor) // User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 will listen for his own "voice" // User2 will listen for his own "voice"
muc2.addUserStatusListener(new DefaultUserStatusListener() { muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -563,7 +549,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User3 joins the new room (as a visitor) // User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 will lister for user2's "voice" // User3 will lister for user2's "voice"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -585,9 +571,7 @@ public class MultiUserChatTest extends TestCase {
} }
catch (XMPPException e) { catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError(); XMPPError xmppError = e.getXMPPError();
assertNotNull( assertNotNull("No XMPPError was received granting voice", xmppError);
"No XMPPError was received granting voice",
xmppError);
assertEquals( assertEquals(
"A visitor was able to grant voice to another visitor", "A visitor was able to grant voice to another visitor",
403, 403,
@ -628,8 +612,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -640,7 +624,7 @@ public class MultiUserChatTest extends TestCase {
makeRoomModerated(); makeRoomModerated();
// User2 joins the new room (as a visitor) // User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 will listen for moderator privileges // User2 will listen for moderator privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() { muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -663,7 +647,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User3 joins the new room (as a visitor) // User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 will lister for user2's moderator privileges // User3 will lister for user2's moderator privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -692,9 +676,7 @@ public class MultiUserChatTest extends TestCase {
} }
catch (XMPPException e) { catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError(); XMPPError xmppError = e.getXMPPError();
assertNotNull( assertNotNull("No XMPPError was received granting moderator privileges", xmppError);
"No XMPPError was received granting moderator privileges",
xmppError);
assertEquals( assertEquals(
"A visitor was able to grant moderator privileges to another visitor", "A visitor was able to grant moderator privileges to another visitor",
403, 403,
@ -728,12 +710,8 @@ public class MultiUserChatTest extends TestCase {
muc.revokeModerator("testbot2"); muc.revokeModerator("testbot2");
Thread.sleep(300); Thread.sleep(300);
assertNull( assertNull("User2 received a false revoke voice notification", answer[1]);
"User2 received a false revoke voice notification", assertNull("User3 received a false user2's voice privileges notification", answer[3]);
answer[1]);
assertNull(
"User3 received a false user2's voice privileges notification",
answer[3]);
assertEquals( assertEquals(
"User2 didn't receive the revoke moderator privileges notification", "User2 didn't receive the revoke moderator privileges notification",
"I'm not a moderator", "I'm not a moderator",
@ -743,24 +721,19 @@ public class MultiUserChatTest extends TestCase {
room + "/testbot2", room + "/testbot2",
answer[7]); answer[7]);
// Check that the room's owner can grant moderator privileges to a participant // Check that the room's owner can grant moderator privileges to a participant
clearAnswer(answer); clearAnswer(answer);
muc.grantModerator("testbot2"); muc.grantModerator("testbot2");
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
assertNull( assertNull("User2 received a false grant voice notification", answer[0]);
"User2 received a false grant voice notification",
answer[0]);
assertEquals( assertEquals(
"User2 didn't receive the grant moderator privileges notification", "User2 didn't receive the grant moderator privileges notification",
"I'm a moderator", "I'm a moderator",
answer[4]); answer[4]);
// Check that ParticipantStatusListener is working OK // Check that ParticipantStatusListener is working OK
assertNull( assertNull("User3 received a false user2's grant voice notification", answer[2]);
"User3 received a false user2's grant voice notification",
answer[2]);
assertEquals( assertEquals(
"User3 didn't receive user2's grant moderator privileges notification", "User3 didn't receive user2's grant moderator privileges notification",
room + "/testbot2", room + "/testbot2",
@ -786,8 +759,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -798,7 +771,7 @@ public class MultiUserChatTest extends TestCase {
makeRoomModerated(); makeRoomModerated();
// User2 joins the new room (as a visitor) // User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 will listen for membership privileges // User2 will listen for membership privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() { muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -813,7 +786,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User3 joins the new room (as a visitor) // User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 will lister for user2's membership privileges // User3 will lister for user2's membership privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -829,7 +802,7 @@ public class MultiUserChatTest extends TestCase {
try { try {
// Check whether a visitor can grant membership privileges to another visitor // Check whether a visitor can grant membership privileges to another visitor
muc2.grantMembership("gato5@" + conn2.getHost()); muc2.grantMembership(getBareJID(2));
fail("User2 was able to grant membership privileges"); fail("User2 was able to grant membership privileges");
} }
catch (XMPPException e) { catch (XMPPException e) {
@ -844,7 +817,7 @@ public class MultiUserChatTest extends TestCase {
} }
// Check that the room's owner can grant membership privileges to a visitor // Check that the room's owner can grant membership privileges to a visitor
muc.grantMembership("gato4@" + conn2.getHost()); muc.grantMembership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -860,7 +833,7 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can revoke membership privileges from a member // Check that the room's owner can revoke membership privileges from a member
// and make the occupant a visitor // and make the occupant a visitor
muc.revokeMembership("gato4@" + conn2.getHost()); muc.revokeMembership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -880,8 +853,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -892,7 +865,7 @@ public class MultiUserChatTest extends TestCase {
makeRoomModerated(); makeRoomModerated();
// User2 joins the new room (as a visitor) // User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 will listen for admin privileges // User2 will listen for admin privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() { muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -915,7 +888,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User3 joins the new room (as a visitor) // User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 will lister for user2's admin privileges // User3 will lister for user2's admin privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -939,14 +912,12 @@ public class MultiUserChatTest extends TestCase {
try { try {
// Check whether a visitor can grant admin privileges to another visitor // Check whether a visitor can grant admin privileges to another visitor
muc2.grantAdmin("gato5@" + conn2.getHost()); muc2.grantAdmin(getBareJID(2));
fail("User2 was able to grant admin privileges"); fail("User2 was able to grant admin privileges");
} }
catch (XMPPException e) { catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError(); XMPPError xmppError = e.getXMPPError();
assertNotNull( assertNotNull("No XMPPError was received granting admin privileges", xmppError);
"No XMPPError was received granting admin privileges",
xmppError);
assertEquals( assertEquals(
"A visitor was able to grant admin privileges to another visitor", "A visitor was able to grant admin privileges to another visitor",
403, 403,
@ -954,7 +925,7 @@ public class MultiUserChatTest extends TestCase {
} }
// Check that the room's owner can grant admin privileges to a visitor // Check that the room's owner can grant admin privileges to a visitor
muc.grantAdmin("gato4@" + conn2.getHost()); muc.grantAdmin(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -970,7 +941,7 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can revoke admin privileges from an admin // Check that the room's owner can revoke admin privileges from an admin
// and make the occupant a visitor // and make the occupant a visitor
muc.revokeMembership("gato4@" + conn2.getHost()); muc.revokeMembership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -986,9 +957,9 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can grant admin privileges to a member // Check that the room's owner can grant admin privileges to a member
clearAnswer(answer); clearAnswer(answer);
muc.grantMembership("gato4@" + conn2.getHost()); muc.grantMembership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
muc.grantAdmin("gato4@" + conn2.getHost()); muc.grantAdmin(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1013,7 +984,7 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can revoke admin privileges from an admin // Check that the room's owner can revoke admin privileges from an admin
// and make the occupant a member // and make the occupant a member
clearAnswer(answer); clearAnswer(answer);
muc.revokeAdmin("gato4@" + conn2.getHost()); muc.revokeAdmin(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1041,8 +1012,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -1053,7 +1024,7 @@ public class MultiUserChatTest extends TestCase {
makeRoomModerated(); makeRoomModerated();
// User2 joins the new room (as a visitor) // User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
// User2 will listen for ownership privileges // User2 will listen for ownership privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() { muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -1084,7 +1055,7 @@ public class MultiUserChatTest extends TestCase {
}); });
// User3 joins the new room (as a visitor) // User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room); MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3"); muc3.join("testbot3");
// User3 will lister for user2's ownership privileges // User3 will lister for user2's ownership privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -1116,14 +1087,12 @@ public class MultiUserChatTest extends TestCase {
try { try {
// Check whether a visitor can grant ownership privileges to another visitor // Check whether a visitor can grant ownership privileges to another visitor
muc2.grantOwnership("gato5@" + conn2.getHost()); muc2.grantOwnership(getBareJID(2));
fail("User2 was able to grant ownership privileges"); fail("User2 was able to grant ownership privileges");
} }
catch (XMPPException e) { catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError(); XMPPError xmppError = e.getXMPPError();
assertNotNull( assertNotNull("No XMPPError was received granting ownership privileges", xmppError);
"No XMPPError was received granting ownership privileges",
xmppError);
assertEquals( assertEquals(
"A visitor was able to grant ownership privileges to another visitor", "A visitor was able to grant ownership privileges to another visitor",
403, 403,
@ -1131,7 +1100,7 @@ public class MultiUserChatTest extends TestCase {
} }
// Check that the room's owner can grant ownership privileges to a visitor // Check that the room's owner can grant ownership privileges to a visitor
muc.grantOwnership("gato4@" + conn2.getHost()); muc.grantOwnership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1147,7 +1116,7 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can revoke ownership privileges from an owner // Check that the room's owner can revoke ownership privileges from an owner
// and make the occupant a visitor // and make the occupant a visitor
muc.revokeMembership("gato4@" + conn2.getHost()); muc.revokeMembership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1163,9 +1132,9 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can grant ownership privileges to a member // Check that the room's owner can grant ownership privileges to a member
clearAnswer(answer); clearAnswer(answer);
muc.grantMembership("gato4@" + conn2.getHost()); muc.grantMembership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
muc.grantOwnership("gato4@" + conn2.getHost()); muc.grantOwnership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1190,7 +1159,7 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can revoke ownership privileges from an owner // Check that the room's owner can revoke ownership privileges from an owner
// and make the occupant a member // and make the occupant a member
clearAnswer(answer); clearAnswer(answer);
muc.revokeAdmin("gato4@" + conn2.getHost()); muc.revokeAdmin(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1214,9 +1183,9 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can grant ownership privileges to an admin // Check that the room's owner can grant ownership privileges to an admin
clearAnswer(answer); clearAnswer(answer);
muc.grantAdmin("gato4@" + conn2.getHost()); muc.grantAdmin(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
muc.grantOwnership("gato4@" + conn2.getHost()); muc.grantOwnership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1241,7 +1210,7 @@ public class MultiUserChatTest extends TestCase {
// Check that the room's owner can revoke ownership privileges from an owner // Check that the room's owner can revoke ownership privileges from an owner
// and make the occupant an admin // and make the occupant an admin
clearAnswer(answer); clearAnswer(answer);
muc.revokeOwnership("gato4@" + conn2.getHost()); muc.revokeOwnership(getBareJID(1));
Thread.sleep(300); Thread.sleep(300);
// Check that UserStatusListener is working OK // Check that UserStatusListener is working OK
@ -1269,8 +1238,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave(); muc3.leave();
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage());
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage());
} }
} }
@ -1283,7 +1252,7 @@ public class MultiUserChatTest extends TestCase {
} }
private void clearAnswer(String[] answer) { private void clearAnswer(String[] answer) {
for (int i=0; i < answer.length; i++) { for (int i = 0; i < answer.length; i++) {
answer[i] = null; answer[i] = null;
} }
} }
@ -1291,35 +1260,13 @@ public class MultiUserChatTest extends TestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
try { try {
// Connect to the server
conn1 = new XMPPConnection(host);
conn2 = new XMPPConnection(host);
conn3 = new XMPPConnection(host);
// 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");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
conn3.login("gato5", "gato5");
user1 = "gato3@" + conn1.getHost() + "/Smack";
user2 = "gato4@" + conn2.getHost() + "/Smack";
user3 = "gato5@" + conn2.getHost() + "/Smack";
// User1 creates the room // User1 creates the room
muc = new MultiUserChat(conn1, room); muc = new MultiUserChat(getConnection(0), room);
muc.create("testbot"); muc.create("testbot");
// User1 sends an empty room configuration form which indicates that we want // User1 sends an empty room configuration form which indicates that we want
// an instant room // an instant room
muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT)); muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
} }
catch (Exception e) { catch (Exception e) {
fail(e.getMessage()); fail(e.getMessage());
@ -1327,18 +1274,13 @@ public class MultiUserChatTest extends TestCase {
} }
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
super.tearDown();
// Destroy the new room // Destroy the new room
muc.destroy("The room has almost no activity...", null); muc.destroy("The room has almost no activity...", null);
// Delete the created accounts for the test super.tearDown();
conn1.getAccountManager().deleteAccount(); }
conn2.getAccountManager().deleteAccount();
conn3.getAccountManager().deleteAccount();
// Close all the connections protected int getMaxConnections() {
conn1.close(); return 3;
conn2.close();
conn3.close();
} }
} }

View file

@ -55,8 +55,7 @@ package org.jivesoftware.smackx.packet;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*; import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
/** /**
* *
@ -64,13 +63,7 @@ import junit.framework.TestCase;
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class MessageEventTest extends TestCase { public class MessageEventTest extends SmackTestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
/** /**
* Constructor for MessageEventTest. * Constructor for MessageEventTest.
@ -89,7 +82,7 @@ public class MessageEventTest extends TestCase {
*/ */
public void testSendMessageEventRequest() { public void testSendMessageEventRequest() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
@ -108,7 +101,8 @@ public class MessageEventTest extends TestCase {
chat1.sendMessage(msg); 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(200); Thread.sleep(200);
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending the message"); fail("An error occured sending the message");
} }
} }
@ -124,7 +118,7 @@ public class MessageEventTest extends TestCase {
*/ */
public void testSendMessageEventRequestAndDisplayNotifications() { public void testSendMessageEventRequestAndDisplayNotifications() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
// Create a Listener that listens for Messages with the extension "jabber:x:roster" // 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 // This listener will listen on the conn2 and answer an ACK if everything is ok
@ -140,12 +134,13 @@ public class MessageEventTest extends TestCase {
"Message event is a request not a notification", "Message event is a request not a notification",
!messageEvent.isMessageEventRequest()); !messageEvent.isMessageEventRequest());
System.out.println(messageEvent.toXML()); System.out.println(messageEvent.toXML());
} catch (ClassCastException e) { }
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
} }
} }
}; };
conn1.addPacketListener(packetListener, packetFilter); getConnection(0).addPacketListener(packetListener, packetFilter);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
@ -164,50 +159,13 @@ public class MessageEventTest extends TestCase {
chat1.sendMessage(msg); 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(200); Thread.sleep(200);
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending the message"); fail("An error occured sending the message");
} }
} }
/* protected int getMaxConnections() {
* @see TestCase#setUp() return 2;
*/
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

@ -9,27 +9,16 @@ import java.util.Iterator;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*; import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.*; import org.jivesoftware.smackx.*;
import junit.framework.TestCase;
/** /**
* *
* Test the Roster Exchange extension using the low level API * Test the Roster Exchange extension using the low level API
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class RosterExchangeTest extends TestCase { public class RosterExchangeTest extends SmackTestCase {
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.
@ -46,15 +35,15 @@ public class RosterExchangeTest extends TestCase {
*/ */
public void testSendRosterEntries() { public void testSendRosterEntries() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items."); msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message // Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange); msg.addExtension(rosterExchange);
// Send the message that contains the roster // Send the message that contains the roster
@ -73,8 +62,8 @@ public class RosterExchangeTest extends TestCase {
*/ */
public void testSendAndReceiveRosterEntries() { public void testSendAndReceiveRosterEntries() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID()); final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// Create a Listener that listens for Messages with the extension "jabber:x:roster" // 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 // This listener will listen on the conn2 and answer an ACK if everything is ok
@ -103,15 +92,15 @@ public class RosterExchangeTest extends TestCase {
} }
} }
}; };
conn2.addPacketListener(packetListener, packetFilter); getConnection(1).addPacketListener(packetListener, packetFilter);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items."); msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message // Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange); msg.addExtension(rosterExchange);
// Send the message that contains the roster // Send the message that contains the roster
@ -133,8 +122,8 @@ public class RosterExchangeTest extends TestCase {
*/ */
public void testSendAndAcceptRosterEntries() { public void testSendAndAcceptRosterEntries() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID()); final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// Create a Listener that listens for Messages with the extension "jabber:x:roster" // 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 // This listener will listen on the conn2, save the roster entries and answer an ACK if everything is ok
@ -153,7 +142,7 @@ public class RosterExchangeTest extends TestCase {
// Add the roster entries to user2's roster // Add the roster entries to user2's roster
for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) { for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next(); RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
conn2.getRoster().createEntry( getConnection(1).getRoster().createEntry(
remoteRosterEntry.getUser(), remoteRosterEntry.getUser(),
remoteRosterEntry.getName(), remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames()); remoteRosterEntry.getGroupArrayNames());
@ -170,15 +159,15 @@ public class RosterExchangeTest extends TestCase {
} }
} }
}; };
conn2.addPacketListener(packetListener, packetFilter); getConnection(1).addPacketListener(packetListener, packetFilter);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items."); msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message // Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0); assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(conn1.getRoster()); RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange); msg.addExtension(rosterExchange);
// Send the message that contains the roster // Send the message that contains the roster
@ -190,45 +179,21 @@ public class RosterExchangeTest extends TestCase {
// Wait for 10 seconds for a reply // Wait for 10 seconds for a reply
msg = chat1.nextMessage(5000); msg = chat1.nextMessage(5000);
assertNotNull("No reply received", msg); assertNotNull("No reply received", msg);
assertTrue("Roster2 has no entries", conn2.getRoster().getEntryCount() > 0); try {
Thread.sleep(200);
} catch (Exception e) {
}
assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
} }
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
try { try {
// Connect to the server getConnection(0).getRoster().createEntry(
conn1 = new XMPPConnection("localhost"); getBareJID(2),
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", "gato5",
new String[] { "Friends, Coworker" }); new String[] { "Friends, Coworker" });
conn1.getRoster().createEntry("gato6@" + conn4.getHost(), "gato6", null); getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
Thread.sleep(300); Thread.sleep(300);
} catch (Exception e) { } catch (Exception e) {
@ -236,22 +201,8 @@ public class RosterExchangeTest extends TestCase {
} }
} }
/* protected int getMaxConnections() {
* @see TestCase#tearDown() return 4;
*/
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

@ -57,21 +57,14 @@ import java.util.*;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*; import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
/** /**
* Test the XHTML extension using the low level API * Test the XHTML extension using the low level API
* *
* @author Gaston Dombiak * @author Gaston Dombiak
*/ */
public class XHTMLExtensionTest extends TestCase { public class XHTMLExtensionTest extends SmackTestCase {
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;
@ -91,7 +84,7 @@ public class XHTMLExtensionTest extends TestCase {
*/ */
public void testSendSimpleXHTMLMessage() { public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2 // User1 creates a chat with user2
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
@ -107,7 +100,8 @@ public class XHTMLExtensionTest extends TestCase {
try { try {
chat1.sendMessage(msg); chat1.sendMessage(msg);
Thread.sleep(200); Thread.sleep(200);
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending the message with XHTML"); fail("An error occured sending the message with XHTML");
} }
} }
@ -122,8 +116,8 @@ public class XHTMLExtensionTest extends TestCase {
*/ */
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID()); final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// Create a Listener that listens for Messages with the extension // Create a Listener that listens for Messages with the extension
//"http://jabber.org/protocol/xhtml-im" //"http://jabber.org/protocol/xhtml-im"
@ -147,17 +141,19 @@ public class XHTMLExtensionTest extends TestCase {
String body = (String) it.next(); String body = (String) it.next();
System.out.println(body); System.out.println(body);
} }
} catch (ClassCastException e) { }
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
} }
try { try {
chat2.sendMessage("ok"); chat2.sendMessage("ok");
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending ack " + e.getMessage()); fail("An error occured sending ack " + e.getMessage());
} }
} }
}; };
conn2.addPacketListener(packetListener, packetFilter); getConnection(1).addPacketListener(packetListener, packetFilter);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
@ -172,7 +168,8 @@ public class XHTMLExtensionTest extends TestCase {
// User1 sends the message that contains the XHTML to user2 // User1 sends the message that contains the XHTML to user2
try { try {
chat1.sendMessage(msg); chat1.sendMessage(msg);
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending the message with XHTML"); fail("An error occured sending the message with XHTML");
} }
// Wait for 2 seconds for a reply // Wait for 2 seconds for a reply
@ -190,8 +187,8 @@ public class XHTMLExtensionTest extends TestCase {
*/ */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = conn1.createChat(user2); Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID()); final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// Create a Listener that listens for Messages with the extension // Create a Listener that listens for Messages with the extension
//"http://jabber.org/protocol/xhtml-im" //"http://jabber.org/protocol/xhtml-im"
@ -217,12 +214,13 @@ public class XHTMLExtensionTest extends TestCase {
System.out.println((String) it.next()); System.out.println((String) it.next());
} }
bodiesReceived = received; bodiesReceived = received;
} catch (ClassCastException e) { }
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
} }
} }
}; };
conn2.addPacketListener(packetListener, packetFilter); getConnection(1).addPacketListener(packetListener, packetFilter);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = chat1.createMessage();
@ -243,7 +241,8 @@ public class XHTMLExtensionTest extends TestCase {
bodiesReceived = 0; bodiesReceived = 0;
chat1.sendMessage(msg); chat1.sendMessage(msg);
Thread.sleep(300); Thread.sleep(300);
} catch (Exception e) { }
catch (Exception e) {
fail("An error occured sending the message with XHTML"); fail("An error occured sending the message with XHTML");
} }
// Wait half second so that the complete test can run // Wait half second so that the complete test can run
@ -253,46 +252,8 @@ public class XHTMLExtensionTest extends TestCase {
bodiesReceived); bodiesReceived);
} }
/* protected int getMaxConnections() {
* @see TestCase#setUp() return 2;
*/
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();
} }
} }