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