    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.Hashtable;
    9
    10  import javax.jms.JMSException;
    11
    12  import org.codehaus.activemq.message.ActiveMQObjectMessage;
    13  import org.codehaus.activemq.message.ActiveMQObjectMessageReader;
    14  import org.codehaus.activemq.message.ActiveMQObjectMessageWriter;
    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 ActiveMQObjectMessageReaderTest extends ActiveMQMessageReaderTest {
    22
    23  	private Hashtable table;
    24  	public static void main(String[] args) {
    25  		junit.textui.TestRunner.run(ActiveMQObjectMessageReaderTest.class);
    26  	}
    27
    28  	/*
    29  	 * @see TestCase#setUp()
    30  	 */
    31  	protected void setUp() throws Exception {
    32  		super.setUp();
    33  		this.table = new Hashtable();
    34  		for(int i = 0; i < 10; i++){
    35  			String str = "test" +i;
    36  			this.table.put(str,str);
    37  		}
    38  	}
    39
    40  	/*
    41  	 * @see TestCase#tearDown()
    42  	 */
    43  	protected void tearDown() throws Exception {
    44  		super.tearDown();
    45  	}
    46
    47  	/***
    48  	 * Constructor for ActiveMQTextMessageReaderTest.
    49  	 *
    50  	 * @param arg0
    51  	 */
    52  	public ActiveMQObjectMessageReaderTest(String arg0) {
    53  		super(arg0);
    54  	}
    55
    56  	public void testGetPacketType() {
    57  		ActiveMQObjectMessage msg = new ActiveMQObjectMessage();
    58  		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_OBJECT_MESSAGE);
    59  	}
    60
    61  	public void testReadPacket() {
    62  		ActiveMQObjectMessageReader reader = new ActiveMQObjectMessageReader();
    63  		ActiveMQObjectMessageWriter writer = new ActiveMQObjectMessageWriter();
    64  		ActiveMQObjectMessage msg1 = new ActiveMQObjectMessage();
    65  		try {
    66  			msg1.setObject(this.table);
    67  			super.initializeMessage(msg1);
    68  			byte[] data = writer.writePacketToByteArray(msg1);
    69  			ActiveMQObjectMessage msg2 = (ActiveMQObjectMessage) reader.readPacketFromByteArray(data);
    70  			super.testEquals(msg1, msg2);
    71  			assertTrue(msg1.getObject().equals(msg2.getObject()));
    72  		}
    73  		catch (IOException e) {
    74  			e.printStackTrace();
    75  			assertTrue(false);
    76  		}
    77  		catch (JMSException ex) {
    78  			ex.printStackTrace();
    79  			assertTrue(false);
    80  		}
    81  	}
    82
    83  	public void testTime(){
    84
    85  		ActiveMQObjectMessageReader reader = new ActiveMQObjectMessageReader();
    86  		ActiveMQObjectMessageWriter writer = new ActiveMQObjectMessageWriter();
    87  		ActiveMQObjectMessage msg1 = new ActiveMQObjectMessage();
    88
    89  		ActiveMQObjectMessage msg2 = null;
    90  		try {
    91  			int count = 10000;
    92  			long startTime = System.currentTimeMillis();
    93
    94  			msg1.setObject(this.table);
    95  			super.initializeMessage(msg1);
    96  			for (int i = 0; i < count; i++){
    97  				byte[] data = writer.writePacketToByteArray(msg1);
    98  				msg2 = (ActiveMQObjectMessage) reader.readPacketFromByteArray(data);
    99  			}
    100 			long finishTime = System.currentTimeMillis();
    101 			long totalTime = finishTime-startTime;
    102 			long ps = (count * 1000)/totalTime;
    103 			System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps +" per sec.");
    104
    105 		}catch(Throwable e){
    106 			e.printStackTrace();
    107 			assertTrue(false);
    108 		}
    109 	}
    110
    111 }