    1   /*
    2    * Created on Mar 8, 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
    10  /***
    11   * To change the template for this generated type comment go to
    12   * Window - Preferences - Java - Code Generation - Code and Comments
    13   */
    14  public class ReceiptReaderTest extends PacketTestSupport {
    15      private String id;
    16      private String correlationId;
    17
    18      public static void main(String[] args) {
    19          junit.textui.TestRunner.run(ReceiptReaderTest.class);
    20      }
    21
    22      /*
    23       * @see TestCase#setUp()
    24       */
    25      protected void setUp() throws Exception {
    26          super.setUp();
    27          this.id = "testid";
    28          this.correlationId = "testCorrelationId";
    29      }
    30
    31      /*
    32       * @see TestCase#tearDown()
    33       */
    34      protected void tearDown() throws Exception {
    35          super.tearDown();
    36      }
    37
    38      public void testGetPacketType() {
    39          ReceiptReader reader = new ReceiptReader();
    40          assertTrue(reader.getPacketType() == Packet.RECEIPT_INFO);
    41      }
    42
    43      public void testReadPacket() {
    44          Receipt info = (Receipt) createPacket();
    45
    46          ReceiptWriter writer = new ReceiptWriter();
    47          ReceiptReader reader = new ReceiptReader();
    48          try {
    49              byte[] data = writer.writePacketToByteArray(info);
    50              Receipt receipt = (Receipt) reader.readPacketFromByteArray(data);
    51
    52              assertPacket(receipt, info);
    53          }
    54          catch (Throwable e) {
    55              e.printStackTrace();
    56              assertTrue(false);
    57          }
    58      }
    59
    60      public void testTime() {
    61
    62          Receipt info = (Receipt) createPacket();
    63
    64          ReceiptWriter writer = new ReceiptWriter();
    65          ReceiptReader reader = new ReceiptReader();
    66
    67          try {
    68              int count = 100000;
    69              long startTime = System.currentTimeMillis();
    70              for (int i = 0; i < count; i++) {
    71                  byte[] data = writer.writePacketToByteArray(info);
    72                  Receipt test = (Receipt) reader.readPacketFromByteArray(data);
    73              }
    74              long finishTime = System.currentTimeMillis();
    75              long totalTime = finishTime - startTime;
    76              long ps = (count * 1000) / totalTime;
    77              System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps + " per sec.");
    78
    79          }
    80          catch (Throwable e) {
    81              e.printStackTrace();
    82              assertTrue(false);
    83          }
    84      }
    85
    86      protected void assertValidPacket(Packet packet) {
    87          Receipt receipt = (Receipt) packet;
    88          assertTrue(receipt.getId().equals(this.id));
    89          assertTrue(receipt.getCorrelationId().equals(this.correlationId));
    90      }
    91
    92      protected Packet createPacket() {
    93          Receipt info = new Receipt();
    94          info.setId(this.id);
    95          info.setCorrelationId(this.correlationId);
    96          return info;
    97      }
    98
    99  }