1 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 ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.DataInputStream ; 30 import java.io.DataOutputStream ; 31 import java.io.IOException ; 32 33 import junit.framework.TestCase; 34 35 39 public class MarshallingWithCachingTest extends TestCase { 40 41 protected long commandCount = Short.MAX_VALUE; 42 protected String connectionId = "Cheese"; 43 protected ActiveMQDestination destination = new ActiveMQQueue("Foo"); 44 protected int endOfStreamMarker = 0x12345678; 45 46 protected ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 47 protected DataOutputStream ds = new DataOutputStream (buffer); 48 protected OpenWireFormat openWireformat; 49 protected long logGroup = 10000; 50 51 52 53 public void testReadAndWriteLotsOfCommands() throws Exception { 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 ds.close(); 64 65 System.out.println("Unmarshalling: " + commandCount + " objects"); 66 67 ByteArrayInputStream in = new ByteArrayInputStream (buffer.toByteArray()); 68 DataInputStream dis = new DataInputStream (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 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 try { 87 dis.readByte(); 88 fail("Should have reached the end of the stream"); 89 } 90 catch (IOException e) { 91 } 93 } 94 95 protected void logProgress(String 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 { 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 object) throws IOException { 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 actual = Long.toHexString(id.getValue()); 139 String 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 |