KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > DataStructureTestSupport


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.command;
19
20 import java.io.IOException JavaDoc;
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.Arrays JavaDoc;
24
25 import junit.framework.AssertionFailedError;
26 import junit.framework.TestCase;
27
28 import org.apache.activemq.CombinationTestSupport;
29 import org.apache.activemq.openwire.OpenWireFormat;
30 import org.apache.activemq.util.ByteSequence;
31 import org.apache.activemq.wireformat.WireFormat;
32
33 public abstract class DataStructureTestSupport extends CombinationTestSupport {
34     public boolean cacheEnabled;
35     public WireFormat wireFormat;
36
37     public void assertBeanMarshalls(Object JavaDoc original) throws IOException JavaDoc {
38         Object JavaDoc o = marshalAndUnmarshall(original, wireFormat);
39         assertNotNull(o);
40         assertEquals(original, o);
41         // assertEquals(original.getClass(), o.getClass());
42
//
43
// Method[] methods = original.getClass().getMethods();
44
// for (int i = 0; i < methods.length; i++) {
45
// Method method = methods[i];
46
// if( ( method.getName().startsWith("get")
47
// || method.getName().startsWith("is")
48
// )
49
// && method.getParameterTypes().length==0
50
// && method.getReturnType()!=null
51
// ) {
52
// try {
53
// Object expect = method.invoke(original, null);
54
// Object was = method.invoke(o, null);
55
// assertEquals(expect, was);
56
// } catch (IllegalArgumentException e) {
57
// } catch (IllegalAccessException e) {
58
// } catch (InvocationTargetException e) {
59
// }
60
// }
61
// }
62
}
63
64     static public void assertEquals(Object JavaDoc expect, Object JavaDoc was) {
65         if (expect == null ^ was == null)
66             throw new AssertionFailedError("Not equals, expected: " + expect + ", was: " + was);
67         if (expect == null)
68             return;
69         if (expect.getClass() != was.getClass())
70             throw new AssertionFailedError("Not equals, classes don't match. expected: " + expect.getClass() + ", was: " + was.getClass());
71         if (expect.getClass().isArray()) {
72             Class JavaDoc componentType = expect.getClass().getComponentType();
73             if (componentType.isPrimitive()) {
74                 boolean ok = false;
75                 if (componentType == byte.class) {
76                     ok = Arrays.equals((byte[]) expect, (byte[]) was);
77                 }
78                 if (componentType == char.class) {
79                     ok = Arrays.equals((char[]) expect, (char[]) was);
80                 }
81                 if (componentType == short.class) {
82                     ok = Arrays.equals((short[]) expect, (short[]) was);
83                 }
84                 if (componentType == int.class) {
85                     ok = Arrays.equals((int[]) expect, (int[]) was);
86                 }
87                 if (componentType == long.class) {
88                     ok = Arrays.equals((long[]) expect, (long[]) was);
89                 }
90                 if (componentType == double.class) {
91                     ok = Arrays.equals((double[]) expect, (double[]) was);
92                 }
93                 if (componentType == float.class) {
94                     ok = Arrays.equals((float[]) expect, (float[]) was);
95                 }
96                 if (!ok) {
97                     throw new AssertionFailedError("Arrays not equal");
98                 }
99             }
100             else {
101                 Object JavaDoc expectArray[] = (Object JavaDoc[]) expect;
102                 Object JavaDoc wasArray[] = (Object JavaDoc[]) was;
103                 if (expectArray.length != wasArray.length)
104                     throw new AssertionFailedError("Not equals, array lengths don't match. expected: " + expectArray.length + ", was: " + wasArray.length);
105                 for (int i = 0; i < wasArray.length; i++) {
106                     assertEquals(expectArray[i], wasArray[i]);
107                 }
108
109             }
110         }
111         else if (expect instanceof Command) {
112             assertEquals(expect.getClass(), was.getClass());
113             Method JavaDoc[] methods = expect.getClass().getMethods();
114             for (int i = 0; i < methods.length; i++) {
115                 Method JavaDoc method = methods[i];
116                 if ((method.getName().startsWith("get") || method.getName().startsWith("is")) && method.getParameterTypes().length == 0
117                         && method.getReturnType() != null) {
118
119                     // Check to see if there is a setter for the method.
120
try {
121                         if (method.getName().startsWith("get")) {
122                             expect.getClass().getMethod(method.getName().replaceFirst("get", "set"), new Class JavaDoc[] { method.getReturnType() });
123                         }
124                         else {
125                             expect.getClass().getMethod(method.getName().replaceFirst("is", "set"), new Class JavaDoc[] { method.getReturnType() });
126                         }
127                     }
128                     catch (Throwable JavaDoc ignore) {
129                         continue;
130                     }
131
132                     try {
133                         assertEquals(method.invoke(expect, null), method.invoke(was, null));
134                     }
135                     catch (IllegalArgumentException JavaDoc e) {
136                     }
137                     catch (IllegalAccessException JavaDoc e) {
138                     }
139                     catch (InvocationTargetException JavaDoc e) {
140                     }
141                 }
142             }
143         }
144         else {
145             TestCase.assertEquals(expect, was);
146         }
147     }
148
149     protected void setUp() throws Exception JavaDoc {
150         wireFormat = createWireFormat();
151         super.setUp();
152     }
153     
154     protected WireFormat createWireFormat() {
155         OpenWireFormat answer = new OpenWireFormat();
156         answer.setCacheEnabled(cacheEnabled);
157         return answer;
158     }
159
160     protected Object JavaDoc marshalAndUnmarshall(Object JavaDoc original, WireFormat wireFormat) throws IOException JavaDoc {
161         ByteSequence packet = wireFormat.marshal(original);
162         return wireFormat.unmarshal(packet);
163     }
164
165 }
166
Popular Tags