1 52 53 package org.jivesoftware.smackx; 54 55 import java.util.Iterator ; 56 57 import org.jivesoftware.smack.*; 58 import org.jivesoftware.smack.packet.*; 59 import org.jivesoftware.smack.test.SmackTestCase; 60 61 66 public class XHTMLManagerTest extends SmackTestCase { 67 68 private int bodiesSent; 69 private int bodiesReceived; 70 71 75 public XHTMLManagerTest(String name) { 76 super(name); 77 } 78 79 84 public void testSendSimpleXHTMLMessage() { 85 Chat chat1 = getConnection(0).createChat(getBareJID(1)); 87 88 Message msg = chat1.createMessage(); 90 msg.setSubject("Any subject you want"); 91 msg.setBody("Hey John, this is my new green!!!!"); 92 93 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 XHTMLManager.addBody(msg, xhtmlText.toString()); 106 107 try { 109 chat1.sendMessage(msg); 110 Thread.sleep(200); 111 } catch (Exception e) { 112 fail("An error occured sending the message with XHTML"); 113 } 114 } 115 116 124 public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() { 125 Chat chat1 = getConnection(0).createChat(getBareJID(1)); 127 final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); 128 129 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 it = XHTMLManager.getBodies(message); it.hasNext();) { 142 String body = (String ) it.next(); 143 System.out.println(body); 144 } 145 } catch (ClassCastException e) { 146 fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); 147 } 148 try { 149 chat2.sendMessage("ok"); 150 } catch (Exception e) { 151 fail("An error occured sending ack " + e.getMessage()); 152 } 153 } 154 }; 155 chat2.addMessageListener(packetListener); 156 157 Message msg = chat1.createMessage(); 159 msg.setSubject("Any subject you want"); 160 msg.setBody("Hey John, this is my new green!!!!"); 161 162 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 XHTMLManager.addBody(msg, xhtmlText.toString()); 175 176 try { 178 chat1.sendMessage(msg); 179 } catch (Exception e) { 180 fail("An error occured sending the message with XHTML"); 181 } 182 msg = chat1.nextMessage(1000); 184 assertNotNull("No reply received", msg); 185 } 186 187 195 public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() { 196 Chat chat1 = getConnection(0).createChat(getBareJID(1)); 198 final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); 199 200 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 it = XHTMLManager.getBodies(message); it.hasNext();) { 214 received++; 215 String body = (String ) it.next(); 216 System.out.println(body); 217 } 218 bodiesReceived = received; 219 } catch (ClassCastException e) { 220 fail("ClassCastException - Most probable cause is that smack providers is misconfigured"); 221 } 222 } 223 }; 224 chat2.addMessageListener(packetListener); 225 226 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 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 XHTMLManager.addBody(msg, xhtmlText.toString()); 247 248 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 XHTMLManager.addBody(msg, xhtmlText.toString()); 263 264 try { 266 bodiesSent = 2; 267 bodiesReceived = 0; 268 chat1.sendMessage(msg); 269 Thread.sleep(300); 271 } catch (Exception 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 |