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 junit.framework.TestCase;
10  
11  /***
12   * To change the template for this generated type comment go to
13   * Window - Preferences - Java - Code Generation - Code and Comments
14   */
15  public class ProducerInfoReaderTest extends TestCase {
16      private String producerId;
17      private ActiveMQDestination destination;
18      private String clientId;
19      private long startTime;
20      private boolean started;
21  
22      public static void main(String[] args) {
23          junit.textui.TestRunner.run(ProducerInfoReaderTest.class);
24      }
25  
26      /*
27       * @see TestCase#setUp()
28       */
29      protected void setUp() throws Exception {
30          super.setUp();
31          this.producerId = "testProducerId";
32          this.clientId = "testclientId";
33          this.destination = new ActiveMQTopic("testtopic");
34          this.startTime = System.currentTimeMillis();
35          this.started = true;
36      }
37  
38      /*
39       * @see TestCase#tearDown()
40       */
41      protected void tearDown() throws Exception {
42          super.tearDown();
43      }
44  
45      /***
46       * Constructor for ProducerInfoReaderTest.
47       *
48       * @param arg0
49       */
50      public ProducerInfoReaderTest(String arg0) {
51          super(arg0);
52      }
53  
54      public void testGetPacketType() {
55          ProducerInfoReader reader = new ProducerInfoReader();
56          assertTrue(reader.getPacketType() == Packet.PRODUCER_INFO);
57      }
58  
59      public void testReadPacket() {
60          ProducerInfo info = new ProducerInfo();
61          info.setId(this.producerId);
62          info.setClientId(this.clientId);
63          info.setDestination(this.destination);
64          info.setStartTime(this.startTime);
65          info.setStarted(this.started);
66  
67          ProducerInfoWriter writer = new ProducerInfoWriter();
68          ProducerInfoReader reader = new ProducerInfoReader();
69          try {
70              byte[] data = writer.writePacketToByteArray(info);
71              ProducerInfo testInfo = (ProducerInfo) reader.readPacketFromByteArray(data);
72  
73              assertTrue(testInfo.getId().equals(this.producerId));
74              assertTrue(testInfo.getClientId().equals(this.clientId));
75              assertTrue(testInfo.getDestination().equals(this.destination));
76              assertTrue(testInfo.getStartTime() == this.startTime);
77              assertTrue(testInfo.isStarted() == this.started);
78          }
79          catch (Throwable e) {
80              e.printStackTrace();
81              assertTrue(false);
82          }
83      }
84  
85      public void testTime() {
86  
87          ProducerInfo info = new ProducerInfo();
88          info.setId(this.producerId);
89          info.setClientId(this.clientId);
90          info.setDestination(this.destination);
91          info.setStartTime(this.startTime);
92          info.setStarted(this.started);
93  
94          ProducerInfoWriter writer = new ProducerInfoWriter();
95          ProducerInfoReader reader = new ProducerInfoReader();
96          ProducerInfo testInfo = null;
97          try {
98              int count = 100000;
99              long startTime = System.currentTimeMillis();
100             for (int i = 0; i < count; i++) {
101                 byte[] data = writer.writePacketToByteArray(info);
102                 testInfo = (ProducerInfo) reader.readPacketFromByteArray(data);
103             }
104             long finishTime = System.currentTimeMillis();
105             long totalTime = finishTime - startTime;
106             long ps = (count * 1000) / totalTime;
107             System.out.println("Time taken :" + totalTime + " for " + count + "iterations, = " + ps + " per sec.");
108 
109         }
110         catch (Throwable e) {
111             e.printStackTrace();
112             assertTrue(false);
113         }
114     }
115 
116 }