1 18 package org.apache.activemq.openwire; 19 20 import java.beans.BeanInfo ; 21 import java.beans.Introspector ; 22 import java.beans.PropertyDescriptor ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.DataInputStream ; 26 import java.io.DataOutputStream ; 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.InputStream ; 31 import java.lang.reflect.Array ; 32 import java.lang.reflect.Method ; 33 import java.net.URI ; 34 import java.net.URL ; 35 import java.util.HashSet ; 36 import java.util.Set ; 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 [] EMPTY_ARGUMENTS = {}; 68 private static Throwable singletonException = new Exception ("shared exception"); 69 70 static final File moduleBaseDir; 71 static final File controlDir; 72 static final File classFileDir; 73 74 static { 75 File basedir = null; 76 try { 77 URL resource = DataFileGeneratorTestSupport.class.getResource("DataFileGeneratorTestSupport.class"); 78 URI baseURI = new URI (resource.toString()).resolve("../../../../.."); 79 basedir = new File (baseURI).getCanonicalFile(); 80 } 81 catch (Exception e) { 82 throw new RuntimeException (e); 83 } 84 moduleBaseDir = basedir; 85 controlDir = new File (moduleBaseDir, "src/test/resources/openwire-control"); 86 classFileDir = new File (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 { 93 generateControlFile(); 94 assertControlFileIsEqual(); 95 } 96 97 public void testGenerateAndReParsingIsTheSame() throws Exception { 98 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 99 DataOutputStream ds = new DataOutputStream (buffer); 100 Object expected = createObject(); 101 log.info("Created: " + expected); 102 openWireformat.marshal(expected, ds); 103 ds.close(); 104 105 ByteArrayInputStream in = new ByteArrayInputStream (buffer.toByteArray()); 107 DataInputStream dis = new DataInputStream (in); 108 Object actual = openWireformat.unmarshal(dis); 109 110 log.info("Parsed: " + actual); 111 112 assertBeansEqual("", new HashSet (), expected, actual); 113 } 114 115 protected void assertBeansEqual(String message, Set comparedObjects, Object expected, Object actual) throws Exception { 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 type = expected.getClass(); 122 assertEquals("Should be of same type", type, actual.getClass()); 123 BeanInfo beanInfo = Introspector.getBeanInfo(type); 124 PropertyDescriptor [] descriptors = beanInfo.getPropertyDescriptors(); 125 for (int i = 0; i < descriptors.length; i++) { 126 PropertyDescriptor descriptor = descriptors[i]; 127 Method method = descriptor.getReadMethod(); 128 if (method != null) { 129 String name = descriptor.getName(); 130 Object expectedValue = null; 131 Object actualValue = null; 132 try { 133 expectedValue = method.invoke(expected, EMPTY_ARGUMENTS); 134 actualValue = method.invoke(actual, EMPTY_ARGUMENTS); 135 } 136 catch (Exception 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 name, Set comparedObjects, Object expectedValue, Object actualValue) throws Exception { 145 String message = "Property " + name + " not equal"; 146 if (expectedValue == null) { 147 assertNull("Property " + name + " should be null", actualValue); 148 } 149 else if (expectedValue instanceof Object []) { 150 assertArrayEqual(message, comparedObjects, (Object []) expectedValue, (Object []) actualValue); 151 } 152 else if (expectedValue.getClass().isArray()) { 153 assertPrimitiveArrayEqual(message, comparedObjects, expectedValue, actualValue); 154 } 155 else { 156 if (expectedValue instanceof Exception ) { 157 assertExceptionsEqual(message, (Exception ) 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 message,Set comparedObjects, Object [] expected, Object [] actual) throws Exception { 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 message, Set comparedObjects, Object expected, Object actual) throws ArrayIndexOutOfBoundsException , IllegalArgumentException , Exception { 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 message, ByteSequence expected, Object 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 message, Exception expected, Object actualValue) { 201 assertTrue(message + ". Actual value should be an exception but was: " + actualValue, actualValue instanceof Exception ); 202 Exception actual = (Exception ) actualValue; 203 assertEquals(message, expected.getMessage(), actual.getMessage()); 204 } 205 206 protected void setUp() throws Exception { 207 super.setUp(); 208 openWireformat = createOpenWireFormat(); 209 } 210 211 public void generateControlFile() throws Exception { 212 controlDir.mkdirs(); 213 File dataFile = new File (controlDir, getClass().getName() + ".bin"); 214 215 FileOutputStream os = new FileOutputStream (dataFile); 216 DataOutputStream ds = new DataOutputStream (os); 217 openWireformat.marshal(createObject(), ds); 218 ds.close(); 219 } 220 221 public InputStream generateInputStream() throws Exception { 222 223 ByteArrayOutputStream os = new ByteArrayOutputStream (); 224 DataOutputStream ds = new DataOutputStream (os); 225 openWireformat.marshal(createObject(), ds); 226 ds.close(); 227 228 return new ByteArrayInputStream (os.toByteArray()); 229 } 230 231 public void assertControlFileIsEqual() throws Exception { 232 File dataFile = new File (controlDir, getClass().getName() + ".bin"); 233 FileInputStream is1 = new FileInputStream (dataFile); 234 int pos = 0; 235 try { 236 InputStream 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 createObject() throws Exception ; 255 256 protected void populateObject(Object info) throws Exception { 257 } 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 text) { 270 return new BrokerId(text); 271 } 272 273 protected TransactionId createTransactionId(String string) { 274 return new LocalTransactionId(createConnectionId(string), ++counter); 275 } 276 277 protected ConnectionId createConnectionId(String string) { 278 return new ConnectionId(string); 279 } 280 281 protected SessionId createSessionId(String string) { 282 return new SessionId(createConnectionId(string), ++counter); 283 } 284 285 protected ProducerId createProducerId(String string) { 286 return new ProducerId(createSessionId(string), ++counter); 287 } 288 289 protected ConsumerId createConsumerId(String string) { 290 return new ConsumerId(createSessionId(string), ++counter); 291 } 292 293 protected MessageId createMessageId(String string) { 294 return new MessageId(createProducerId(string), ++counter); 295 } 296 297 protected ActiveMQDestination createActiveMQDestination(String string) { 298 return new ActiveMQQueue(string); 299 } 300 301 protected Message createMessage(String string) throws Exception { 302 ActiveMQTextMessage message = (ActiveMQTextMessage) ActiveMQTextMessageTest.SINGLETON.createObject(); 303 message.setText(string); 304 return message; 305 } 306 307 protected BrokerInfo createBrokerInfo(String string) throws Exception { 308 return (BrokerInfo) BrokerInfoTest.SINGLETON.createObject(); 309 } 310 311 protected MessageAck createMessageAck(String string) throws Exception { 312 return (MessageAck) MessageAckTest.SINGLETON.createObject(); 313 } 314 315 protected DataStructure createDataStructure(String string) throws Exception { 316 return createBrokerInfo(string); 317 } 318 319 protected Throwable createThrowable(String string) { 320 return singletonException; 323 } 324 325 protected BooleanExpression createBooleanExpression(String string) { 326 return new NetworkBridgeFilter(new BrokerId(string), 10); 327 } 328 329 } 330 | Popular Tags |