1 18 package org.apache.activemq; 19 20 import java.net.URISyntaxException ; 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Vector ; 24 25 import javax.jms.BytesMessage ; 26 import javax.jms.ConnectionFactory ; 27 import javax.jms.DeliveryMode ; 28 import javax.jms.Destination ; 29 import javax.jms.JMSException ; 30 import javax.jms.MapMessage ; 31 import javax.jms.MessageConsumer ; 32 import javax.jms.MessageEOFException ; 33 import javax.jms.MessageProducer ; 34 import javax.jms.ObjectMessage ; 35 import javax.jms.Session ; 36 import javax.jms.StreamMessage ; 37 import javax.jms.TextMessage ; 38 39 import junit.framework.Test; 40 41 import org.apache.activemq.ActiveMQConnectionFactory; 42 import org.apache.activemq.command.ActiveMQDestination; 43 44 49 public class JMSMessageTest extends JmsTestSupport { 50 51 public ActiveMQDestination destination; 52 public int deliveryMode; 53 public int prefetch; 54 public int ackMode; 55 public byte destinationType; 56 public boolean durableConsumer; 57 public String connectURL; 58 59 62 public void initCombos() { 63 addCombinationValues("connectURL", new Object [] { 64 "vm://localhost?marshal=false", 65 "vm://localhost?marshal=true" 66 }); 67 addCombinationValues("deliveryMode", new Object [] { 68 new Integer (DeliveryMode.NON_PERSISTENT), 69 new Integer (DeliveryMode.PERSISTENT) }); 70 addCombinationValues("destinationType", new Object [] { 71 new Byte (ActiveMQDestination.QUEUE_TYPE)}); 72 } 73 74 public void testTextMessage() throws Exception { 75 76 connection.start(); 78 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 79 destination = createDestination(session, destinationType); 80 MessageConsumer consumer = session.createConsumer(destination); 81 MessageProducer producer = session.createProducer(destination); 82 83 { 85 TextMessage message = session.createTextMessage(); 86 message.setText("Hi"); 87 producer.send(message); 88 } 89 90 { 92 TextMessage message = (TextMessage )consumer.receive(1000); 93 assertNotNull(message); 94 assertEquals( "Hi", message.getText() ); 95 } 96 97 assertNull(consumer.receiveNoWait()); 98 } 99 100 public static Test suite() { 101 return suite(JMSMessageTest.class); 102 } 103 104 public static void main(String [] args) { 105 junit.textui.TestRunner.run(suite()); 106 } 107 108 protected ConnectionFactory createConnectionFactory() throws URISyntaxException { 109 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectURL); 110 return factory; 111 } 112 113 public void testBytesMessageLength() throws Exception { 114 115 connection.start(); 117 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 118 destination = createDestination(session, destinationType); 119 MessageConsumer consumer = session.createConsumer(destination); 120 MessageProducer producer = session.createProducer(destination); 121 122 { 124 BytesMessage message = session.createBytesMessage(); 125 message.writeInt(1); 126 message.writeInt(2); 127 message.writeInt(3); 128 message.writeInt(4); 129 producer.send(message); 130 } 131 132 { 134 BytesMessage message = (BytesMessage ) consumer.receive(1000); 135 assertNotNull(message); 136 assertEquals(16, message.getBodyLength() ); 137 } 138 139 assertNull(consumer.receiveNoWait()); 140 } 141 142 public void testObjectMessage() throws Exception { 143 144 connection.start(); 146 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 147 destination = createDestination(session, destinationType); 148 MessageConsumer consumer = session.createConsumer(destination); 149 MessageProducer producer = session.createProducer(destination); 150 151 { 153 ObjectMessage message = session.createObjectMessage(); 154 message.setObject("Hi"); 155 producer.send(message); 156 } 157 158 { 160 ObjectMessage message = (ObjectMessage )consumer.receive(1000); 161 assertNotNull(message); 162 assertEquals( "Hi", message.getObject() ); 163 } 164 assertNull(consumer.receiveNoWait()); 165 } 166 167 public void testBytesMessage() throws Exception { 168 169 connection.start(); 171 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 172 destination = createDestination(session, destinationType); 173 MessageConsumer consumer = session.createConsumer(destination); 174 MessageProducer producer = session.createProducer(destination); 175 176 { 178 BytesMessage message = session.createBytesMessage(); 179 message.writeBoolean(true); 180 producer.send(message); 181 } 182 183 { 185 BytesMessage message = (BytesMessage )consumer.receive(1000); 186 assertNotNull(message); 187 assertTrue( message.readBoolean() ); 188 189 try { 190 message.readByte(); 191 fail("Expected exception not thrown."); 192 } catch (MessageEOFException e) { 193 } 194 195 } 196 assertNull(consumer.receiveNoWait()); 197 } 198 199 public void testStreamMessage() throws Exception { 200 201 connection.start(); 203 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 204 destination = createDestination(session, destinationType); 205 MessageConsumer consumer = session.createConsumer(destination); 206 MessageProducer producer = session.createProducer(destination); 207 208 { 210 StreamMessage message = session.createStreamMessage(); 211 message.writeString("This is a test to see how it works."); 212 producer.send(message); 213 } 214 215 { 217 StreamMessage message = (StreamMessage ) consumer.receive(1000); 218 assertNotNull(message); 219 220 try { 222 message.readByte(); 223 fail("Should have received NumberFormatException"); 224 } catch (NumberFormatException e) { 225 } 226 227 assertEquals("This is a test to see how it works.", message.readString() ); 228 229 try { 231 message.readByte(); 232 fail("Should have received MessageEOFException"); 233 } catch (MessageEOFException e) { 234 } 235 } 236 assertNull(consumer.receiveNoWait()); 237 } 238 239 public void testMapMessage() throws Exception { 240 241 connection.start(); 243 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 244 destination = createDestination(session, destinationType); 245 MessageConsumer consumer = session.createConsumer(destination); 246 MessageProducer producer = session.createProducer(destination); 247 248 { 250 MapMessage message = session.createMapMessage(); 251 message.setBoolean("boolKey", true); 252 producer.send(message); 253 } 254 255 { 257 MapMessage message = (MapMessage )consumer.receive(1000); 258 assertNotNull(message); 259 assertTrue( message.getBoolean("boolKey") ); 260 } 261 assertNull(consumer.receiveNoWait()); 262 } 263 264 static class ForeignMessage implements TextMessage { 265 266 private String messageId; 267 private long timestamp; 268 private String correlationId; 269 private Destination replyTo; 270 private Destination destination; 271 public int deliveryMode; 272 private boolean redelivered; 273 private String type; 274 private long expiration; 275 private int priority; 276 private String text; 277 HashMap props = new HashMap (); 278 279 public String getJMSMessageID() throws JMSException { 280 return messageId; 281 } 282 283 public void setJMSMessageID(String arg0) throws JMSException { 284 messageId = arg0; 285 } 286 287 public long getJMSTimestamp() throws JMSException { 288 return timestamp; 289 } 290 291 public void setJMSTimestamp(long arg0) throws JMSException { 292 timestamp = arg0; 293 } 294 295 public byte[] getJMSCorrelationIDAsBytes() throws JMSException { 296 return null; 297 } 298 public void setJMSCorrelationIDAsBytes(byte[] arg0) throws JMSException { 299 } 300 301 public void setJMSCorrelationID(String arg0) throws JMSException { 302 correlationId = arg0; 303 } 304 public String getJMSCorrelationID() throws JMSException { 305 return correlationId; 306 } 307 308 public Destination getJMSReplyTo() throws JMSException { 309 return replyTo; 310 } 311 public void setJMSReplyTo(Destination arg0) throws JMSException { 312 replyTo = arg0; 313 } 314 315 public Destination getJMSDestination() throws JMSException { 316 return destination; 317 } 318 319 public void setJMSDestination(Destination arg0) throws JMSException { 320 destination = arg0; 321 } 322 323 public int getJMSDeliveryMode() throws JMSException { 324 return deliveryMode; 325 } 326 327 public void setJMSDeliveryMode(int arg0) throws JMSException { 328 deliveryMode = arg0; 329 } 330 331 public boolean getJMSRedelivered() throws JMSException { 332 return redelivered; 333 } 334 335 public void setJMSRedelivered(boolean arg0) throws JMSException { 336 redelivered=arg0; 337 } 338 339 public String getJMSType() throws JMSException { 340 return type; 341 } 342 343 public void setJMSType(String arg0) throws JMSException { 344 type=arg0; 345 } 346 347 public long getJMSExpiration() throws JMSException { 348 return expiration; 349 } 350 351 public void setJMSExpiration(long arg0) throws JMSException { 352 expiration = arg0; 353 } 354 355 public int getJMSPriority() throws JMSException { 356 return priority; 357 } 358 359 public void setJMSPriority(int arg0) throws JMSException { 360 priority=arg0; 361 } 362 363 public void clearProperties() throws JMSException { 364 } 365 public boolean propertyExists(String arg0) throws JMSException { 366 return false; 367 } 368 public boolean getBooleanProperty(String arg0) throws JMSException { 369 return false; 370 } 371 public byte getByteProperty(String arg0) throws JMSException { 372 return 0; 373 } 374 public short getShortProperty(String arg0) throws JMSException { 375 return 0; 376 } 377 public int getIntProperty(String arg0) throws JMSException { 378 return 0; 379 } 380 public long getLongProperty(String arg0) throws JMSException { 381 return 0; 382 } 383 public float getFloatProperty(String arg0) throws JMSException { 384 return 0; 385 } 386 public double getDoubleProperty(String arg0) throws JMSException { 387 return 0; 388 } 389 public String getStringProperty(String arg0) throws JMSException { 390 return (String ) props.get(arg0); 391 } 392 public Object getObjectProperty(String arg0) throws JMSException { 393 return props.get(arg0); 394 } 395 public Enumeration getPropertyNames() throws JMSException { 396 return new Vector (props.keySet()).elements(); 397 } 398 public void setBooleanProperty(String arg0, boolean arg1) throws JMSException { 399 } 400 public void setByteProperty(String arg0, byte arg1) throws JMSException { 401 } 402 public void setShortProperty(String arg0, short arg1) throws JMSException { 403 } 404 public void setIntProperty(String arg0, int arg1) throws JMSException { 405 } 406 public void setLongProperty(String arg0, long arg1) throws JMSException { 407 } 408 public void setFloatProperty(String arg0, float arg1) throws JMSException { 409 } 410 public void setDoubleProperty(String arg0, double arg1) throws JMSException { 411 } 412 public void setStringProperty(String arg0, String arg1) throws JMSException { 413 props.put( arg0, arg1); 414 } 415 public void setObjectProperty(String arg0, Object arg1) throws JMSException { 416 props.put( arg0, arg1); 417 } 418 419 public void acknowledge() throws JMSException { 420 } 421 public void clearBody() throws JMSException { 422 } 423 424 public void setText(String arg0) throws JMSException { 425 text = arg0; 426 } 427 public String getText() throws JMSException { 428 return text; 429 } 430 } 431 432 public void testForeignMessage() throws Exception { 433 434 connection.start(); 436 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 437 destination = createDestination(session, destinationType); 438 MessageConsumer consumer = session.createConsumer(destination); 439 MessageProducer producer = session.createProducer(destination); 440 441 { 443 ForeignMessage message = new ForeignMessage(); 444 message.text= "Hello"; 445 message.setStringProperty("test", "value"); 446 producer.send(message); 447 } 448 449 { 451 TextMessage message = (TextMessage )consumer.receive(1000); 452 assertNotNull(message); 453 assertEquals( "Hello", message.getText() ); 454 assertEquals( "value", message.getStringProperty("test") ); 455 } 456 457 assertNull(consumer.receiveNoWait()); 458 } 459 460 461 } 462 | Popular Tags |