ActiveMQBytesMessageReaderTest xref

    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 java.io.IOException;
    8
    9   import org.codehaus.activemq.message.ActiveMQBytesMessage;
    10  import org.codehaus.activemq.message.ActiveMQBytesMessageReader;
    11  import org.codehaus.activemq.message.ActiveMQBytesMessageWriter;
    12  import org.codehaus.activemq.message.Packet;
    13
    14  /***
    15   * @author Rob To change the template for this generated type comment go to Window - Preferences - Java - Code
    16   *         Generation - Code and Comments
    17   */
    18  public class ActiveMQBytesMessageReaderTest extends ActiveMQMessageReaderTest {
    19
    20  	private byte[] testdata;
    21  	public static void main(String[] args) {
    22  		junit.textui.TestRunner.run(ActiveMQBytesMessageReaderTest.class);
    23  	}
    24
    25  	/*
    26  	 * @see TestCase#setUp()
    27  	 */
    28  	protected void setUp() throws Exception {
    29  		super.setUp();
    30  		testdata = new byte[1024 *2];
    31  		for (int i = 0; i < testdata.length;i++){
    32  			testdata[i] = (byte)i;
    33  		}
    34
    35  	}
    36
    37  	/*
    38  	 * @see TestCase#tearDown()
    39  	 */
    40  	protected void tearDown() throws Exception {
    41  		super.tearDown();
    42  	}
    43
    44  	/***
    45  	 * Constructor for ActiveMQTextMessageReaderTest.
    46  	 *
    47  	 * @param arg0
    48  	 */
    49  	public ActiveMQBytesMessageReaderTest(String arg0) {
    50  		super(arg0);
    51  	}
    52
    53  	public void testGetPacketType() {
    54  		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    55  		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_BYTES_MESSAGE);
    56  	}
    57
    58  	public void testReadPacket() {
    59  		ActiveMQBytesMessageReader reader = new ActiveMQBytesMessageReader();
    60  		ActiveMQBytesMessageWriter writer = new ActiveMQBytesMessageWriter();
    61  		ActiveMQBytesMessage msg1 = new ActiveMQBytesMessage();
    62  		try {
    63  			msg1.setBodyAsBytes(testdata);
    64  			super.initializeMessage(msg1);
    65  			byte[] data = writer.writePacketToByteArray(msg1);
    66  			ActiveMQBytesMessage msg2 = (ActiveMQBytesMessage) reader.readPacketFromByteArray(data);
    67  			super.testEquals(msg1, msg2);
    68  			byte[] d2 = msg2.getBodyAsBytes();
    69  			assertTrue(d2.length==testdata.length);
    70  			for(int i = 0; i < testdata.length; i++)
    71  			assertTrue(d2[i]==testdata[i]);
    72  		}
    73  		catch (IOException e) {
    74  			e.printStackTrace();
    75  			assertTrue(false);
    76  		}
    77
    78  	}
    79
    80  	public void testTime(){
    81
    82  		ActiveMQBytesMessageReader reader = new ActiveMQBytesMessageReader();
    83  		ActiveMQBytesMessageWriter writer = new ActiveMQBytesMessageWriter();
    84  		ActiveMQBytesMessage msg1 = new ActiveMQBytesMessage();
    85
    86  		ActiveMQBytesMessage msg2 = null;
    87  		try {
    88  			int count = 10000;
    89  			long startTime = System.currentTimeMillis();
    90  			msg1.setBodyAsBytes(this.testdata);
    91  			super.initializeMessage(msg1);
    92  			for (int i = 0; i < count; i++){
    93  				byte[] data = writer.writePacketToByteArray(msg1);
    94  				msg2 = (ActiveMQBytesMessage) reader.readPacketFromByteArray(data);
    95  			}
    96  			long finishTime = System.currentTimeMillis();
    97  			long totalTime = finishTime-startTime;
    98  			long ps = (count * 1000)/totalTime;
    99  			System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps +" per sec.");
    100
    101 		}catch(Throwable e){
    102 			e.printStackTrace();
    103 			assertTrue(false);
    104 		}
    105 	}
    106
    107 }