    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      private boolean persistent;
    27      private boolean xaTransacted;
    28      private ActiveMQDestination destination;
    29
    30
    31  	public static void main(String[] args) {
    32  		junit.textui.TestRunner.run(MessageAckReaderTest.class);
    33  	}
    34
    35  	/*
    36  	 * @see TestCase#setUp()
    37  	 */
    38  	protected void setUp() throws Exception {
    39  		super.setUp();
    40          this.id = "test more idf";
    41  		this.consumerId = "testid";
    42          this.messageId = "ID:ewrre-erer-err";
    43          this.transactedId = "45454";
    44          this.messageRead = true;
    45          this.persistent=true;
    46          this.xaTransacted = false;
    47          this.destination = new ActiveMQTopic("FOO.BAR");
    48  	}
    49
    50  	/*
    51  	 * @see TestCase#tearDown()
    52  	 */
    53  	protected void tearDown() throws Exception {
    54  		super.tearDown();
    55  	}
    56
    57  	/***
    58  	 * Constructor for ProducerInfoReaderTest.
    59  	 *
    60  	 * @param arg0
    61  	 */
    62  	public MessageAckReaderTest(String arg0) {
    63  		super(arg0);
    64  	}
    65
    66  	public void testGetPacketType() {
    67  		MessageAckReader reader = new MessageAckReader();
    68  		assertTrue(reader.getPacketType() == Packet.ACTIVEMQ_MSG_ACK);
    69  	}
    70
    71  	public void testReadPacket() {
    72  		MessageAck ack = new MessageAck();
    73  		ack.setId(this.id);
    74          ack.setConsumerId(this.consumerId);
    75  		ack.setTransactionId(this.transactedId);
    76          ack.setMessageID(messageId);
    77          ack.setMessageRead(this.messageRead);
    78          ack.setPersistent(persistent);
    79          ack.setXaTransacted(xaTransacted);
    80          ack.setDestination(destination);
    81
    82  		MessageAckWriter writer = new MessageAckWriter();
    83  		MessageAckReader reader = new MessageAckReader();
    84  		try {
    85  			byte[] data = writer.writePacketToByteArray(ack);
    86  			MessageAck mack = (MessageAck) reader.readPacketFromByteArray(data);
    87
    88  			assertTrue(mack.getId().equals(this.id));
    89  			assertTrue(mack.getConsumerId().equals(this.consumerId));
    90              assertTrue(mack.getTransactionId().equals(this.transactedId));
    91              assertTrue(mack.isMessageRead()==this.messageRead);
    92              assertTrue(mack.isPersistent()==this.persistent);
    93              assertTrue(mack.isXaTransacted()==this.xaTransacted);
    94              assertTrue(mack.getDestination().equals(this.destination));
    95  		}
    96  		catch (Throwable e) {
    97  			e.printStackTrace();
    98  			assertTrue(false);
    99  		}
    100 	}
    101
    102 	public void testTime() {
    103
    104 		MessageAck ack = new MessageAck();
    105         ack.setId(this.id);
    106         ack.setConsumerId(this.consumerId);
    107         ack.setTransactionId(this.transactedId);
    108         ack.setMessageID(messageId);
    109         ack.setMessageRead(this.messageRead);
    110         ack.setPersistent(persistent);
    111         ack.setXaTransacted(xaTransacted);
    112         ack.setDestination(destination);
    113
    114 		MessageAckWriter writer = new MessageAckWriter();
    115 		MessageAckReader reader = new MessageAckReader();
    116 		try {
    117 			int count = 100000;
    118 			long startTime = System.currentTimeMillis();
    119 			for (int i = 0; i < count; i++) {
    120 				byte[] data = writer.writePacketToByteArray(ack);
    121 				MessageAck mack = (MessageAck) reader.readPacketFromByteArray(data);
    122 			}
    123 			long finishTime = System.currentTimeMillis();
    124 			long totalTime = finishTime - startTime;
    125 			long ps = (count * 1000) / totalTime;
    126 			System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps + " per sec.");
    127
    128 		}
    129 		catch (Throwable e) {
    130 			e.printStackTrace();
    131 			assertTrue(false);
    132 		}
    133 	}
    134
    135 }