KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > openwire > DataFileGeneratorTestSupport


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.openwire;
19
20 import java.beans.BeanInfo JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.DataInputStream JavaDoc;
26 import java.io.DataOutputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.lang.reflect.Array JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.net.URI JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.Set JavaDoc;
37
38 import org.apache.activemq.command.ActiveMQDestination;
39 import org.apache.activemq.command.ActiveMQQueue;
40 import org.apache.activemq.command.ActiveMQTextMessage;
41 import org.apache.activemq.command.BrokerId;
42 import org.apache.activemq.command.BrokerInfo;
43 import org.apache.activemq.command.ConnectionId;
44 import org.apache.activemq.command.ConsumerId;
45 import org.apache.activemq.command.DataStructure;
46 import org.apache.activemq.command.LocalTransactionId;
47 import org.apache.activemq.command.Message;
48 import org.apache.activemq.command.MessageAck;
49 import org.apache.activemq.command.MessageId;
50 import org.apache.activemq.command.NetworkBridgeFilter;
51 import org.apache.activemq.command.ProducerId;
52 import org.apache.activemq.command.SessionId;
53 import org.apache.activemq.command.TransactionId;
54 import org.apache.activemq.filter.BooleanExpression;
55 import org.apache.activemq.openwire.v1.ActiveMQTextMessageTest;
56 import org.apache.activemq.openwire.v1.BrokerInfoTest;
57 import org.apache.activemq.openwire.v1.MessageAckTest;
58 import org.apache.activemq.test.TestSupport;
59 import org.apache.activemq.util.ByteSequence;
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.logging.LogFactory;
62
63 public abstract class DataFileGeneratorTestSupport extends TestSupport {
64
65     private static final Log log = LogFactory.getLog(DataFileGeneratorTestSupport.class);
66     
67     protected static final Object JavaDoc[] EMPTY_ARGUMENTS = {};
68     private static Throwable JavaDoc singletonException = new Exception JavaDoc("shared exception");
69
70     static final File JavaDoc moduleBaseDir;
71     static final File JavaDoc controlDir;
72     static final File JavaDoc classFileDir;
73
74     static {
75         File JavaDoc basedir = null;
76         try {
77             URL JavaDoc resource = DataFileGeneratorTestSupport.class.getResource("DataFileGeneratorTestSupport.class");
78             URI JavaDoc baseURI = new URI JavaDoc(resource.toString()).resolve("../../../../..");
79             basedir = new File JavaDoc(baseURI).getCanonicalFile();
80         }
81         catch (Exception JavaDoc e) {
82             throw new RuntimeException JavaDoc(e);
83         }
84         moduleBaseDir = basedir;
85         controlDir = new File JavaDoc(moduleBaseDir, "src/test/resources/openwire-control");
86         classFileDir = new File JavaDoc(moduleBaseDir, "src/test/java/org/activemq/openwire");
87     }
88
89     private int counter;
90     private OpenWireFormat openWireformat;
91
92     public void XXXX_testControlFileIsValid() throws Exception JavaDoc {
93         generateControlFile();
94         assertControlFileIsEqual();
95     }
96
97     public void testGenerateAndReParsingIsTheSame() throws Exception JavaDoc {
98         ByteArrayOutputStream JavaDoc buffer = new ByteArrayOutputStream JavaDoc();
99         DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(buffer);
100         Object JavaDoc expected = createObject();
101         log.info("Created: " + expected);
102         openWireformat.marshal(expected, ds);
103         ds.close();
104
105         // now lets try parse it back again
106
ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(buffer.toByteArray());
107         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(in);
108         Object JavaDoc actual = openWireformat.unmarshal(dis);
109
110         log.info("Parsed: " + actual);
111
112         assertBeansEqual("", new HashSet JavaDoc(), expected, actual);
113     }
114
115     protected void assertBeansEqual(String JavaDoc message, Set JavaDoc comparedObjects, Object JavaDoc expected, Object JavaDoc actual) throws Exception JavaDoc {
116         assertNotNull("Actual object should be equal to: " + expected + " but was null", actual);
117         if (comparedObjects.contains(expected)) {
118             return;
119         }
120         comparedObjects.add(expected);
121         Class JavaDoc type = expected.getClass();
122         assertEquals("Should be of same type", type, actual.getClass());
123         BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(type);
124         PropertyDescriptor JavaDoc[] descriptors = beanInfo.getPropertyDescriptors();
125         for (int i = 0; i < descriptors.length; i++) {
126             PropertyDescriptor JavaDoc descriptor = descriptors[i];
127             Method JavaDoc method = descriptor.getReadMethod();
128             if (method != null) {
129                 String JavaDoc name = descriptor.getName();
130                 Object JavaDoc expectedValue = null;
131                 Object JavaDoc actualValue = null;
132                 try {
133                     expectedValue = method.invoke(expected, EMPTY_ARGUMENTS);
134                     actualValue = method.invoke(actual, EMPTY_ARGUMENTS);
135                 }
136                 catch (Exception JavaDoc e) {
137                     log.info("Failed to access property: " + name);
138                 }
139                 assertPropertyValuesEqual(message + name, comparedObjects, expectedValue, actualValue);
140             }
141         }
142     }
143
144     protected void assertPropertyValuesEqual(String JavaDoc name, Set JavaDoc comparedObjects, Object JavaDoc expectedValue, Object JavaDoc actualValue) throws Exception JavaDoc {
145         String JavaDoc message = "Property " + name + " not equal";
146         if (expectedValue == null) {
147             assertNull("Property " + name + " should be null", actualValue);
148         }
149         else if (expectedValue instanceof Object JavaDoc[]) {
150             assertArrayEqual(message, comparedObjects, (Object JavaDoc[]) expectedValue, (Object JavaDoc[]) actualValue);
151         }
152         else if (expectedValue.getClass().isArray()) {
153             assertPrimitiveArrayEqual(message, comparedObjects, expectedValue, actualValue);
154         }
155         else {
156             if (expectedValue instanceof Exception JavaDoc) {
157                 assertExceptionsEqual(message, (Exception JavaDoc) expectedValue, actualValue);
158             }
159             else if (expectedValue instanceof ByteSequence) {
160                 assertByteSequencesEqual(message, (ByteSequence) expectedValue, actualValue);
161             }
162             else if (expectedValue instanceof DataStructure) {
163                 assertBeansEqual(message + name, comparedObjects, expectedValue, actualValue);
164             }
165             else {
166                 assertEquals(message, expectedValue, actualValue);
167             }
168                 
169         }
170     }
171
172     protected void assertArrayEqual(String JavaDoc message,Set JavaDoc comparedObjects, Object JavaDoc[] expected, Object JavaDoc[] actual) throws Exception JavaDoc {
173         assertEquals(message + ". Array length", expected.length, actual.length);
174         for (int i = 0; i < expected.length; i++) {
175             assertPropertyValuesEqual(message + ". element: " + i, comparedObjects, expected[i], actual[i]);
176         }
177     }
178     
179     protected void assertPrimitiveArrayEqual(String JavaDoc message, Set JavaDoc comparedObjects, Object JavaDoc expected, Object JavaDoc actual) throws ArrayIndexOutOfBoundsException JavaDoc, IllegalArgumentException JavaDoc, Exception JavaDoc {
180         int length = Array.getLength(expected);
181         assertEquals(message + ". Array length", length, Array.getLength(actual));
182         for (int i = 0; i < length; i++) {
183             assertPropertyValuesEqual(message + ". element: " + i, comparedObjects, Array.get(expected, i), Array.get(actual, i));
184         }
185     }
186     protected void assertByteSequencesEqual(String JavaDoc message, ByteSequence expected, Object JavaDoc actualValue) {
187         assertTrue(message + ". Actual value should be a ByteSequence but was: " + actualValue, actualValue instanceof ByteSequence);
188         ByteSequence actual = (ByteSequence) actualValue;
189         int length = expected.getLength();
190         assertEquals(message + ". Length", length, actual.getLength());
191         int offset = expected.getOffset();
192         assertEquals(message + ". Offset", offset, actual.getOffset());
193         byte[] data = expected.getData();
194         byte[] actualData = actual.getData();
195         for (int i = 0; i < length; i++) {
196             assertEquals(message + ". Offset " + i, data[offset + i], actualData[offset + i]);
197         }
198     }
199
200     protected void assertExceptionsEqual(String JavaDoc message, Exception JavaDoc expected, Object JavaDoc actualValue) {
201         assertTrue(message + ". Actual value should be an exception but was: " + actualValue, actualValue instanceof Exception JavaDoc);
202         Exception JavaDoc actual = (Exception JavaDoc) actualValue;
203         assertEquals(message, expected.getMessage(), actual.getMessage());
204     }
205
206     protected void setUp() throws Exception JavaDoc {
207         super.setUp();
208         openWireformat = createOpenWireFormat();
209     }
210
211     public void generateControlFile() throws Exception JavaDoc {
212         controlDir.mkdirs();
213         File JavaDoc dataFile = new File JavaDoc(controlDir, getClass().getName() + ".bin");
214
215         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(dataFile);
216         DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(os);
217         openWireformat.marshal(createObject(), ds);
218         ds.close();
219     }
220
221     public InputStream JavaDoc generateInputStream() throws Exception JavaDoc {
222
223         ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
224         DataOutputStream JavaDoc ds = new DataOutputStream JavaDoc(os);
225         openWireformat.marshal(createObject(), ds);
226         ds.close();
227
228         return new ByteArrayInputStream JavaDoc(os.toByteArray());
229     }
230
231     public void assertControlFileIsEqual() throws Exception JavaDoc {
232         File JavaDoc dataFile = new File JavaDoc(controlDir, getClass().getName() + ".bin");
233         FileInputStream JavaDoc is1 = new FileInputStream JavaDoc(dataFile);
234         int pos = 0;
235         try {
236             InputStream JavaDoc is2 = generateInputStream();
237             int a = is1.read();
238             int b = is2.read();
239             pos++;
240             assertEquals("Data does not match control file: " + dataFile + " at byte position " + pos, a, b);
241             while (a >= 0 && b >= 0) {
242                 a = is1.read();
243                 b = is2.read();
244                 pos++;
245                 assertEquals("Data does not match control file: " + dataFile + " at byte position " + pos, a, b);
246             }
247             is2.close();
248         }
249         finally {
250             is1.close();
251         }
252     }
253
254     protected abstract Object JavaDoc createObject() throws Exception JavaDoc;
255
256     protected void populateObject(Object JavaDoc info) throws Exception JavaDoc {
257         // empty method to allow derived classes to call super
258
// to simplify generated code
259
}
260
261     protected OpenWireFormat createOpenWireFormat() {
262         OpenWireFormat wf = new OpenWireFormat();
263         wf.setCacheEnabled(true);
264         wf.setStackTraceEnabled(false);
265         wf.setVersion(OpenWireFormat.DEFAULT_VERSION);
266         return wf;
267     }
268
269     protected BrokerId createBrokerId(String JavaDoc text) {
270         return new BrokerId(text);
271     }
272
273     protected TransactionId createTransactionId(String JavaDoc string) {
274         return new LocalTransactionId(createConnectionId(string), ++counter);
275     }
276
277     protected ConnectionId createConnectionId(String JavaDoc string) {
278         return new ConnectionId(string);
279     }
280
281     protected SessionId createSessionId(String JavaDoc string) {
282         return new SessionId(createConnectionId(string), ++counter);
283     }
284
285     protected ProducerId createProducerId(String JavaDoc string) {
286         return new ProducerId(createSessionId(string), ++counter);
287     }
288
289     protected ConsumerId createConsumerId(String JavaDoc string) {
290         return new ConsumerId(createSessionId(string), ++counter);
291     }
292
293     protected MessageId createMessageId(String JavaDoc string) {
294         return new MessageId(createProducerId(string), ++counter);
295     }
296
297     protected ActiveMQDestination createActiveMQDestination(String JavaDoc string) {
298         return new ActiveMQQueue(string);
299     }
300
301     protected Message createMessage(String JavaDoc string) throws Exception JavaDoc {
302         ActiveMQTextMessage message = (ActiveMQTextMessage) ActiveMQTextMessageTest.SINGLETON.createObject();
303         message.setText(string);
304         return message;
305     }
306
307     protected BrokerInfo createBrokerInfo(String JavaDoc string) throws Exception JavaDoc {
308         return (BrokerInfo) BrokerInfoTest.SINGLETON.createObject();
309     }
310
311     protected MessageAck createMessageAck(String JavaDoc string) throws Exception JavaDoc {
312         return (MessageAck) MessageAckTest.SINGLETON.createObject();
313     }
314
315     protected DataStructure createDataStructure(String JavaDoc string) throws Exception JavaDoc {
316         return createBrokerInfo(string);
317     }
318
319     protected Throwable JavaDoc createThrowable(String JavaDoc string) {
320         // we have issues with stack frames not being equal so share the same
321
// exception each time
322
return singletonException;
323     }
324     
325     protected BooleanExpression createBooleanExpression(String JavaDoc string) {
326         return new NetworkBridgeFilter(new BrokerId(string), 10);
327     }
328
329 }
330
Popular Tags