KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > muc > MultiUserChatTest


1 /**
2 * $RCSfile$
3 * $Revision: 2729 $
4 * $Date: 2005-08-26 23:21:24 -0300 (Fri, 26 Aug 2005) $
5 *
6 * Copyright (C) 2002-2003 Jive Software. All rights reserved.
7 * ====================================================================
8 * The Jive Software License (based on Apache Software License, Version 1.1)
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. The end-user documentation included with the redistribution,
23 * if any, must include the following acknowledgment:
24 * "This product includes software developed by
25 * Jive Software (http://www.jivesoftware.com)."
26 * Alternately, this acknowledgment may appear in the software itself,
27 * if and wherever such third-party acknowledgments normally appear.
28 *
29 * 4. The names "Smack" and "Jive Software" must not be used to
30 * endorse or promote products derived from this software without
31 * prior written permission. For written permission, please
32 * contact webmaster@jivesoftware.com.
33 *
34 * 5. Products derived from this software may not be called "Smack",
35 * nor may "Smack" appear in their name, without prior written
36 * permission of Jive Software.
37 *
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 * ====================================================================
51 */

52
53 package org.jivesoftware.smackx.muc;
54
55 import java.util.*;
56 import java.text.SimpleDateFormat JavaDoc;
57
58 import org.jivesoftware.smack.*;
59 import org.jivesoftware.smack.filter.*;
60 import org.jivesoftware.smack.packet.*;
61 import org.jivesoftware.smack.test.SmackTestCase;
62 import org.jivesoftware.smackx.Form;
63 import org.jivesoftware.smackx.packet.XHTMLExtension;
64 import org.jivesoftware.smackx.packet.DelayInformation;
65
66 /**
67  * Tests the new MUC functionalities.
68  *
69  * @author Gaston Dombiak
70  */

