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

View File

@ -54,16 +54,10 @@ package org.jivesoftware.smack;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import junit.framework.TestCase;
public class PacketReaderTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
public class PacketReaderTest extends SmackTestCase {
/**
* Constructor for PacketReaderTest.
@ -86,13 +80,13 @@ public class PacketReaderTest extends TestCase {
return "<query xmlns=\"my:ns:test\"/>";
}
};
iqPacket.setTo(user2);
iqPacket.setTo(getBareJID(1));
iqPacket.setType(IQ.Type.GET);
// Send the IQ and wait for the answer
PacketCollector collector = conn1.createPacketCollector(
PacketCollector collector = getConnection(0).createPacketCollector(
new PacketIDFilter(iqPacket.getPacketID()));
conn1.sendPacket(iqPacket);
getConnection(0).sendPacket(iqPacket);
IQ response = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
if (response == null) {
fail("No response from the other user.");
@ -102,45 +96,7 @@ public class PacketReaderTest extends TestCase {
collector.cancel();
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
protected int getMaxConnections() {
return 2;
}
}

View File

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

View File

@ -53,24 +53,16 @@
package org.jivesoftware.smackx;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import junit.framework.TestCase;
import org.jivesoftware.smack.test.SmackTestCase;
/**
* Tests the DataForms extensions.
*
* @author Gaston Dombiak
*/
public class FormTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
public class FormTest extends SmackTestCase {
/**
* Constructor for FormTest.
@ -109,10 +101,20 @@ public class FormTest extends TestCase {
field.setLabel("Enter a description");
field.setType(FormField.TYPE_TEXT_MULTI);
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
Chat chat = conn1.createChat(user2);
Chat chat2 = new Chat(conn2, user1, chat.getThreadID());
Chat chat = getConnection(0).createChat(getBareJID(1));
Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat.getThreadID());
Message msg = chat.createMessage();
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
Form completedForm = formToRespond.createAnswerForm();
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(
"description",
"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.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());
// Send the message with the completed form
chat2.sendMessage(msg2);
@ -152,6 +165,9 @@ public class FormTest extends TestCase {
assertEquals(
completedForm.getField("name").getValues().next(),
"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) {
@ -159,41 +175,8 @@ public class FormTest extends TestCase {
}
}
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());
}
}
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();
protected int getMaxConnections() {
return 2;
}
}

View File

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

View File

@ -56,8 +56,7 @@ import java.util.ArrayList;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import junit.framework.TestCase;
import org.jivesoftware.smack.test.SmackTestCase;
/**
*
@ -65,13 +64,7 @@ import junit.framework.TestCase;
*
* @author Gaston Dombiak
*/
public class MessageEventManagerTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
public class MessageEventManagerTest extends SmackTestCase {
/**
* Constructor for MessageEventManagerTest.
@ -90,7 +83,7 @@ public class MessageEventManagerTest extends TestCase {
*/
public void testSendMessageEventRequest() {
// 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
Message msg = chat1.createMessage();
@ -119,9 +112,9 @@ public class MessageEventManagerTest extends TestCase {
*/
public void testSendMessageEventRequestAndDisplayNotifications() {
// 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
.addMessageEventNotificationListener(new MessageEventNotificationListener() {
public void deliveredNotification(String from, String packetID) {
@ -185,9 +178,9 @@ public class MessageEventManagerTest extends TestCase {
resultsExpected.add("cancelledNotification");
// 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
.addMessageEventNotificationListener(new MessageEventNotificationListener() {
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
.addMessageEventRequestListener(new DefaultMessageEventRequestListener() {
public void deliveredNotificationRequested(
@ -258,9 +251,9 @@ public class MessageEventManagerTest extends TestCase {
// Send the message that contains the notifications request
try {
chat1.sendMessage(msg);
messageEventManager2.sendDisplayedNotification(user1, msg.getPacketID());
messageEventManager2.sendComposingNotification(user1, msg.getPacketID());
messageEventManager2.sendCancelledNotification(user1, msg.getPacketID());
messageEventManager2.sendDisplayedNotification(getBareJID(0), msg.getPacketID());
messageEventManager2.sendComposingNotification(getBareJID(0), msg.getPacketID());
messageEventManager2.sendCancelledNotification(getBareJID(0), msg.getPacketID());
// Wait half second so that the complete test can run
Thread.sleep(500);
assertTrue(
@ -275,45 +268,7 @@ public class MessageEventManagerTest extends TestCase {
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
protected int getMaxConnections() {
return 2;
}
}

View File

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

View File

@ -52,20 +52,16 @@
package org.jivesoftware.smackx;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.packet.DiscoverItems;
import junit.framework.TestCase;
/**
* Tests the service discovery functionality.
*
* @author Gaston Dombiak
*/
public class ServiceDiscoveryManagerTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
public class ServiceDiscoveryManagerTest extends SmackTestCase {
/**
* Constructor for ServiceDiscoveryManagerTest.
@ -81,18 +77,18 @@ public class ServiceDiscoveryManagerTest extends TestCase {
public void testXHTMLFeature() {
// Check for local XHTML service support
// By default the XHTML service support is enabled in all the connections
assertTrue(XHTMLManager.isServiceEnabled(conn1));
assertTrue(XHTMLManager.isServiceEnabled(conn2));
assertTrue(XHTMLManager.isServiceEnabled(getConnection(0)));
assertTrue(XHTMLManager.isServiceEnabled(getConnection(1)));
// 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
XHTMLManager.setServiceEnabled(conn1, false);
XHTMLManager.setServiceEnabled(getConnection(0), false);
// Check for local XHTML service support
assertFalse(XHTMLManager.isServiceEnabled(conn1));
assertTrue(XHTMLManager.isServiceEnabled(conn2));
assertFalse(XHTMLManager.isServiceEnabled(getConnection(0)));
assertTrue(XHTMLManager.isServiceEnabled(getConnection(1)));
// 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() {
// TODO Remove this line when the "additional services for extensions" are
// implemented
new ServiceDiscoveryManager(conn1);
new ServiceDiscoveryManager(getConnection(0));
DiscoverItems itemsToPublish = new DiscoverItems();
DiscoverItems.Item itemToPublish = new DiscoverItems.Item("pubsub.shakespeare.lit");
@ -111,7 +107,7 @@ public class ServiceDiscoveryManagerTest extends TestCase {
itemsToPublish.addItem(itemToPublish);
try {
ServiceDiscoveryManager.getInstanceFor(conn1).publishItems("host", itemsToPublish);
ServiceDiscoveryManager.getInstanceFor(getConnection(0)).publishItems("host", itemsToPublish);
}
catch (XMPPException e) {
fail(e.getMessage());
@ -119,42 +115,7 @@ public class ServiceDiscoveryManagerTest extends TestCase {
}*/
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("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();
protected int getMaxConnections() {
return 2;
}
}

View File

@ -56,21 +56,14 @@ import java.util.Iterator;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import junit.framework.TestCase;
import org.jivesoftware.smack.test.SmackTestCase;
/**
* Test the XHTML extension using the high level API
*
* @author Gaston Dombiak
*/
public class XHTMLManagerTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
public class XHTMLManagerTest extends SmackTestCase {
private int bodiesSent;
private int bodiesReceived;
@ -90,7 +83,7 @@ public class XHTMLManagerTest extends TestCase {
*/
public void testSendSimpleXHTMLMessage() {
// 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
Message msg = chat1.createMessage();
@ -130,8 +123,8 @@ public class XHTMLManagerTest extends TestCase {
*/
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID());
Chat chat1 = getConnection(0).createChat(getBareJID(1));
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
// an XHTML extension. Answer an ACK if everything is ok
@ -201,8 +194,8 @@ public class XHTMLManagerTest extends TestCase {
*/
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID());
Chat chat1 = getConnection(0).createChat(getBareJID(1));
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
// an XHTML extension. Answer an ACK if everything is ok
@ -283,46 +276,9 @@ public class XHTMLManagerTest extends TestCase {
bodiesSent,
bodiesReceived);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.getMessage());
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
protected int getMaxConnections() {
return 2;
}
}

View File

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

View File

@ -57,27 +57,17 @@ import java.util.Iterator;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.Form;
import junit.framework.TestCase;
/**
* Tests the new MUC functionalities.
*
* @author Gaston Dombiak
*/
public class MultiUserChatTest extends TestCase {
public class MultiUserChatTest extends SmackTestCase {
private String host = "gatoux";
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 String room = "fruta124@" + getMUCDomain();
private MultiUserChat muc;
@ -92,19 +82,19 @@ public class MultiUserChatTest extends TestCase {
/*public void testDiscussionHistory() {
try {
// User2 joins the room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 sends some messages to the room
muc2.sendMessage("Message 1");
muc2.sendMessage("Message 2");
muc2.sendMessage("Message 3");
// 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();
history.setMaxStanzas(2);
muc3.join("testbot3", null, history, SmackConfiguration.getPacketReplyTimeout());
Message msg;
msg = muc3.nextMessage(1000);
assertNotNull("First message is null", msg);
@ -114,26 +104,26 @@ public class MultiUserChatTest extends TestCase {
assertEquals("Body of second message is incorrect", "Message 3", msg.getBody());
msg = muc3.nextMessage(1000);
assertNull("Third message is not null", msg);
// User2 leaves the room
muc2.leave();
// User3 leaves the room
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}*/
public void testParticipantPresence() {
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
Thread.sleep(300);
// User1 checks the presence of user2 in the room
Presence presence = muc.getParticipantPresence(room + "/testbot2");
assertNotNull("Presence of user2 in room is missing", presence);
@ -176,8 +166,8 @@ public class MultiUserChatTest extends TestCase {
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -185,21 +175,21 @@ public class MultiUserChatTest extends TestCase {
final String[] answer = new String[2];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User3 is listening to MUC invitations
MultiUserChat.addInvitationListener(conn3, new InvitationListener() {
MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {
public void invitationReceived(
XMPPConnection conn,
String room,
String inviter,
String reason,
String password) {
// Indicate that the invitation was received
answer[0] = reason;
// Reject the invitation
MultiUserChat.decline(conn, room, inviter, "I'm busy right now");
// Indicate that the invitation was received
answer[0] = reason;
// Reject the invitation
MultiUserChat.decline(conn, room, inviter, "I'm busy right now");
}
});
@ -212,9 +202,9 @@ public class MultiUserChatTest extends TestCase {
});
// 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);
assertEquals(
"Invitation was not received",
"Meet me in this excellent room",
@ -223,58 +213,55 @@ public class MultiUserChatTest extends TestCase {
// from users that aren't participants of the room. Comment out this line when
// running the test against a server that correctly implements JEP-45
//assertEquals("Rejection was not received", "I'm busy right now", answer[1]);
// User2 leaves the room
muc2.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
public void testDiscoverJoinedRooms() {
try {
// 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());
assertEquals(
"Joined room is incorrect",
joinedRooms.next(),
room);
assertEquals("Joined room is incorrect", joinedRooms.next(), room);
assertFalse("User has joined more than one room", joinedRooms.hasNext());
// Leave the new room
muc.leave();
// 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());
muc.join("testbot");
}
catch (XMPPException e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
public void testDiscoverMUCSupport() {
// 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);
}
public void testPrivateChat() {
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
conn1.addPacketListener(new PacketListener() {
getConnection(0).addPacketListener(new PacketListener() {
public void processPacket(Packet 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(
"Sender of chat is incorrect",
room + "/testbot2",
@ -289,30 +276,29 @@ public class MultiUserChatTest extends TestCase {
},
new AndFilter(
new MessageTypeFilter(Message.Type.CHAT),
new PacketTypeFilter(Message.class))
);
new PacketTypeFilter(Message.class)));
// Start a private chat with another participant
Chat chat = muc2.createPrivateChat(room + "/testbot");
chat.sendMessage("Hello there");
Message response = chat.nextMessage(2000);
assertEquals("Sender of response is incorrect",room + "/testbot", response.getFrom());
assertEquals("Body of response is incorrect","ACK", response.getBody());
assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
assertEquals("Body of response is incorrect", "ACK", response.getBody());
// User2 leaves the room
muc2.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
// TODO This test is commented because jabberd2 doesn't support discovering reserved nicknames
/*public void testReservedNickname() {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
String reservedNickname = muc2.getReservedNickname();
assertNull("Reserved nickname is not null", reservedNickname);
@ -325,11 +311,11 @@ public class MultiUserChatTest extends TestCase {
muc.changeSubject("Initial Subject");
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 wants to be notified every time the room's subject is changed.
@ -356,7 +342,7 @@ public class MultiUserChatTest extends TestCase {
403,
xmppError.getCode());
}
// Check that every MUC updates its subject when an allowed user changes the subject
// in a room
muc.changeSubject("New Subject1");
@ -382,8 +368,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -391,7 +377,7 @@ public class MultiUserChatTest extends TestCase {
final String[] answer = new String[3];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will lister for his own "kicking"
muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -403,7 +389,7 @@ public class MultiUserChatTest extends TestCase {
});
// User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's "kicking"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -420,27 +406,27 @@ public class MultiUserChatTest extends TestCase {
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received when kicking a room owner",
xmppError);
assertNotNull("No XMPPError was received when kicking a room owner", xmppError);
assertEquals(
"A simple participant was able to kick another participant from the room",
403,
xmppError.getCode());
}
// Check that the room's owner can kick a simple participant
muc.kickParticipant("testbot2", "Because I'm the owner");
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());
// Check that UserStatusListener is working OK
assertEquals(
"User2 didn't receive the correct initiator of the kick",
"gato3@" + conn1.getHost(),
getBareJID(0),
answer[0]);
assertEquals(
"User2 didn't receive the correct reason for the kick",
@ -452,15 +438,15 @@ public class MultiUserChatTest extends TestCase {
"User3 didn't receive the correct kicked participant",
room + "/testbot2",
answer[2]);
// User2 leaves the room
muc2.leave();
// User3 leaves the room
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -468,7 +454,7 @@ public class MultiUserChatTest extends TestCase {
final String[] answer = new String[3];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will lister for his own "banning"
muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -480,7 +466,7 @@ public class MultiUserChatTest extends TestCase {
});
// User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's "banning"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -492,32 +478,32 @@ public class MultiUserChatTest extends TestCase {
try {
// 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");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received when banning a room owner",
xmppError);
assertNotNull("No XMPPError was received when banning a room owner", xmppError);
assertEquals(
"A simple participant was able to ban another participant from the room",
403,
xmppError.getCode());
}
// 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);
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());
// Check that UserStatusListener is working OK
assertEquals(
"User2 didn't receive the correct initiator of the ban",
"gato3@" + conn1.getHost(),
getBareJID(0),
answer[0]);
assertEquals(
"User2 didn't receive the correct reason for the banning",
@ -529,15 +515,15 @@ public class MultiUserChatTest extends TestCase {
"User3 didn't receive the correct banned JID",
room + "/testbot2",
answer[2]);
// User2 leaves the room
muc2.leave();
// User3 leaves the room
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -546,9 +532,9 @@ public class MultiUserChatTest extends TestCase {
try {
makeRoomModerated();
// User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will listen for his own "voice"
muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -563,7 +549,7 @@ public class MultiUserChatTest extends TestCase {
});
// User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's "voice"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -585,15 +571,13 @@ public class MultiUserChatTest extends TestCase {
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received granting voice",
xmppError);
assertNotNull("No XMPPError was received granting voice", xmppError);
assertEquals(
"A visitor was able to grant voice to another visitor",
403,
xmppError.getCode());
}
// Check that the room's owner can grant voice to a participant
muc.grantVoice("testbot2");
Thread.sleep(300);
@ -628,8 +612,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -638,9 +622,9 @@ public class MultiUserChatTest extends TestCase {
try {
makeRoomModerated();
// User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will listen for moderator privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -663,7 +647,7 @@ public class MultiUserChatTest extends TestCase {
});
// User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's moderator privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -692,15 +676,13 @@ public class MultiUserChatTest extends TestCase {
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received granting moderator privileges",
xmppError);
assertNotNull("No XMPPError was received granting moderator privileges", xmppError);
assertEquals(
"A visitor was able to grant moderator privileges to another visitor",
403,
xmppError.getCode());
}
// Check that the room's owner can grant moderator privileges to a visitor
muc.grantModerator("testbot2");
Thread.sleep(300);
@ -728,12 +710,8 @@ public class MultiUserChatTest extends TestCase {
muc.revokeModerator("testbot2");
Thread.sleep(300);
assertNull(
"User2 received a false revoke voice notification",
answer[1]);
assertNull(
"User3 received a false user2's voice privileges notification",
answer[3]);
assertNull("User2 received a false revoke voice notification", answer[1]);
assertNull("User3 received a false user2's voice privileges notification", answer[3]);
assertEquals(
"User2 didn't receive the revoke moderator privileges notification",
"I'm not a moderator",
@ -743,24 +721,19 @@ public class MultiUserChatTest extends TestCase {
room + "/testbot2",
answer[7]);
// Check that the room's owner can grant moderator privileges to a participant
clearAnswer(answer);
muc.grantModerator("testbot2");
Thread.sleep(300);
// Check that UserStatusListener is working OK
assertNull(
"User2 received a false grant voice notification",
answer[0]);
assertNull("User2 received a false grant voice notification", answer[0]);
assertEquals(
"User2 didn't receive the grant moderator privileges notification",
"I'm a moderator",
answer[4]);
// Check that ParticipantStatusListener is working OK
assertNull(
"User3 received a false user2's grant voice notification",
answer[2]);
assertNull("User3 received a false user2's grant voice notification", answer[2]);
assertEquals(
"User3 didn't receive user2's grant moderator privileges notification",
room + "/testbot2",
@ -786,8 +759,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -796,9 +769,9 @@ public class MultiUserChatTest extends TestCase {
try {
makeRoomModerated();
// User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will listen for membership privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -813,7 +786,7 @@ public class MultiUserChatTest extends TestCase {
});
// User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's membership privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -829,7 +802,7 @@ public class MultiUserChatTest extends TestCase {
try {
// 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");
}
catch (XMPPException e) {
@ -842,9 +815,9 @@ public class MultiUserChatTest extends TestCase {
403,
xmppError.getCode());
}
// 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);
// 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
// and make the occupant a visitor
muc.revokeMembership("gato4@" + conn2.getHost());
muc.revokeMembership(getBareJID(1));
Thread.sleep(300);
// Check that UserStatusListener is working OK
@ -873,15 +846,15 @@ public class MultiUserChatTest extends TestCase {
"User3 didn't receive user2's revoke membership notification",
room + "/testbot2",
answer[3]);
// User2 leaves the room
muc2.leave();
// User3 leaves the room
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -890,9 +863,9 @@ public class MultiUserChatTest extends TestCase {
try {
makeRoomModerated();
// User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will listen for admin privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -915,7 +888,7 @@ public class MultiUserChatTest extends TestCase {
});
// User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's admin privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -939,22 +912,20 @@ public class MultiUserChatTest extends TestCase {
try {
// 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");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received granting admin privileges",
xmppError);
assertNotNull("No XMPPError was received granting admin privileges", xmppError);
assertEquals(
"A visitor was able to grant admin privileges to another visitor",
403,
xmppError.getCode());
}
// 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);
// 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
// and make the occupant a visitor
muc.revokeMembership("gato4@" + conn2.getHost());
muc.revokeMembership(getBareJID(1));
Thread.sleep(300);
// Check that UserStatusListener is working OK
@ -983,12 +954,12 @@ public class MultiUserChatTest extends TestCase {
"User3 didn't receive user2's revoke admin notification",
room + "/testbot2",
answer[7]);
// Check that the room's owner can grant admin privileges to a member
clearAnswer(answer);
muc.grantMembership("gato4@" + conn2.getHost());
muc.grantMembership(getBareJID(1));
Thread.sleep(300);
muc.grantAdmin("gato4@" + conn2.getHost());
muc.grantAdmin(getBareJID(1));
Thread.sleep(300);
// 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
// and make the occupant a member
clearAnswer(answer);
muc.revokeAdmin("gato4@" + conn2.getHost());
muc.revokeAdmin(getBareJID(1));
Thread.sleep(300);
// Check that UserStatusListener is working OK
@ -1041,8 +1012,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -1051,9 +1022,9 @@ public class MultiUserChatTest extends TestCase {
try {
makeRoomModerated();
// User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(conn2, room);
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will listen for ownership privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() {
@ -1084,7 +1055,7 @@ public class MultiUserChatTest extends TestCase {
});
// User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(conn3, room);
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's ownership privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
@ -1116,22 +1087,20 @@ public class MultiUserChatTest extends TestCase {
try {
// 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");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received granting ownership privileges",
xmppError);
assertNotNull("No XMPPError was received granting ownership privileges", xmppError);
assertEquals(
"A visitor was able to grant ownership privileges to another visitor",
403,
xmppError.getCode());
}
// 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);
// 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
// and make the occupant a visitor
muc.revokeMembership("gato4@" + conn2.getHost());
muc.revokeMembership(getBareJID(1));
Thread.sleep(300);
// Check that UserStatusListener is working OK
@ -1160,12 +1129,12 @@ public class MultiUserChatTest extends TestCase {
"User3 didn't receive user2's revoke ownership notification",
room + "/testbot2",
answer[11]);
// Check that the room's owner can grant ownership privileges to a member
clearAnswer(answer);
muc.grantMembership("gato4@" + conn2.getHost());
muc.grantMembership(getBareJID(1));
Thread.sleep(300);
muc.grantOwnership("gato4@" + conn2.getHost());
muc.grantOwnership(getBareJID(1));
Thread.sleep(300);
// 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
// and make the occupant a member
clearAnswer(answer);
muc.revokeAdmin("gato4@" + conn2.getHost());
muc.revokeAdmin(getBareJID(1));
Thread.sleep(300);
// 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
clearAnswer(answer);
muc.grantAdmin("gato4@" + conn2.getHost());
muc.grantAdmin(getBareJID(1));
Thread.sleep(300);
muc.grantOwnership("gato4@" + conn2.getHost());
muc.grantOwnership(getBareJID(1));
Thread.sleep(300);
// 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
// and make the occupant an admin
clearAnswer(answer);
muc.revokeOwnership("gato4@" + conn2.getHost());
muc.revokeOwnership(getBareJID(1));
Thread.sleep(300);
// Check that UserStatusListener is working OK
@ -1269,8 +1238,8 @@ public class MultiUserChatTest extends TestCase {
muc3.leave();
}
catch (Exception e) {
fail(e.getMessage());
e.printStackTrace();
fail(e.getMessage());
}
}
@ -1281,9 +1250,9 @@ public class MultiUserChatTest extends TestCase {
answerForm.setAnswer("muc#owner_moderatedroom", "1");
muc.sendConfigurationForm(answerForm);
}
private void clearAnswer(String[] answer) {
for (int i=0; i < answer.length; i++) {
for (int i = 0; i < answer.length; i++) {
answer[i] = null;
}
}
@ -1291,35 +1260,13 @@ public class MultiUserChatTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
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
muc = new MultiUserChat(conn1, room);
muc = new MultiUserChat(getConnection(0), room);
muc.create("testbot");
// User1 sends an empty room configuration form which indicates that we want
// an instant room
muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
}
catch (Exception e) {
fail(e.getMessage());
@ -1327,18 +1274,13 @@ public class MultiUserChatTest extends TestCase {
}
protected void tearDown() throws Exception {
super.tearDown();
// Destroy the new room
muc.destroy("The room has almost no activity...", null);
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
conn3.getAccountManager().deleteAccount();
super.tearDown();
}
// Close all the connections
conn1.close();
conn2.close();
conn3.close();
protected int getMaxConnections() {
return 3;
}
}

View File

@ -55,8 +55,7 @@ package org.jivesoftware.smackx.packet;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import junit.framework.TestCase;
import org.jivesoftware.smack.test.SmackTestCase;
/**
*
@ -64,13 +63,7 @@ import junit.framework.TestCase;
*
* @author Gaston Dombiak
*/
public class MessageEventTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private String user1 = null;
private String user2 = null;
public class MessageEventTest extends SmackTestCase {
/**
* Constructor for MessageEventTest.
@ -89,7 +82,7 @@ public class MessageEventTest extends TestCase {
*/
public void testSendMessageEventRequest() {
// 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
Message msg = chat1.createMessage();
@ -108,7 +101,8 @@ public class MessageEventTest extends TestCase {
chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(200);
} catch (Exception e) {
}
catch (Exception e) {
fail("An error occured sending the message");
}
}
@ -124,7 +118,7 @@ public class MessageEventTest extends TestCase {
*/
public void testSendMessageEventRequestAndDisplayNotifications() {
// 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"
// 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",
!messageEvent.isMessageEventRequest());
System.out.println(messageEvent.toXML());
} catch (ClassCastException e) {
}
catch (ClassCastException e) {
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
Message msg = chat1.createMessage();
@ -164,50 +159,13 @@ public class MessageEventTest extends TestCase {
chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(200);
} catch (Exception e) {
}
catch (Exception e) {
fail("An error occured sending the message");
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
} catch (Exception e) {
fail(e.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();
protected int getMaxConnections() {
return 2;
}
}

View File

@ -9,27 +9,16 @@ import java.util.Iterator;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.*;
import junit.framework.TestCase;
/**
*
* Test the Roster Exchange extension using the low level API
*
* @author Gaston Dombiak
*/
public class RosterExchangeTest extends TestCase {
private XMPPConnection conn1 = null;
private XMPPConnection conn2 = null;
private XMPPConnection conn3 = null;
private XMPPConnection conn4 = null;
private String user1 = null;
private String user2 = null;
private String user3 = null;
private String user4 = null;
public class RosterExchangeTest extends SmackTestCase {
/**
* Constructor for RosterExchangeTest.
@ -46,15 +35,15 @@ public class RosterExchangeTest extends TestCase {
*/
public void testSendRosterEntries() {
// 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
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(conn1.getRoster());
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange);
// Send the message that contains the roster
@ -73,8 +62,8 @@ public class RosterExchangeTest extends TestCase {
*/
public void testSendAndReceiveRosterEntries() {
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID());
Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// 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
@ -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
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(conn1.getRoster());
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange);
// Send the message that contains the roster
@ -133,8 +122,8 @@ public class RosterExchangeTest extends TestCase {
*/
public void testSendAndAcceptRosterEntries() {
// Create a chat for each connection
Chat chat1 = conn1.createChat(user2);
final Chat chat2 = new Chat(conn2, user1, chat1.getThreadID());
Chat chat1 = getConnection(0).createChat(getBareJID(1));
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
// 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
@ -153,7 +142,7 @@ public class RosterExchangeTest extends TestCase {
// Add the roster entries to user2's roster
for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
conn2.getRoster().createEntry(
getConnection(1).getRoster().createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
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
Message msg = chat1.createMessage();
msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message
assertTrue("Roster has no entries", conn1.getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(conn1.getRoster());
assertTrue("Roster has no entries", getConnection(0).getRoster().getEntryCount() > 0);
RosterExchange rosterExchange = new RosterExchange(getConnection(0).getRoster());
msg.addExtension(rosterExchange);
// Send the message that contains the roster
@ -190,45 +179,21 @@ public class RosterExchangeTest extends TestCase {
// Wait for 10 seconds for a reply
msg = chat1.nextMessage(5000);
assertNotNull("No reply received", msg);
assertTrue("Roster2 has no entries", conn2.getRoster().getEntryCount() > 0);
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 {
super.setUp();
try {
// Connect to the server
conn1 = new XMPPConnection("localhost");
conn2 = new XMPPConnection("localhost");
conn3 = new XMPPConnection("localhost");
conn4 = new XMPPConnection("localhost");
// Create the test accounts
if (!conn1.getAccountManager().supportsAccountCreation())
fail("Server does not support account creation");
conn1.getAccountManager().createAccount("gato3", "gato3");
conn2.getAccountManager().createAccount("gato4", "gato4");
conn3.getAccountManager().createAccount("gato5", "gato5");
conn4.getAccountManager().createAccount("gato6", "gato6");
// Login with the test accounts
conn1.login("gato3", "gato3");
conn2.login("gato4", "gato4");
conn3.login("gato5", "gato5");
conn4.login("gato6", "gato6");
user1 = "gato3@" + conn1.getHost();
user2 = "gato4@" + conn2.getHost();
user3 = "gato5@" + conn3.getHost();
user4 = "gato6@" + conn4.getHost();
conn1.getRoster().createEntry(
"gato5@" + conn3.getHost(),
getConnection(0).getRoster().createEntry(
getBareJID(2),
"gato5",
new String[] { "Friends, Coworker" });
conn1.getRoster().createEntry("gato6@" + conn4.getHost(), "gato6", null);
getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
Thread.sleep(300);
} catch (Exception e) {
@ -236,22 +201,8 @@ public class RosterExchangeTest extends TestCase {
}
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
// Delete the created accounts for the test
conn1.getAccountManager().deleteAccount();
conn2.getAccountManager().deleteAccount();
conn3.getAccountManager().deleteAccount();
conn4.getAccountManager().deleteAccount();
// Close all the connections
conn1.close();
conn2.close();
conn3.close();
conn4.close();
protected int getMaxConnections() {
return 4;
}
}

View File

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