mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-26 16:22:06 +01:00
Converted this class to include only the tests that must be run when a room already exists
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2311 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
f840527300
commit
435fcd22c5
1 changed files with 260 additions and 130 deletions
|
@ -49,12 +49,14 @@
|
|||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
package org.jivesoftware.smackx;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.jivesoftware.smack.*;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.jivesoftware.smack.filter.*;
|
||||
import org.jivesoftware.smack.packet.*;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
@ -65,7 +67,18 @@ import junit.framework.TestCase;
|
|||
*/
|
||||
public class MultiUserChatTest extends TestCase {
|
||||
|
||||
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 MultiUserChat muc;
|
||||
|
||||
/**
|
||||
* Constructor for MultiUserChatTest.
|
||||
|
@ -75,103 +88,210 @@ public class MultiUserChatTest extends TestCase {
|
|||
super(arg0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests joining and leaving a room.
|
||||
*
|
||||
*/
|
||||
public void testJoinAndInvite() {
|
||||
MultiUserChat muc = new MultiUserChat(conn1, "dpspanish@muc.jabber.org");
|
||||
|
||||
|
||||
public void testParticipantPresence() {
|
||||
try {
|
||||
// Join the room
|
||||
muc.join("testbot", SmackConfiguration.getPacketReplyTimeout(), null, 0, -1, -1, null);
|
||||
// User2 joins the new room
|
||||
MultiUserChat muc2 = new MultiUserChat(conn2, room);
|
||||
muc2.join("testbot2",SmackConfiguration.getPacketReplyTimeout(),null,0,-1,-1,null);
|
||||
Thread.sleep(300);
|
||||
|
||||
// Invite another user to join to the room
|
||||
muc.invite("gdombiak@jabber.org","Meet me in this excellent room");
|
||||
// User1 checks the presence of user2 in the room
|
||||
Presence presence = muc.getParticipantPresence(room + "/testbot2");
|
||||
assertNotNull("Presence of user2 in room is missing", presence);
|
||||
assertEquals(
|
||||
"Presence mode of user2 is wrong",
|
||||
Presence.Mode.AVAILABLE,
|
||||
presence.getMode());
|
||||
|
||||
muc.changeAvailabilityStatus("Gone to have lunch", Presence.Mode.AWAY);
|
||||
// User2 changes his availability to AWAY
|
||||
muc2.changeAvailabilityStatus("Gone to have lunch", Presence.Mode.AWAY);
|
||||
Thread.sleep(200);
|
||||
// User1 checks the presence of user2 in the room
|
||||
presence = muc.getParticipantPresence(room + "/testbot2");
|
||||
assertNotNull("Presence of user2 in room is missing", presence);
|
||||
assertEquals("Presence mode of user2 is wrong", Presence.Mode.AWAY, presence.getMode());
|
||||
assertEquals(
|
||||
"Presence status of user2 is wrong",
|
||||
"Gone to have lunch",
|
||||
presence.getStatus());
|
||||
|
||||
muc.changeNickname("testbot2");
|
||||
// User2 changes his nickname
|
||||
muc2.changeNickname("testbotII");
|
||||
Thread.sleep(200);
|
||||
// User1 checks the presence of user2 in the room
|
||||
presence = muc.getParticipantPresence(room + "/testbot2");
|
||||
assertNull("Presence of participant testbot2 still exists", presence);
|
||||
presence = muc.getParticipantPresence(room + "/testbotII");
|
||||
assertNotNull("Presence of participant testbotII does not exist", presence);
|
||||
assertEquals(
|
||||
"Presence of participant testbotII has a wrong from",
|
||||
room + "/testbotII",
|
||||
presence.getFrom());
|
||||
|
||||
// User2 leaves the room
|
||||
muc2.leave();
|
||||
Thread.sleep(200);
|
||||
// User1 checks the presence of user2 in the room
|
||||
presence = muc.getParticipantPresence(room + "/testbotII");
|
||||
assertNull("Presence of participant testbotII still exists", presence);
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testInvitation() {
|
||||
final String[] answer = new String[2];
|
||||
try {
|
||||
// User2 joins the new room
|
||||
MultiUserChat muc2 = new MultiUserChat(conn2, room);
|
||||
muc2.join("testbot2",SmackConfiguration.getPacketReplyTimeout(),null,0,-1,-1,null);
|
||||
|
||||
// User3 is listening to MUC invitations
|
||||
MultiUserChat.addInvitationListener(conn3, 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");
|
||||
}
|
||||
});
|
||||
|
||||
// User2 is listening to invitation rejections
|
||||
muc2.addInvitationRejectionListener(new InvitationRejectionListener() {
|
||||
public void invitationDeclined(String invitee, String reason) {
|
||||
// Indicate that the rejection was received
|
||||
answer[1] = reason;
|
||||
}
|
||||
});
|
||||
|
||||
// User2 invites user3 to join to the room
|
||||
muc2.invite(user3, "Meet me in this excellent room");
|
||||
Thread.sleep(300);
|
||||
|
||||
assertEquals("Invitation was not received", "Meet me in this excellent room", answer[0]);
|
||||
// TODO This line was commented because jabberd2 is not accepting rejections
|
||||
// from users that aren't participants of the room. Remove the commented 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]);
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// TODO This test is commented because jabberd2 is responding an incorrect disco packet
|
||||
/*public void testDiscoverJoinedRooms() {
|
||||
try {
|
||||
// Check that user1 has joined only to one room
|
||||
Iterator joinedRooms = MultiUserChat.getJoinedRooms(conn2, user1);
|
||||
assertTrue("Joined rooms shouldn't be empty", joinedRooms.hasNext());
|
||||
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);
|
||||
assertFalse("Joined rooms should be empty", joinedRooms.hasNext());
|
||||
|
||||
muc.join("testbot");
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}*/
|
||||
|
||||
// Leave the room
|
||||
muc.leave();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a new "Reserved Room".
|
||||
*/
|
||||
public void testCreateReservedRoom() {
|
||||
MultiUserChat muc = new MultiUserChat(conn1, "fruta124@muc.jabber.org");
|
||||
|
||||
public void testPrivateChat() {
|
||||
try {
|
||||
// Create the room
|
||||
muc.create("testbot");
|
||||
// User2 joins the new room
|
||||
MultiUserChat muc2 = new MultiUserChat(conn2, room);
|
||||
muc2.join("testbot2",SmackConfiguration.getPacketReplyTimeout(),null,0,-1,-1,null);
|
||||
|
||||
// Get the the room's configuration form
|
||||
Form form = muc.getConfigurationForm();
|
||||
assertNotNull("No room configuration form", form);
|
||||
// Create a new form to submit based on the original form
|
||||
Form submitForm = form.createAnswerForm();
|
||||
// Add default answers to the form to submit
|
||||
for (Iterator fields=form.getFields();fields.hasNext();) {
|
||||
FormField field = (FormField) fields.next();
|
||||
if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
|
||||
// Add the field values to a List
|
||||
List values = new ArrayList();
|
||||
for (Iterator it=field.getValues();it.hasNext();) {
|
||||
values.add((String)it.next());
|
||||
}
|
||||
// Sets the new answer to form to submit
|
||||
submitForm.setAnswer(field.getVariable(), values);
|
||||
}
|
||||
}
|
||||
// Update the new room's configuration
|
||||
muc.sendConfigurationForm(submitForm);
|
||||
|
||||
// Destroy the new room
|
||||
muc.destroy("The room has almost no activity...", null);
|
||||
conn1.addPacketListener(new PacketListener() {
|
||||
public void processPacket(Packet packet) {
|
||||
Message message = (Message) packet;
|
||||
Chat chat2 = new Chat(conn1, message.getFrom(), message.getThread());
|
||||
assertEquals(
|
||||
"Sender of chat is incorrect",
|
||||
room + "/testbot2",
|
||||
message.getFrom());
|
||||
try {
|
||||
chat2.sendMessage("ACK");
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
System.out.println(e.getMessage());
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
},
|
||||
new AndFilter(
|
||||
new MessageTypeFilter(Message.Type.CHAT),
|
||||
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());
|
||||
}
|
||||
catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a new "Instant Room".
|
||||
*/
|
||||
public void testCreateInstantRoom() {
|
||||
MultiUserChat muc = new MultiUserChat(conn1, "fruta124@muc.jabber.org");
|
||||
// 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);
|
||||
|
||||
try {
|
||||
// Create the room
|
||||
muc.create("testbot");
|
||||
|
||||
// Send an empty room configuration form which indicates that we want
|
||||
// an instant room
|
||||
muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
|
||||
|
||||
// Destroy the new room
|
||||
muc.destroy("The room has almost no activity...", null);
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
String reservedNickname = muc2.getReservedNickname();
|
||||
assertNull("Reserved nickname is not null", reservedNickname);
|
||||
}*/
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
try {
|
||||
// Connect to the server
|
||||
conn1 = new XMPPConnection("jabber.org");
|
||||
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("jatul", "jatul");
|
||||
conn1.login("gato3", "gato3");
|
||||
conn2.login("gato4", "gato4");
|
||||
conn3.login("gato5", "gato5");
|
||||
|
||||
user1 = "gato3@" + conn1.getHost();
|
||||
user2 = "gato4@" + conn2.getHost();
|
||||
user3 = "gato5@" + conn2.getHost();
|
||||
|
||||
// User1 creates the room
|
||||
muc = new MultiUserChat(conn1, 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) {
|
||||
|
@ -181,7 +301,17 @@ public class MultiUserChatTest extends TestCase {
|
|||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
// Close the connection
|
||||
// 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();
|
||||
|
||||
// Close all the connections
|
||||
conn1.close();
|
||||
conn2.close();
|
||||
conn3.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue