1 18 package org.apache.activemq.openwire; 19 20 import org.apache.activemq.command.SessionId; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.DataInputStream ; 27 import java.io.DataOutputStream ; 28 import java.io.IOException ; 29 30 import junit.framework.TestCase; 31 32 36 public class NumberRangesWhileMarshallingTest extends TestCase { 37 38 private static final Log log = LogFactory.getLog(NumberRangesWhileMarshallingTest.class); 39 40 protected String connectionId = "Cheese"; 41 protected ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 42 protected DataOutputStream ds = new DataOutputStream (buffer); 43 protected OpenWireFormat openWireformat; 44 protected int endOfStreamMarker = 0x12345678; 45 46 47 public void testLongNumberRanges() throws Exception { 48 long[] numberValues = { 49 0, 1, 0x7e, 0x7f, 0x80, 0x81, 0xf0, 0xff, 51 0x7eff, 0x7fffL, 0x8001L, 0x8000L, 0xe000L, 0xe0001L, 0xff00L, 0xffffL, 53 0x10000L, 55 0x700000L, 56 0x12345678L, 57 0x72345678L, 58 0x7fffffffL, 59 0x80000000L, 60 0x80000001L, 61 0xE0000001L, 62 0xFFFFFFFFL, 63 64 0x123456781L, 66 0x1234567812L, 67 0x12345678123L, 68 0x123456781234L, 69 0x1234567812345L, 70 0x12345678123456L, 71 0x7e345678123456L, 72 0x7fffffffffffffL, 73 0x80000000000000L, 74 0x80000000000001L, 75 0xe0000000000001L, 76 0xffffffffffffffL, 77 78 0x1234567812345678L, 80 0x7fffffffffffffffL, 81 0x8000000000000000L, 82 0x8000000000000001L, 83 0xe000000000000001L, 84 0xffffffffffffffffL, 85 1 }; 86 87 for (int i = 0; i < numberValues.length; i++) { 88 long value = numberValues[i]; 89 90 SessionId object = new SessionId(); 91 object.setConnectionId(connectionId); 92 object.setValue(value); 93 writeObject(object); 94 } 95 ds.writeInt(endOfStreamMarker); 96 97 ds.close(); 99 100 ByteArrayInputStream in = new ByteArrayInputStream (buffer.toByteArray()); 101 DataInputStream dis = new DataInputStream (in); 102 for (int i = 0; i < numberValues.length; i++) { 103 long value = numberValues[i]; 104 String expected = Long.toHexString(value); 105 log.info("Unmarshaling value: " + i + " = " + expected); 106 107 SessionId command = (SessionId) openWireformat.unmarshal(dis); 108 assertEquals("connection ID in object: "+ i, connectionId, command.getConnectionId()); 109 String actual = Long.toHexString(command.getValue()); 110 assertEquals("value of object: "+ i + " was: " + actual, expected, actual); 111 } 112 int marker = dis.readInt(); 113 assertEquals("Marker int", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker)); 114 115 try { 117 byte value = dis.readByte(); 118 fail("Should have reached the end of the stream"); 119 } 120 catch (IOException e) { 121 } 123 } 124 125 protected void setUp() throws Exception { 126 super.setUp(); 127 openWireformat = createOpenWireFormat(); 128 } 129 130 protected OpenWireFormat createOpenWireFormat() { 131 OpenWireFormat wf = new OpenWireFormat(); 132 wf.setCacheEnabled(true); 133 wf.setStackTraceEnabled(false); 134 wf.setVersion(1); 135 return wf; 136 } 137 138 private void writeObject(Object object) throws IOException { 139 openWireformat.marshal(object, ds); 140 } 141 } 142 | Popular Tags |