1 22 package org.jboss.test.jbossmq.test; 23 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import javax.jms.JMSException ; 28 import javax.jms.Message ; 29 import javax.jms.ObjectMessage ; 30 import javax.jms.Queue ; 31 import javax.jms.QueueConnection ; 32 import javax.jms.QueueConnectionFactory ; 33 import javax.jms.QueueReceiver ; 34 import javax.jms.QueueSender ; 35 import javax.jms.QueueSession ; 36 import javax.jms.Session ; 37 import javax.jms.TextMessage ; 38 import javax.naming.Context ; 39 40 import org.jboss.logging.Logger; 41 import org.jboss.test.JBossTestCase; 42 43 50 public class MessageBodyUnitTestCase extends JBossTestCase 51 { 52 public static final String QUEUE_FACTORY = "ConnectionFactory"; 54 public static final String TEST_QUEUE = "queue/testQueue"; 55 56 Context context; 57 QueueConnection queueConnection; 58 QueueSession session; 59 Queue queue; 60 61 QueueReceiver receiver; 62 QueueSender sender; 63 64 Logger log; 65 66 public MessageBodyUnitTestCase(String name) throws Exception 67 { 68 super(name); 69 log = getLog(); 70 } 71 72 protected void setUp() throws Exception 73 { 74 connect(); 75 } 76 77 protected void tearDown() throws Exception 78 { 79 disconnect(); 80 } 81 82 protected void connect() throws Exception 83 { 84 log.debug("connecting"); 85 if (context == null) 86 { 87 context = getInitialContext(); 88 } 89 90 QueueConnectionFactory queueFactory = (QueueConnectionFactory ) context.lookup(QUEUE_FACTORY); 91 queueConnection = queueFactory.createQueueConnection(); 92 log.debug("connected"); 93 94 queueConnection.start(); 95 session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 96 log.debug("session established"); 97 98 queue = (Queue ) context.lookup(TEST_QUEUE); 99 100 receiver = session.createReceiver(queue); 101 sender = session.createSender(queue); 102 log.debug("sender established"); 103 104 drainQueue(); 105 log.debug("end of connect call"); 106 } 107 108 protected void disconnect() throws Exception 109 { 110 queueConnection.close(); 111 } 112 113 private void drainQueue() throws Exception 114 { 115 log.debug("draining queue"); 116 117 Message message = receiver.receive(2000); 118 int c = 0; 119 while (message != null) 120 { 121 message = receiver.receive(2000); 122 c++; 123 } 124 125 if (c != 0) 126 log.debug("Drained " + c + " messages from the queue"); 127 128 log.debug("drained queue"); 129 130 } 131 132 protected void validate(String payload) throws Exception 133 { 134 log.debug("validating text |" + payload + "|"); 135 136 TextMessage outMessage = session.createTextMessage(); 137 outMessage.setText(payload); 138 log.debug("sending |" + payload + "|"); 139 sender.send(outMessage); 140 141 log.debug("receiving |" + payload + "|"); 142 TextMessage inMessage = (TextMessage ) receiver.receive(); 143 log.debug("received |" + payload + "|"); 144 String inPayload = inMessage.getText(); 145 146 assertEquals("Message body text test", payload, inPayload); 147 log.debug("validated text " + payload); 148 } 149 150 public void testTextMessageBody() throws Exception 151 { 152 log.debug("testing text"); 153 154 validate("ordinary text"); 155 validate(" "); 156 validate(""); 157 char c[] = new char[1024 * 32]; 159 Arrays.fill(c, 'x'); 160 validate(new String (c)); 161 Arrays.fill(c, '\u0130'); validate(new String (c)); 163 Arrays.fill(c, '\u0008'); 164 validate(new String (c)); 165 log.debug("tested text"); 166 } 167 168 protected void validate(java.io.Serializable payload) throws Exception 169 { 170 ObjectMessage outMessage = session.createObjectMessage(); 171 outMessage.setObject(payload); 172 sender.send(outMessage); 173 174 ObjectMessage inMessage = (ObjectMessage ) receiver.receive(); 175 Object inPayload = inMessage.getObject(); 176 177 assertEquals("Message body object test", payload, inPayload); 178 } 179 180 public void testObjectMessageBody() throws Exception 181 { 182 log.debug("testing object"); 183 validate(new Integer (0)); 184 validate(new Integer (1)); 185 validate(new Integer (-1)); 186 validate(new Integer (Integer.MAX_VALUE)); 187 validate(new Integer (Integer.MIN_VALUE)); 188 validate(new Integer (-1)); 189 validate(new Float (1.0)); 190 validate(new Float (0.0)); 191 validate(new Float (-1.0)); 192 validate(new Float (Float.MAX_VALUE)); 193 validate(new Float (Float.MIN_VALUE)); 194 validate(new Float (Float.NaN)); 195 validate(new Float (Float.POSITIVE_INFINITY)); 196 validate(new Float (Float.NEGATIVE_INFINITY)); 197 validate(new Float (1.0)); 198 HashMap m = new HashMap (); m.put("file", new java.io.File ("somefile.txt")); 200 m.put("url", new java.net.URL ("http://example.net")); 201 validate(m); 202 validate((java.io.Serializable )Collections.nCopies(10000, "Repeat")); 203 } 204 205 208 public void testNullProperties() throws Exception 209 { 210 TextMessage message = session.createTextMessage(); 211 212 message.setStringProperty("THE_PROP", null); 213 message.setObjectProperty("THE_PROP2", null); 214 215 try 216 { 217 message.setStringProperty("", null); 218 fail("empty string property"); 219 } 220 catch (IllegalArgumentException e) {} 221 222 try 223 { 224 message.setStringProperty(null, null); 225 fail("null property"); 226 } 227 catch (IllegalArgumentException e) {} 228 } 229 230 public void testInvalidPropertyName() throws Exception 231 { 232 Message message = session.createMessage(); 233 234 String [] invalid = new String [] 235 { 236 "invalid-hyphen", 237 "1digitfirst", 238 "NULL", 239 "TRUE", 240 "FALSE", 241 "NOT", 242 "AND", 243 "OR", 244 "BETWEEN", 245 "LIKE", 246 "IN", 247 "IS", 248 "ESCAPE" 249 }; 250 251 for (int i = 0; i < invalid.length; ++i) 252 { 253 try 254 { 255 message.setStringProperty(invalid[i], "whatever"); 256 fail("expected error for invalid property name " + invalid[i]); 257 } 258 catch (IllegalArgumentException expected) 259 { 260 } 261 } 262 263 String [] valid = new String [] 264 { 265 "identifier", 266 "_", 267 "$", 268 "_xSx", 269 "$x_x", 270 "A1", 271 "null", 272 "true", 273 "false", 274 "not", 275 "and", 276 "or", 277 "between", 278 "like", 279 "in", 280 "is", 281 "escape" 282 }; 283 284 for (int i = 0; i < invalid.length; ++i) 285 message.setStringProperty(valid[i], "whatever"); 286 } 287 288 291 public void testVendorProperties() throws Exception 292 { 293 TextMessage message = session.createTextMessage(); 294 295 try 296 { 297 message.setStringProperty("JMS_JBOSS_SCHEDULED_DELIVERY", "whenever"); 298 fail("invalid type"); 299 } 300 catch (JMSException e) {} 301 try 302 { 303 message.setObjectProperty("JMS_JBOSS_SCHEDULED_DELIVERY", "10234"); 304 fail("invalid type"); 305 } 306 catch (JMSException e) {} 307 try 308 { 309 message.setStringProperty("JMS_JBOSS_REDELIVERY_COUNT", "fruity"); 310 fail("invalid type"); 311 } 312 catch (JMSException e) {} 313 try 314 { 315 message.setStringProperty("JMS_JBOSS_REDELIVERY_LIMIT", "fruity"); 316 fail("invalid type"); 317 } 318 catch (JMSException e) {} 319 320 message.setLongProperty("JMS_JBOSS_SCHEDULED_DELIVERY", 10234); 321 message.setIntProperty("JMS_JBOSS_REDELIVERY_COUNT", 123); 322 message.setShortProperty("JMS_JBOSS_REDELIVERY_LIMIT", (short)1); 323 } 324 325 public static junit.framework.Test suite() throws Exception 326 { 327 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 328 return getDeploySetup(MessageBodyUnitTestCase.class, 329 loader.getResource("messaging/test-destinations-service.xml").toString()); 330 } 331 332 } 333 | Popular Tags |