    1   /*
    2    * Created on Mar 3, 2004
    3    *
    4    * To change the template for this generated file go to
    5    * Window - Preferences - Java - Code Generation - Code and Comments
    6    */
    7   package org.codehaus.activemq.message;
    8
    9   import junit.framework.TestCase;
    10
    11  import javax.jms.JMSException;
    12  import javax.jms.Message;
    13  import javax.jms.MessageNotWriteableException;
    14  import java.util.Enumeration;
    15  import java.util.Hashtable;
    16
    17  /***
    18   * To change the template for this generated type comment go to
    19   * Window - Preferences - Java - Code Generation - Code and Comments
    20   */
    21  public class ActiveMQMessageTest extends TestCase {
    22      private String jmsMessageID;
    23      private String jmsClientID;
    24      private String jmsCorrelationID;
    25      private ActiveMQDestination jmsDestination;
    26      private ActiveMQDestination jmsReplyTo;
    27      private int jmsDeliveryMode;
    28      private boolean jmsRedelivered;
    29      private String jmsType;
    30      private long jmsExpiration;
    31      private int jmsPriority;
    32      private long jmsTimestamp;
    33      private Hashtable properties;
    34      private boolean readOnlyProperties;
    35      protected boolean readOnlyMessage;
    36      private String entryBrokerName;
    37      private String entryClusterName;
    38      private int[] consumerIDs;
    39
    40
    41      public static void main(String[] args) {
    42      }
    43
    44      /*
    45       * @see TestCase#setUp()
    46       */
    47      protected void setUp() throws Exception {
    48          super.setUp();
    49          this.jmsMessageID = "testid";
    50          this.jmsClientID = "testclientid";
    51          this.jmsCorrelationID = "testcorrelationid";
    52          this.jmsDestination = new ActiveMQTopic("test.topic");
    53          this.jmsReplyTo = new ActiveMQTemporaryTopic("test.replyto.topic");
    54          this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
    55          this.jmsRedelivered = true;
    56          this.jmsType = "test type";
    57          this.jmsExpiration = 100000;
    58          this.jmsPriority = 5;
    59          this.jmsTimestamp = System.currentTimeMillis();
    60          this.properties = new Hashtable();
    61          this.readOnlyProperties = false;
    62          this.readOnlyMessage = false;
    63          this.entryBrokerName = "test broker";
    64          this.entryClusterName = "test cluster";
    65          this.consumerIDs = new int[3];
    66          for (int i = 0; i < this.consumerIDs.length; i++) {
    67              this.consumerIDs[i] = i;
    68          }
    69
    70      }
    71
    72      /*
    73       * @see TestCase#tearDown()
    74       */
    75      protected void tearDown() throws Exception {
    76          super.tearDown();
    77      }
    78
    79      /***
    80       * Constructor for ActiveMQMessageTest.
    81       *
    82       * @param arg0
    83       */
    84      public ActiveMQMessageTest(String arg0) {
    85          super(arg0);
    86      }
    87
    88      public void testHashCode() {
    89          ActiveMQMessage msg = new ActiveMQMessage();
    90          assertTrue(msg.hashCode() > 0);
    91          msg.setJMSMessageID(this.jmsMessageID);
    92          assertTrue(msg.hashCode() == jmsMessageID.hashCode());
    93      }
    94
    95      public void testGetPacketType() {
    96          ActiveMQMessage msg = new ActiveMQMessage();
    97          assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_MESSAGE);
    98      }
    99
    100     public void testSetReadOnly() {
    101         ActiveMQMessage msg = new ActiveMQMessage();
    102         msg.setReadOnly(true);
    103         boolean test = false;
    104         try {
    105             msg.setIntProperty("test", 1);
    106         }
    107         catch (MessageNotWriteableException me) {
    108             test = true;
    109         }
    110         catch (JMSException e) {
    111             e.printStackTrace(System.err);
    112             test = false;
    113         }
    114         assertTrue(test);
    115     }
    116
    117     /*
    118      * Class to test for boolean equals(Object)
    119      */
    120     public void testEqualsObject() {
    121         ActiveMQMessage msg1 = new ActiveMQMessage();
    122         ActiveMQMessage msg2 = new ActiveMQMessage();
    123         msg1.setJMSMessageID(this.jmsMessageID);
    124         assertTrue(!msg1.equals(msg2));
    125         msg2.setJMSMessageID(this.jmsMessageID);
    126         assertTrue(msg1.equals(msg2));
    127     }
    128
    129     public void testShallowCopy() throws Exception {
    130         ActiveMQMessage msg1 = new ActiveMQMessage();
    131         msg1.setJMSMessageID(jmsMessageID);
    132         ActiveMQMessage msg2 = msg1.shallowCopy();
    133         assertTrue(msg1 != msg2 && msg1.equals(msg2));
    134     }
    135
    136     public void testInitializeOther() {
    137         this.jmsMessageID = "testid";
    138         this.jmsClientID = "testclientid";
    139         this.jmsCorrelationID = "testcorrelationid";
    140         this.jmsDestination = new ActiveMQTopic("test.topic");
    141         this.jmsReplyTo = new ActiveMQTemporaryTopic("test.replyto.topic");
    142         this.jmsDeliveryMode = Message.DEFAULT_DELIVERY_MODE;
    143         this.jmsRedelivered = true;
    144         this.jmsType = "test type";
    145         this.jmsExpiration = 100000;
    146         this.jmsPriority = 5;
    147         this.jmsTimestamp = System.currentTimeMillis();
    148         this.properties = new Hashtable();
    149         this.readOnlyProperties = false;
    150         this.readOnlyMessage = false;
    151         this.entryBrokerName = "test broker";
    152         this.entryClusterName = "test cluster";
    153         this.consumerIDs = new int[3];
    154
    155         ActiveMQMessage msg1 = new ActiveMQMessage();
    156         msg1.setJMSMessageID(this.jmsMessageID);
    157         msg1.setJMSClientID(this.jmsClientID);
    158         msg1.setJMSCorrelationID(this.jmsCorrelationID);
    159         msg1.setJMSDestination(this.jmsDestination);
    160         msg1.setJMSReplyTo(this.jmsReplyTo);
    161         msg1.setJMSDeliveryMode(this.jmsDeliveryMode);
    162         msg1.setJMSRedelivered(this.jmsRedelivered);
    163         msg1.setJMSType(this.jmsType);
    164         msg1.setJMSExpiration(this.jmsExpiration);
    165         msg1.setJMSPriority(this.jmsPriority);
    166         msg1.setJMSTimestamp(this.jmsTimestamp);
    167         msg1.setProperties(this.properties);
    168         msg1.setReadOnly(true);
    169         msg1.setEntryBrokerName(this.entryBrokerName);
    170         msg1.setEntryClusterName(this.entryClusterName);
    171         msg1.setConsumerNos(this.consumerIDs);
    172         ActiveMQMessage msg2 = new ActiveMQMessage();
    173         msg1.initializeOther(msg2);
    174         assertTrue(msg1.getJMSMessageID().equals(msg2.getJMSMessageID()));
    175         assertTrue(msg1.getJMSClientID().equals(msg2.getJMSClientID()));
    176         assertTrue(msg1.getJMSCorrelationID().equals(msg2.getJMSCorrelationID()));
    177         assertTrue(msg1.getJMSDestination().equals(msg2.getJMSDestination()));
    178         assertTrue(msg1.getJMSReplyTo().equals(msg2.getJMSReplyTo()));
    179         assertTrue(msg1.getJMSDeliveryMode() == msg2.getJMSDeliveryMode());
    180         assertTrue(msg1.getJMSRedelivered() == msg2.getJMSRedelivered());
    181         assertTrue(msg1.getJMSType().equals(msg2.getJMSType()));
    182         assertTrue(msg1.getJMSExpiration() == msg2.getJMSExpiration());
    183         assertTrue(msg1.getJMSPriority() == msg2.getJMSPriority());
    184         assertTrue(msg1.getJMSTimestamp() == msg2.getJMSTimestamp());
    185         assertTrue(msg1.getProperties().equals(msg2.getProperties()));
    186         assertTrue(msg1.getEntryBrokerName().equals(msg2.getEntryBrokerName()));
    187         assertTrue(msg1.getEntryClusterName().equals(msg2.getEntryClusterName()));
    188         assertTrue(msg1.getConsumerNos().equals(msg2.getConsumerNos()));
    189
    190     }
    191
    192     public void testGetJMSMessageID() {
    193         ActiveMQMessage msg = new ActiveMQMessage();
    194         msg.setJMSMessageID(this.jmsMessageID);
    195         assertTrue(msg.getJMSMessageID() == this.jmsMessageID);
    196     }
    197
    198     public void testSetJMSMessageID() {
    199
    200         ActiveMQMessage msg = new ActiveMQMessage();
    201         msg.setJMSMessageID(this.jmsMessageID);
    202         assertTrue(msg.getJMSMessageID() == this.jmsMessageID);
    203     }
    204
    205     public void testGetJMSTimestamp() {
    206         ActiveMQMessage msg = new ActiveMQMessage();
    207         msg.setJMSTimestamp(this.jmsTimestamp);
    208         assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp);
    209     }
    210
    211     public void testSetJMSTimestamp() {
    212         ActiveMQMessage msg = new ActiveMQMessage();
    213         msg.setJMSTimestamp(this.jmsTimestamp);
    214         assertTrue(msg.getJMSTimestamp() == this.jmsTimestamp);
    215     }
    216
    217     public void testGetJMSCorrelationIDAsBytes() {
    218         ActiveMQMessage msg = new ActiveMQMessage();
    219         msg.setJMSCorrelationID(this.jmsCorrelationID);
    220         byte[] testbytes = msg.getJMSCorrelationIDAsBytes();
    221         String str2 = new String(testbytes);
    222         assertTrue(this.jmsCorrelationID.equals(str2));
    223     }
    224
    225     public void testSetJMSCorrelationIDAsBytes() {
    226         ActiveMQMessage msg = new ActiveMQMessage();
    227         byte[] testbytes = this.jmsCorrelationID.getBytes();
    228         msg.setJMSCorrelationIDAsBytes(testbytes);
    229         testbytes = msg.getJMSCorrelationIDAsBytes();
    230         String str2 = new String(testbytes);
    231         assertTrue(this.jmsCorrelationID.equals(str2));
    232     }
    233
    234     public void testSetJMSCorrelationID() {
    235         ActiveMQMessage msg = new ActiveMQMessage();
    236         msg.setJMSCorrelationID(this.jmsCorrelationID);
    237         assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID));
    238     }
    239
    240     public void testGetJMSCorrelationID() {
    241         ActiveMQMessage msg = new ActiveMQMessage();
    242         msg.setJMSCorrelationID(this.jmsCorrelationID);
    243         assertTrue(msg.getJMSCorrelationID().equals(this.jmsCorrelationID));
    244     }
    245
    246     public void testGetJMSReplyTo() {
    247         ActiveMQMessage msg = new ActiveMQMessage();
    248         msg.setJMSReplyTo(this.jmsReplyTo);
    249         assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo));
    250     }
    251
    252     public void testSetJMSReplyTo() {
    253         ActiveMQMessage msg = new ActiveMQMessage();
    254         msg.setJMSReplyTo(this.jmsReplyTo);
    255         assertTrue(msg.getJMSReplyTo().equals(this.jmsReplyTo));
    256     }
    257
    258     public void testGetJMSDestination() {
    259         ActiveMQMessage msg = new ActiveMQMessage();
    260         msg.setJMSDestination(this.jmsDestination);
    261         assertTrue(msg.getJMSDestination().equals(this.jmsDestination));
    262     }
    263
    264     public void testSetJMSDestination() {
    265         ActiveMQMessage msg = new ActiveMQMessage();
    266         msg.setJMSDestination(this.jmsDestination);
    267         assertTrue(msg.getJMSDestination().equals(this.jmsDestination));
    268     }
    269
    270     public void testGetJMSDeliveryMode() {
    271         ActiveMQMessage msg = new ActiveMQMessage();
    272         msg.setJMSDeliveryMode(this.jmsDeliveryMode);
    273         assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode);
    274     }
    275
    276     public void testSetJMSDeliveryMode() {
    277         ActiveMQMessage msg = new ActiveMQMessage();
    278         msg.setJMSDeliveryMode(this.jmsDeliveryMode);
    279         assertTrue(msg.getJMSDeliveryMode() == this.jmsDeliveryMode);
    280     }
    281
    282     public void testGetJMSRedelivered() {
    283         ActiveMQMessage msg = new ActiveMQMessage();
    284         msg.setJMSRedelivered(this.jmsRedelivered);
    285         assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered);
    286     }
    287
    288     public void testSetJMSRedelivered() {
    289         ActiveMQMessage msg = new ActiveMQMessage();
    290         msg.setJMSRedelivered(this.jmsRedelivered);
    291         assertTrue(msg.getJMSRedelivered() == this.jmsRedelivered);
    292     }
    293
    294     public void testGetJMSType() {
    295         ActiveMQMessage msg = new ActiveMQMessage();
    296         msg.setJMSType(this.jmsType);
    297         assertTrue(msg.getJMSType().equals(this.jmsType));
    298     }
    299
    300     public void testSetJMSType() {
    301         ActiveMQMessage msg = new ActiveMQMessage();
    302         msg.setJMSType(this.jmsType);
    303         assertTrue(msg.getJMSType().equals(this.jmsType));
    304     }
    305
    306     public void testGetJMSExpiration() {
    307         ActiveMQMessage msg = new ActiveMQMessage();
    308         msg.setJMSExpiration(this.jmsExpiration);
    309         assertTrue(msg.getJMSExpiration() == this.jmsExpiration);
    310     }
    311
    312     public void testSetJMSExpiration() {
    313         ActiveMQMessage msg = new ActiveMQMessage();
    314         msg.setJMSExpiration(this.jmsExpiration);
    315         assertTrue(msg.getJMSExpiration() == this.jmsExpiration);
    316     }
    317
    318     public void testGetJMSPriority() {
    319         ActiveMQMessage msg = new ActiveMQMessage();
    320         msg.setJMSPriority(this.jmsPriority);
    321         assertTrue(msg.getJMSPriority() == this.jmsPriority);
    322     }
    323
    324     public void testSetJMSPriority() {
    325         ActiveMQMessage msg = new ActiveMQMessage();
    326         msg.setJMSPriority(this.jmsPriority);
    327         assertTrue(msg.getJMSPriority() == this.jmsPriority);
    328     }
    329
    330     public void testClearProperties() {
    331         Hashtable props = new Hashtable();
    332         props.put("test", new Integer(1));
    333         ActiveMQMessage msg = new ActiveMQMessage();
    334         msg.setProperties(props);
    335         msg.clearProperties();
    336         assertTrue(props.size() == 0);
    337     }
    338
    339     public void testPropertyExists() {
    340         Hashtable props = new Hashtable();
    341         props.put("test", new Integer(1));
    342         ActiveMQMessage msg = new ActiveMQMessage();
    343         msg.setProperties(props);
    344         msg.clearProperties();
    345         assertTrue(props.size() == 0);
    346     }
    347
    348     public void testGetBooleanProperty() {
    349         ActiveMQMessage msg = new ActiveMQMessage();
    350         try {
    351             String name = "booleanProperty";
    352             msg.setBooleanProperty(name, true);
    353             assertTrue(msg.getBooleanProperty(name));
    354         }
    355         catch (JMSException e) {
    356             e.printStackTrace();
    357         }
    358     }
    359
    360     public void testGetByteProperty() {
    361         ActiveMQMessage msg = new ActiveMQMessage();
    362         try {
    363             String name = "byteProperty";
    364             msg.setByteProperty(name, (byte) 1);
    365             assertTrue(msg.getByteProperty(name) == 1);
    366         }
    367         catch (JMSException e) {
    368             e.printStackTrace();
    369         }
    370     }
    371
    372     public void testGetShortProperty() {
    373         ActiveMQMessage msg = new ActiveMQMessage();
    374         try {
    375             String name = "shortProperty";
    376             msg.setShortProperty(name, (short) 1);
    377             assertTrue(msg.getShortProperty(name) == 1);
    378         }
    379         catch (JMSException e) {
    380             e.printStackTrace();
    381         }
    382     }
    383
    384     public void testGetIntProperty() {
    385         ActiveMQMessage msg = new ActiveMQMessage();
    386         try {
    387             String name = "intProperty";
    388             msg.setIntProperty(name, 1);
    389             assertTrue(msg.getIntProperty(name) == 1);
    390         }
    391         catch (JMSException e) {
    392             e.printStackTrace();
    393         }
    394     }
    395
    396     public void testGetLongProperty() {
    397         ActiveMQMessage msg = new ActiveMQMessage();
    398         try {
    399             String name = "longProperty";
    400             msg.setLongProperty(name, 1);
    401             assertTrue(msg.getLongProperty(name) == 1);
    402         }
    403         catch (JMSException e) {
    404             e.printStackTrace();
    405         }
    406     }
    407
    408     public void testGetFloatProperty() {
    409         ActiveMQMessage msg = new ActiveMQMessage();
    410         try {
    411             String name = "floatProperty";
    412             msg.setFloatProperty(name, 1.3f);
    413             assertTrue(msg.getFloatProperty(name) == 1.3f);
    414         }
    415         catch (JMSException e) {
    416             e.printStackTrace();
    417         }
    418     }
    419
    420     public void testGetDoubleProperty() {
    421         ActiveMQMessage msg = new ActiveMQMessage();
    422         try {
    423             String name = "doubleProperty";
    424             msg.setDoubleProperty(name, 1.3d);
    425             assertTrue(msg.getDoubleProperty(name) == 1.3);
    426         }
    427         catch (JMSException e) {
    428             e.printStackTrace();
    429         }
    430     }
    431
    432     public void testGetStringProperty() {
    433         ActiveMQMessage msg = new ActiveMQMessage();
    434         try {
    435             String name = "stringProperty";
    436             msg.setStringProperty(name, name);
    437             assertTrue(msg.getStringProperty(name).equals(name));
    438         }
    439         catch (JMSException e) {
    440             e.printStackTrace();
    441         }
    442     }
    443
    444     public void testGetObjectProperty() {
    445         ActiveMQMessage msg = new ActiveMQMessage();
    446         try {
    447             String name = "floatProperty";
    448             msg.setFloatProperty(name, 1.3f);
    449             assertTrue(msg.getObjectProperty(name) instanceof Float);
    450             assertTrue(((Float) msg.getObjectProperty(name)).floatValue() == 1.3f);
    451         }
    452         catch (JMSException e) {
    453             e.printStackTrace();
    454         }
    455     }
    456
    457     public void testGetPropertyNames() {
    458         ActiveMQMessage msg = new ActiveMQMessage();
    459         try {
    460             String name = "floatProperty";
    461             msg.setFloatProperty(name, 1.3f);
    462             for (Enumeration iter = msg.getPropertyNames(); iter.hasMoreElements();) {
    463                 assertTrue(iter.nextElement().equals(name));
    464             }
    465         }
    466         catch (JMSException e) {
    467             e.printStackTrace();
    468         }
    469     }
    470
    471     public void testGetProperties() {
    472         ActiveMQMessage msg = new ActiveMQMessage();
    473         try {
    474             String name = "floatProperty";
    475             msg.setFloatProperty(name, 1.3f);
    476             Hashtable props = msg.getProperties();
    477             assertTrue(props.get(name) instanceof Float);
    478             assertTrue(((Float) props.get(name)).floatValue() == 1.3f);
    479         }
    480         catch (JMSException e) {
    481             e.printStackTrace();
    482         }
    483     }
    484
    485
    486     public void testSetObjectProperty() {
    487         ActiveMQMessage msg = new ActiveMQMessage();
    488         try {
    489             String name = "floatProperty";
    490             msg.setFloatProperty(name, 1.3f);
    491             Hashtable props = msg.getProperties();
    492             assertTrue(props.get(name) instanceof Float);
    493             assertTrue(((Float) props.get(name)).floatValue() == 1.3f);
    494         }
    495         catch (JMSException e) {
    496             e.printStackTrace();
    497         }
    498     }
    499
    500     public void testAcknowledge() {
    501         //TODO Implement acknowledge().
    502     }
    503
    504     public void testClearBody() {
    505         //TODO Implement clearBody().
    506     }
    507
    508
    509     public void testGetEntryBrokerName() {
    510         ActiveMQMessage msg = new ActiveMQMessage();
    511         msg.setEntryBrokerName(this.entryBrokerName);
    512         assertTrue(msg.getEntryBrokerName() == this.entryBrokerName);
    513     }
    514
    515
    516     public void testGetEntryClusterName() {
    517         ActiveMQMessage msg = new ActiveMQMessage();
    518         msg.setEntryClusterName(this.entryClusterName);
    519         assertTrue(msg.getEntryClusterName() == this.entryClusterName);
    520     }
    521
    522
    523     public void testGetConsumerIDs() {
    524         ActiveMQMessage msg = new ActiveMQMessage();
    525         msg.setConsumerNos(this.consumerIDs);
    526         assertTrue(msg.getConsumerNos() == this.consumerIDs);
    527     }
    528
    529 }