KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > XHTMLManagerTest


1 /**
2  * $RCSfile$
3  * $Revision: 2339 $
4  * $Date: 2004-07-12 10:36:13 -0300 (Mon, 12 Jul 2004) $
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;
54
55 import java.util.Iterator JavaDoc;
56
57 import org.jivesoftware.smack.*;
58 import org.jivesoftware.smack.packet.*;
59 import org.jivesoftware.smack.test.SmackTestCase;
60
61 /**
62  * Test the XHTML extension using the high level API
63  *
64  * @author Gaston Dombiak
65  */

66 public class XHTMLManagerTest extends SmackTestCase {
67
68     private int bodiesSent;
69     private int bodiesReceived;
70
71     /**
72      * Constructor for XHTMLManagerTest.
73      * @param name
74      */

75     public XHTMLManagerTest(String JavaDoc name) {
76         super(name);
77     }
78
79     /**
80      * High level API test.
81      * This is a simple test to use with a XMPP client and check if the client receives the message
82      * 1. User_1 will send a message with formatted text (XHTML) to user_2
83      */

84     public void testSendSimpleXHTMLMessage() {
85         // User1 creates a chat with user2
86
Chat chat1 = getConnection(0).createChat(getBareJID(1));
87
88         // User1 creates a message to send to user2
89
Message msg = chat1.createMessage();
90         msg.setSubject("Any subject you want");
91         msg.setBody("Hey John, this is my new green!!!!");
92
93         // Create an XHTMLText to send with the message
94
XHTMLText xhtmlText = new XHTMLText(null, null);
95         xhtmlText.appendOpenParagraphTag("font-size:large");
96         xhtmlText.append("Hey John, this is my new ");
97         xhtmlText.appendOpenSpanTag("color:green");
98         xhtmlText.append("green");
99         xhtmlText.appendCloseSpanTag();
100         xhtmlText.appendOpenEmTag();
101         xhtmlText.append("!!!!");
102         xhtmlText.appendCloseEmTag();
103         xhtmlText.appendCloseParagraphTag();
104         // Add the XHTML text to the message
105
XHTMLManager.addBody(msg, xhtmlText.toString());
106
107         // User1 sends the message that contains the XHTML to user2
108
try {
109             chat1.sendMessage(msg);
110             Thread.sleep(200);
111         } catch (Exception JavaDoc e) {
112             fail("An error occured sending the message with XHTML");
113         }
114     }
115
116     /**
117     * High level API test.
118     * 1. User_1 will send a message with XHTML to user_2
119     * 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
120     * is fine
121     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
122     * something is wrong
123     */

124     public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
125         // Create a chat for each connection
126
Chat chat1 = getConnection(0).createChat(getBareJID(1));
127         final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
128
129         // Create a listener for the chat that will check if the received message includes
130
// an XHTML extension. Answer an ACK if everything is ok
131
PacketListener packetListener = new PacketListener() {
132             public void processPacket(Packet packet) {
133                 Message message = (Message) packet;
134                 assertTrue(
135                     "The received message is not an XHTML Message",
136                     XHTMLManager.isXHTMLMessage(message));
137                 try {
138                     assertTrue(
139                         "Message without XHTML bodies",
140                         XHTMLManager.getBodies(message).hasNext());
141                     for (Iterator JavaDoc it = XHTMLManager.getBodies(message); it.hasNext();) {
142                         String JavaDoc body = (String JavaDoc) it.next();
143                         System.out.println(body);
144                     }
145                 } catch (ClassCastException JavaDoc e) {
146                     fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
147                 }
148                 try {
149                     chat2.sendMessage("ok");
150                 } catch (Exception JavaDoc e) {
151                     fail("An error occured sending ack " + e.getMessage());
152                 }
153             }
154         };
155         chat2.addMessageListener(packetListener);
156
157         // User1 creates a message to send to user2
158
Message msg = chat1.createMessage();
159         msg.setSubject("Any subject you want");
160         msg.setBody("Hey John, this is my new green!!!!");
161
162         // Create an XHTMLText to send with the message
163
XHTMLText xhtmlText = new XHTMLText(null, null);
164         xhtmlText.appendOpenParagraphTag("font-size:large");
165         xhtmlText.append("Hey John, this is my new ");
166         xhtmlText.appendOpenSpanTag("color:green");
167         xhtmlText.append("green");
168         xhtmlText.appendCloseSpanTag();
169         xhtmlText.appendOpenEmTag();
170         xhtmlText.append("!!!!");
171         xhtmlText.appendCloseEmTag();
172         xhtmlText.appendCloseParagraphTag();
173         // Add the XHTML text to the message
174
XHTMLManager.addBody(msg, xhtmlText.toString());
175
176         // User1 sends the message that contains the XHTML to user2
177
try {
178             chat1.sendMessage(msg);
179         } catch (Exception JavaDoc e) {
180             fail("An error occured sending the message with XHTML");
181         }
182         // Wait for 1 second for a reply
183
msg = chat1.nextMessage(1000);
184         assertNotNull("No reply received", msg);
185     }
186
187     /**
188     * Low level API test. Test a message with two XHTML bodies and several XHTML tags.
189     * 1. User_1 will send a message with XHTML to user_2
190     * 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
191     * is fine
192     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
193     * something is wrong
194     */

