1 18 package org.apache.activemq.command; 19 20 import java.io.IOException ; 21 import java.util.Enumeration ; 22 import java.util.Map ; 23 24 import javax.jms.JMSException ; 25 import javax.jms.Message ; 26 import javax.jms.MessageFormatException ; 27 import javax.jms.MessageNotWriteableException ; 28 29 import junit.framework.TestCase; 30 31 import org.apache.activemq.openwire.OpenWireFormat; 32 import org.apache.activemq.state.CommandVisitor; 33 import org.apache.activemq.util.ByteSequence; 34 import org.apache.activemq.wireformat.WireFormat; 35 36 39 public class ActiveMQMessageTest extends TestCase { 40 41 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 42 .getLog(ActiveMQMessageTest.class); 43 44 private String jmsMessageID; 45 private String jmsCorrelationID; 46 private ActiveMQDestination jmsDestination; 47 private ActiveMQDestination jmsReplyTo; 48 private int jmsDeliveryMode; 49 private boolean jmsRedelivered; 50 private String jmsType; 51 private long jmsExpiration; 52 private int jmsPriority; 53 private long jmsTimestamp; 54 protected boolean readOnlyMessage; 55 private long[] consumerIDs; 56 57 58 public static void main(String [] args) { 59 } 60 61 64 protected void setUp() throws Exception { 65 super.setUp(); 66 this.jmsMessageID = "testid"; 67 this.jmsCorrelationID = "testcorrelationid"; 68 this.jmsDestination = new ActiveMQTopic("test.topic"); 69 this.jmsReplyTo = new ActiveMQTempTopic("test.replyto.topic:001"); 70 this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE; 71 this.jmsRedelivered = true; 72 this.jmsType = "test type"; 73 this.jmsExpiration = 100000; 74 this.jmsPriority = 5; 75 this.jmsTimestamp = System.currentTimeMillis(); 76 this.readOnlyMessage = false; 77 this.consumerIDs = new long[3]; 78 for (int i = 0; i < this.consumerIDs.length; i++) { 79 this.consumerIDs[i] = i; 80 } 81 82 } 83 84 87 protected void tearDown() throws Exception { 88 super.tearDown(); 89 } 90 91 96 public ActiveMQMessageTest(String arg0) { 97 super(arg0); 98 } 99 100 public void testGetDataStructureType() { 101 ActiveMQMessage msg = new ActiveMQMessage(); 102 assertEquals(msg.getDataStructureType(), CommandTypes.ACTIVEMQ_MESSAGE); 103 } 104 105 public void testHashCode() throws Exception { 106 ActiveMQMessage msg = new ActiveMQMessage(); 107 msg.setJMSMessageID(this.jmsMessageID); 108 assertTrue(msg.hashCode() == jmsMessageID.hashCode()); 109 } 110 111 public void testSetReadOnly() { 112 ActiveMQMessage msg = new ActiveMQMessage(); 113 msg.setReadOnlyProperties(true); 114 boolean test = false; 115 try { 116 msg.setIntProperty("test", 1); 117 } catch (MessageNotWriteableException me) { 118 test = true; 119 } catch (JMSException e) { 120 e.printStackTrace(System.err); 121 test = false; 122 } 123 assertTrue(test); 124 } 125 126 public void testSetToForeignJMSID() throws Exception { 127 ActiveMQMessage msg = new ActiveMQMessage(); 128 msg.setJMSMessageID("ID:EMS-SERVER.8B443C380083:429"); 129 130 } 131 134 public void testEqualsObject() throws Exception { 135 ActiveMQMessage msg1 = new ActiveMQMessage(); 136 ActiveMQMessage msg2 = new ActiveMQMessage(); 137 msg1.setJMSMessageID(this.jmsMessageID); 138 assertTrue(!msg1.equals(msg2)); 139 msg2.setJMSMessageID(this.jmsMessageID); 140 assertTrue(msg1.equals(msg2)); 141 } 142 143 public void testShallowCopy() throws Exception { 144 ActiveMQMessage msg1 = new ActiveMQMessage(); 145 msg1.setJMSMessageID(jmsMessageID); 146 ActiveMQMessage msg2 = (ActiveMQMessage) msg1.copy(); 147 assertTrue(msg1 != msg2 && msg1.equals(msg2)); 148 } 149 150 public void testCopy() throws Exception { 151 this.jmsMessageID = "testid"; 152 this.jmsCorrelationID = "testcorrelationid"; 153 this.jmsDestination = new ActiveMQTopic("test.topic"); 154 this.jmsReplyTo = new ActiveMQTempTopic("test.replyto.topic:001"); 155 this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE; 156 this.jmsRedelivered = true; 157 this.jmsType = "test type"; 158 this.jmsExpiration = 100000; 159 this.jmsPriority = 5; 160 this.jmsTimestamp = System.currentTimeMillis(); 161 this.readOnlyMessage = false; 162 163 ActiveMQMessage msg1 = new ActiveMQMessage(); 164 msg1.setJMSMessageID(this.jmsMessageID); 165 msg1.setJMSCorrelationID(this.jmsCorrelationID); 166 msg1.setJMSDestination(this.jmsDestination); 167 msg1.setJMSReplyTo(this.jmsReplyTo); 168 msg1.setJMSDeliveryMode(this.jmsDeliveryMode); 169 msg1.setJMSRedelivered(this.jmsRedelivered); 170 msg1.setJMSType(this.jmsType); 171 msg1.setJMSExpiration(this.jmsExpiration); 172 msg1.setJMSPriority(this.jmsPriority); 173 msg1.setJMSTimestamp(this.jmsTimestamp); 174 msg1.setReadOnlyProperties(true); 175 ActiveMQMessage msg2 = new ActiveMQMessage(); 176 msg1.copy(msg2); 177 assertTrue(msg1.getJMSMessageID().equals(msg2.getJMSMessageID())); 178 assertTrue(msg1.getJMSCorrelationID().equals(msg2.getJMSCorrelationID())); 179 assertTrue(msg1.getJMSDestination().equals(msg2.getJMSDestination())); 180 assertTrue(msg1.getJMSReplyTo().equals(msg2.getJMSReplyTo())); 181 assertTrue(msg1.getJMSDeliveryMode() == msg2.getJMSDeliveryMode()); 182 assertTrue(msg1.getJMSRedelivered() == msg2.getJMSRedelivered()); 183 assertTrue(msg1.getJMSType().equals(msg2.getJMSType())); 184 assertTrue(msg1.getJMSExpiration() == msg2.getJMSExpiration()); 185 assertTrue(msg1.getJMSPriority() == msg2.getJMSPriority()); 186 assertTrue(msg1.getJMSTimestamp() == msg2.getJMSTimestamp()); 187 188 log.info("Message is: " + msg1); 189 } 190 191 public void testGetAndSetJMSMessageID() throws Exception { 192 ActiveMQMessage msg = new ActiveMQMessage(); 193 msg.setJMSMessageID(this.jmsMessageID); 194 assertEquals(msg.getJMSMessageID(), this.jmsMessageID); 195 } 196 197 public void testGetAndSetJMSTimestamp() { 198 ActiveMQMessage msg = new ActiveMQMessage(); 199 msg.setJMSTimestamp(this.jmsTimestamp); 200 assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp); 201 } 202 203 public void testGetJMSCorrelationIDAsBytes() throws Exception { 204 ActiveMQMessage msg = new ActiveMQMessage(); 205 msg.setJMSCorrelationID(this.jmsCorrelationID); 206 byte[] testbytes = msg.getJMSCorrelationIDAsBytes(); 207 String str2 = new String (testbytes); 208 assertTrue(this.jmsCorrelationID.equals(str2)); 209 } 210 211 public void testSetJMSCorrelationIDAsBytes() throws Exception { 212 ActiveMQMessage msg = new ActiveMQMessage(); 213 byte[] testbytes = this.jmsCorrelationID.getBytes(); 214 msg.setJMSCorrelationIDAsBytes(testbytes); 215 testbytes = msg.getJMSCorrelationIDAsBytes(); 216 String str2 = new String (testbytes); 217 assertTrue(this.jmsCorrelationID.equals(str2)); 218 } 219 220 public void testGetAndSetJMSCorrelationID() { 221 ActiveMQMessage msg = new ActiveMQMessage(); 222 msg.setJMSCorrelationID(this.jmsCorrelationID); 223 assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID)); 224 } 225 226 public void testGetAndSetJMSReplyTo() throws JMSException { 227 ActiveMQMessage msg = new ActiveMQMessage(); 228 msg.setJMSReplyTo(this.jmsReplyTo); 229 assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo)); 230 } 231 232 public void testGetAndSetJMSDestination() throws Exception { 233 ActiveMQMessage msg = new ActiveMQMessage(); 234 msg.setJMSDestination(this.jmsDestination); 235 assertTrue(msg.getJMSDestination().equals(this.jmsDestination)); 236 } 237 238 public void testGetAndSetJMSDeliveryMode() { 239 ActiveMQMessage msg = new ActiveMQMessage(); 240 msg.setJMSDeliveryMode(this.jmsDeliveryMode); 241 assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode); 242 } 243 244 public void testGetAndSetMSRedelivered() { 245 ActiveMQMessage msg = new ActiveMQMessage(); 246 msg.setJMSRedelivered(this.jmsRedelivered); 247 assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered); 248 } 249 250 public void testGetAndSetJMSType() { 251 ActiveMQMessage msg = new ActiveMQMessage(); 252 msg.setJMSType(this.jmsType); 253 assertTrue(msg.getJMSType().equals(this.jmsType)); 254 } 255 256 public void testGetAndSetJMSExpiration() { 257 ActiveMQMessage msg = new ActiveMQMessage(); 258 msg.setJMSExpiration(this.jmsExpiration); 259 assertTrue(msg.getJMSExpiration() == this.jmsExpiration); 260 } 261 262 public void testGetAndSetJMSPriority() { 263 ActiveMQMessage msg = new ActiveMQMessage(); 264 msg.setJMSPriority(this.jmsPriority); 265 assertTrue(msg.getJMSPriority() == this.jmsPriority); 266 } 267 268 public void testClearProperties() throws JMSException { 269 ActiveMQMessage msg = new ActiveMQMessage(); 270 msg.setStringProperty("test", "test"); 271 msg.setContent(new ByteSequence(new byte[1],0,0)); 272 msg.setJMSMessageID(this.jmsMessageID); 273 msg.clearProperties(); 274 assertNull(msg.getStringProperty("test")); 275 assertNotNull(msg.getJMSMessageID()); 276 assertNotNull(msg.getContent()); 277 } 278 279 public void testPropertyExists() throws JMSException { 280 ActiveMQMessage msg = new ActiveMQMessage(); 281 msg.setStringProperty("test", "test"); 282 assertTrue(msg.propertyExists("test")); 283 } 284 285 public void testGetBooleanProperty() throws JMSException { 286 ActiveMQMessage msg = new ActiveMQMessage(); 287 String name = "booleanProperty"; 288 msg.setBooleanProperty(name, true); 289 assertTrue(msg.getBooleanProperty(name)); 290 } 291 292 public void testGetByteProperty() throws JMSException { 293 ActiveMQMessage msg = new ActiveMQMessage(); 294 String name = "byteProperty"; 295 msg.setByteProperty(name, (byte) 1); 296 assertTrue(msg.getByteProperty(name) == 1); 297 } 298 299 public void testGetShortProperty() throws JMSException { 300 ActiveMQMessage msg = new ActiveMQMessage(); 301 String name = "shortProperty"; 302 msg.setShortProperty(name, (short) 1); 303 assertTrue(msg.getShortProperty(name) == 1); 304 } 305 306 public void testGetIntProperty() throws JMSException { 307 ActiveMQMessage msg = new ActiveMQMessage(); 308 String name = "intProperty"; 309 msg.setIntProperty(name, 1); 310 assertTrue(msg.getIntProperty(name) == 1); 311 } 312 313 public void testGetLongProperty() throws JMSException { 314 ActiveMQMessage msg = new ActiveMQMessage(); 315 String name = "longProperty"; 316 msg.setLongProperty(name, 1); 317 assertTrue(msg.getLongProperty(name) == 1); 318 } 319 320 public void testGetFloatProperty() throws JMSException { 321 ActiveMQMessage msg = new ActiveMQMessage(); 322 String name = "floatProperty"; 323 msg.setFloatProperty(name, 1.3f); 324 assertTrue(msg.getFloatProperty(name) == 1.3f); 325 } 326 327 public void testGetDoubleProperty() throws JMSException { 328 ActiveMQMessage msg = new ActiveMQMessage(); 329 String name = "doubleProperty"; 330 msg.setDoubleProperty(name, 1.3d); 331 assertTrue(msg.getDoubleProperty(name) == 1.3); 332 } 333 334 public void testGetStringProperty() throws JMSException { 335 ActiveMQMessage msg = new ActiveMQMessage(); 336 String name = "stringProperty"; 337 msg.setStringProperty(name, name); 338 assertTrue(msg.getStringProperty(name).equals(name)); 339 } 340 341 public void testGetObjectProperty() throws JMSException { 342 ActiveMQMessage msg = new ActiveMQMessage(); 343 String name = "floatProperty"; 344 msg.setFloatProperty(name, 1.3f); 345 assertTrue(msg.getObjectProperty(name) instanceof Float ); 346 assertTrue(((Float ) msg.getObjectProperty(name)).floatValue() == 1.3f); 347 } 348 349 public void testGetPropertyNames() throws JMSException { 350 ActiveMQMessage msg = new ActiveMQMessage(); 351 String name = "floatProperty"; 352 msg.setFloatProperty(name, 1.3f); 353 for (Enumeration iter = msg.getPropertyNames(); iter.hasMoreElements();) { 354 assertTrue(iter.nextElement().equals(name)); 355 } 356 } 357 358 public void testSetObjectProperty() throws JMSException { 359 ActiveMQMessage msg = new ActiveMQMessage(); 360 String name = "property"; 361 362 try { 363 msg.setObjectProperty(name, "string"); 364 msg.setObjectProperty(name, Byte.valueOf("1")); 365 msg.setObjectProperty(name, Short.valueOf("1")); 366 msg.setObjectProperty(name, Integer.valueOf("1")); 367 msg.setObjectProperty(name, Long.valueOf("1")); 368 msg.setObjectProperty(name, Float.valueOf("1.1f")); 369 msg.setObjectProperty(name, Double.valueOf("1.1")); 370 msg.setObjectProperty(name, Boolean.TRUE); 371 msg.setObjectProperty(name, null); 372 } catch (MessageFormatException e) { 373 fail("should accept object primitives and String"); 374 } 375 try { 376 msg.setObjectProperty(name, new byte[5]); 377 fail("should accept only object primitives and String"); 378 } catch (MessageFormatException e) { 379 } 380 try { 381 msg.setObjectProperty(name, new Object ()); 382 fail("should accept only object primitives and String"); 383 } catch (MessageFormatException e) { 384 } 385 } 386 387 public void testConvertProperties() throws Exception { 388 org.apache.activemq.command.Message msg = new org.apache.activemq.command.Message() { 389 public org.apache.activemq.command.Message copy() { 390 return null; 391 } 392 public void beforeMarshall(WireFormat wireFormat) throws IOException { 393 super.beforeMarshall(wireFormat); 394 } 395 public byte getDataStructureType() { 396 return 0; 397 } 398 public Response visit(CommandVisitor visitor) throws Exception { 399 return null; 400 } 401 }; 402 403 msg.setProperty("stringProperty", "string"); 404 msg.setProperty("byteProperty", Byte.valueOf("1")); 405 msg.setProperty("shortProperty", Short.valueOf("1")); 406 msg.setProperty("intProperty", Integer.valueOf("1")); 407 msg.setProperty("longProperty", Long.valueOf("1")); 408 msg.setProperty("floatProperty", Float.valueOf("1.1f")); 409 msg.setProperty("doubleProperty", Double.valueOf("1.1")); 410 msg.setProperty("booleanProperty", Boolean.TRUE); 411 msg.setProperty("nullProperty", null); 412 413 msg.beforeMarshall(new OpenWireFormat()); 414 415 Map properties = msg.getProperties(); 416 assertEquals(properties.get("stringProperty"),"string"); 417 assertEquals(((Byte )properties.get("byteProperty")).byteValue(),1); 418 assertEquals(((Short )properties.get("shortProperty")).shortValue(),1); 419 assertEquals(((Integer )properties.get("intProperty")).intValue(),1); 420 assertEquals(((Long )properties.get("longProperty")).longValue(),1); 421 assertEquals(((Float )properties.get("floatProperty")).floatValue(),1.1f,0); 422 assertEquals(((Double )properties.get("doubleProperty")).doubleValue(),1.1,0); 423 assertEquals(((Boolean )properties.get("booleanProperty")).booleanValue(),true); 424 assertNull(properties.get("nullProperty")); 425 426 } 427 428 public void testSetNullProperty() throws JMSException { 429 Message msg = new ActiveMQMessage(); 430 String name = "cheese"; 431 msg.setStringProperty(name, "Cheddar"); 432 assertEquals("Cheddar", msg.getStringProperty(name)); 433 434 msg.setStringProperty(name, null); 435 assertEquals(null, msg.getStringProperty(name)); 436 } 437 438 public void testSetNullPropertyName() throws JMSException { 439 Message msg = new ActiveMQMessage(); 440 441 try { 442 msg.setStringProperty(null, "Cheese"); 443 fail("Should have thrown exception"); 444 } catch (IllegalArgumentException e) { 445 log.info("Worked, caught: " + e); 446 } 447 } 448 449 public void testSetEmptyPropertyName() throws JMSException { 450 Message msg = new ActiveMQMessage(); 451 452 try { 453 msg.setStringProperty("", "Cheese"); 454 fail("Should have thrown exception"); 455 } catch (IllegalArgumentException e) { 456 log.info("Worked, caught: " + e); 457 } 458 } 459 460 public void testGetAndSetJMSXDeliveryCount() throws JMSException { 461 Message msg = new ActiveMQMessage(); 462 msg.setIntProperty("JMSXDeliveryCount", 1); 463 int count = msg.getIntProperty("JMSXDeliveryCount"); 464 assertTrue("expected delivery count = 1 - got: " + count, count == 1); 465 } 466 467 public void testClearBody() throws JMSException { 468 ActiveMQBytesMessage message = new ActiveMQBytesMessage(); 469 message.clearBody(); 470 assertFalse(message.isReadOnlyBody()); 471 assertNull(message.getContent()); 472 } 473 474 public void testBooleanPropertyConversion() throws JMSException { 475 ActiveMQMessage msg = new ActiveMQMessage(); 476 String propertyName = "property"; 477 msg.setBooleanProperty(propertyName, true); 478 479 assertEquals(((Boolean ) msg.getObjectProperty(propertyName)).booleanValue(), true); 480 assertTrue(msg.getBooleanProperty(propertyName)); 481 assertEquals(msg.getStringProperty(propertyName), "true"); 482 try { 483 msg.getByteProperty(propertyName); 484 fail("Should have thrown exception"); 485 } catch (MessageFormatException e) { 486 } 487 try { 488 msg.getShortProperty(propertyName); 489 fail("Should have thrown exception"); 490 } catch (MessageFormatException e) { 491 } 492 try { 493 msg.getIntProperty(propertyName); 494 fail("Should have thrown exception"); 495 } catch (MessageFormatException e) { 496 } 497 try { 498 msg.getLongProperty(propertyName); 499 fail("Should have thrown exception"); 500 } catch (MessageFormatException e) { 501 } 502 try { 503 msg.getFloatProperty(propertyName); 504 fail("Should have thrown exception"); 505 } catch (MessageFormatException e) { 506 } 507 try { 508 msg.getDoubleProperty(propertyName); 509 fail("Should have thrown exception"); 510 } catch (MessageFormatException e) { 511 } 512 } 513 514 public void testBytePropertyConversion() throws JMSException { 515 ActiveMQMessage msg = new ActiveMQMessage(); 516 String propertyName = "property"; 517 msg.setByteProperty(propertyName, (byte) 1); 518 519 assertEquals(((Byte ) msg.getObjectProperty(propertyName)).byteValue(), 1); 520 assertEquals(msg.getByteProperty(propertyName), 1); 521 assertEquals(msg.getShortProperty(propertyName), 1); 522 assertEquals(msg.getIntProperty(propertyName), 1); 523 assertEquals(msg.getLongProperty(propertyName), 1); 524 assertEquals(msg.getStringProperty(propertyName), "1"); 525 try { 526 msg.getBooleanProperty(propertyName); 527 fail("Should have thrown exception"); 528 } catch (MessageFormatException e) { 529 } 530 try { 531 msg.getFloatProperty(propertyName); 532 fail("Should have thrown exception"); 533 } catch (MessageFormatException e) { 534 } 535 try { 536 msg.getDoubleProperty(propertyName); 537 fail("Should have thrown exception"); 538 } catch (MessageFormatException e) { 539 } 540 } 541 542 public void testShortPropertyConversion() throws JMSException { 543 ActiveMQMessage msg = new ActiveMQMessage(); 544 String propertyName = "property"; 545 msg.setShortProperty(propertyName, (short) 1); 546 547 assertEquals(((Short ) msg.getObjectProperty(propertyName)).shortValue(), 1); 548 assertEquals(msg.getShortProperty(propertyName), 1); 549 assertEquals(msg.getIntProperty(propertyName), 1); 550 assertEquals(msg.getLongProperty(propertyName), 1); 551 assertEquals(msg.getStringProperty(propertyName), "1"); 552 try { 553 msg.getBooleanProperty(propertyName); 554 fail("Should have thrown exception"); 555 } catch (MessageFormatException e) { 556 } 557 try { 558 msg.getByteProperty(propertyName); 559 fail("Should have thrown exception"); 560 } catch (MessageFormatException e) { 561 } 562 try { 563 msg.getFloatProperty(propertyName); 564 fail("Should have thrown exception"); 565 } catch (MessageFormatException e) { 566 } 567 try { 568 msg.getDoubleProperty(propertyName); 569 fail("Should have thrown exception"); 570 } catch (MessageFormatException e) { 571 } 572 } 573 574 public void testIntPropertyConversion() throws JMSException { 575 ActiveMQMessage msg = new ActiveMQMessage(); 576 String propertyName = "property"; 577 msg.setIntProperty(propertyName, (int) 1); 578 579 assertEquals(((Integer ) msg.getObjectProperty(propertyName)).intValue(), 1); 580 assertEquals(msg.getIntProperty(propertyName), 1); 581 assertEquals(msg.getLongProperty(propertyName), 1); 582 assertEquals(msg.getStringProperty(propertyName), "1"); 583 try { 584 msg.getBooleanProperty(propertyName); 585 fail("Should have thrown exception"); 586 } catch (MessageFormatException e) { 587 } 588 try { 589 msg.getByteProperty(propertyName); 590 fail("Should have thrown exception"); 591 } catch (MessageFormatException e) { 592 } 593 try { 594 msg.getShortProperty(propertyName); 595 fail("Should have thrown exception"); 596 } catch (MessageFormatException e) { 597 } 598 try { 599 msg.getFloatProperty(propertyName); 600 fail("Should have thrown exception"); 601 } catch (MessageFormatException e) { 602 } 603 try { 604 msg.getDoubleProperty(propertyName); 605 fail("Should have thrown exception"); 606 } catch (MessageFormatException e) { 607 } 608 } 609 610 public void testLongPropertyConversion() throws JMSException { 611 ActiveMQMessage msg = new ActiveMQMessage(); 612 String propertyName = "property"; 613 msg.setLongProperty(propertyName, 1); 614 615 assertEquals(((Long ) msg.getObjectProperty(propertyName)).longValue(), 1); 616 assertEquals(msg.getLongProperty(propertyName), 1); 617 assertEquals(msg.getStringProperty(propertyName), "1"); 618 try { 619 msg.getBooleanProperty(propertyName); 620 fail("Should have thrown exception"); 621 } catch (MessageFormatException e) { 622 } 623 try { 624 msg.getByteProperty(propertyName); 625 fail("Should have thrown exception"); 626 } catch (MessageFormatException e) { 627 } 628 try { 629 msg.getShortProperty(propertyName); 630 fail("Should have thrown exception"); 631 } catch (MessageFormatException e) { 632 } 633 try { 634 msg.getIntProperty(propertyName); 635 fail("Should have thrown exception"); 636 } catch (MessageFormatException e) { 637 } 638 try { 639 msg.getFloatProperty(propertyName); 640 fail("Should have thrown exception"); 641 } catch (MessageFormatException e) { 642 } 643 try { 644 msg.getDoubleProperty(propertyName); 645 fail("Should have thrown exception"); 646 } catch (MessageFormatException e) { 647 } 648 } 649 650 public void testFloatPropertyConversion() throws JMSException { 651 ActiveMQMessage msg = new ActiveMQMessage(); 652 String propertyName = "property"; 653 msg.setFloatProperty(propertyName, (float) 1.5); 654 assertEquals(((Float ) msg.getObjectProperty(propertyName)).floatValue(), 1.5, 0); 655 assertEquals(msg.getFloatProperty(propertyName), 1.5, 0); 656 assertEquals(msg.getDoubleProperty(propertyName), 1.5, 0); 657 assertEquals(msg.getStringProperty(propertyName), "1.5"); 658 try { 659 msg.getBooleanProperty(propertyName); 660 fail("Should have thrown exception"); 661 } catch (MessageFormatException e) { 662 } 663 try { 664 msg.getByteProperty(propertyName); 665 fail("Should have thrown exception"); 666 } catch (MessageFormatException e) { 667 } 668 try { 669 msg.getShortProperty(propertyName); 670 fail("Should have thrown exception"); 671 } catch (MessageFormatException e) { 672 } 673 try { 674 msg.getIntProperty(propertyName); 675 fail("Should have thrown exception"); 676 } catch (MessageFormatException e) { 677 } 678 try { 679 msg.getLongProperty(propertyName); 680 fail("Should have thrown exception"); 681 } catch (MessageFormatException e) { 682 } 683 } 684 685 public void testDoublePropertyConversion() throws JMSException { 686 ActiveMQMessage msg = new ActiveMQMessage(); 687 String propertyName = "property"; 688 msg.setDoubleProperty(propertyName, 1.5); 689 assertEquals(((Double ) msg.getObjectProperty(propertyName)).doubleValue(), 1.5, 0); 690 assertEquals(msg.getDoubleProperty(propertyName), 1.5, 0); 691 assertEquals(msg.getStringProperty(propertyName), "1.5"); 692 try { 693 msg.getBooleanProperty(propertyName); 694 fail("Should have thrown exception"); 695 } catch (MessageFormatException e) { 696 } 697 try { 698 msg.getByteProperty(propertyName); 699 fail("Should have thrown exception"); 700 } catch (MessageFormatException e) { 701 } 702 try { 703 msg.getShortProperty(propertyName); 704 fail("Should have thrown exception"); 705 } catch (MessageFormatException e) { 706 } 707 try { 708 msg.getIntProperty(propertyName); 709 fail("Should have thrown exception"); 710 } catch (MessageFormatException e) { 711 } 712 try { 713 msg.getLongProperty(propertyName); 714 fail("Should have thrown exception"); 715 } catch (MessageFormatException e) { 716 } 717 try { 718 msg.getFloatProperty(propertyName); 719 fail("Should have thrown exception"); 720 } catch (MessageFormatException e) { 721 } 722 } 723 724 public void testStringPropertyConversion() throws JMSException { 725 ActiveMQMessage msg = new ActiveMQMessage(); 726 String propertyName = "property"; 727 String stringValue = "true"; 728 msg.setStringProperty(propertyName, stringValue); 729 assertEquals(msg.getStringProperty(propertyName), stringValue); 730 assertEquals((String ) msg.getObjectProperty(propertyName), stringValue); 731 assertEquals(msg.getBooleanProperty(propertyName), true); 732 733 stringValue = "1"; 734 msg.setStringProperty(propertyName, stringValue); 735 assertEquals(msg.getByteProperty(propertyName), 1); 736 assertEquals(msg.getShortProperty(propertyName), 1); 737 assertEquals(msg.getIntProperty(propertyName), 1); 738 assertEquals(msg.getLongProperty(propertyName), 1); 739 740 stringValue = "1.5"; 741 msg.setStringProperty(propertyName, stringValue); 742 assertEquals(msg.getFloatProperty(propertyName), 1.5, 0); 743 assertEquals(msg.getDoubleProperty(propertyName), 1.5, 0); 744 745 stringValue = "bad"; 746 msg.setStringProperty(propertyName, stringValue); 747 try { 748 msg.getByteProperty(propertyName); 749 fail("Should have thrown exception"); 750 } catch (NumberFormatException e) { 751 } 752 try { 753 msg.getShortProperty(propertyName); 754 fail("Should have thrown exception"); 755 } catch (NumberFormatException e) { 756 } 757 try { 758 msg.getIntProperty(propertyName); 759 fail("Should have thrown exception"); 760 } catch (NumberFormatException e) { 761 } 762 try { 763 msg.getLongProperty(propertyName); 764 fail("Should have thrown exception"); 765 } catch (NumberFormatException e) { 766 } 767 try { 768 msg.getFloatProperty(propertyName); 769 fail("Should have thrown exception"); 770 } catch (NumberFormatException e) { 771 } 772 try { 773 msg.getDoubleProperty(propertyName); 774 fail("Should have thrown exception"); 775 } catch (NumberFormatException e) { 776 } 777 assertFalse(msg.getBooleanProperty(propertyName)); 778 } 779 780 public void testObjectPropertyConversion() throws JMSException { 781 ActiveMQMessage msg = new ActiveMQMessage(); 782 String propertyName = "property"; 783 Object obj = new Object (); 784 try { 785 ((org.apache.activemq.command.Message)msg).setProperty(propertyName, obj); } catch (IOException e) { 787 } 788 try { 789 msg.getStringProperty(propertyName); 790 fail("Should have thrown exception"); 791 } catch (MessageFormatException e) { 792 } 793 try { 794 msg.getBooleanProperty(propertyName); 795 fail("Should have thrown exception"); 796 } catch (MessageFormatException e) { 797 } 798 try { 799 msg.getByteProperty(propertyName); 800 fail("Should have thrown exception"); 801 } catch (MessageFormatException e) { 802 } 803 try { 804 msg.getShortProperty(propertyName); 805 fail("Should have thrown exception"); 806 } catch (MessageFormatException e) { 807 } 808 try { 809 msg.getIntProperty(propertyName); 810 fail("Should have thrown exception"); 811 } catch (MessageFormatException e) { 812 } 813 try { 814 msg.getLongProperty(propertyName); 815 fail("Should have thrown exception"); 816 } catch (MessageFormatException e) { 817 } 818 try { 819 msg.getFloatProperty(propertyName); 820 fail("Should have thrown exception"); 821 } catch (MessageFormatException e) { 822 } 823 try { 824 msg.getDoubleProperty(propertyName); 825 fail("Should have thrown exception"); 826 } catch (MessageFormatException e) { 827 } 828 829 } 830 831 public void testReadOnlyProperties() throws JMSException { 832 ActiveMQMessage msg = new ActiveMQMessage(); 833 String propertyName = "property"; 834 msg.setReadOnlyProperties(true); 835 836 try { 837 msg.setObjectProperty(propertyName, new Object ()); 838 fail("Should have thrown exception"); 839 } catch (MessageNotWriteableException e) { 840 } 841 try { 842 msg.setStringProperty(propertyName, "test"); 843 fail("Should have thrown exception"); 844 } catch (MessageNotWriteableException e) { 845 } 846 try { 847 msg.setBooleanProperty(propertyName, true); 848 fail("Should have thrown exception"); 849 } catch (MessageNotWriteableException e) { 850 } 851 try { 852 msg.setByteProperty(propertyName, (byte) 1); 853 fail("Should have thrown exception"); 854 } catch (MessageNotWriteableException e) { 855 } 856 try { 857 msg.setShortProperty(propertyName, (short) 1); 858 fail("Should have thrown exception"); 859 } catch (MessageNotWriteableException e) { 860 } 861 try { 862 msg.setIntProperty(propertyName, 1); 863 fail("Should have thrown exception"); 864 } catch (MessageNotWriteableException e) { 865 } 866 try { 867 msg.setLongProperty(propertyName, 1); 868 fail("Should have thrown exception"); 869 } catch (MessageNotWriteableException e) { 870 } 871 try { 872 msg.setFloatProperty(propertyName, (float) 1.5); 873 fail("Should have thrown exception"); 874 } catch (MessageNotWriteableException e) { 875 } 876 try { 877 msg.setDoubleProperty(propertyName, 1.5); 878 fail("Should have thrown exception"); 879 } catch (MessageNotWriteableException e) { 880 } 881 } 882 883 public void testIsExpired() { 884 ActiveMQMessage msg = new ActiveMQMessage(); 885 msg.setJMSExpiration(System.currentTimeMillis() - 1); 886 assertTrue(msg.isExpired()); 887 msg.setJMSExpiration(System.currentTimeMillis() + 10000); 888 assertFalse(msg.isExpired()); 889 } 890 891 } 892 | Popular Tags |