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