    1   /*
    2    * Created on Mar 8, 2004
    3    *
    4    * To change the template for this generated file go to
    5    * Window - Preferences - Java - Code Generation - Code and Comments
    6    */
    7   package org.codehaus.activemq.message;
    8
    9   import org.codehaus.activemq.message.ActiveMQDestination;
    10  import org.codehaus.activemq.message.ActiveMQTopic;
    11  import org.codehaus.activemq.message.ConsumerInfo;
    12  import org.codehaus.activemq.message.ConsumerInfoReader;
    13  import org.codehaus.activemq.message.ConsumerInfoWriter;
    14  import org.codehaus.activemq.message.Packet;
    15
    16  import junit.framework.TestCase;
    17
    18  /***
    19   *
    20   * To change the template for this generated type comment go to
    21   * Window - Preferences - Java - Code Generation - Code and Comments
    22   */
    23  public class ConsumerInfoReaderTest extends TestCase
    24  {
    25  	private String consumerId;
    26  	private ActiveMQDestination destination;
    27  	private String clientId;
    28  	private String subscriberName;
    29  	private String selector;
    30  	private long startTime;
    31  	private boolean started;
    32  	private int consumerNo;
    33      private int prefetchNumber;
    34
    35  	public static void main(String[] args)
    36  	{
    37  		junit.textui.TestRunner.run(ConsumerInfoReaderTest.class);
    38  	}
    39
    40  	/*
    41  	 * @see TestCase#setUp()
    42  	 */
    43  	protected void setUp() throws Exception
    44  	{
    45  		super.setUp();
    46  		this.consumerId = "testConsumerId";
    47  		this.clientId = "testclientId";
    48  		this.subscriberName = "testsubscriberName";
    49  		this.selector = "testselector";
    50  		this.destination = new ActiveMQTopic("testtopic");
    51  		this.startTime = System.currentTimeMillis();
    52  		this.started = true;
    53  		this.consumerNo = 3;
    54          this.prefetchNumber = 13;
    55  	}
    56
    57  	/*
    58  	 * @see TestCase#tearDown()
    59  	 */
    60  	protected void tearDown() throws Exception
    61  	{
    62  		super.tearDown();
    63  	}
    64
    65  	/***
    66  	 * Constructor for ConsumerInfoReaderTest.
    67  	 * @param arg0
    68  	 */
    69  	public ConsumerInfoReaderTest(String arg0)
    70  	{
    71  		super(arg0);
    72  	}
    73
    74  	public void testGetPacketType()
    75  	{
    76  		ConsumerInfoReader reader = new ConsumerInfoReader();
    77  		assertTrue(reader.getPacketType()==Packet.CONSUMER_INFO);
    78  	}
    79
    80  	public void testReadPacket()
    81  	{
    82  		ConsumerInfo info = new ConsumerInfo();
    83  		info.setId(this.consumerId);
    84  		info.setConsumerNo(this.consumerNo);
    85          info.setPrefetchNumber(this.prefetchNumber);
    86  		info.setClientId(this.clientId);
    87  		info.setConsumerName(this.subscriberName);
    88  		info.setSelector(this.selector);
    89  		info.setDestination(this.destination);
    90  		info.setStartTime(this.startTime);
    91  		info.setStarted(this.started);
    92
    93  		ConsumerInfoWriter writer = new ConsumerInfoWriter();
    94  		ConsumerInfoReader reader = new ConsumerInfoReader();
    95  		try {
    96  		byte[] data = writer.writePacketToByteArray(info);
    97  		ConsumerInfo testInfo = (ConsumerInfo) reader.readPacketFromByteArray(data);
    98
    99  			assertTrue(testInfo.getId().equals(this.consumerId));
    100 			assertTrue(testInfo.getConsumerNo()==this.consumerNo);
    101             assertTrue(testInfo.getPrefetchNumber() == this.prefetchNumber);
    102 			assertTrue(testInfo.getClientId().equals(this.clientId));
    103 			assertTrue(testInfo.getConsumerName().equals(this.subscriberName));
    104 			assertTrue(testInfo.getSelector().equals(this.selector));
    105 			assertTrue(testInfo.getDestination().equals(this.destination));
    106 			assertTrue(testInfo.getStartTime() == this.startTime);
    107 			assertTrue(testInfo.isStarted()==this.started);
    108 		}catch(Throwable e){
    109 			e.printStackTrace();
    110 			assertTrue(false);
    111 		}
    112 	}
    113
    114 	public void testTime(){
    115
    116 		ConsumerInfo info = new ConsumerInfo();
    117 		info.setId(this.consumerId);
    118 		info.setConsumerNo(this.consumerNo);
    119         info.setPrefetchNumber(this.prefetchNumber);
    120 		info.setClientId(this.clientId);
    121 		info.setConsumerName(this.subscriberName);
    122 		info.setSelector(this.selector);
    123 		info.setDestination(this.destination);
    124 		info.setStartTime(this.startTime);
    125 		info.setStarted(this.started);
    126
    127 		ConsumerInfoWriter writer = new ConsumerInfoWriter();
    128 		ConsumerInfoReader reader = new ConsumerInfoReader();
    129 		ConsumerInfo testInfo = null;
    130 		try {
    131 			int count = 100000;
    132 			long startTime = System.currentTimeMillis();
    133 			for (int i = 0; i < count; i++){
    134 				byte[] data = writer.writePacketToByteArray(info);
    135 				testInfo = (ConsumerInfo) reader.readPacketFromByteArray(data);
    136 			}
    137 			long finishTime = System.currentTimeMillis();
    138 			long totalTime = finishTime-startTime;
    139 			long ps = (count * 1000)/totalTime;
    140 			System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps +" per sec.");
    141
    142 		}catch(Throwable e){
    143 			e.printStackTrace();
    144 			assertTrue(false);
    145 		}
    146 	}
    147
    148 }