Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/io/impl/ProducerInfoReaderTest.java


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