    1   /*
    2    * Created on Mar 8, 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 org.codehaus.activemq.message.ActiveMQDestination;
    8   import org.codehaus.activemq.message.ActiveMQTopic;
    9   import org.codehaus.activemq.message.MessageAck;
    10  import org.codehaus.activemq.message.MessageAckReader;
    11  import org.codehaus.activemq.message.MessageAckWriter;
    12  import org.codehaus.activemq.message.Packet;
    13
    14  import junit.framework.TestCase;
    15
    16  /***
    17   * To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code
    18   * and Comments
    19   */
    20  public class MessageAckReaderTest extends TestCase {
    21  	private String id;
    22      private String consumerId;
    23      private String messageId;
    24      private String transactedId;
    25      private boolean messageRead;
    26
    27
    28  	public static void main(String[] args) {
    29  		junit.textui.TestRunner.run(MessageAckReaderTest.class);
    30  	}
    31
    32  	/*
    33  	 * @see TestCase#setUp()
    34  	 */
    35  	protected void setUp() throws Exception {
    36  		super.setUp();
    37          this.id = "test more idf";
    38  		this.consumerId = "testid";
    39          this.messageId = "ID:ewrre-erer-err";
    40          this.transactedId = "45454";
    41          this.messageRead = true;
    42  	}
    43
    44  	/*
    45  	 * @see TestCase#tearDown()
    46  	 */
    47  	protected void tearDown() throws Exception {
    48  		super.tearDown();
    49  	}
    50
    51  	/***
    52  	 * Constructor for ProducerInfoReaderTest.
    53  	 *
    54  	 * @param arg0
    55  	 */
    56  	public MessageAckReaderTest(String arg0) {
    57  		super(arg0);
    58  	}
    59
    60  	public void testGetPacketType() {
    61  		MessageAckReader reader = new MessageAckReader();
    62  		assertTrue(reader.getPacketType() == Packet.ACTIVEMQ_MSG_ACK);
    63  	}
    64
    65  	public void testReadPacket() {
    66  		MessageAck ack = new MessageAck();
    67  		ack.setId(this.id);
    68          ack.setConsumerId(this.consumerId);
    69  		ack.setTransactionId(this.transactedId);
    70          ack.setMessageID(messageId);
    71          ack.setMessageRead(this.messageRead);
    72
    73  		MessageAckWriter writer = new MessageAckWriter();
    74  		MessageAckReader reader = new MessageAckReader();
    75  		try {
    76  			byte[] data = writer.writePacketToByteArray(ack);
    77  			MessageAck mack = (MessageAck) reader.readPacketFromByteArray(data);
    78
    79  			assertTrue(mack.getId().equals(this.id));
    80  			assertTrue(mack.getConsumerId().equals(this.consumerId));
    81              assertTrue(mack.getTransactionId().equals(this.transactedId));
    82              assertTrue(mack.isMessageRead()==this.messageRead);
    83  		}
    84  		catch (Throwable e) {
    85  			e.printStackTrace();
    86  			assertTrue(false);
    87  		}
    88  	}
    89
    90  	public void testTime() {
    91
    92  		MessageAck ack = new MessageAck();
    93          ack.setId(this.id);
    94          ack.setConsumerId(this.consumerId);
    95          ack.setTransactionId(this.transactedId);
    96          ack.setMessageID(messageId);
    97          ack.setMessageRead(this.messageRead);
    98
    99  		MessageAckWriter writer = new MessageAckWriter();
    100 		MessageAckReader reader = new MessageAckReader();
    101 		try {
    102 			int count = 100000;
    103 			long startTime = System.currentTimeMillis();
    104 			for (int i = 0; i < count; i++) {
    105 				byte[] data = writer.writePacketToByteArray(ack);
    106 				MessageAck mack = (MessageAck) reader.readPacketFromByteArray(data);
    107 			}
    108 			long finishTime = System.currentTimeMillis();
    109 			long totalTime = finishTime - startTime;
    110 			long ps = (count * 1000) / totalTime;
    111 			System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps + " per sec.");
    112
    113 		}
    114 		catch (Throwable e) {
    115 			e.printStackTrace();
    116 			assertTrue(false);
    117 		}
    118 	}
    119
    120 }