1 52 53 package org.jivesoftware.smackx.muc; 54 55 import java.util.*; 56 import java.text.SimpleDateFormat ; 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 71 public class MultiUserChatTest extends SmackTestCase { 72 73 private String room; 74 75 private MultiUserChat muc; 76 77 81 public MultiUserChatTest(String arg0) { 82 super(arg0); 83 } 84 85 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 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 groupchat.sendMessage("Hello"); 107 message = muc.nextMessage(1000); 109 assertNotNull("A MUC client didn't receive the message from an old client", message); 110 muc.sendMessage("Bye"); 112 message = groupchat.nextMessage(1000); 114 assertNotNull("An old client didn't receive the message from a MUC client", message); 115 groupchat.leave(); 117 Thread.sleep(300); 118 Occupant occupant = muc.getOccupant(room + "/testbot2"); 120 assertNull("Occupant testbot2 still exists", occupant); 121 } 122 catch (Exception e) { 123 e.printStackTrace(); 124 fail(e.getMessage()); 125 } 126 } 127 128 public void testDiscussionHistory() { 129 try { 130 muc.sendMessage("Message 1"); 132 muc.sendMessage("Message 2"); 133 Thread.sleep(5000); 135 muc.sendMessage("Message 3"); 136 137 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 msg = muc2.nextMessage(1000); 146 DelayInformation delay = (DelayInformation) msg.getExtension("x", "jabber:x:delay"); 147 SimpleDateFormat UTC_FORMAT = new SimpleDateFormat ("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 msg = muc2.nextMessage(1000); 155 assertNull("Second message is not null", msg); 156 157 158 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 msg = muc3.nextMessage(1000); 166 assertNotNull("First message is null", msg); 167 assertEquals("Body of first message is incorrect", "Message 2", msg.getBody()); 168 msg = muc3.nextMessage(1000); 170 assertNotNull("Second message is null", msg); 171 assertEquals("Body of second message is incorrect", "Message 3", msg.getBody()); 172 msg = muc3.nextMessage(1000); 174 assertNull("Third message is not null", msg); 175 176 muc2.leave(); 178 muc3.leave(); 180 181 } 182 catch (Exception e) { 183 e.printStackTrace(); 184 fail(e.getMessage()); 185 } 186 } 187 188 public void testParticipantPresence() { 189 try { 190 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 192 muc2.join("testbot2"); 193 Thread.sleep(400); 194 195 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 muc2.changeAvailabilityStatus("Gone to have lunch", Presence.Mode.AWAY); 205 Thread.sleep(200); 206 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 muc2.changeNickname("testbotII"); 217 Thread.sleep(200); 218 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 muc2.leave(); 230 Thread.sleep(250); 231 presence = muc.getOccupantPresence(room + "/testbotII"); 233 assertNull("Presence of participant testbotII still exists", presence); 234 235 } 236 catch (Exception e) { 237 e.printStackTrace(); 238 fail(e.getMessage()); 239 } 240 } 241 242 public void testAnonymousParticipant() { 243 try { 244 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 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 muc2.leave(); 261 anonConnection.close(); 262 Thread.sleep(250); 263 presence = muc.getOccupantPresence(room + "/testbot2"); 265 assertNull("Presence of participant testbotII still exists", presence); 266 267 } 268 catch (Exception e) { 269 e.printStackTrace(); 270 fail(e.getMessage()); 271 } 272 } 273 274 public void testInvitation() { 275 final String [] answer = new String [2]; 276 try { 277 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 279 muc2.join("testbot2"); 280 281 MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() { 283 public void invitationReceived( 284 XMPPConnection conn, 285 String room, 286 String inviter, 287 String reason, 288 String password, 289 Message message) { 290 answer[0] = reason; 292 MultiUserChat.decline(conn, room, inviter, "I'm busy right now"); 294 } 295 }); 296 297 muc2.addInvitationRejectionListener(new InvitationRejectionListener() { 299 public void invitationDeclined(String invitee, String reason) { 300 answer[1] = reason; 302 } 303 }); 304 305 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 muc2.leave(); 317 } 318 catch (Exception e) { 319 e.printStackTrace(); 320 fail(e.getMessage()); 321 } 322 } 323 324 public void testInvitationWithMessage() { 325 final String [] answer = new String [2]; 326 try { 327 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 329 muc2.join("testbot2"); 330 331 MultiUserChat.addInvitationListener(getConnection(2), new InvitationListener() { 333 public void invitationReceived( 334 XMPPConnection conn, 335 String room, 336 String inviter, 337 String reason, 338 String password, 339 Message message) { 340 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 ) extension.getBodies().next(); 346 } 347 }); 348 349 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 muc2.leave(); 366 } 367 catch (Exception e) { 368 e.printStackTrace(); 369 fail(e.getMessage()); 370 } 371 } 372 373 public void testDiscoverJoinedRooms() { 374 try { 375 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 muc.leave(); 383 384 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 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 Collection rooms = MultiUserChat.getHostedRooms(getConnection(1), 430 (String ) services.toArray()[0]); 431 assertFalse("No room was found", rooms.isEmpty()); 433 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 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 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 muc2.leave(); 478 } 479 catch (Exception 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 String reservedNickname = muc2.getReservedNickname(); 490 assertNull("Reserved nickname is not null", reservedNickname); 491 492 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 reservedNickname = muc2.getReservedNickname(); 502 assertEquals("Reserved nickname is wrong", "MyNick", reservedNickname); 503 504 muc2.join("MyNick"); 506 muc2.leave(); 507 508 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 muc3.join("MyNotReservedNick"); 527 muc3.leave(); 528 529 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 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 [] answer = new String [2]; 566 try { 567 muc.changeSubject("Initial Subject"); 569 570 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 572 muc2.join("testbot2"); 573 574 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 576 muc3.join("testbot3"); 577 578 muc3.addSubjectUpdatedListener(new SubjectUpdatedListener() { 580 public void subjectUpdated(String subject, String from) { 581 answer[0] = subject; 582 answer[1] = from; 583 } 584 }); 585 586 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 muc.changeSubject("New Subject1"); 606 Thread.sleep(300); 607 assertEquals( 609 "User2 didn't receive the subject notification", 610 "New Subject1", 611 muc2.getSubject()); 612 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 muc2.leave(); 624 muc3.leave(); 626 } 627 catch (Exception e) { 628 e.printStackTrace(); 629 fail(e.getMessage()); 630 } 631 } 632 633 public void testKickParticipant() { 634 final String [] answer = new String [3]; 635 try { 636 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 638 muc2.join("testbot2"); 639 muc2.addUserStatusListener(new DefaultUserStatusListener() { 641 public void kicked(String actor, String reason) { 642 super.kicked(actor, reason); 643 answer[0] = actor; 644 answer[1] = reason; 645 } 646 }); 647 648 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 650 muc3.join("testbot3"); 651 muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { 653 public void kicked(String participant) { 654 super.kicked(participant); 655 answer[2] = participant; 656 } 657 }); 658 659 try { 660 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 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 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 assertEquals( 695 "User3 didn't receive the correct kicked participant", 696 room + "/testbot2", 697 answer[2]); 698 699 muc2.leave(); 701 muc3.leave(); 703 } 704 catch (Exception e) { 705 e.printStackTrace(); 706 fail(e.getMessage()); 707 } 708 } 709 710 public void testBanUser() { 711 final String [] answer = new String [3]; 712 try { 713 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 715 muc2.join("testbot2"); 716 muc2.addUserStatusListener(new DefaultUserStatusListener() { 718 public void banned(String actor, String reason) { 719 super.banned(actor, reason); 720 answer[0] = actor; 721 answer[1] = reason; 722 } 723 }); 724 725 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 727 muc3.join("testbot3"); 728 muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { 730 public void banned(String participant) { 731 super.banned(participant); 732 answer[2] = participant; 733 } 734 }); 735 736 try { 737 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 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 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 assertEquals( 772 "User3 didn't receive the correct banned JID", 773 room + "/testbot2", 774 answer[2]); 775 776 muc2.leave(); 778 muc3.leave(); 780 } 781 catch (Exception e) { 782 e.printStackTrace(); 783 fail(e.getMessage()); 784 } 785 } 786 787 public void testVoice() { 788 final String [] answer = new String [4]; 789 try { 790 791 makeRoomModerated(); 792 793 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 795 muc2.join("testbot2"); 796 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 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 810 muc3.join("testbot3"); 811 muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { 813 public void voiceGranted(String participant) { 814 super.voiceGranted(participant); 815 answer[2] = participant; 816 } 817 818 public void voiceRevoked(String participant) { 819 super.voiceRevoked(participant); 820 answer[3] = participant; 821 } 822 }); 823 824 try { 825 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 muc.grantVoice("testbot2"); 840 Thread.sleep(300); 841 842 assertEquals( 844 "User2 didn't receive the grant voice notification", 845 "canSpeak", 846 answer[0]); 847 assertEquals( 849 "User3 didn't receive user2's grant voice notification", 850 room + "/testbot2", 851 answer[2]); 852 853 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 muc2.leave(); 868 muc3.leave(); 870 } 871 catch (Exception e) { 872 e.printStackTrace(); 873 fail(e.getMessage()); 874 } 875 } 876 877 public void testModerator() { 878 final String [] answer = new String [8]; 879 try { 880 881 makeRoomModerated(); 882 883 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 885 muc2.join("testbot2"); 886 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 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 908 muc3.join("testbot3"); 909 muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { 911 public void voiceGranted(String participant) { 912 super.voiceGranted(participant); 913 answer[2] = participant; 914 } 915 public void voiceRevoked(String participant) { 916 super.voiceRevoked(participant); 917 answer[3] = participant; 918 } 919 public void moderatorGranted(String participant) { 920 super.moderatorGranted(participant); 921 answer[6] = participant; 922 } 923 public void moderatorRevoked(String participant) { 924 super.moderatorRevoked(participant); 925 answer[7] = participant; 926 } 927 }); 928 929 try { 930 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 muc.grantModerator("testbot2"); 945 Thread.sleep(300); 946 947 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 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 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 clearAnswer(answer); 983 muc.grantModerator("testbot2"); 984 Thread.sleep(300); 985 986 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 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 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 muc2.leave(); 1015 muc3.leave(); 1017 } 1018 catch (Exception e) { 1019 e.printStackTrace(); 1020 fail(e.getMessage()); 1021 } 1022 } 1023 1024 public void testMembership() { 1025 final String [] answer = new String [4]; 1026 try { 1027 1028 makeRoomModerated(); 1029 1030 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 1032 muc2.join("testbot2"); 1033 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 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 1047 muc3.join("testbot3"); 1048 muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { 1050 public void membershipGranted(String participant) { 1051 super.membershipGranted(participant); 1052 answer[2] = participant; 1053 } 1054 public void membershipRevoked(String participant) { 1055 super.membershipRevoked(participant); 1056 answer[3] = participant; 1057 } 1058 }); 1059 1060 try { 1061 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 muc.grantMembership(getBareJID(1)); 1078 Thread.sleep(300); 1079 1080 assertEquals( 1082 "User2 didn't receive the grant membership notification", 1083 "I'm a member", 1084 answer[0]); 1085 assertEquals( 1087 "User3 didn't receive user2's grant membership notification", 1088 room + "/testbot2", 1089 answer[2]); 1090 1091 muc.revokeMembership(getBareJID(1)); 1094 Thread.sleep(300); 1095 1096 assertEquals( 1098 "User2 didn't receive the revoke membership notification", 1099 "I'm not a member", 1100 answer[1]); 1101 assertEquals( 1103 "User3 didn't receive user2's revoke membership notification", 1104 room + "/testbot2", 1105 answer[3]); 1106 1107 muc2.leave(); 1109 muc3.leave(); 1111 } 1112 catch (Exception e) { 1113 e.printStackTrace(); 1114 fail(e.getMessage()); 1115 } 1116 } 1117 1118 public void testAdmin() { 1119 final String [] answer = new String [8]; 1120 try { 1121 1122 makeRoomModerated(); 1123 1124 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 1126 muc2.join("testbot2"); 1127 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 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 1149 muc3.join("testbot3"); 1150 muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { 1152 public void membershipGranted(String participant) { 1153 super.membershipGranted(participant); 1154 answer[4] = participant; 1155 } 1156 public void membershipRevoked(String participant) { 1157 super.membershipRevoked(participant); 1158 answer[5] = participant; 1159 } 1160 public void adminGranted(String participant) { 1161 super.adminGranted(participant); 1162 answer[6] = participant; 1163 } 1164 public void adminRevoked(String participant) { 1165 super.adminRevoked(participant); 1166 answer[7] = participant; 1167 } 1168 }); 1169 1170 try { 1171 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 muc.grantAdmin(getBareJID(1)); 1186 Thread.sleep(300); 1187 1188 assertEquals( 1190 "User2 didn't receive the grant admin notification", 1191 "I'm an admin", 1192 answer[2]); 1193 assertEquals( 1195 "User3 didn't receive user2's grant admin notification", 1196 room + "/testbot2", 1197 answer[6]); 1198 1199 muc.revokeMembership(getBareJID(1)); 1202 Thread.sleep(300); 1203 1204 assertEquals( 1206 "User2 didn't receive the revoke admin notification", 1207 "I'm not an admin", 1208 answer[3]); 1209 assertEquals( 1211 "User3 didn't receive user2's revoke admin notification", 1212 room + "/testbot2", 1213 answer[7]); 1214 1215 clearAnswer(answer); 1217 muc.grantMembership(getBareJID(1)); 1218 Thread.sleep(300); 1219 muc.grantAdmin(getBareJID(1)); 1220 Thread.sleep(300); 1221 1222 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 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 clearAnswer(answer); 1244 muc.revokeAdmin(getBareJID(1)); 1245 Thread.sleep(300); 1246 1247 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 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 muc2.leave(); 1268 muc3.leave(); 1270 } 1271 catch (Exception e) { 1272 e.printStackTrace(); 1273 fail(e.getMessage()); 1274 } 1275 } 1276 1277 public void testOwnership() { 1278 final String [] answer = new String [12]; 1279 try { 1280 1281 makeRoomModerated(); 1282 1283 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 1285 muc2.join("testbot2"); 1286 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 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 1316 muc3.join("testbot3"); 1317 muc3.addParticipantStatusListener(new DefaultParticipantStatusListener() { 1319 public void membershipGranted(String participant) { 1320 super.membershipGranted(participant); 1321 answer[6] = participant; 1322 } 1323 public void membershipRevoked(String participant) { 1324 super.membershipRevoked(participant); 1325 answer[7] = participant; 1326 } 1327 public void adminGranted(String participant) { 1328 super.adminGranted(participant); 1329 answer[8] = participant; 1330 } 1331 public void adminRevoked(String participant) { 1332 super.adminRevoked(participant); 1333 answer[9] = participant; 1334 } 1335 public void ownershipGranted(String participant) { 1336 super.ownershipGranted(participant); 1337 answer[10] = participant; 1338 } 1339 public void ownershipRevoked(String participant) { 1340 super.ownershipRevoked(participant); 1341 answer[11] = participant; 1342 } 1343 }); 1344 1345 try { 1346 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 muc.grantOwnership(getBareJID(1)); 1361 Thread.sleep(300); 1362 1363 assertEquals( 1365 "User2 didn't receive the grant ownership notification", 1366 "I'm an owner", 1367 answer[4]); 1368 assertEquals( 1370 "User3 didn't receive user2's grant ownership notification", 1371 room + "/testbot2", 1372 answer[10]); 1373 1374 muc.revokeMembership(getBareJID(1)); 1377 Thread.sleep(300); 1378 1379 assertEquals( 1381 "User2 didn't receive the revoke ownership notification", 1382 "I'm not an owner", 1383 answer[5]); 1384 assertEquals( 1386 "User3 didn't receive user2's revoke ownership notification", 1387 room + "/testbot2", 1388 answer[11]); 1389 1390 clearAnswer(answer); 1392 muc.grantMembership(getBareJID(1)); 1393 Thread.sleep(300); 1394 muc.grantOwnership(getBareJID(1)); 1395 Thread.sleep(300); 1396 1397 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 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 clearAnswer(answer); 1419 muc.revokeAdmin(getBareJID(1)); 1420 Thread.sleep(300); 1421 1422 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 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 clearAnswer(answer); 1443 muc.grantAdmin(getBareJID(1)); 1444 Thread.sleep(300); 1445 muc.grantOwnership(getBareJID(1)); 1446 Thread.sleep(300); 1447 1448 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 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 clearAnswer(answer); 1470 muc.revokeOwnership(getBareJID(1)); 1471 Thread.sleep(300); 1472 1473 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 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 muc2.leave(); 1494 muc3.leave(); 1496 } 1497 catch (Exception e) { 1498 e.printStackTrace(); 1499 fail(e.getMessage()); 1500 } 1501 } 1502 1503 public void testGetAffiliationList() { 1504 try { 1505 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 1507 muc2.join("testbot2"); 1508 1509 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 1511 muc3.join("testbot3"); 1512 1513 muc.grantOwnership(getBareJID(1)); 1515 muc.grantModerator("testbot3"); 1517 1518 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 affiliates = muc.getAdmins(); 1540 assertEquals("Room has admins", 0, affiliates.size()); 1541 1542 affiliates = muc.getMembers(); 1544 assertEquals("Room has admins", 0, affiliates.size()); 1545 muc.grantMembership(getBareJID(1)); 1547 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 affiliates = muc.getOutcasts(); 1555 assertEquals("Room has outcasts", 0, affiliates.size()); 1556 1557 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 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 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 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 e) { 1608 e.printStackTrace(); 1609 fail(e.getMessage()); 1610 } 1611 } 1612 1613 1616 public void testJoinLeftEvents() { 1617 final String [] answer = new String [8]; 1618 try { 1619 muc.addParticipantStatusListener(new DefaultParticipantStatusListener() { 1621 public void joined(String 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 participant) { 1631 super.left(participant); 1632 if ((room + "/testbot2").equals(participant)) { 1633 answer[2] = participant; 1634 } 1635 else if (!(room + "/testbot").equals(participant)) { 1637 answer[3] = participant; 1638 } 1639 } 1640 }); 1641 1642 MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); 1644 muc2.addParticipantStatusListener(new DefaultParticipantStatusListener() { 1646 public void joined(String 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 participant) { 1656 super.left(participant); 1657 if ((room + "/testbot").equals(participant)) { 1658 answer[6] = participant; 1659 } 1660 else if (!(room + "/testbot2").equals(participant)){ 1662 answer[7] = participant; 1663 } 1664 } 1665 }); 1666 muc2.join("testbot2"); 1667 1668 MultiUserChat muc3 = new MultiUserChat(getConnection(2), room); 1670 muc3.join("testbot3"); 1671 1672 Thread.sleep(150); 1673 1674 muc3.leave(); 1676 1677 Thread.sleep(150); 1678 muc2.leave(); 1680 1681 Thread.sleep(250); 1682 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 e) { 1716 e.printStackTrace(); 1717 fail(e.getMessage()); 1718 } 1719 } 1720 1721 public void testManyResources() { 1722 try { 1723 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 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 for (int i = 0; i < mucs.length; i++) { 1741 mucs[i].sendMessage("I'm resource-" + i); 1742 } 1743 1744 Thread.sleep(200); 1745 1746 for (int i = 0; i < mucs.length; i++) { 1748 mucs[i].leave(); 1749 conns[i].close(); 1750 } 1751 1752 } catch (Exception e) { 1753 e.printStackTrace(); 1754 fail(e.getMessage()); 1755 } 1756 1757 } 1758 1759 private void makeRoomModerated() throws XMPPException { 1760 Form form = muc.getConfigurationForm(); 1762 Form answerForm = form.createAnswerForm(); 1763 answerForm.setAnswer("muc#roomconfig_moderatedroom", true); 1764 try { 1766 List owners = new ArrayList(); 1767 owners.add(getBareJID(0)); 1768 answerForm.setAnswer("muc#roomconfig_roomowners", owners); 1769 } 1770 catch (IllegalArgumentException e) { 1771 } 1773 muc.sendConfigurationForm(answerForm); 1774 } 1775 1776 private void clearAnswer(String [] answer) { 1777 for (int i = 0; i < answer.length; i++) { 1778 answer[i] = null; 1779 } 1780 } 1781 1782 protected void setUp() throws Exception { 1783 XMPPConnection.DEBUG_ENABLED = false; 1784 super.setUp(); 1785 room = "fruta124@" + getMUCDomain(); 1786 try { 1787 muc = new MultiUserChat(getConnection(0), room); 1789 muc.create("testbot"); 1790 1791 muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT)); 1794 } 1795 catch (Exception e) { 1796 fail(e.getMessage()); 1797 } 1798 } 1799 1800 protected void tearDown() throws Exception { 1801 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 |