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