    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   import junit.framework.TestCase;
    10
    11  import javax.jms.JMSException;
    12  import java.io.ByteArrayInputStream;
    13  import java.io.ByteArrayOutputStream;
    14  import java.io.DataInputStream;
    15  import java.io.DataOutputStream;
    16  import java.io.IOException;
    17
    18  /***
    19   * @version $Revision: 1.4 $
    20   */
    21  public abstract class PacketTestSupport extends TestCase {
    22      protected WireFormat wireFormat = new DefaultWireFormat();
    23      protected PacketWriter writer;
    24      protected PacketReader reader;
    25      protected int packetCount = 10;
    26
    27      public void testWireFormat() throws IOException, JMSException {
    28          WireFormat wireFormat = new DefaultWireFormat();
    29
    30          Packet packet = createPacket();
    31          System.out.println("Created packet: " + packet + " with type: " + packet.getPacketType());
    32
    33          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    34          DataOutputStream dataOut = new DataOutputStream(buffer);
    35          wireFormat.writePacket(packet, dataOut);
    36          dataOut.close();
    37          byte[] bytes = buffer.toByteArray();
    38
    39          DataInputStream dataIn = new DataInputStream(new ByteArrayInputStream(bytes));
    40          int type = dataIn.readByte();
    41          System.out.println("Read type: " + type);
    42          Packet otherPacket = wireFormat.readPacket(type, dataIn);
    43
    44          System.out.println("Read packet: " + otherPacket);
    45          assertPacket(otherPacket, packet);
    46      }
    47
    48      /*
    49      public void testReadPacket() throws IOException {
    50          Packet packet = createPacket();
    51          byte[] data = writer.writePacketToByteArray(packet);
    52          Packet testInfo = reader.readPacketFromByteArray(data);
    53          assertValidPacket(testInfo);
    54      }
    55      */
    56
    57      public void testToFromBytes() throws JMSException, IOException {
    58          Packet packet = createPacket();
    59          byte[] data = wireFormat.toBytes(packet);
    60
    61          System.out.println("Written byte array of size: " + data.length);
    62
    63          Packet value = wireFormat.fromBytes(data);
    64          assertValidPacket(value);
    65      }
    66
    67      public void testReadAndWriteDataInOut() throws JMSException, IOException {
    68          Packet packet = createPacket();
    69          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    70          DataOutputStream out = new DataOutputStream(buffer);
    71          wireFormat.writePacket(packet, out);
    72          out.close();
    73
    74          byte[] data = buffer.toByteArray();
    75          System.out.println("Written byte array of size: " + data.length);
    76
    77          DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));
    78          Packet value = wireFormat.readPacket(in);
    79          assertValidPacket(value);
    80      }
    81
    82
    83      public void testReadAndWriteDataInOutLoop() throws JMSException, IOException {
    84          Packet packet = createPacket();
    85          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    86          DataOutputStream out = new DataOutputStream(buffer);
    87
    88          for (int i = 0; i < packetCount; i++) {
    89              wireFormat.writePacket(packet, out);
    90          }
    91
    92          DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    93          for (int i = 0; i < packetCount; i++) {
    94              Packet value = wireFormat.readPacket(in);
    95              assertValidPacket(value);
    96          }
    97      }
    98
    99      protected void assertPacket(Packet packet, Packet expected) {
    100         assertValidPacket(packet);
    101     }
    102
    103     protected abstract Packet createPacket();
    104
    105
    106     protected abstract void assertValidPacket(Packet packet);
    107
    108 }