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