KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > soaktest > MarshallingWithCachingTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.soaktest;
19
20 import org.apache.activemq.command.ActiveMQDestination;
21 import org.apache.activemq.command.ActiveMQQueue;
22 import org.apache.activemq.command.DataStructure;
23 import org.apache.activemq.command.ProducerId;
24 import org.apache.activemq.command.ProducerInfo;
25 import org.apache.activemq.openwire.OpenWireFormat;
26
27 import java.io.ByteArrayInputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.DataInputStream JavaDoc;
30 import java.io.DataOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 import junit.framework.TestCase;
34
35 /**
36  *
37  * @version $Revision: 383541 $
38  */

39 public class MarshallingWithCachingTest extends TestCase {
40
41     protected long commandCount = Short.MAX_VALUE;
42     protected String JavaDoc connectionId = "Cheese";
43     protected ActiveMQDestination destination = new ActiveMQQueue("Foo");
44     protected int endOfStreamMarker = 0x12345678;
45     
46     protected ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
47     protected DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(buffer);
48     protected OpenWireFormat openWireformat;
49     protected long logGroup = 10000;
50     
51
52     
53     public void testReadAndWriteLotsOfCommands() throws Exception JavaDoc {
54         System.out.println("Marshalling: " + commandCount + " objects");
55         for (long i = 0; i < commandCount ; i++) {
56             logProgress("Marshalling", i);
57             DataStructure object = createDataStructure(i);
58             writeObject(object);
59         }
60         ds.writeInt(endOfStreamMarker);
61         
62         // now lets read from the stream
63
ds.close();
64         
65         System.out.println("Unmarshalling: " + commandCount + " objects");
66         
67         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(buffer.toByteArray());
68         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(in);
69         for (long i = 0; i < commandCount ; i++) {
70             logProgress("Unmarshalling", i);
71             DataStructure command = null;
72             try {
73                 command = (DataStructure) openWireformat.unmarshal(dis);
74             }
75             catch (Exception JavaDoc e) {
76                 e.printStackTrace();
77                 fail("Failed to unmarshal object: " + i + ". Reason: " + e);
78             }
79             assertDataStructureExpected(command, i);
80         }
81
82         int marker = dis.readInt();
83         assertEquals("Marker int", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker));
84         
85         // lets try read and we should get an exception
86
try {
87             dis.readByte();
88             fail("Should have reached the end of the stream");
89         }
90         catch (IOException JavaDoc e) {
91             // worked!
92
}
93     }
94
95     protected void logProgress(String JavaDoc message, long i) {
96         if (i % logGroup == 0) {
97             System.out.println(message + " at object: " + i);
98         }
99     }
100
101     protected void setUp() throws Exception JavaDoc {
102         super.setUp();
103         openWireformat = createOpenWireFormat();
104     }
105
106     protected OpenWireFormat createOpenWireFormat() {
107         OpenWireFormat wf = new OpenWireFormat();
108         wf.setCacheEnabled(true);
109         wf.setStackTraceEnabled(true);
110         wf.setVersion(1);
111         return wf;
112     }
113
114     private void writeObject(Object JavaDoc object) throws IOException JavaDoc {
115         openWireformat.marshal(object, ds);
116     }
117     
118     protected DataStructure createDataStructure(long index) {
119         ProducerId id = new ProducerId();
120         id.setConnectionId(connectionId);
121         id.setSessionId(index);
122         id.setValue(index);
123         
124         ProducerInfo object = new ProducerInfo();
125         object.setProducerId(id);
126         object.setDestination(destination);
127         return object;
128     }
129     
130     protected void assertDataStructureExpected(DataStructure object, long i) {
131         assertEquals("Type of object for index: " + i, ProducerInfo.class, object.getClass());
132         ProducerInfo command = (ProducerInfo) object;
133         
134         ProducerId id = command.getProducerId();
135         assertNotNull("ProducerID for object at index: " + i, id);
136         
137         assertEquals("connection ID in object: "+ i, connectionId, id.getConnectionId());
138         String JavaDoc actual = Long.toHexString(id.getValue());
139         String JavaDoc expected = Long.toHexString(i);
140         assertEquals("value of object: "+ i + " was: " + actual, expected, actual);
141         assertEquals("value of object: "+ i + " was: " + actual, i, id.getSessionId());
142     }
143 }
144
Popular Tags