KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile$
3  * $Revision: 2729 $
4  * $Date: 2005-08-26 23:21:24 -0300 (Fri, 26 Aug 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack;
22
23 import org.jivesoftware.smack.test.SmackTestCase;
24 import org.jivesoftware.smack.packet.Presence;
25 import org.jivesoftware.smack.packet.Message;
26 import org.jivesoftware.smack.filter.MessageTypeFilter;
27
28 /**
29  * Tests sending messages to other clients.
30  *
31  * @author Gaston Dombiak
32  */

33 public class MessageTest extends SmackTestCase {
34
35     public MessageTest(String JavaDoc arg0) {
36         super(arg0);
37     }
38
39     /**
40      * Check that when a client becomes unavailable all messages sent to the client are stored offline. So that when
41      * the client becomes available again the offline messages are received.
42      */

43     public void testOfflineMessage() {
44         // Make user2 unavailable
45
getConnection(1).sendPacket(new Presence(Presence.Type.UNAVAILABLE));
46
47         try {
48             Thread.sleep(500);
49
50             // User1 sends some messages to User2 which is not available at the moment
51
Chat chat = getConnection(0).createChat(getBareJID(1));
52             chat.sendMessage("Test 1");
53             chat.sendMessage("Test 2");
54
55             Thread.sleep(500);
56
57             // User2 becomes available again
58
PacketCollector collector = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.CHAT));
59             getConnection(1).sendPacket(new Presence(Presence.Type.AVAILABLE));
60
61             // Check that offline messages are retrieved by user2 which is now available
62
Message message = (Message) collector.nextResult(2500);
63             assertNotNull(message);
64             message = (Message) collector.nextResult(2000);
65             assertNotNull(message);
66             message = (Message) collector.nextResult(1000);
67             assertNull(message);
68
69         } catch (Exception JavaDoc e) {
70             e.printStackTrace();
71             fail(e.getMessage());
72         }
73     }
74
75     /**
76      * Check that two clients are able to send messages with a body of 4K characters and their
77      * connections are not being closed.
78      */

79     public void testHugeMessage() {
80         // User2 becomes available again
81
PacketCollector collector = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.CHAT));
82
83         // Create message with a body of 4K characters
84
Message msg = new Message(getFullJID(1), Message.Type.CHAT);
85         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(5000);
86         for (int i=0; i<=4000; i++) {
87             sb.append("X");
88         }
89         msg.setBody(sb.toString());
90
91         // Send the first message
92
getConnection(0).sendPacket(msg);
93         // Check that the connection that sent the message is still connected
94
assertTrue("Connection was closed", getConnection(0).isConnected());
95         // Check that the message was received
96
Message rcv = (Message) collector.nextResult(1000);
97         assertNotNull("No Message was received", rcv);
98
99         // Send the second message
100
getConnection(0).sendPacket(msg);
101         // Check that the connection that sent the message is still connected
102
assertTrue("Connection was closed", getConnection(0).isConnected());
103         // Check that the second message was received
104
rcv = (Message) collector.nextResult(1000);
105         assertNotNull("No Message was received", rcv);
106
107         // Try now sending huge messages over an SSL connection
108
XMPPConnection conn = null;
109         try {
110             conn = new SSLXMPPConnection(getServiceName());
111             conn.login(getUsername(0), getUsername(0), "Other resource");
112
113             // Send the first message
114
conn.sendPacket(msg);
115             // Check that the connection that sent the message is still connected
116
assertTrue("Connection was closed", conn.isConnected());
117             // Check that the message was received
118
rcv = (Message) collector.nextResult(1000);
119             assertNotNull("No Message was received", rcv);
120         } catch (XMPPException e) {
121             fail(e.getMessage());
122         }
123         finally {
124             if (conn != null) {
125                 conn.close();
126             }
127         }
128
129     }
130
131     protected int getMaxConnections() {
132         return 2;
133     }
134
135     protected void setUp() throws Exception JavaDoc {
136         XMPPConnection.DEBUG_ENABLED = false;
137         super.setUp();
138     }
139 }
140
Popular Tags