ActiveMQMessageReaderTest xref

    1   /*
    2    * Created on Mar 9, 2004 To change the template for this generated file go to Window - Preferences - Java - Code
    3    * Generation - Code and Comments
    4    */
    5   package org.codehaus.activemq.message;
    6
    7   import java.io.IOException;
    8   import java.util.Hashtable;
    9
    10  import javax.jms.DeliveryMode;
    11  import javax.jms.Message;
    12
    13  import org.codehaus.activemq.message.ActiveMQDestination;
    14  import org.codehaus.activemq.message.ActiveMQMessage;
    15  import org.codehaus.activemq.message.ActiveMQMessageReader;
    16  import org.codehaus.activemq.message.ActiveMQMessageWriter;
    17  import org.codehaus.activemq.message.ActiveMQTemporaryTopic;
    18  import org.codehaus.activemq.message.ActiveMQTopic;
    19  import org.codehaus.activemq.message.Packet;
    20
    21  import junit.framework.TestCase;
    22
    23  /***
    24   * To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code
    25   * and Comments
    26   */
    27  public class ActiveMQMessageReaderTest extends TestCase {
    28
    29  	private String jmsMessageID;
    30  	private String jmsClientID;
    31  	private String jmsCorrelationID;
    32  	private ActiveMQDestination jmsDestination;
    33  	private ActiveMQDestination jmsReplyTo;
    34  	private int jmsDeliveryMode;
    35  	private boolean jmsRedelivered;
    36  	private String jmsType;
    37  	private long jmsExpiration;
    38  	private int jmsPriority;
    39  	private long jmsTimestamp;
    40  	private Hashtable properties;
    41  	private String entryBrokerName;
    42  	private String entryClusterName;
    43  	private int[] consumerIDs;
    44  	private String transactionId;
    45
    46  	public static void main(String[] args) {
    47  		junit.textui.TestRunner.run(ActiveMQMessageReaderTest.class);
    48  	}
    49
    50  	/*
    51  	 * @see TestCase#setUp()
    52  	 */
    53  	protected void setUp() throws Exception {
    54  		super.setUp();
    55  		this.jmsMessageID = "testid";
    56  		this.jmsClientID = "testclientid";
    57  		this.jmsCorrelationID = "testcorrelationid";
    58  		this.jmsDestination = new ActiveMQTopic("test.topic");
    59  		this.jmsReplyTo = new ActiveMQTemporaryTopic("test.replyto.topic");
    60  		this.jmsDeliveryMode = DeliveryMode.NON_PERSISTENT;
    61  		this.jmsRedelivered = true;
    62  		this.jmsType = "test type";
    63  		this.jmsExpiration = 100000;
    64  		this.jmsPriority = 5;
    65  		this.jmsTimestamp = System.currentTimeMillis();
    66  		this.entryBrokerName = "test broker";
    67  		this.entryClusterName = "test cluster";
    68  		this.consumerIDs = new int[3];
    69  		this.transactionId = "testtransactonId";
    70  		for (int i = 0; i < this.consumerIDs.length; i++) {
    71  			this.consumerIDs[i] = i;
    72  		}
    73  		this.properties= new Hashtable();
    74  		this.properties.put("doubletype",new Double(4.6d));
    75  		this.properties.put("floatType",new Float(3.3f));
    76  		this.properties.put("inttype",new Integer(2));
    77  		this.properties.put("longtype",new Long(45l));
    78  	}
    79
    80  	protected void initializeMessage(ActiveMQMessage msg) {
    81  		msg.setJMSMessageID(this.jmsMessageID);
    82  		msg.setJMSClientID(this.jmsClientID);
    83  		msg.setJMSCorrelationID(this.jmsCorrelationID);
    84  		msg.setJMSDestination(this.jmsDestination);
    85  		msg.setJMSReplyTo(this.jmsReplyTo);
    86  		msg.setJMSDeliveryMode(this.jmsDeliveryMode);
    87  		msg.setJMSRedelivered(this.jmsRedelivered);
    88  		msg.setJMSType(this.jmsType);
    89  		msg.setJMSExpiration(this.jmsExpiration);
    90  		msg.setJMSPriority(this.jmsPriority);
    91  		msg.setJMSTimestamp(this.jmsTimestamp);
    92  		msg.setProperties(this.properties);
    93  		msg.setReadOnly(true);
    94  		msg.setEntryBrokerName(this.entryBrokerName);
    95  		msg.setEntryClusterName(this.entryClusterName);
    96  		msg.setConsumerNos(this.consumerIDs);
    97  		msg.setTransactionId(this.transactionId);
    98  	}
    99
    100 	protected void testEquals(ActiveMQMessage msg1, ActiveMQMessage msg2) {
    101 		assertTrue(msg1.getJMSMessageID().equals(msg2.getJMSMessageID()));
    102 		assertTrue(msg1.getJMSClientID().equals(msg2.getJMSClientID()));
    103 		assertTrue(msg1.getJMSCorrelationID().equals(msg2.getJMSCorrelationID()));
    104 		assertTrue(msg1.getJMSDestination().equals(msg2.getJMSDestination()));
    105 		assertTrue(msg1.getJMSReplyTo().equals(msg2.getJMSReplyTo()));
    106 		assertTrue(msg1.getJMSDeliveryMode() == msg2.getJMSDeliveryMode());
    107 		assertTrue(msg1.getJMSRedelivered() == msg2.getJMSRedelivered());
    108 		assertTrue(msg1.getJMSType().equals(msg2.getJMSType()));
    109 		assertTrue(msg1.getJMSExpiration() == msg2.getJMSExpiration());
    110 		assertTrue(msg1.getJMSPriority() == msg2.getJMSPriority());
    111 		assertTrue(msg1.getJMSTimestamp() == msg2.getJMSTimestamp());
    112 		assertTrue(msg1.getEntryBrokerName().equals(msg2.getEntryBrokerName()));
    113 		assertTrue(msg1.getEntryClusterName().equals(msg2.getEntryClusterName()));
    114 		assertTrue(msg1.getTransactionId().equals(msg2.getTransactionId()));
    115
    116 		int[] cid1 = msg1.getConsumerNos();
    117 		int[] cid2 = msg2.getConsumerNos();
    118 		assertTrue(cid1.length==cid2.length);
    119 		for(int i =0; i < cid1.length;i++){
    120 			assertTrue(cid1[i]==cid2[i]);
    121 		}
    122 		assertTrue(msg1.getProperties().equals(msg2.getProperties()));
    123 	}
    124
    125 	/*
    126 	 * @see TestCase#tearDown()
    127 	 */
    128 	protected void tearDown() throws Exception {
    129 		super.tearDown();
    130 	}
    131
    132 	/***
    133 	 * Constructor for ActiveMQMessageReaderTest.
    134 	 *
    135 	 * @param arg0
    136 	 */
    137 	public ActiveMQMessageReaderTest(String arg0) {
    138 		super(arg0);
    139 	}
    140
    141 	public void testGetPacketType() {
    142 		ActiveMQMessageReader reader = new ActiveMQMessageReader();
    143 		assertTrue(reader.getPacketType() == Packet.ACTIVEMQ_MESSAGE);
    144 	}
    145
    146 	public void testReadPacket() throws Exception{
    147 		ActiveMQMessageReader reader = new ActiveMQMessageReader();
    148 		ActiveMQMessageWriter writer = new ActiveMQMessageWriter();
    149 		ActiveMQMessage msg1 = new ActiveMQMessage();
    150 		initializeMessage(msg1);
    151
    152 		byte[] data = writer.writePacketToByteArray(msg1);
    153 		ActiveMQMessage msg2 = (ActiveMQMessage) reader.readPacketFromByteArray(data);
    154 		testEquals(msg1, msg2);
    155          ActiveMQMessage msg3 = msg2.shallowCopy();
    156          testEquals(msg1,msg3);
    157          data = writer.writePacketToByteArray(msg3);
    158          ActiveMQMessage msg4 = (ActiveMQMessage) reader.readPacketFromByteArray(data);
    159          testEquals(msg1,msg4);
    160
    161
    162 	}
    163
    164 }