195     public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
196         // Create a chat for each connection
197
Chat chat1 = getConnection(0).createChat(getBareJID(1));
198         final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
199
200         // Create a listener for the chat that will check if the received message includes
201
// an XHTML extension. Answer an ACK if everything is ok
202
PacketListener packetListener = new PacketListener() {
203             public void processPacket(Packet packet) {
204                 int received = 0;
205                 Message message = (Message) packet;
206                 assertTrue(
207                     "The received message is not an XHTML Message",
208                     XHTMLManager.isXHTMLMessage(message));
209                 try {
210                     assertTrue(
211                         "Message without XHTML bodies",
212                         XHTMLManager.getBodies(message).hasNext());
213                     for (Iterator JavaDoc it = XHTMLManager.getBodies(message); it.hasNext();) {
214                         received++;
215                         String JavaDoc body = (String JavaDoc) it.next();
216                         System.out.println(body);
217                     }
218                     bodiesReceived = received;
219                 } catch (ClassCastException JavaDoc e) {
220                     fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
221                 }
222             }
223         };
224         chat2.addMessageListener(packetListener);
225
226         // User1 creates a message to send to user2
227
Message msg = chat1.createMessage();
228         msg.setSubject("Any subject you want");
229         msg.setBody(
230             "awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
231
232         // Create an XHTMLText to send with the message (in Spanish)
233
XHTMLText xhtmlText = new XHTMLText(null, "es-ES");
234         xhtmlText.appendOpenHeaderTag(1, null);
235         xhtmlText.append("impresionante!");
236         xhtmlText.appendCloseHeaderTag(1);
237         xhtmlText.appendOpenParagraphTag(null);
238         xhtmlText.append("Como Emerson dijo una vez:");
239         xhtmlText.appendCloseParagraphTag();
240         xhtmlText.appendOpenBlockQuoteTag(null);
241         xhtmlText.appendOpenParagraphTag(null);
242         xhtmlText.append("Una consistencia ridícula es el espantajo de mentes pequeñas.");
243         xhtmlText.appendCloseParagraphTag();
244         xhtmlText.appendCloseBlockQuoteTag();
245         // Add the XHTML text to the message
246
XHTMLManager.addBody(msg, xhtmlText.toString());
247
248         // Create an XHTMLText to send with the message (in English)
249
xhtmlText = new XHTMLText(null, "en-US");
250         xhtmlText.appendOpenHeaderTag(1, null);
251         xhtmlText.append("awesome!");
252         xhtmlText.appendCloseHeaderTag(1);
253         xhtmlText.appendOpenParagraphTag(null);
254         xhtmlText.append("As Emerson once said:");
255         xhtmlText.appendCloseParagraphTag();
256         xhtmlText.appendOpenBlockQuoteTag(null);
257         xhtmlText.appendOpenParagraphTag(null);
258         xhtmlText.append("A foolish consistency is the hobgoblin of little minds.");
259         xhtmlText.appendCloseParagraphTag();
260         xhtmlText.appendCloseBlockQuoteTag();
261         // Add the XHTML text to the message
262
XHTMLManager.addBody(msg, xhtmlText.toString());
263
264         // User1 sends the message that contains the XHTML to user2
265
try {
266             bodiesSent = 2;
267             bodiesReceived = 0;
268             chat1.sendMessage(msg);
269             // Wait half second so that the complete test can run
270
Thread.sleep(300);
271         } catch (Exception JavaDoc e) {
272             fail("An error occured sending the message with XHTML");
273         }
274         assertEquals(
275             "Number of sent and received XHTMP bodies does not match",
276             bodiesSent,
277             bodiesReceived);
278     }
279
280     protected int getMaxConnections() {
281         return 2;
282     }
283
284 }
285
Popular Tags