/
MultiUserChatTest.java
1904 lines (1702 loc) · 75.5 KB
/
MultiUserChatTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
* ====================================================================
* The Jive Software License (based on Apache Software License, Version 1.1)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Jive Software (http://www.jivesoftware.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Smack" and "Jive Software" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@jivesoftware.com.
*
* 5. Products derived from this software may not be called "Smack",
* nor may "Smack" appear in their name, without prior written
* permission of Jive Software.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.jivesoftware.smackx.muc;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.DelayInformation;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.XHTMLExtension;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Tests the new MUC functionalities.
*
* @author Gaston Dombiak
*/
public class MultiUserChatTest extends SmackTestCase {
private String room;
private MultiUserChat muc;
public MultiUserChatTest(String arg0) {
super(arg0);
}
/**
* Test the compatibility of the MUC service with clients that still use the old groupchat
* protocol.
*/
public void testGroupchatCompatibility() {
// TODO: this test needs to be re-written so that it manually emulates the old-style
// TODO: group chat protocol. Tne GroupChat class was deleted for Smack 3.0.
/*try {
Message message;
GroupChat groupchat = new GroupChat(getConnection(1), room);
groupchat.join("testbot2");
Thread.sleep(400);
// User1 checks the presence of user2 in the room
Presence presence = muc.getOccupantPresence(room + "/testbot2");
assertNotNull("Presence of user2 in room is missing", presence);
assertEquals(
"Presence mode of user2 is wrong",
Presence.Mode.available,
presence.getMode());
// User using old client send a message
groupchat.sendMessage("Hello");
// Check that the rest of the occupants (that are support MUC) received the message
message = muc.nextMessage(1000);
assertNotNull("A MUC client didn't receive the message from an old client", message);
// User that supports MUC send a message
muc.sendMessage("Bye");
// Check that the client the doesn't support MUC received the message
message = groupchat.nextMessage(1000);
assertNotNull("An old client didn't receive the message from a MUC client", message);
// User that doesn't support MUC leaves the room
groupchat.leave();
Thread.sleep(300);
// User1 checks the that user2 is not present in the room
Occupant occupant = muc.getOccupant(room + "/testbot2");
assertNull("Occupant testbot2 still exists", occupant);
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}*/
}
public void testDiscussionHistory() {
try {
// User1 sends some messages to the room
muc.sendMessage("Message 1");
muc.sendMessage("Message 2");
// Wait 5 seconds before sending the last message
Thread.sleep(5000);
muc.sendMessage("Message 3");
// User2 joins the room requesting to receive the messages of the last 2 seconds.
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
DiscussionHistory history = new DiscussionHistory();
history.setSeconds(2);
muc2.join("testbot2", null, history, SmackConfiguration.getPacketReplyTimeout());
Message msg;
// Get first historic message
msg = muc2.nextMessage(1000);
assertNotNull("First message is null", msg);
DelayInformation delay = (DelayInformation) msg.getExtension("x", "jabber:x:delay");
assertNotNull("Message contains no delay information", delay);
SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
UTC_FORMAT.setTimeZone(TimeZone.getDefault());
System.out.println(UTC_FORMAT.format(delay.getStamp()));
assertEquals("Body of first message is incorrect", "Message 3", msg.getBody());
// Try to get second historic message
msg = muc2.nextMessage(1000);
assertNull("Second message is not null", msg);
// User3 joins the room requesting to receive the last 2 messages.
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
history = new DiscussionHistory();
history.setMaxStanzas(2);
muc3.join("testbot3", null, history, SmackConfiguration.getPacketReplyTimeout());
// Get first historic message
msg = muc3.nextMessage(1000);
assertNotNull("First message is null", msg);
assertEquals("Body of first message is incorrect", "Message 2", msg.getBody());
// Get second historic message
msg = muc3.nextMessage(1000);
assertNotNull("Second message is null", msg);
assertEquals("Body of second message is incorrect", "Message 3", msg.getBody());
// Try to get third historic message
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) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testParticipantPresence() {
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
Thread.sleep(400);
// User1 checks the presence of user2 in the room
Presence presence = muc.getOccupantPresence(room + "/testbot2");
assertNotNull("Presence of user2 in room is missing", presence);
assertTrue(
"Presence mode of user2 is wrong",
presence.getMode() == null || presence.getMode() == Presence.Mode.available);
// 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.getOccupantPresence(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());
// User2 changes his nickname
muc2.changeNickname("testbotII");
Thread.sleep(200);
// User1 checks the presence of user2 in the room
presence = muc.getOccupantPresence(room + "/testbot2");
assertNull("Presence of participant testbot2 still exists", presence);
presence = muc.getOccupantPresence(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(250);
// User1 checks the presence of user2 in the room
presence = muc.getOccupantPresence(room + "/testbotII");
assertNull("Presence of participant testbotII still exists", presence);
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testAnonymousParticipant() {
try {
// Anonymous user joins the new room
ConnectionConfiguration connectionConfiguration =
new ConnectionConfiguration(getHost(), getPort(), getServiceName());
XMPPConnection anonConnection = new XMPPConnection(connectionConfiguration);
anonConnection.connect();
anonConnection.loginAnonymously();
MultiUserChat muc2 = new MultiUserChat(anonConnection, room);
muc2.join("testbot2");
Thread.sleep(400);
// User1 checks the presence of Anonymous user in the room
Presence presence = muc.getOccupantPresence(room + "/testbot2");
assertNotNull("Presence of user2 in room is missing", presence);
assertTrue(
"Presence mode of user2 is wrong",
presence.getMode() == null || presence.getMode() == Presence.Mode.available);
// Anonymous user leaves the room
muc2.leave();
anonConnection.disconnect();
Thread.sleep(250);
// User1 checks the presence of Anonymous user in the room
presence = muc.getOccupantPresence(room + "/testbot2");
assertNull("Presence of participant testbotII still exists", presence);
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testInvitation() {
final String[] answer = new String[2];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User3 is listening to MUC invitations
MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {
public void invitationReceived(
Connection conn,
String room,
String inviter,
String reason,
String password,
Message message) {
// 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(getFullJID(2), "Meet me in this excellent room");
Thread.sleep(350);
assertEquals(
"Invitation was not received",
"Meet me in this excellent room",
answer[0]);
assertEquals("Rejection was not received", "I'm busy right now", answer[1]);
// User2 leaves the room
muc2.leave();
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testInvitationWithMessage() {
final String[] answer = new String[2];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User3 is listening to MUC invitations
MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() {
public void invitationReceived(
Connection conn,
String room,
String inviter,
String reason,
String password,
Message message) {
// Indicate that the invitation was received
answer[0] = reason;
XHTMLExtension extension = (XHTMLExtension) message.getExtension("html",
"http://jabber.org/protocol/xhtml-im");
assertNotNull("An extension was not found in the invitation", extension);
answer[1] = (String) extension.getBodies().next();
}
});
// User2 invites user3 to join to the room
Message msg = new Message();
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody("<body>Meet me in this excellent room</body>");
msg.addExtension(xhtmlExtension);
muc2.invite(msg , getFullJID(2), "Meet me in this excellent room");
Thread.sleep(350);
assertEquals(
"Invitation was not received",
"Meet me in this excellent room",
answer[0]);
assertEquals("Rejection was not received",
"<body>Meet me in this excellent room</body>", answer[1]);
// User2 leaves the room
muc2.leave();
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testDiscoverJoinedRooms() {
try {
// Check that user1 has joined only to one room
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);
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(getConnection(1), getFullJID(0));
assertFalse("Joined rooms should be empty", joinedRooms.hasNext());
muc.join("testbot");
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testDiscoverMUCSupport() {
// Discover user1 support of MUC
boolean supports = MultiUserChat.isServiceEnabled(getConnection(1), getFullJID(0));
assertTrue("Couldn't detect that user1 supports MUC", supports);
}
public void testDiscoverRoomInfo() {
try {
makeRoomModerated();
RoomInfo info = MultiUserChat.getRoomInfo(getConnection(1), room);
assertFalse("Room is members-only", info.isMembersOnly());
assertTrue("Room is moderated", info.isModerated());
assertFalse("Room is Nonanonymous", info.isNonanonymous());
assertFalse("Room is PasswordProtected", info.isPasswordProtected());
assertFalse("Room is Persistent", info.isPersistent());
assertEquals("Room's description is incorrect", "fruta124", info.getDescription());
assertEquals("Room's subject is incorrect", "", info.getSubject());
assertEquals("Number of occupants is incorrect", 1, info.getOccupantsCount());
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testDiscoverMUCService() {
try {
Collection services = MultiUserChat.getServiceNames(getConnection(1));
assertFalse("No MUC service was found", services.isEmpty());
// Discover the hosted rooms by the chat service.
Collection rooms = MultiUserChat.getHostedRooms(getConnection(1),
(String) services.toArray()[0]);
// Check that we have discovered the room used by this test
assertFalse("No room was found", rooms.isEmpty());
// Check that we have discovered the room used by this test
boolean found = false;
for (Object room1 : rooms) {
HostedRoom hostedRoom = (HostedRoom) room1;
if (room.equals(hostedRoom.getJid())) {
found = true;
break;
}
}
assertTrue("JID of room was not found", found);
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testPrivateChat() {
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
getConnection(0).getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat2, boolean createdLocally) {
assertEquals(
"Sender of chat is incorrect",
room + "/testbot2",
chat2.getParticipant());
try {
chat2.sendMessage("ACK");
}
catch (XMPPException e) {
fail(e.getMessage());
}
}
});
// Start a private chat with another participant
Chat chat = muc2.createPrivateChat(room + "/testbot", null);
PacketCollector collector = chat.createCollector();
chat.sendMessage("Hello there");
Message response = (Message) collector.nextResult(2000);
assertNotNull("No response", response);
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) {
e.printStackTrace();
fail(e.getMessage());
}
}
/**
* Tests that IQ packets can be sent to/from room occupants. This case will try to discover
* information about other room occupants.
*/
public void testPrivateIQ() {
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 discovers information about User1
DiscoverInfo info = ServiceDiscoveryManager.getInstanceFor(getConnection(1))
.discoverInfo(room + "/testbot", null);
assertNotNull("No info was discovered from room occupant", info);
assertEquals("Wrong IQ type", IQ.Type.RESULT, info.getType());
assertEquals("Wrong IQ sender", room + "/testbot", info.getFrom());
// User2 leaves the room
muc2.leave();
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testReservedNickname() {
try {
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
// Check that user2 doesn't have a reserved nickname yet
String reservedNickname = muc2.getReservedNickname();
assertNull("Reserved nickname is not null", reservedNickname);
// User2 registers with the room and reserves a nickname
Form registrationForm = muc2.getRegistrationForm();
Form answerForm = registrationForm.createAnswerForm();
answerForm.setAnswer("muc#register_first", "MyFirstName");
answerForm.setAnswer("muc#register_last", "MyLastName");
answerForm.setAnswer("muc#register_roomnick", "MyNick");
muc2.sendRegistrationForm(answerForm);
// Check that user2 has a reserved nickname
reservedNickname = muc2.getReservedNickname();
assertEquals("Reserved nickname is wrong", "MyNick", reservedNickname);
// Check that user2 can join the room using his reserved nickname
muc2.join("MyNick");
muc2.leave();
// Check that other users cannot join the room with user2's reserved nickname
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
try {
muc3.join("MyNick");
fail("Other user was able to join with other user's reserved nickname");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received when joining with other user's reserved nickname",
xmppError);
assertEquals(
"Different error code was received while joining with other user's reserved nickname",
409,
xmppError.getCode());
}
// Check that user3 can join the room using his own nickname (not reserved)
muc3.join("MyNotReservedNick");
muc3.leave();
// Check that another user cannot reserve an already reserved nickname
registrationForm = muc3.getRegistrationForm();
answerForm = registrationForm.createAnswerForm();
answerForm.setAnswer("muc#register_first", "MyFirstName 2");
answerForm.setAnswer("muc#register_last", "MyLastName 2");
answerForm.setAnswer("muc#register_roomnick", "MyNick");
try {
muc3.sendRegistrationForm(answerForm);
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received when reserving an already reserved nickname",
xmppError);
assertEquals(
"Different error code was received while reserving an already reserved nickname",
409,
xmppError.getCode());
}
// Check that another user can reserve a new nickname
registrationForm = muc3.getRegistrationForm();
answerForm = registrationForm.createAnswerForm();
answerForm.setAnswer("muc#register_first", "MyFirstName 2");
answerForm.setAnswer("muc#register_last", "MyLastName 2");
answerForm.setAnswer("muc#register_roomnick", "MyNick 2");
muc3.sendRegistrationForm(answerForm);
}
catch (XMPPException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testChangeSubject() {
final String[] answer = new String[2];
try {
// User1 sets an initial subject
muc.changeSubject("Initial Subject");
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 wants to be notified every time the room's subject is changed.
muc3.addSubjectUpdatedListener(new SubjectUpdatedListener() {
public void subjectUpdated(String subject, String from) {
answer[0] = subject;
answer[1] = from;
}
});
// Check that a 403 error is received when a not allowed user tries to change the
// subject in a room
try {
muc2.changeSubject("New Subject2");
fail("User2 was allowed to change the room's subject");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
assertNotNull(
"No XMPPError was received when changing the room's subject",
xmppError);
assertEquals(
"Different error code was received while changing the room's subject",
403,
xmppError.getCode());
}
// Check that every MUC updates its subject when an allowed user changes the subject
// in a room
muc.changeSubject("New Subject1");
Thread.sleep(300);
// Check that User2's MUC has updated its subject
assertEquals(
"User2 didn't receive the subject notification",
"New Subject1",
muc2.getSubject());
// Check that SubjectUpdatedListener is working OK
assertEquals(
"User3 didn't receive the subject notification",
"New Subject1",
answer[0]);
assertEquals(
"User3 didn't receive the correct user that changed the subject",
room + "/testbot",
answer[1]);
// User2 leaves the room
muc2.leave();
// User3 leaves the room
muc3.leave();
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testKickParticipant() {
final String[] answer = new String[3];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will lister for his own "kicking"
muc2.addUserStatusListener(new DefaultUserStatusListener() {
public void kicked(String actor, String reason) {
super.kicked(actor, reason);
answer[0] = actor;
answer[1] = reason;
}
});
// User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's "kicking"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
public void kicked(String participant, String actor, String reason) {
super.kicked(participant, actor, reason);
answer[2] = participant;
}
});
try {
// Check whether a simple participant can kick a room owner or not
muc2.kickParticipant("testbot", "Because I'm bad");
fail("User2 was able to kick a room owner");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
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.getOccupant(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",
getBareJID(0),
answer[0]);
assertEquals(
"User2 didn't receive the correct reason for the kick",
"Because I'm the owner",
answer[1]);
// Check that ParticipantStatusListener is working OK
assertEquals(
"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) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testBanUser() {
final String[] answer = new String[3];
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will lister for his own "banning"
muc2.addUserStatusListener(new DefaultUserStatusListener() {
public void banned(String actor, String reason) {
super.banned(actor, reason);
answer[0] = actor;
answer[1] = reason;
}
});
// User3 joins the new room
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's "banning"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
public void banned(String participant, String actor, String reason) {
super.banned(participant, actor, reason);
answer[2] = participant;
}
});
try {
// Check whether a simple participant can ban a room owner or not
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);
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(getBareJID(1), "Because I'm the owner");
Thread.sleep(300);
assertNull(
"User2 wasn't banned from the room",
muc.getOccupant(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",
getBareJID(0),
answer[0]);
assertEquals(
"User2 didn't receive the correct reason for the banning",
"Because I'm the owner",
answer[1]);
// Check that ParticipantStatusListener is working OK
assertEquals(
"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) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testVoice() {
final String[] answer = new String[4];
try {
makeRoomModerated();
// User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will listen for his own "voice"
muc2.addUserStatusListener(new DefaultUserStatusListener() {
public void voiceGranted() {
super.voiceGranted();
answer[0] = "canSpeak";
}
public void voiceRevoked() {
super.voiceRevoked();
answer[1] = "cannot speak";
}
});
// User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's "voice"
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
public void voiceGranted(String participant) {
super.voiceGranted(participant);
answer[2] = participant;
}
public void voiceRevoked(String participant) {
super.voiceRevoked(participant);
answer[3] = participant;
}
});
try {
// Check whether a visitor can grant voice to another visitor
muc2.grantVoice("testbot3");
fail("User2 was able to grant voice");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
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);
// Check that UserStatusListener is working OK
assertEquals(
"User2 didn't receive the grant voice notification",
"canSpeak",
answer[0]);
// Check that ParticipantStatusListener is working OK
assertEquals(
"User3 didn't receive user2's grant voice notification",
room + "/testbot2",
answer[2]);
// Check that the room's owner can revoke voice from a participant
muc.revokeVoice("testbot2");
Thread.sleep(300);
assertEquals(
"User2 didn't receive the revoke voice notification",
"cannot speak",
answer[1]);
assertEquals(
"User3 didn't receive user2's revoke voice notification",
room + "/testbot2",
answer[3]);
// User2 leaves the room
muc2.leave();
// User3 leaves the room
muc3.leave();
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
public void testModerator() {
final String[] answer = new String[8];
try {
makeRoomModerated();
// User2 joins the new room (as a visitor)
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
// User2 will listen for moderator privileges
muc2.addUserStatusListener(new DefaultUserStatusListener() {
public void voiceGranted() {
super.voiceGranted();
answer[0] = "canSpeak";
}
public void voiceRevoked() {
super.voiceRevoked();
answer[1] = "cannot speak";
}
public void moderatorGranted() {
super.moderatorGranted();
answer[4] = "I'm a moderator";
}
public void moderatorRevoked() {
super.moderatorRevoked();
answer[5] = "I'm not a moderator";
}
});
// User3 joins the new room (as a visitor)
MultiUserChat muc3 = new MultiUserChat(getConnection(2), room);
muc3.join("testbot3");
// User3 will lister for user2's moderator privileges
muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() {
public void voiceGranted(String participant) {
super.voiceGranted(participant);
answer[2] = participant;
}
public void voiceRevoked(String participant) {
super.voiceRevoked(participant);
answer[3] = participant;
}
public void moderatorGranted(String participant) {
super.moderatorGranted(participant);
answer[6] = participant;
}
public void moderatorRevoked(String participant) {
super.moderatorRevoked(participant);
answer[7] = participant;
}
});
try {
// Check whether a visitor can grant moderator privileges to another visitor
muc2.grantModerator("testbot3");
fail("User2 was able to grant moderator privileges");
}
catch (XMPPException e) {
XMPPError xmppError = e.getXMPPError();
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);
// Check that UserStatusListener is working OK
assertEquals(
"User2 didn't receive the grant voice notification",
"canSpeak",
answer[0]);
assertEquals(
"User2 didn't receive the grant moderator privileges notification",
"I'm a moderator",
answer[4]);
// Check that ParticipantStatusListener is working OK
assertEquals(
"User3 didn't receive user2's grant voice notification",
room + "/testbot2",
answer[2]);
assertEquals(
"User3 didn't receive user2's grant moderator privileges notification",
room + "/testbot2",