71 public class MultiUserChatTest extends SmackTestCase {
72
73     private String JavaDoc room;
74
75     private MultiUserChat muc;
76
77     /**
78      * Constructor for MultiUserChatTest.
79      * @param arg0
80      */

81     public MultiUserChatTest(String JavaDoc arg0) {
82         super(arg0);
83     }
84
85     /**
86      * Test the compatibility of the MUC service with clients that still use the old groupchat
87      * protocol.
88      */

89     public void testGroupchatCompatibility() {
90         try {
91             Message message;
92             
93             GroupChat groupchat = new GroupChat(getConnection(1), room);
94             groupchat.join("testbot2");
95             Thread.sleep(400);
96
97             // User1 checks the presence of user2 in the room
98
Presence presence = muc.getOccupantPresence(room + "/testbot2");
99             assertNotNull("Presence of user2 in room is missing", presence);
100             assertEquals(
101                 "Presence mode of user2 is wrong",
102                 Presence.Mode.AVAILABLE,
103                 presence.getMode());
104             
105             // User using old client send a message
106
groupchat.sendMessage("Hello");
107             // Check that the rest of the occupants (that are support MUC) received the message
108
message = muc.nextMessage(1000);
109             assertNotNull("A MUC client didn't receive the message from an old client", message);
110             // User that supports MUC send a message
111
muc.sendMessage("Bye");
112             // Check that the client the doesn't support MUC received the message
113
message = groupchat.nextMessage(1000);
114             assertNotNull("An old client didn't receive the message from a MUC client", message);
115             // User that doesn't support MUC leaves the room
116
groupchat.leave();
117             Thread.sleep(300);
118             // User1 checks the that user2 is not present in the room
119
Occupant occupant = muc.getOccupant(room + "/testbot2");
120             assertNull("Occupant testbot2 still exists", occupant);
121         }
122         catch (Exception JavaDoc e) {
123             e.printStackTrace();
124             fail(e.getMessage());
125         }
126     }
127
128     public void testDiscussionHistory() {
129         try {
130             // User1 sends some messages to the room
131
muc.sendMessage("Message 1");
132             muc.sendMessage("Message 2");
133             // Wait 5 seconds before sending the last message
134
Thread.sleep(5000);
135             muc.sendMessage("Message 3");
136
137             // User2 joins the room requesting to receive the messages of the last 2 seconds.
138
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
139             DiscussionHistory history = new DiscussionHistory();
140             history.setSeconds(2);
141             muc2.join("testbot2", null, history, SmackConfiguration.getPacketReplyTimeout());
142     
143             Message msg;
144             // Get first historic message
145
msg = muc2.nextMessage(1000);
146             DelayInformation delay = (DelayInformation) msg.getExtension("x", "jabber:x:delay");
147             SimpleDateFormat JavaDoc UTC_FORMAT = new SimpleDateFormat JavaDoc("yyyyMMdd'T'HH:mm:ss");
148             UTC_FORMAT.setTimeZone(TimeZone.getDefault());
149             System.out.println(UTC_FORMAT.format(delay.getStamp()));
150             
151             assertNotNull("First message is null", msg);
152             assertEquals("Body of first message is incorrect", "Message 3", msg.getBody());
153             // Try to get second historic message
154
msg = muc2.nextMessage(1000);
155             assertNull("Second message is not null", msg);
156
157
158             // User3 joins the room requesting to receive the last 2 messages.
159
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
160             history = new DiscussionHistory();
161             history.setMaxStanzas(2);
162             muc3.join("testbot3", null, history, SmackConfiguration.getPacketReplyTimeout());
163
164             // Get first historic message
165
msg = muc3.nextMessage(1000);
166             assertNotNull("First message is null", msg);
167             assertEquals("Body of first message is incorrect", "Message 2", msg.getBody());
168             // Get second historic message
169
msg = muc3.nextMessage(1000);
170             assertNotNull("Second message is null", msg);
171             assertEquals("Body of second message is incorrect", "Message 3", msg.getBody());
172             // Try to get third historic message
173
msg = muc3.nextMessage(1000);
174             assertNull("Third message is not null", msg);
175     
176             // User2 leaves the room
177
muc2.leave();
178             // User3 leaves the room
179
muc3.leave();
180     
181         }
182         catch (Exception JavaDoc e) {
183             e.printStackTrace();
184             fail(e.getMessage());
185         }
186     }
187
188     public void testParticipantPresence() {
189         try {
190             // User2 joins the new room
191
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
192             muc2.join("testbot2");
193             Thread.sleep(400);
194
195             // User1 checks the presence of user2 in the room
196
Presence presence = muc.getOccupantPresence(room + "/testbot2");
197             assertNotNull("Presence of user2 in room is missing", presence);
198             assertEquals(
199                 "Presence mode of user2 is wrong",
200                 Presence.Mode.AVAILABLE,
201                 presence.getMode());
202
203             // User2 changes his availability to AWAY
204
muc2.changeAvailabilityStatus("Gone to have lunch", Presence.Mode.AWAY);
205             Thread.sleep(200);
206             // User1 checks the presence of user2 in the room
207
presence = muc.getOccupantPresence(room + "/testbot2");
208             assertNotNull("Presence of user2 in room is missing", presence);
209             assertEquals("Presence mode of user2 is wrong", Presence.Mode.AWAY, presence.getMode());
210             assertEquals(
211                 "Presence status of user2 is wrong",
212                 "Gone to have lunch",
213                 presence.getStatus());
214
215             // User2 changes his nickname
216
muc2.changeNickname("testbotII");
217             Thread.sleep(200);
218             // User1 checks the presence of user2 in the room
219
presence = muc.getOccupantPresence(room + "/testbot2");
220             assertNull("Presence of participant testbot2 still exists", presence);
221             presence = muc.getOccupantPresence(room + "/testbotII");
222             assertNotNull("Presence of participant testbotII does not exist", presence);
223             assertEquals(
224                 "Presence of participant testbotII has a wrong from",
225                 room + "/testbotII",
226                 presence.getFrom());
227
228             // User2 leaves the room
229
muc2.leave();
230             Thread.sleep(250);
231             // User1 checks the presence of user2 in the room
232
presence = muc.getOccupantPresence(room + "/testbotII");
233             assertNull("Presence of participant testbotII still exists", presence);
234
235         }
236         catch (Exception JavaDoc e) {
237             e.printStackTrace();
238             fail(e.getMessage());
239         }
240     }
241
242     public void testAnonymousParticipant() {
243         try {
244             // Anonymous user joins the new room
245
XMPPConnection anonConnection = new XMPPConnection(getHost(), getPort());
246             anonConnection.loginAnonymously();
247             MultiUserChat muc2 = new MultiUserChat(anonConnection, room);
248             muc2.join("testbot2");
249             Thread.sleep(400);
250
251             // User1 checks the presence of Anonymous user in the room
252
Presence presence = muc.getOccupantPresence(room + "/testbot2");
253             assertNotNull("Presence of user2 in room is missing", presence);
254             assertEquals(
255                 "Presence mode of user2 is wrong",
256                 Presence.Mode.AVAILABLE,
257                 presence.getMode());
258
259             // Anonymous user leaves the room
260
muc2.leave();
261             anonConnection.close();
262             Thread.sleep(250);
263             // User1 checks the presence of Anonymous user in the room
264
presence = muc.getOccupantPresence(room + "/testbot2");
265             assertNull("Presence of participant testbotII still exists", presence);
266
267         }
268         catch (Exception JavaDoc e) {
269             e.printStackTrace();
270             fail(e.getMessage());
271         }
272     }
273
274     public void testInvitation() {
275         final String JavaDoc[] answer = new String JavaDoc[2];
276         try {
277             // User2 joins the new room
278
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
279             muc2.join("testbot2");
280
281             // User3 is listening to MUC invitations
282
MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {
283                 public void invitationReceived(
284                     XMPPConnection conn,
285                     String JavaDoc room,
286                     String JavaDoc inviter,
287                     String JavaDoc reason,
288                     String JavaDoc password,
289                     Message message) {
290                     // Indicate that the invitation was received
291
answer[0] = reason;
292                     // Reject the invitation
293
MultiUserChat.decline(conn, room, inviter, "I'm busy right now");
294                 }
295             });
296
297             // User2 is listening to invitation rejections
298
muc2.addInvitationRejectionListener(new InvitationRejectionListener() {
299                 public void invitationDeclined(String JavaDoc invitee, String JavaDoc reason) {
300                     // Indicate that the rejection was received
301
answer[1] = reason;
302                 }
303             });
304
305             // User2 invites user3 to join to the room
306
muc2.invite(getFullJID(2), "Meet me in this excellent room");
307             Thread.sleep(350);
308
309             assertEquals(
310                 "Invitation was not received",
311                 "Meet me in this excellent room",
312                 answer[0]);
313             assertEquals("Rejection was not received", "I'm busy right now", answer[1]);
314
315             // User2 leaves the room
316
muc2.leave();
317         }
318         catch (Exception JavaDoc e) {
319             e.printStackTrace();
320             fail(e.getMessage());
321         }
322     }
323
324     public void testInvitationWithMessage() {
325         final String JavaDoc[] answer = new String JavaDoc[2];
326         try {
327             // User2 joins the new room
328
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
329             muc2.join("testbot2");
330
331             // User3 is listening to MUC invitations
332
MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {
333                 public void invitationReceived(
334                     XMPPConnection conn,
335                     String JavaDoc room,
336                     String JavaDoc inviter,
337                     String JavaDoc reason,
338                     String JavaDoc password,
339                     Message message) {
340                     // Indicate that the invitation was received
341
answer[0] = reason;
342                     XHTMLExtension extension = (XHTMLExtension) message.getExtension("html",
343                             "http://jabber.org/protocol/xhtml-im");
344                     assertNotNull("An extension was not found in the invitation", extension);
345                     answer[1] = (String JavaDoc) extension.getBodies().next();
346                 }
347             });
348
349             // User2 invites user3 to join to the room
350
Message msg = new Message();
351             XHTMLExtension xhtmlExtension = new XHTMLExtension();
352             xhtmlExtension.addBody("<body>Meet me in this excellent room</body>");
353             msg.addExtension(xhtmlExtension);
354             muc2.invite(msg , getFullJID(2), "Meet me in this excellent room");
355             Thread.sleep(350);
356
357             assertEquals(
358                 "Invitation was not received",
359                 "Meet me in this excellent room",
360                 answer[0]);
361             assertEquals("Rejection was not received",
362                     "<body>Meet me in this excellent room</body>", answer[1]);
363
364             // User2 leaves the room
365
muc2.leave();
366         }
367         catch (Exception JavaDoc e) {
368             e.printStackTrace();
369             fail(e.getMessage());
370         }
371     }
372
373     public void testDiscoverJoinedRooms() {
374         try {
375             // Check that user1 has joined only to one room
376
Iterator joinedRooms = MultiUserChat.getJoinedRooms(getConnection(1), getFullJID(0));
377             assertTrue("Joined rooms shouldn't be empty", joinedRooms.hasNext());
378             assertEquals("Joined room is incorrect", joinedRooms.next(), room);
379             assertFalse("User has joined more than one room", joinedRooms.hasNext());
380
381             // Leave the new room
382
muc.leave();
383
384             // Check that user1 is not currently join any room
385
joinedRooms = MultiUserChat.getJoinedRooms(getConnection(1), getFullJID(0));
386             assertFalse("Joined rooms should be empty", joinedRooms.hasNext());
387
388             muc.join("testbot");
389         }
390         catch (XMPPException e) {
391             e.printStackTrace();
392             fail(e.getMessage());
393         }
394     }
395
396     public void testDiscoverMUCSupport() {
397         // Discover user1 support of MUC
398
boolean supports = MultiUserChat.isServiceEnabled(getConnection(1), getFullJID(0));
399         assertTrue("Couldn't detect that user1 supports MUC", supports);
400     }
401
402     public void testDiscoverRoomInfo() {
403         try {
404             makeRoomModerated();
405
406             RoomInfo info = MultiUserChat.getRoomInfo(getConnection(1), room);
407
408             assertFalse("Room is members-only", info.isMembersOnly());
409             assertTrue("Room is moderated", info.isModerated());
410             assertFalse("Room is Nonanonymous", info.isNonanonymous());
411             assertFalse("Room is PasswordProtected", info.isPasswordProtected());
412             assertFalse("Room is Persistent", info.isPersistent());
413             assertEquals("Room's description is incorrect", "fruta124", info.getDescription());
414             assertEquals("Room's subject is incorrect", "", info.getSubject());
415             assertEquals("Number of occupants is incorrect", 1, info.getOccupantsCount());
416         }
417         catch (XMPPException e) {
418             e.printStackTrace();
419             fail(e.getMessage());
420         }
421     }
422
423     public void testDiscoverMUCService() {
424         try {
425             Collection services = MultiUserChat.getServiceNames(getConnection(1));
426             assertFalse("No MUC service was found", services.isEmpty());
427
428             // Discover the hosted rooms by the chat service.
429
Collection rooms = MultiUserChat.getHostedRooms(getConnection(1),
430                     (String JavaDoc) services.toArray()[0]);
431             // Check that we have discovered the room used by this test
432
assertFalse("No room was found", rooms.isEmpty());
433             // Check that we have discovered the room used by this test
434
assertEquals("Wrong room JID found", room, ((HostedRoom)rooms.toArray()[0]).getJid());
435         }
436         catch (XMPPException e) {
437             e.printStackTrace();
438             fail(e.getMessage());
439         }
440     }
441
442     public void testPrivateChat() {
443         try {
444             // User2 joins the new room
445
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
446             muc2.join("testbot2");
447
448             getConnection(0).addPacketListener(new PacketListener() {
449                 public void processPacket(Packet packet) {
450                     Message message = (Message) packet;
451                     Chat chat2 = new Chat(getConnection(0), message.getFrom(), message.getThread());
452                     assertEquals(
453                         "Sender of chat is incorrect",
454                         room + "/testbot2",
455                         message.getFrom());
456                     try {
457                         chat2.sendMessage("ACK");
458                     }
459                     catch (XMPPException e) {
460                         fail(e.getMessage());
461                     }
462                 }
463             },
464                 new AndFilter(
465                     new MessageTypeFilter(Message.Type.CHAT),
466                     new PacketTypeFilter(Message.class)));
467
468             // Start a private chat with another participant
469
Chat chat = muc2.createPrivateChat(room + "/testbot");
470             chat.sendMessage("Hello there");
471
472             Message response = chat.nextMessage(2000);
473             assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
474             assertEquals("Body of response is incorrect", "ACK", response.getBody());
475
476             // User2 leaves the room
477
muc2.leave();
478         }
479         catch (Exception JavaDoc e) {
480             e.printStackTrace();
481             fail(e.getMessage());
482         }
483     }
484
485     public void testReservedNickname() {
486         try {
487             MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
488             // Check that user2 doesn't have a reserved nickname yet
489
String JavaDoc reservedNickname = muc2.getReservedNickname();
490             assertNull("Reserved nickname is not null", reservedNickname);
491             
492             // User2 registers with the room and reserves a nickname
493
Form registrationForm = muc2.getRegistrationForm();
494             Form answerForm = registrationForm.createAnswerForm();
495             answerForm.setAnswer("muc#register_first", "MyFirstName");
496             answerForm.setAnswer("muc#register_last", "MyLastName");
497             answerForm.setAnswer("muc#register_roomnick", "MyNick");
498             muc2.sendRegistrationForm(answerForm);
499             
500             // Check that user2 has a reserved nickname
501
reservedNickname = muc2.getReservedNickname();
502             assertEquals("Reserved nickname is wrong", "MyNick", reservedNickname);
503             
504             // Check that user2 can join the room using his reserved nickname
505
muc2.join("MyNick");
506             muc2.leave();
507             
508             // Check that other users cannot join the room with user2's reserved nickname
509
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
510             try {
511                 muc3.join("MyNick");
512                 fail("Other user was able to join with other user's reserved nickname");
513             }
514             catch (XMPPException e) {
515                 XMPPError xmppError = e.getXMPPError();
516                 assertNotNull(
517                     "No XMPPError was received when joining with other user's reserved nickname",
518                     xmppError);
519                 assertEquals(
520                     "Different error code was received while joining with other user's reserved nickname",
521                     409,
522                     xmppError.getCode());
523             }
524             
525             // Check that user3 can join the room using his own nickname (not reserved)
526
muc3.join("MyNotReservedNick");
527             muc3.leave();
528             
529             // Check that another user cannot reserve an already reserved nickname
530
registrationForm = muc3.getRegistrationForm();
531             answerForm = registrationForm.createAnswerForm();
532             answerForm.setAnswer("muc#register_first", "MyFirstName 2");
533             answerForm.setAnswer("muc#register_last", "MyLastName 2");
534             answerForm.setAnswer("muc#register_roomnick", "MyNick");
535             try {
536                 muc3.sendRegistrationForm(answerForm);
537             }
538             catch (XMPPException e) {
539                 XMPPError xmppError = e.getXMPPError();
540                 assertNotNull(
541                     "No XMPPError was received when reserving an already reserved nickname",
542                     xmppError);
543                 assertEquals(
544                     "Different error code was received while reserving an already reserved nickname",
545                     409,
546                     xmppError.getCode());
547             }
548             
549             // Check that another user can reserve a new nickname
550
registrationForm = muc3.getRegistrationForm();
551             answerForm = registrationForm.createAnswerForm();
552             answerForm.setAnswer("muc#register_first", "MyFirstName 2");
553             answerForm.setAnswer("muc#register_last", "MyLastName 2");
554             answerForm.setAnswer("muc#register_roomnick", "MyNick 2");
555             muc3.sendRegistrationForm(answerForm);
556             
557         }
558         catch (XMPPException e) {
559             e.printStackTrace();
560             fail(e.getMessage());
561         }
562     }
563
564     public void testChangeSubject() {
565         final String JavaDoc[] answer = new String JavaDoc[2];
566         try {
567             // User1 sets an initial subject
568
muc.changeSubject("Initial Subject");
569
570             // User2 joins the new room
571
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
572             muc2.join("testbot2");
573
574             // User3 joins the new room
575
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
576             muc3.join("testbot3");
577
578             // User3 wants to be notified every time the room's subject is changed.
579
muc3.addSubjectUpdatedListener(new SubjectUpdatedListener() {
580                 public void subjectUpdated(String JavaDoc subject, String JavaDoc from) {
581                     answer[0] = subject;
582                     answer[1] = from;
583                 }
584             });
585
586             // Check that a 403 error is received when a not allowed user tries to change the
587
// subject in a room
588
try {
589                 muc2.changeSubject("New Subject2");
590                 fail("User2 was allowed to change the room's subject");
591             }
592             catch (XMPPException e) {
593                 XMPPError xmppError = e.getXMPPError();
594                 assertNotNull(
595                     "No XMPPError was received when changing the room's subject",
596                     xmppError);
597                 assertEquals(
598                     "Different error code was received while changing the room's subject",
599                     403,
600                     xmppError.getCode());
601             }
602
603             // Check that every MUC updates its subject when an allowed user changes the subject
604
// in a room
605
muc.changeSubject("New Subject1");
606             Thread.sleep(300);
607             // Check that User2's MUC has updated its subject
608
assertEquals(
609                 "User2 didn't receive the subject notification",
610                 "New Subject1",
611                 muc2.getSubject());
612             // Check that SubjectUpdatedListener is working OK
613
assertEquals(
614                 "User3 didn't receive the subject notification",
615                 "New Subject1",
616                 answer[0]);
617             assertEquals(
618                 "User3 didn't receive the correct user that changed the subject",
619                 room + "/testbot",
620                 answer[1]);
621
622             // User2 leaves the room
623
muc2.leave();
624             // User3 leaves the room
625
muc3.leave();
626         }
627         catch (Exception JavaDoc e) {
628             e.printStackTrace();
629             fail(e.getMessage());
630         }
631     }
632
633     public void testKickParticipant() {
634         final String JavaDoc[] answer = new String JavaDoc[3];
635         try {
636             // User2 joins the new room
637
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
638             muc2.join("testbot2");
639             // User2 will lister for his own "kicking"
640
muc2.addUserStatusListener(new DefaultUserStatusListener() {
641                 public void kicked(String JavaDoc actor, String JavaDoc reason) {
642                     super.kicked(actor, reason);
643                     answer[0] = actor;
644                     answer[1] = reason;
645                 }
646             });
647
648             // User3 joins the new room
649
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
650             muc3.join("testbot3");
651             // User3 will lister for user2's "kicking"
652
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
653                 public void kicked(String JavaDoc participant) {
654                     super.kicked(participant);
655                     answer[2] = participant;
656                 }
657             });
658
659             try {
660                 // Check whether a simple participant can kick a room owner or not
661
muc2.kickParticipant("testbot", "Because I'm bad");
662                 fail("User2 was able to kick a room owner");
663             }
664             catch (XMPPException e) {
665                 XMPPError xmppError = e.getXMPPError();
666                 assertNotNull("No XMPPError was received when kicking a room owner", xmppError);
667                 assertEquals(
668                     "A simple participant was able to kick another participant from the room",
669                     403,
670                     xmppError.getCode());
671             }
672
673             // Check that the room's owner can kick a simple participant
674
muc.kickParticipant("testbot2", "Because I'm the owner");
675             Thread.sleep(300);
676
677             assertNull(
678                 "User2 wasn't kicked from the room",
679                 muc.getOccupant(room + "/testbot2"));
680
681             assertFalse("User2 thinks that he's still in the room", muc2.isJoined());
682
683             // Check that UserStatusListener is working OK
684
assertEquals(
685                 "User2 didn't receive the correct initiator of the kick",
686                 getBareJID(0),
687                 answer[0]);
688             assertEquals(
689                 "User2 didn't receive the correct reason for the kick",
690                 "Because I'm the owner",
691                 answer[1]);
692
693             // Check that ParticipantStatusListener is working OK
694
assertEquals(
695                 "User3 didn't receive the correct kicked participant",
696                 room + "/testbot2",
697                 answer[2]);
698
699             // User2 leaves the room
700
muc2.leave();
701             // User3 leaves the room
702
muc3.leave();
703         }
704         catch (Exception JavaDoc e) {
705             e.printStackTrace();
706             fail(e.getMessage());
707         }
708     }
709
710     public void testBanUser() {
711         final String JavaDoc[] answer = new String JavaDoc[3];
712         try {
713             // User2 joins the new room
714
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
715             muc2.join("testbot2");
716             // User2 will lister for his own "banning"
717
muc2.addUserStatusListener(new DefaultUserStatusListener() {
718                 public void banned(String JavaDoc actor, String JavaDoc reason) {
719                     super.banned(actor, reason);
720                     answer[0] = actor;
721                     answer[1] = reason;
722                 }
723             });
724
725             // User3 joins the new room
726
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
727             muc3.join("testbot3");
728             // User3 will lister for user2's "banning"
729
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
730                 public void banned(String JavaDoc participant) {
731                     super.banned(participant);
732                     answer[2] = participant;
733                 }
734             });
735
736             try {
737                 // Check whether a simple participant can ban a room owner or not
738
muc2.banUser(getBareJID(0), "Because I'm bad");
739                 fail("User2 was able to ban a room owner");
740             }
741             catch (XMPPException e) {
742                 XMPPError xmppError = e.getXMPPError();
743                 assertNotNull("No XMPPError was received when banning a room owner", xmppError);
744                 assertEquals(
745                     "A simple participant was able to ban another participant from the room",
746                     403,
747                     xmppError.getCode());
748             }
749
750             // Check that the room's owner can ban a simple participant
751
muc.banUser(getBareJID(1), "Because I'm the owner");
752             Thread.sleep(300);
753
754             assertNull(
755                 "User2 wasn't banned from the room",
756                 muc.getOccupant(room + "/testbot2"));
757
758             assertFalse("User2 thinks that he's still in the room", muc2.isJoined());
759
760             // Check that UserStatusListener is working OK
761
assertEquals(
762                 "User2 didn't receive the correct initiator of the ban",
763                 getBareJID(0),
764                 answer[0]);
765             assertEquals(
766                 "User2 didn't receive the correct reason for the banning",
767                 "Because I'm the owner",
768                 answer[1]);
769
770             // Check that ParticipantStatusListener is working OK
771
assertEquals(
772                 "User3 didn't receive the correct banned JID",
773                 room + "/testbot2",
774                 answer[2]);
775
776             // User2 leaves the room
777
muc2.leave();
778             // User3 leaves the room
779
muc3.leave();
780         }
781         catch (Exception JavaDoc e) {
782             e.printStackTrace();
783             fail(e.getMessage());
784         }
785     }
786
787     public void testVoice() {
788         final String JavaDoc[] answer = new String JavaDoc[4];
789         try {
790
791             makeRoomModerated();
792
793             // User2 joins the new room (as a visitor)
794
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
795             muc2.join("testbot2");
796             // User2 will listen for his own "voice"
797
muc2.addUserStatusListener(new DefaultUserStatusListener() {
798                 public void voiceGranted() {
799                     super.voiceGranted();
800                     answer[0] = "canSpeak";
801                 }
802                 public void voiceRevoked() {
803                     super.voiceRevoked();
804                     answer[1] = "cannot speak";
805                 }
806             });
807
808             // User3 joins the new room (as a visitor)
809
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
810             muc3.join("testbot3");
811             // User3 will lister for user2's "voice"
812
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
813                 public void voiceGranted(String JavaDoc participant) {
814                     super.voiceGranted(participant);
815                     answer[2] = participant;
816                 }
817
818                 public void voiceRevoked(String JavaDoc participant) {
819                     super.voiceRevoked(participant);
820                     answer[3] = participant;
821                 }
822             });
823
824             try {
825                 // Check whether a visitor can grant voice to another visitor
826
muc2.grantVoice("testbot3");
827                 fail("User2 was able to grant voice");
828             }
829             catch (XMPPException e) {
830                 XMPPError xmppError = e.getXMPPError();
831                 assertNotNull("No XMPPError was received granting voice", xmppError);
832                 assertEquals(
833                     "A visitor was able to grant voice to another visitor",
834                     403,
835                     xmppError.getCode());
836             }
837
838             // Check that the room's owner can grant voice to a participant
839
muc.grantVoice("testbot2");
840             Thread.sleep(300);
841
842             // Check that UserStatusListener is working OK
843
assertEquals(
844                 "User2 didn't receive the grant voice notification",
845                 "canSpeak",
846                 answer[0]);
847             // Check that ParticipantStatusListener is working OK
848
assertEquals(
849                 "User3 didn't receive user2's grant voice notification",
850                 room + "/testbot2",
851                 answer[2]);
852
853             // Check that the room's owner can revoke voice from a participant
854
muc.revokeVoice("testbot2");
855             Thread.sleep(300);
856
857             assertEquals(
858                 "User2 didn't receive the revoke voice notification",
859                 "cannot speak",
860                 answer[1]);
861             assertEquals(
862                 "User3 didn't receive user2's revoke voice notification",
863                 room + "/testbot2",
864                 answer[3]);
865
866             // User2 leaves the room
867
muc2.leave();
868             // User3 leaves the room
869
muc3.leave();
870         }
871         catch (Exception JavaDoc e) {
872             e.printStackTrace();
873             fail(e.getMessage());
874         }
875     }
876
877     public void testModerator() {
878         final String JavaDoc[] answer = new String JavaDoc[8];
879         try {
880
881             makeRoomModerated();
882
883             // User2 joins the new room (as a visitor)
884
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
885             muc2.join("testbot2");
886             // User2 will listen for moderator privileges
887
muc2.addUserStatusListener(new DefaultUserStatusListener() {
888                 public void voiceGranted() {
889                     super.voiceGranted();
890                     answer[0] = "canSpeak";
891                 }
892                 public void voiceRevoked() {
893                     super.voiceRevoked();
894                     answer[1] = "cannot speak";
895                 }
896                 public void moderatorGranted() {
897                     super.moderatorGranted();
898                     answer[4] = "I'm a moderator";
899                 }
900                 public void moderatorRevoked() {
901                     super.moderatorRevoked();
902                     answer[5] = "I'm not a moderator";
903                 }
904             });
905
906             // User3 joins the new room (as a visitor)
907
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
908             muc3.join("testbot3");
909             // User3 will lister for user2's moderator privileges
910
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
911                 public void voiceGranted(String JavaDoc participant) {
912                     super.voiceGranted(participant);
913                     answer[2] = participant;
914                 }
915                 public void voiceRevoked(String JavaDoc participant) {
916                     super.voiceRevoked(participant);
917                     answer[3] = participant;
918                 }
919                 public void moderatorGranted(String JavaDoc participant) {
920                     super.moderatorGranted(participant);
921                     answer[6] = participant;
922                 }
923                 public void moderatorRevoked(String JavaDoc participant) {
924                     super.moderatorRevoked(participant);
925                     answer[7] = participant;
926                 }
927             });
928
929             try {
930                 // Check whether a visitor can grant moderator privileges to another visitor
931
muc2.grantModerator("testbot3");
932                 fail("User2 was able to grant moderator privileges");
933             }
934             catch (XMPPException e) {
935                 XMPPError xmppError = e.getXMPPError();
936                 assertNotNull("No XMPPError was received granting moderator privileges", xmppError);
937                 assertEquals(
938                     "A visitor was able to grant moderator privileges to another visitor",
939                     403,
940                     xmppError.getCode());
941             }
942
943             // Check that the room's owner can grant moderator privileges to a visitor
944
muc.grantModerator("testbot2");
945             Thread.sleep(300);
946
947             // Check that UserStatusListener is working OK
948
assertEquals(
949                 "User2 didn't receive the grant voice notification",
950                 "canSpeak",
951                 answer[0]);
952             assertEquals(
953                 "User2 didn't receive the grant moderator privileges notification",
954                 "I'm a moderator",
955                 answer[4]);
956             // Check that ParticipantStatusListener is working OK
957
assertEquals(
958                 "User3 didn't receive user2's grant voice notification",
959                 room + "/testbot2",
960                 answer[2]);
961             assertEquals(
962                 "User3 didn't receive user2's grant moderator privileges notification",
963                 room + "/testbot2",
964                 answer[6]);
965
966             // Check that the room's owner can revoke moderator privileges from a moderator
967
muc.revokeModerator("testbot2");
968             Thread.sleep(300);
969
970             assertNull("User2 received a false revoke voice notification", answer[1]);
971             assertNull("User3 received a false user2's voice privileges notification", answer[3]);
972             assertEquals(
973                 "User2 didn't receive the revoke moderator privileges notification",
974                 "I'm not a moderator",
975                 answer[5]);
976             assertEquals(
977                 "User3 didn't receive user2's revoke moderator privileges notification",
978                 room + "/testbot2",
979                 answer[7]);
980
981             // Check that the room's owner can grant moderator privileges to a participant
982
clearAnswer(answer);
983             muc.grantModerator("testbot2");
984             Thread.sleep(300);
985
986             // Check that UserStatusListener is working OK
987
assertNull("User2 received a false grant voice notification", answer[0]);
988             assertEquals(
989                 "User2 didn't receive the grant moderator privileges notification",
990                 "I'm a moderator",
991                 answer[4]);
992             // Check that ParticipantStatusListener is working OK
993
assertNull("User3 received a false user2's grant voice notification", answer[2]);
994             assertEquals(
995                 "User3 didn't receive user2's grant moderator privileges notification",
996                 room + "/testbot2",
997                 answer[6]);
998
999             // Check that the room's owner can revoke voice from a moderator
1000
clearAnswer(answer);
1001            muc.revokeVoice("testbot2");
1002            Thread.sleep(300);
1003
1004            assertEquals(
1005                "User2 didn't receive the revoke voice notification",
1006                "cannot speak",
1007                answer[1]);
1008            assertEquals(
1009                "User3 didn't receive user2's revoke voice notification",
1010                room + "/testbot2",
1011                answer[3]);
1012
1013            // User2 leaves the room
1014
muc2.leave();
1015            // User3 leaves the room
1016
muc3.leave();
1017        }
1018        catch (Exception JavaDoc e) {
1019            e.printStackTrace();
1020            fail(e.getMessage());
1021        }
1022    }
1023
1024    public void testMembership() {
1025        final String JavaDoc[] answer = new String JavaDoc[4];
1026        try {
1027
1028            makeRoomModerated();
1029
1030            // User2 joins the new room (as a visitor)
1031
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
1032            muc2.join("testbot2");
1033            // User2 will listen for membership privileges
1034
muc2.addUserStatusListener(new DefaultUserStatusListener() {
1035                public void membershipGranted() {
1036                    super.membershipGranted();
1037                    answer[0] = "I'm a member";
1038                }
1039                public void membershipRevoked() {
1040                    super.membershipRevoked();
1041                    answer[1] = "I'm not a member";
1042                }
1043            });
1044
1045            // User3 joins the new room (as a visitor)
1046
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
1047            muc3.join("testbot3");
1048            // User3 will lister for user2's membership privileges
1049
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
1050                public void membershipGranted(String JavaDoc participant) {
1051                    super.membershipGranted(participant);
1052                    answer[2] = participant;
1053                }
1054                public void membershipRevoked(String JavaDoc participant) {
1055                    super.membershipRevoked(participant);
1056                    answer[3] = participant;
1057                }
1058            });
1059
1060            try {
1061                // Check whether a visitor can grant membership privileges to another visitor
1062
muc2.grantMembership(getBareJID(2));
1063                fail("User2 was able to grant membership privileges");
1064            }
1065            catch (XMPPException e) {
1066                XMPPError xmppError = e.getXMPPError();
1067                assertNotNull(
1068                    "No XMPPError was received granting membership privileges",
1069                    xmppError);
1070                assertEquals(
1071                    "A visitor was able to grant membership privileges to another visitor",
1072                    403,
1073                    xmppError.getCode());
1074            }
1075
1076            // Check that the room's owner can grant membership privileges to a visitor
1077
muc.grantMembership(getBareJID(1));
1078            Thread.sleep(300);
1079
1080            // Check that UserStatusListener is working OK
1081
assertEquals(
1082                "User2 didn't receive the grant membership notification",
1083                "I'm a member",
1084                answer[0]);
1085            // Check that ParticipantStatusListener is working OK
1086
assertEquals(
1087                "User3 didn't receive user2's grant membership notification",
1088                room + "/testbot2",
1089                answer[2]);
1090
1091            // Check that the room's owner can revoke membership privileges from a member
1092
// and make the occupant a visitor
1093
muc.revokeMembership(getBareJID(1));
1094            Thread.sleep(300);
1095
1096            // Check that UserStatusListener is working OK
1097
assertEquals(
1098                "User2 didn't receive the revoke membership notification",
1099                "I'm not a member",
1100                answer[1]);
1101            // Check that ParticipantStatusListener is working OK
1102
assertEquals(
1103                "User3 didn't receive user2's revoke membership notification",
1104                room + "/testbot2",
1105                answer[3]);
1106
1107            // User2 leaves the room
1108
muc2.leave();
1109            // User3 leaves the room
1110
muc3.leave();
1111        }
1112        catch (Exception JavaDoc e) {
1113            e.printStackTrace();
1114            fail(e.getMessage());
1115        }
1116    }
1117
1118    public void testAdmin() {
1119        final String JavaDoc[] answer = new String JavaDoc[8];
1120        try {
1121
1122            makeRoomModerated();
1123
1124            // User2 joins the new room (as a visitor)
1125
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
1126            muc2.join("testbot2");
1127            // User2 will listen for admin privileges
1128
muc2.addUserStatusListener(new DefaultUserStatusListener() {
1129                public void membershipGranted() {
1130                    super.membershipGranted();
1131                    answer[0] = "I'm a member";
1132                }
1133                public void membershipRevoked() {
1134                    super.membershipRevoked();
1135                    answer[1] = "I'm not a member";
1136                }
1137                public void adminGranted() {
1138                    super.adminGranted();
1139                    answer[2] = "I'm an admin";
1140                }
1141                public void adminRevoked() {
1142                    super.adminRevoked();
1143                    answer[3] = "I'm not an admin";
1144                }
1145            });
1146
1147            // User3 joins the new room (as a visitor)
1148
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
1149            muc3.join("testbot3");
1150            // User3 will lister for user2's admin privileges
1151
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
1152                public void membershipGranted(String JavaDoc participant) {
1153                    super.membershipGranted(participant);
1154                    answer[4] = participant;
1155                }
1156                public void membershipRevoked(String JavaDoc participant) {
1157                    super.membershipRevoked(participant);
1158                    answer[5] = participant;
1159                }
1160                public void adminGranted(String JavaDoc participant) {
1161                    super.adminGranted(participant);
1162                    answer[6] = participant;
1163                }
1164                public void adminRevoked(String JavaDoc participant) {
1165                    super.adminRevoked(participant);
1166                    answer[7] = participant;
1167                }
1168            });
1169
1170            try {
1171                // Check whether a visitor can grant admin privileges to another visitor
1172
muc2.grantAdmin(getBareJID(2));
1173                fail("User2 was able to grant admin privileges");
1174            }
1175            catch (XMPPException e) {
1176                XMPPError xmppError = e.getXMPPError();
1177                assertNotNull("No XMPPError was received granting admin privileges", xmppError);
1178                assertEquals(
1179                    "A visitor was able to grant admin privileges to another visitor",
1180                    403,
1181                    xmppError.getCode());
1182            }
1183
1184            // Check that the room's owner can grant admin privileges to a visitor
1185
muc.grantAdmin(getBareJID(1));
1186            Thread.sleep(300);
1187
1188            // Check that UserStatusListener is working OK
1189
assertEquals(
1190                "User2 didn't receive the grant admin notification",
1191                "I'm an admin",
1192                answer[2]);
1193            // Check that ParticipantStatusListener is working OK
1194
assertEquals(
1195                "User3 didn't receive user2's grant admin notification",
1196                room + "/testbot2",
1197                answer[6]);
1198
1199            // Check that the room's owner can revoke admin privileges from an admin
1200
// and make the occupant a visitor
1201
muc.revokeMembership(getBareJID(1));
1202            Thread.sleep(300);
1203
1204            // Check that UserStatusListener is working OK
1205
assertEquals(
1206                "User2 didn't receive the revoke admin notification",
1207                "I'm not an admin",
1208                answer[3]);
1209            // Check that ParticipantStatusListener is working OK
1210
assertEquals(
1211                "User3 didn't receive user2's revoke admin notification",
1212                room + "/testbot2",
1213                answer[7]);
1214
1215            // Check that the room's owner can grant admin privileges to a member
1216
clearAnswer(answer);
1217            muc.grantMembership(getBareJID(1));
1218            Thread.sleep(300);
1219            muc.grantAdmin(getBareJID(1));
1220            Thread.sleep(300);
1221
1222            // Check that UserStatusListener is working OK
1223
assertEquals(
1224                "User2 didn't receive the revoke membership notification",
1225                "I'm not a member",
1226                answer[1]);
1227            assertEquals(
1228                "User2 didn't receive the grant admin notification",
1229                "I'm an admin",
1230                answer[2]);
1231            // Check that ParticipantStatusListener is working OK
1232
assertEquals(
1233                "User3 didn't receive user2's revoke membership notification",
1234                room + "/testbot2",
1235                answer[5]);
1236            assertEquals(
1237                "User3 didn't receive user2's grant admin notification",
1238                room + "/testbot2",
1239                answer[6]);
1240
1241            // Check that the room's owner can revoke admin privileges from an admin
1242
// and make the occupant a member
1243
clearAnswer(answer);
1244            muc.revokeAdmin(getBareJID(1));
1245            Thread.sleep(300);
1246
1247            // Check that UserStatusListener is working OK
1248
assertEquals(
1249                "User2 didn't receive the revoke admin notification",
1250                "I'm not an admin",
1251                answer[3]);
1252            assertEquals(
1253                "User2 didn't receive the grant membership notification",
1254                "I'm a member",
1255                answer[0]);
1256            // Check that ParticipantStatusListener is working OK
1257
assertEquals(
1258                "User3 didn't receive user2's revoke admin notification",
1259                room + "/testbot2",
1260                answer[7]);
1261            assertEquals(
1262                "User3 didn't receive user2's grant membership notification",
1263                room + "/testbot2",
1264                answer[4]);
1265
1266            // User2 leaves the room
1267
muc2.leave();
1268            // User3 leaves the room
1269
muc3.leave();
1270        }
1271        catch (Exception JavaDoc e) {
1272            e.printStackTrace();
1273            fail(e.getMessage());
1274        }
1275    }
1276
1277    public void testOwnership() {
1278        final String JavaDoc[] answer = new String JavaDoc[12];
1279        try {
1280
1281            makeRoomModerated();
1282
1283            // User2 joins the new room (as a visitor)
1284
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
1285            muc2.join("testbot2");
1286            // User2 will listen for ownership privileges
1287
muc2.addUserStatusListener(new DefaultUserStatusListener() {
1288                public void membershipGranted() {
1289                    super.membershipGranted();
1290                    answer[0] = "I'm a member";
1291                }
1292                public void membershipRevoked() {
1293                    super.membershipRevoked();
1294                    answer[1] = "I'm not a member";
1295                }
1296                public void adminGranted() {
1297                    super.adminGranted();
1298                    answer[2] = "I'm an admin";
1299                }
1300                public void adminRevoked() {
1301                    super.adminRevoked();
1302                    answer[3] = "I'm not an admin";
1303                }
1304                public void ownershipGranted() {
1305                    super.ownershipGranted();
1306                    answer[4] = "I'm an owner";
1307                }
1308                public void ownershipRevoked() {
1309                    super.ownershipRevoked();
1310                    answer[5] = "I'm not an owner";
1311                }
1312            });
1313
1314            // User3 joins the new room (as a visitor)
1315
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
1316            muc3.join("testbot3");
1317            // User3 will lister for user2's ownership privileges
1318
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
1319                public void membershipGranted(String JavaDoc participant) {
1320                    super.membershipGranted(participant);
1321                    answer[6] = participant;
1322                }
1323                public void membershipRevoked(String JavaDoc participant) {
1324                    super.membershipRevoked(participant);
1325                    answer[7] = participant;
1326                }
1327                public void adminGranted(String JavaDoc participant) {
1328                    super.adminGranted(participant);
1329                    answer[8] = participant;
1330                }
1331                public void adminRevoked(String JavaDoc participant) {
1332                    super.adminRevoked(participant);
1333                    answer[9] = participant;
1334                }
1335                public void ownershipGranted(String JavaDoc participant) {
1336                    super.ownershipGranted(participant);
1337                    answer[10] = participant;
1338                }
1339                public void ownershipRevoked(String JavaDoc participant) {
1340                    super.ownershipRevoked(participant);
1341                    answer[11] = participant;
1342                }
1343            });
1344
1345            try {
1346                // Check whether a visitor can grant ownership privileges to another visitor
1347
muc2.grantOwnership(getBareJID(2));
1348                fail("User2 was able to grant ownership privileges");
1349            }
1350            catch (XMPPException e) {
1351                XMPPError xmppError = e.getXMPPError();
1352                assertNotNull("No XMPPError was received granting ownership privileges", xmppError);
1353                assertEquals(
1354                    "A visitor was able to grant ownership privileges to another visitor",
1355                    403,
1356                    xmppError.getCode());
1357            }
1358
1359            // Check that the room's owner can grant ownership privileges to a visitor
1360
muc.grantOwnership(getBareJID(1));
1361            Thread.sleep(300);
1362
1363            // Check that UserStatusListener is working OK
1364
assertEquals(
1365                "User2 didn't receive the grant ownership notification",
1366                "I'm an owner",
1367                answer[4]);
1368            // Check that ParticipantStatusListener is working OK
1369
assertEquals(
1370                "User3 didn't receive user2's grant ownership notification",
1371                room + "/testbot2",
1372                answer[10]);
1373
1374            // Check that the room's owner can revoke ownership privileges from an owner
1375
// and make the occupant a visitor
1376
muc.revokeMembership(getBareJID(1));
1377            Thread.sleep(300);
1378
1379            // Check that UserStatusListener is working OK
1380
assertEquals(
1381                "User2 didn't receive the revoke ownership notification",
1382                "I'm not an owner",
1383                answer[5]);
1384            // Check that ParticipantStatusListener is working OK
1385
assertEquals(
1386                "User3 didn't receive user2's revoke ownership notification",
1387                room + "/testbot2",
1388                answer[11]);
1389
1390            // Check that the room's owner can grant ownership privileges to a member
1391
clearAnswer(answer);
1392            muc.grantMembership(getBareJID(1));
1393            Thread.sleep(300);
1394            muc.grantOwnership(getBareJID(1));
1395            Thread.sleep(300);
1396
1397            // Check that UserStatusListener is working OK
1398
assertEquals(
1399                "User2 didn't receive the revoke membership notification",
1400                "I'm not a member",
1401                answer[1]);
1402            assertEquals(
1403                "User2 didn't receive the grant ownership notification",
1404                "I'm an owner",
1405                answer[4]);
1406            // Check that ParticipantStatusListener is working OK
1407
assertEquals(
1408                "User3 didn't receive user2's revoke membership notification",
1409                room + "/testbot2",
1410                answer[7]);
1411            assertEquals(
1412                "User3 didn't receive user2's grant ownership notification",
1413                room + "/testbot2",
1414                answer[10]);
1415
1416            // Check that the room's owner can revoke ownership privileges from an owner
1417
// and make the occupant a member
1418
clearAnswer(answer);
1419            muc.revokeAdmin(getBareJID(1));
1420            Thread.sleep(300);
1421
1422            // Check that UserStatusListener is working OK
1423
assertEquals(
1424                "User2 didn't receive the revoke ownership notification",
1425                "I'm not an owner",
1426                answer[5]);
1427            assertEquals(
1428                "User2 didn't receive the grant membership notification",
1429                "I'm a member",
1430                answer[0]);
1431            // Check that ParticipantStatusListener is working OK
1432
assertEquals(
1433                "User3 didn't receive user2's revoke ownership notification",
1434                room + "/testbot2",
1435                answer[11]);
1436            assertEquals(
1437                "User3 didn't receive user2's grant membership notification",
1438                room + "/testbot2",
1439                answer[6]);
1440
1441            // Check that the room's owner can grant ownership privileges to an admin
1442
clearAnswer(answer);
1443            muc.grantAdmin(getBareJID(1));
1444            Thread.sleep(300);
1445            muc.grantOwnership(getBareJID(1));
1446            Thread.sleep(300);
1447
1448            // Check that UserStatusListener is working OK
1449
assertEquals(
1450                "User2 didn't receive the revoke admin notification",
1451                "I'm not an admin",
1452                answer[3]);
1453            assertEquals(
1454                "User2 didn't receive the grant ownership notification",
1455                "I'm an owner",
1456                answer[4]);
1457            // Check that ParticipantStatusListener is working OK
1458
assertEquals(
1459                "User3 didn't receive user2's revoke admin notification",
1460                room + "/testbot2",
1461                answer[9]);
1462            assertEquals(
1463                "User3 didn't receive user2's grant ownership notification",
1464                room + "/testbot2",
1465                answer[10]);
1466
1467            // Check that the room's owner can revoke ownership privileges from an owner
1468
// and make the occupant an admin
1469
clearAnswer(answer);
1470            muc.revokeOwnership(getBareJID(1));
1471            Thread.sleep(300);
1472
1473            // Check that UserStatusListener is working OK
1474
assertEquals(
1475                "User2 didn't receive the revoke ownership notification",
1476                "I'm not an owner",
1477                answer[5]);
1478            assertEquals(
1479                "User2 didn't receive the grant admin notification",
1480                "I'm an admin",
1481                answer[2]);
1482            // Check that ParticipantStatusListener is working OK
1483
assertEquals(
1484                "User3 didn't receive user2's revoke ownership notification",
1485                room + "/testbot2",
1486                answer[11]);
1487            assertEquals(
1488                "User3 didn't receive user2's grant admin notification",
1489                room + "/testbot2",
1490                answer[8]);
1491
1492            // User2 leaves the room
1493
muc2.leave();
1494            // User3 leaves the room
1495
muc3.leave();
1496        }
1497        catch (Exception JavaDoc e) {
1498            e.printStackTrace();
1499            fail(e.getMessage());
1500        }
1501    }
1502
1503    public void testGetAffiliationList() {
1504        try {
1505            // User2 joins the new room
1506
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
1507            muc2.join("testbot2");
1508
1509            // User3 joins the new room
1510
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
1511            muc3.join("testbot3");
1512
1513            // Grant ownership privileges to user2
1514
muc.grantOwnership(getBareJID(1));
1515            // Grant moderator privileges to user3
1516
muc.grantModerator("testbot3");
1517
1518            // Check that the owner list is correct
1519
Collection affiliates = muc.getOwners();
1520            assertEquals("Room does not have 2 owners", 2, affiliates.size());
1521            for (Iterator it =affiliates.iterator(); it.hasNext();) {
1522                Affiliate affiliate = (Affiliate)it.next();
1523                if (getBareJID(0).equals(affiliate.getJid())) {
1524                    assertEquals("Wrong affiliation", "owner", affiliate.getAffiliation());
1525                    assertEquals("Wrong role", "moderator", affiliate.getRole());
1526                    assertEquals("Wrong nick", "testbot", affiliate.getNick());
1527                }
1528                else if (getBareJID(1).equals(affiliate.getJid())) {
1529                    assertEquals("Wrong affiliation", "owner", affiliate.getAffiliation());
1530                    assertEquals("Wrong role", "moderator", affiliate.getRole());
1531                    assertEquals("Wrong nick", "testbot2", affiliate.getNick());
1532                }
1533                else {
1534                    fail("Unknown owner " + affiliate.getJid());
1535                }
1536            }
1537
1538            // Check that the admin list is correct
1539
affiliates = muc.getAdmins();
1540            assertEquals("Room has admins", 0, affiliates.size());
1541
1542            // Check that the members list is correct
1543
affiliates = muc.getMembers();
1544            assertEquals("Room has admins", 0, affiliates.size());
1545            // Grant membership privileges to user2
1546
muc.grantMembership(getBareJID(1));
1547            // Check that the members list is correct
1548
affiliates = muc.getMembers();
1549            assertEquals("Room has admins", 1, affiliates.size());
1550            Affiliate affiliate = (Affiliate) affiliates.iterator().next();
1551            assertEquals("Wrong member jid", getBareJID(1), affiliate.getJid());
1552
1553            // Check that the members list is correct
1554
affiliates = muc.getOutcasts();
1555            assertEquals("Room has outcasts", 0, affiliates.size());
1556
1557            // Check that the moderator list is correct
1558
Collection occupants = muc.getModerators();
1559            assertEquals("Room does not have 2 moderators", 2, occupants.size());
1560            for (Iterator it =occupants.iterator(); it.hasNext();) {
1561                Occupant occupant = (Occupant)it.next();
1562                if (getFullJID(0).equals(occupant.getJid())) {
1563                    assertEquals("Wrong affiliation", "owner", occupant.getAffiliation());
1564                    assertEquals("Wrong role", "moderator", occupant.getRole());
1565                    assertEquals("Wrong nick", "testbot", occupant.getNick());
1566                }
1567                else if (getFullJID(2).equals(occupant.getJid())) {
1568                    assertEquals("Wrong affiliation", "none", occupant.getAffiliation());
1569                    assertEquals("Wrong role", "moderator", occupant.getRole());
1570                    assertEquals("Wrong nick", "testbot3", occupant.getNick());
1571                }
1572                else {
1573                    fail("Unknown moderator " + occupant.getJid());
1574                }
1575            }
1576
1577            // Check that the participants list is correct
1578
occupants = muc.getParticipants();
1579            assertEquals("Room does not have 1 participant", 1, occupants.size());
1580            Occupant occupant = (Occupant) occupants.iterator().next();
1581            assertEquals("Wrong participant jid", getFullJID(1), occupant.getJid());
1582
1583            Thread.sleep(500);
1584            
1585            // Check that we can retrieve Occupant information of a given user
1586
occupant = muc.getOccupant(room + "/testbot2");
1587            assertNotNull("Occupant was not found", occupant);
1588            assertEquals("Wrong occupant jid", getFullJID(1), occupant.getJid());
1589            assertEquals("Wrong occupant affiliation", "member", occupant.getAffiliation());
1590            assertEquals("Wrong occupant role", "participant", occupant.getRole());
1591            assertEquals("Wrong occupant nick", "testbot2", occupant.getNick());
1592
1593            try {
1594                // Check whether a member can get the list of owners
1595
muc2.getOwners();
1596                fail("User2 was able to get the list of owners");
1597            }
1598            catch (XMPPException e) {
1599                XMPPError xmppError = e.getXMPPError();
1600                assertNotNull("No XMPPError was received getting the list of owners", xmppError);
1601                assertEquals(
1602                    "A member was able to get the list of owners",
1603                    403,
1604                    xmppError.getCode());
1605            }
1606        }
1607        catch (Exception JavaDoc e) {
1608            e.printStackTrace();
1609            fail(e.getMessage());
1610        }
1611    }
1612
1613    /**
1614     * Check that ParticipantStatusListener is receiving joining and leaving events correctly.
1615     */

