    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.ActiveMQMapMessage;
    13  import org.codehaus.activemq.message.ActiveMQMapMessageReader;
    14  import org.codehaus.activemq.message.ActiveMQMapMessageWriter;
    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 ActiveMQMapMessageReaderTest extends ActiveMQMessageReaderTest {
    22
    23  	private Hashtable table;
    24  	public static void main(String[] args) {
    25  		junit.textui.TestRunner.run(ActiveMQMapMessageReaderTest.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 ActiveMQMapMessageReaderTest(String arg0) {
    53  		super(arg0);
    54  	}
    55
    56  	public void testGetPacketType() {
    57  		ActiveMQMapMessage msg = new ActiveMQMapMessage();
    58  		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_MAP_MESSAGE);
    59  	}
    60
    61  	public void testReadPacket() {
    62  		ActiveMQMapMessageReader reader = new ActiveMQMapMessageReader();
    63  		ActiveMQMapMessageWriter writer = new ActiveMQMapMessageWriter();
    64  		ActiveMQMapMessage msg1 = new ActiveMQMapMessage();
    65  		try {
    66  			msg1.setTable(this.table);
    67  			super.initializeMessage(msg1);
    68  			byte[] data = writer.writePacketToByteArray(msg1);
    69  			ActiveMQMapMessage msg2 = (ActiveMQMapMessage) reader.readPacketFromByteArray(data);
    70  			super.testEquals(msg1, msg2);
    71  			assertTrue(msg1.getTable().equals(msg2.getTable()));
    72  		}
    73          catch(JMSException jmsEx){
    74              jmsEx.printStackTrace();
    75              assertTrue(false);
    76          }
    77  		catch (IOException e) {
    78  			e.printStackTrace();
    79  			assertTrue(false);
    80  		}
    81  	}
    82
    83  	public void testTime() {
    84
    85  		ActiveMQMapMessageReader reader = new ActiveMQMapMessageReader();
    86  		ActiveMQMapMessageWriter writer = new ActiveMQMapMessageWriter();
    87  		ActiveMQMapMessage msg1 = new ActiveMQMapMessage();
    88
    89  		ActiveMQMapMessage msg2 = null;
    90  		try {
    91  			int count = 10000;
    92  			long startTime = System.currentTimeMillis();
    93
    94  			msg1.setTable(this.table);
    95  			super.initializeMessage(msg1);
    96  			for (int i = 0; i < count; i++) {
    97  				byte[] data = writer.writePacketToByteArray(msg1);
    98  				msg2 = (ActiveMQMapMessage) 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 		}
    106 		catch (Throwable e) {
    107 			e.printStackTrace();
    108 			assertTrue(false);
    109 		}
    110 	}
    111
    112 }