    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.5 $
    12   */
    13  public class XATransactionInfoReaderTest extends PacketTestSupport {
    14      protected static final int TYPE = TransactionType.START;
    15      protected static final ActiveMQXid XID = new ActiveMQXid(123, new byte[]{1, 2, 3, 4}, new byte[]{5, 6, 7, 8, 9, 10});
    16
    17      public static void main(String[] args) {
    18          junit.textui.TestRunner.run(XATransactionInfoReaderTest.class);
    19      }
    20
    21      /*
    22       * @see TestCase#setUp()
    23       */
    24      protected void setUp() throws Exception {
    25          super.setUp();
    26      }
    27
    28
    29      public void testGetPacketType() {
    30          XATransactionInfoReader reader = new XATransactionInfoReader();
    31          assertTrue(reader.getPacketType() == Packet.XA_TRANSACTION_INFO);
    32      }
    33
    34      public void testReadXidPacket() throws Exception {
    35          XATransactionInfo info = (XATransactionInfo) createXidPacket();
    36
    37          XATransactionInfoWriter writer = new XATransactionInfoWriter();
    38          XATransactionInfoReader reader = new XATransactionInfoReader();
    39          byte[] data = writer.writePacketToByteArray(info);
    40          XATransactionInfo testInfo = (XATransactionInfo) reader.readPacketFromByteArray(data);
    41
    42          assertPacket(testInfo, info);
    43      }
    44
    45      public void testReadNoXidPacket() throws Exception {
    46          XATransactionInfo info = (XATransactionInfo) createNoXidPacket();
    47
    48          XATransactionInfoWriter writer = new XATransactionInfoWriter();
    49          XATransactionInfoReader reader = new XATransactionInfoReader();
    50          byte[] data = writer.writePacketToByteArray(info);
    51          XATransactionInfo testInfo = (XATransactionInfo) reader.readPacketFromByteArray(data);
    52
    53          assertPacket(testInfo, info);
    54      }
    55
    56      public void testTime() {
    57          XATransactionInfo info = (XATransactionInfo) createXidPacket();
    58
    59          XATransactionInfoWriter writer = new XATransactionInfoWriter();
    60          XATransactionInfoReader reader = new XATransactionInfoReader();
    61          XATransactionInfo testInfo = null;
    62          try {
    63              int count = 100000;
    64              long startTime = System.currentTimeMillis();
    65              for (int i = 0; i < count; i++) {
    66                  byte[] data = writer.writePacketToByteArray(info);
    67                  testInfo = (XATransactionInfo) reader.readPacketFromByteArray(data);
    68              }
    69              long finishTime = System.currentTimeMillis();
    70              long totalTime = finishTime - startTime;
    71              long ps = (count * 1000) / totalTime;
    72              System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps + " per sec.");
    73
    74          }
    75          catch (Throwable e) {
    76              e.printStackTrace();
    77              fail();
    78          }
    79      }
    80
    81      protected void assertPacket(Packet packet, Packet expected) {
    82          XATransactionInfo testInfo = (XATransactionInfo) packet;
    83          XATransactionInfo xaExpected = (XATransactionInfo) expected;
    84          assertEquals(xaExpected.getType(), testInfo.getType());
    85          assertEquals(xaExpected.getXid(), testInfo.getXid());
    86      }
    87
    88      protected void assertValidPacket(Packet packet) {
    89          XATransactionInfo testInfo = (XATransactionInfo) packet;
    90          assertEquals(testInfo.getType(), TYPE);
    91          assertEquals(testInfo.getXid(), XID);
    92      }
    93
    94      protected Packet createPacket() {
    95          return createXidPacket();
    96      }
    97
    98      protected Packet createXidPacket() {
    99          XATransactionInfo info = new XATransactionInfo();
    100         info.setType(TYPE);
    101         info.setXid(XID);
    102         return info;
    103     }
    104
    105     protected Packet createNoXidPacket() {
    106         XATransactionInfo info = new XATransactionInfo();
    107         info.setType(TransactionType.GET_RM_ID);
    108         return info;
    109     }
    110
    111 }