KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > PresencePriorityTest


1 /**
2  * $RCSfile$
3  * $Revision: 2465 $
4  * $Date: 2005-03-15 13:29:30 -0300 (Tue, 15 Mar 2005) $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.smack;
13
14 import org.jivesoftware.smack.test.SmackTestCase;
15 import org.jivesoftware.smack.packet.Presence;
16
17 /**
18  * Ensure that the server is delivering messages to the correct client based on the client's
19  * presence priority.
20  *
21  * @author Gaston Dombiak
22  */

23 public class PresencePriorityTest extends SmackTestCase {
24
25     public PresencePriorityTest(String JavaDoc arg0) {
26         super(arg0);
27     }
28
29     /**
30      * Connection(0) will send messages to the bareJID of Connection(1) where the user of
31      * Connection(1) has logged from two different places with different presence priorities.
32      */

33     public void testMessageToHighestPriority() {
34         boolean wasFiltering = Chat.isFilteredOnThreadID();
35         Chat.setFilteredOnThreadID(false);
36         XMPPConnection conn = null;
37         try {
38             // User_1 will log in again using another resource
39
conn = new XMPPConnection(getHost(), getPort());
40             conn.login(getUsername(1), getUsername(1), "OtherPlace");
41             // Change the presence priorities of User_1
42
getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE, null, 1,
43                     Presence.Mode.AVAILABLE));
44             conn.sendPacket(new Presence(Presence.Type.AVAILABLE, null, 2,
45                     Presence.Mode.AVAILABLE));
46             Thread.sleep(150);
47             // Create the chats between the participants
48
Chat chat0 = new Chat(getConnection(0), getBareJID(1));
49             Chat chat1 = new Chat(getConnection(1), getBareJID(0));
50             Chat chat2 = new Chat(conn, getBareJID(0));
51
52             // Test delivery of message to the presence with highest priority
53
chat0.sendMessage("Hello");
54             assertNotNull("Resource with highest priority didn't receive the message",
55                     chat2.nextMessage(2000));
56             assertNull("Resource with lowest priority received the message",
57                     chat1.nextMessage(1000));
58
59             // Invert the presence priorities of User_1
60
getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE, null, 2,
61                     Presence.Mode.AVAILABLE));
62             conn.sendPacket(new Presence(Presence.Type.AVAILABLE, null, 1,
63                     Presence.Mode.AVAILABLE));
64
65             Thread.sleep(150);
66             // Test delivery of message to the presence with highest priority
67
chat0.sendMessage("Hello");
68             assertNotNull("Resource with highest priority didn't receive the message",
69                     chat1.nextMessage(2000));
70             assertNull("Resource with lowest priority received the message",
71                     chat2.nextMessage(1000));
72
73             // User_1 closes his connection
74
chat2 = null;
75             conn.close();
76             Thread.sleep(150);
77
78             // Test delivery of message to the unique presence of the user_1
79
chat0.sendMessage("Hello");
80             assertNotNull("Resource with highest priority didn't receive the message",
81                     chat1.nextMessage(2000));
82
83             getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE, null, 2,
84                     Presence.Mode.AVAILABLE));
85
86             // User_1 will log in again using another resource
87
conn = new XMPPConnection(getHost(), getPort());
88             conn.login(getUsername(1), getUsername(1), "OtherPlace");
89             conn.sendPacket(new Presence(Presence.Type.AVAILABLE, null, 1,
90                     Presence.Mode.AVAILABLE));
91             chat2 = new Chat(conn, getBareJID(0));
92
93             Thread.sleep(150);
94             // Test delivery of message to the presence with highest priority
95
chat0.sendMessage("Hello");
96             assertNotNull("Resource with highest priority didn't receive the message",
97                     chat1.nextMessage(2000));
98             assertNull("Resource with lowest priority received the message",
99                     chat2.nextMessage(1000));
100
101             // Invert the presence priorities of User_1
102
getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE, null, 1,
103                     Presence.Mode.AVAILABLE));
104             conn.sendPacket(new Presence(Presence.Type.AVAILABLE, null, 2,
105                     Presence.Mode.AVAILABLE));
106
107             Thread.sleep(150);
108             // Test delivery of message to the presence with highest priority
109
chat0.sendMessage("Hello");
110             assertNotNull("Resource with highest priority didn't receive the message",
111                     chat2.nextMessage(2000));
112             assertNull("Resource with lowest priority received the message",
113                     chat1.nextMessage(1000));
114
115         }
116         catch (Exception JavaDoc e) {
117             e.printStackTrace();
118             fail(e.getMessage());
119         }
120         finally {
121             // Restore the previous filtering value so we don't affect other test cases
122
Chat.setFilteredOnThreadID(wasFiltering);
123             if (conn != null) {
124                 conn.close();
125             }
126         }
127     }
128
129     protected int getMaxConnections() {
130         return 2;
131     }
132
133     protected void setUp() throws Exception JavaDoc {
134         XMPPConnection.DEBUG_ENABLED = false;
135         super.setUp();
136     }
137 }
138
Popular Tags