TransactionInfoReaderTest xref

    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   * @version $Revision: 1.4 $
    12   */
    13  public class TransactionInfoReaderTest extends PacketTestSupport {
    14      private String TransactionId;
    15      private int type;
    16
    17      public static void main(String[] args) {
    18          junit.textui.TestRunner.run(TransactionInfoReaderTest.class);
    19      }
    20
    21      /*
    22       * @see TestCase#setUp()
    23       */
    24      protected void setUp() throws Exception {
    25          super.setUp();
    26          this.TransactionId = "testTransactionId";
    27          this.type = 113;
    28      }
    29
    30
    31      public void testGetPacketType() {
    32          TransactionInfoReader reader = new TransactionInfoReader();
    33          assertTrue(reader.getPacketType() == Packet.TRANSACTION_INFO);
    34      }
    35
    36      public void testReadPacket() {
    37          TransactionInfo info = (TransactionInfo) createPacket();
    38
    39          TransactionInfoWriter writer = new TransactionInfoWriter();
    40          TransactionInfoReader reader = new TransactionInfoReader();
    41          try {
    42              byte[] data = writer.writePacketToByteArray(info);
    43              TransactionInfo testInfo = (TransactionInfo) reader.readPacketFromByteArray(data);
    44
    45              assertPacket(testInfo, info);
    46          }
    47          catch (Throwable e) {
    48              e.printStackTrace();
    49              assertTrue(false);
    50          }
    51      }
    52
    53      public void testTime() {
    54          TransactionInfo info = (TransactionInfo) createPacket();
    55
    56          TransactionInfoWriter writer = new TransactionInfoWriter();
    57          TransactionInfoReader reader = new TransactionInfoReader();
    58          TransactionInfo testInfo = null;
    59          try {
    60              int count = 100000;
    61              long startTime = System.currentTimeMillis();
    62              for (int i = 0; i < count; i++) {
    63                  byte[] data = writer.writePacketToByteArray(info);
    64                  testInfo = (TransactionInfo) reader.readPacketFromByteArray(data);
    65              }
    66              long finishTime = System.currentTimeMillis();
    67              long totalTime = finishTime - startTime;
    68              long ps = (count * 1000) / totalTime;
    69              System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps + " per sec.");
    70
    71          }
    72          catch (Throwable e) {
    73              e.printStackTrace();
    74              assertTrue(false);
    75          }
    76      }
    77
    78      protected void assertValidPacket(Packet packet) {
    79          TransactionInfo testInfo = (TransactionInfo) packet;
    80          assertTrue(testInfo.getId().equals(this.TransactionId));
    81          assertTrue(testInfo.getType() == this.type);
    82      }
    83
    84      protected Packet createPacket() {
    85          TransactionInfo info = new TransactionInfo();
    86          info.setId(this.TransactionId);
    87          info.setType(this.type);
    88          return info;
    89      }
    90
    91  }