1616    public void testJoinLeftEvents() {
1617        final String JavaDoc[] answer = new String JavaDoc[8];
1618        try {
1619            // User1 will listen for occupants joining and leaving the room
1620
muc.addParticipantStatusListener(new DefaultParticipantStatusListener() {
1621                public void joined(String JavaDoc participant) {
1622                    super.joined(participant);
1623                    if ((room + "/testbot2").equals(participant)) {
1624                        answer[0] = participant;
1625                    }
1626                    else {
1627                        answer[1] = participant;
1628                    }
1629                }
1630                public void left(String JavaDoc participant) {
1631                    super.left(participant);
1632                    if ((room + "/testbot2").equals(participant)) {
1633                        answer[2] = participant;
1634                    }
1635                    // Skip unavailable presences of the same user
1636
else if (!(room + "/testbot").equals(participant)) {
1637                        answer[3] = participant;
1638                    }
1639                }
1640            });
1641
1642            // User2 joins the new room
1643
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
1644            // User2 will listen for User3 joining and leaving the room
1645
muc2.addParticipantStatusListener(new DefaultParticipantStatusListener() {
1646                public void joined(String JavaDoc participant) {
1647                    super.joined(participant);
1648                    if ((room + "/testbot").equals(participant)) {
1649                        answer[4] = participant;
1650                    }
1651                    else {
1652                        answer[5] = participant;
1653                    }
1654                }
1655                public void left(String JavaDoc participant) {
1656                    super.left(participant);
1657                    if ((room + "/testbot").equals(participant)) {
1658                        answer[6] = participant;
1659                    }
1660                    // Skip unavailable presences of the same user
1661
else if (!(room + "/testbot2").equals(participant)){
1662                        answer[7] = participant;
1663                    }
1664                }
1665            });
1666            muc2.join("testbot2");
1667
1668            // User3 joins the new room
1669
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
1670            muc3.join("testbot3");
1671
1672            Thread.sleep(150);
1673
1674            // User3 leaves the room
1675
muc3.leave();
1676            
1677            Thread.sleep(150);
1678            // User2 leaves the room
1679
muc2.leave();
1680
1681            Thread.sleep(250);
1682            // Check that ParticipantStatusListener is working OK
1683
assertEquals(
1684                "User1 didn't receive the event of User2 joining the room",
1685                room + "/testbot2",
1686                answer[0]);
1687            assertEquals(
1688                "User1 didn't receive the event of User3 joining the room",
1689                room + "/testbot3",
1690                answer[1]);
1691            assertEquals(
1692                "User1 didn't receive the event of User2 leaving the room",
1693                room + "/testbot2",
1694                answer[2]);
1695            assertEquals(
1696                "User1 didn't receive the event of User3 leaving the room",
1697                room + "/testbot3",
1698                answer[3]);
1699            assertEquals(
1700                "User2 didn't receive the event of User1 joining the room",
1701                room + "/testbot",
1702                answer[4]);
1703            assertEquals(
1704                "User2 didn't receive the event of User3 joining the room",
1705                room + "/testbot3",
1706                answer[5]);
1707            assertNull(
1708                "User2 received the event of User1 leaving the room",
1709                answer[6]);
1710            assertEquals(
1711                "User2 didn't receive the event of User3 leaving the room",
1712                room + "/testbot3",
1713                answer[7]);
1714        }
1715        catch (Exception JavaDoc e) {
1716            e.printStackTrace();
1717            fail(e.getMessage());
1718        }
1719    }
1720
1721    public void testManyResources() {
1722        try {
1723            // Create 20 more connections for user2
1724
XMPPConnection[] conns = new XMPPConnection[20];
1725            for (int i = 0; i < conns.length; i++) {
1726                conns[i] = new XMPPConnection(getServiceName());
1727                conns[i].login(getUsername(1), getUsername(1), "resource-" + i);
1728            }
1729
1730            // Join the 20 connections to the same room
1731
MultiUserChat[] mucs = new MultiUserChat[20];
1732            for (int i = 0; i < mucs.length; i++) {
1733                mucs[i] = new MultiUserChat(conns[i], room);
1734                mucs[i].join("resource-" + i);
1735            }
1736
1737            Thread.sleep(200);
1738
1739            // Each connection has something to say
1740
for (int i = 0; i < mucs.length; i++) {
1741                mucs[i].sendMessage("I'm resource-" + i);
1742            }
1743
1744            Thread.sleep(200);
1745
1746            // Each connection leaves the room and closes the connection
1747
for (int i = 0; i < mucs.length; i++) {
1748                mucs[i].leave();
1749                conns[i].close();
1750            }
1751
1752        } catch (Exception JavaDoc e) {
1753            e.printStackTrace();
1754            fail(e.getMessage());
1755        }
1756
1757    }
1758
1759    private void makeRoomModerated() throws XMPPException {
1760        // User1 (which is the room owner) converts the instant room into a moderated room
1761
Form form = muc.getConfigurationForm();
1762        Form answerForm = form.createAnswerForm();
1763        answerForm.setAnswer("muc#roomconfig_moderatedroom", true);
1764        // Keep the room owner
1765
try {
1766            List owners = new ArrayList();
1767            owners.add(getBareJID(0));
1768            answerForm.setAnswer("muc#roomconfig_roomowners", owners);
1769        }
1770        catch (IllegalArgumentException JavaDoc e) {
1771            // Do nothing
1772
}
1773        muc.sendConfigurationForm(answerForm);
1774    }
1775
1776    private void clearAnswer(String JavaDoc[] answer) {
1777        for (int i = 0; i < answer.length; i++) {
1778            answer[i] = null;
1779        }
1780    }
1781
1782    protected void setUp() throws Exception JavaDoc {
1783        XMPPConnection.DEBUG_ENABLED = false;
1784        super.setUp();
1785        room = "fruta124@" + getMUCDomain();
1786        try {
1787            // User1 creates the room
1788
muc = new MultiUserChat(getConnection(0), room);
1789            muc.create("testbot");
1790
1791            // User1 sends an empty room configuration form which indicates that we want
1792
// an instant room
1793
muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
1794        }
1795        catch (Exception JavaDoc e) {
1796            fail(e.getMessage());
1797        }
1798    }
1799
1800    protected void tearDown() throws Exception JavaDoc {
1801        // Destroy the new room
1802
muc.destroy("The room has almost no activity...", null);
1803
1804        super.tearDown();
1805    }
1806
1807    protected int getMaxConnections() {
1808        return 3;
1809    }
1810}
1811
Popular Tags