KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > packet > XHTMLExtensionTest


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.packet;
54
55 import java.util.*;
56
57 import org.jivesoftware.smack.*;
58 import org.jivesoftware.smack.filter.*;
59 import org.jivesoftware.smack.packet.*;
60 import org.jivesoftware.smack.test.SmackTestCase;
61
62 /**
63  * Test the XHTML extension using the low level API
64  *
65  * @author Gaston Dombiak
66  */

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

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

85     public void testSendSimpleXHTMLMessage() {
86         // User1 creates a chat with user2
87
Chat chat1 = getConnection(0).createChat(getBareJID(1));
88
89         // User1 creates a message to send to user2
90
Message msg = chat1.createMessage();
91         msg.setSubject("Any subject you want");
92         msg.setBody("Hey John, this is my new green!!!!");
93         // Create a XHTMLExtension Package and add it to the message
94
XHTMLExtension xhtmlExtension = new XHTMLExtension();
95         xhtmlExtension.addBody(
96             "<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
97         msg.addExtension(xhtmlExtension);
98
99         // User1 sends the message that contains the XHTML to user2
100
try {
101             chat1.sendMessage(msg);
102             Thread.sleep(200);
103         }
104         catch (Exception JavaDoc e) {
105             fail("An error occured sending the message with XHTML");
106         }
107     }
108
109     /**
110     * Low level API test.
111     * 1. User_1 will send a message with XHTML to user_2
112     * 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
113     * is fine
114     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
115     * something is wrong
116     */

117     public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
118         // Create a chat for each connection
119
Chat chat1 = getConnection(0).createChat(getBareJID(1));
120         final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
121
122         // Create a Listener that listens for Messages with the extension
123
//"http://jabber.org/protocol/xhtml-im"
124
// This listener will listen on the conn2 and answer an ACK if everything is ok
125
PacketFilter packetFilter =
126             new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
127         PacketListener packetListener = new PacketListener() {
128             public void processPacket(Packet packet) {
129                 Message message = (Message) packet;
130                 assertNotNull("Body is null", message.getBody());
131                 try {
132                     XHTMLExtension xhtmlExtension =
133                         (XHTMLExtension) message.getExtension(
134                             "html",
135                             "http://jabber.org/protocol/xhtml-im");
136                     assertNotNull(
137                         "Message without extension \"http://jabber.org/protocol/xhtml-im\"",
138                         xhtmlExtension);
139                     assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
140                     for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
141                         String JavaDoc body = (String JavaDoc) it.next();
142                         System.out.println(body);
143                     }
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                 }
151                 catch (Exception JavaDoc e) {
152                     fail("An error occured sending ack " + e.getMessage());
153                 }
154             }
155         };
156         getConnection(1).addPacketListener(packetListener, packetFilter);
157
158         // User1 creates a message to send to user2
159
Message msg = chat1.createMessage();
160         msg.setSubject("Any subject you want");
161         msg.setBody("Hey John, this is my new green!!!!");
162         // Create a XHTMLExtension Package and add it to the message
163
XHTMLExtension xhtmlExtension = new XHTMLExtension();
164         xhtmlExtension.addBody(
165             "<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
166         msg.addExtension(xhtmlExtension);
167
168         // User1 sends the message that contains the XHTML to user2
169
try {
170             chat1.sendMessage(msg);
171         }
172         catch (Exception JavaDoc e) {
173             fail("An error occured sending the message with XHTML");
174         }
175         // Wait for 2 seconds for a reply
176
msg = chat1.nextMessage(1000);
177         assertNotNull("No reply received", msg);
178     }
179
180     /**
181     * Low level API test. Test a message with two XHTML bodies and several XHTML tags.
182     * 1. User_1 will send a message with XHTML to user_2
183     * 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
184     * is fine
185     * 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
186     * something is wrong
187     */

188     public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
189         // Create a chat for each connection
190
Chat chat1 = getConnection(0).createChat(getBareJID(1));
191         final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID());
192
193         // Create a Listener that listens for Messages with the extension
194
//"http://jabber.org/protocol/xhtml-im"
195
// This listener will listen on the conn2 and answer an ACK if everything is ok
196
PacketFilter packetFilter =
197             new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
198         PacketListener packetListener = new PacketListener() {
199             public void processPacket(Packet packet) {
200                 int received = 0;
201                 Message message = (Message) packet;
202                 assertNotNull("Body is null", message.getBody());
203                 try {
204                     XHTMLExtension xhtmlExtension =
205                         (XHTMLExtension) message.getExtension(
206                             "html",
207                             "http://jabber.org/protocol/xhtml-im");
208                     assertNotNull(
209                         "Message without extension \"http://jabber.org/protocol/xhtml-im\"",
210                         xhtmlExtension);
211                     assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
212                     for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
213                         received++;
214                         System.out.println((String JavaDoc) it.next());
215                     }
216                     bodiesReceived = received;
217                 }
218                 catch (ClassCastException JavaDoc e) {
219                     fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
220                 }
221             }
222         };
223         getConnection(1).addPacketListener(packetListener, packetFilter);
224
225         // User1 creates a message to send to user2
226
Message msg = chat1.createMessage();
227         msg.setSubject("Any subject you want");
228         msg.setBody(
229             "awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
230         // Create an XHTMLExtension and add it to the message
231
XHTMLExtension xhtmlExtension = new XHTMLExtension();
232         xhtmlExtension.addBody(
233             "<body xml:lang=\"es-ES\"><h1>impresionante!</h1><p>Como Emerson dijo una vez:</p><blockquote><p>Una consistencia ridícula es el espantajo de mentes pequeñas.</p></blockquote></body>");
234         xhtmlExtension.addBody(
235             "<body xml:lang=\"en-US\"><h1>awesome!</h1><p>As Emerson once said:</p><blockquote><p>A foolish consistency is the hobgoblin of little minds.</p></blockquote></body>");
236         msg.addExtension(xhtmlExtension);
237
238         // User1 sends the message that contains the XHTML to user2
239
try {
240             bodiesSent = xhtmlExtension.getBodiesCount();
241             bodiesReceived = 0;
242             chat1.sendMessage(msg);
243             Thread.sleep(300);
244         }
245         catch (Exception JavaDoc e) {
246             fail("An error occured sending the message with XHTML");
247         }
248         // Wait half second so that the complete test can run
249
assertEquals(
250             "Number of sent and received XHTMP bodies does not match",
251             bodiesSent,
252             bodiesReceived);
253     }
254
255     protected int getMaxConnections() {
256         return 2;
257     }
258
259 }
260
Popular Tags