1 18 package org.apache.activemq.command; 19 20 import java.io.IOException ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.Arrays ; 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 original) throws IOException { 38 Object o = marshalAndUnmarshall(original, wireFormat); 39 assertNotNull(o); 40 assertEquals(original, o); 41 } 63 64 static public void assertEquals(Object expect, Object 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 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 expectArray[] = (Object []) expect; 102 Object wasArray[] = (Object []) 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 [] methods = expect.getClass().getMethods(); 114 for (int i = 0; i < methods.length; i++) { 115 Method method = methods[i]; 116 if ((method.getName().startsWith("get") || method.getName().startsWith("is")) && method.getParameterTypes().length == 0 117 && method.getReturnType() != null) { 118 119 try { 121 if (method.getName().startsWith("get")) { 122 expect.getClass().getMethod(method.getName().replaceFirst("get", "set"), new Class [] { method.getReturnType() }); 123 } 124 else { 125 expect.getClass().getMethod(method.getName().replaceFirst("is", "set"), new Class [] { method.getReturnType() }); 126 } 127 } 128 catch (Throwable ignore) { 129 continue; 130 } 131 132 try { 133 assertEquals(method.invoke(expect, null), method.invoke(was, null)); 134 } 135 catch (IllegalArgumentException e) { 136 } 137 catch (IllegalAccessException e) { 138 } 139 catch (InvocationTargetException e) { 140 } 141 } 142 } 143 } 144 else { 145 TestCase.assertEquals(expect, was); 146 } 147 } 148 149 protected void setUp() throws Exception { 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 marshalAndUnmarshall(Object original, WireFormat wireFormat) throws IOException { 161 ByteSequence packet = wireFormat.marshal(original); 162 return wireFormat.unmarshal(packet); 163 } 164 165 } 166 | Popular Tags |