1 18 package org.apache.activemq.openwire; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.DataInputStream ; 23 import java.io.DataOutputStream ; 24 import java.io.IOException ; 25 26 import junit.framework.AssertionFailedError; 27 import junit.framework.TestCase; 28 29 33 public class BooleanStreamTest extends TestCase { 34 35 protected OpenWireFormat openWireformat; 36 protected int endOfStreamMarker = 0x12345678; 37 int numberOfBytes = 8 * 200; 38 39 interface BooleanValueSet { 40 public boolean getBooleanValueFor(int index, int count); 41 } 42 43 public void testBooleanMarshallingUsingAllTrue() throws Exception { 44 testBooleanStream(numberOfBytes, new BooleanValueSet() { 45 public boolean getBooleanValueFor(int index, int count) { 46 return true; 47 } 48 }); 49 } 50 51 public void testBooleanMarshallingUsingAllFalse() throws Exception { 52 testBooleanStream(numberOfBytes, new BooleanValueSet() { 53 public boolean getBooleanValueFor(int index, int count) { 54 return false; 55 } 56 }); 57 } 58 59 public void testBooleanMarshallingUsingOddAlternateTrueFalse() throws Exception { 60 testBooleanStream(numberOfBytes, new BooleanValueSet() { 61 public boolean getBooleanValueFor(int index, int count) { 62 return (index & 1) == 0; 63 } 64 }); 65 } 66 67 public void testBooleanMarshallingUsingEvenAlternateTrueFalse() throws Exception { 68 testBooleanStream(numberOfBytes, new BooleanValueSet() { 69 public boolean getBooleanValueFor(int index, int count) { 70 return (index & 1) != 0; 71 } 72 }); 73 } 74 75 protected void testBooleanStream(int numberOfBytes, BooleanValueSet valueSet) throws Exception { 76 for (int i = 0; i < numberOfBytes; i++) { 77 try { 78 assertMarshalBooleans(i, valueSet); 79 } catch (Throwable e) { 80 throw (AssertionFailedError) new AssertionFailedError("Iteration failed at: "+i).initCause(e); 81 } 82 } 83 } 84 85 protected void assertMarshalBooleans(int count, BooleanValueSet valueSet) throws Exception { 86 BooleanStream bs = new BooleanStream(); 87 for (int i = 0; i < count; i++) { 88 bs.writeBoolean(valueSet.getBooleanValueFor(i, count)); 89 } 90 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 91 DataOutputStream ds = new DataOutputStream (buffer); 92 bs.marshal(ds); 93 ds.writeInt(endOfStreamMarker); 94 95 ds.close(); 97 98 ByteArrayInputStream in = new ByteArrayInputStream (buffer.toByteArray()); 99 DataInputStream dis = new DataInputStream (in); 100 bs = new BooleanStream(); 101 try { 102 bs.unmarshal(dis); 103 } 104 catch (Exception e) { 105 e.printStackTrace(); 106 fail("Failed to unmarshal: " + count + " booleans: " + e); 107 } 108 109 for (int i = 0; i < count; i++) { 110 boolean expected = valueSet.getBooleanValueFor(i, count); 111 114 try { 115 boolean actual = bs.readBoolean(); 116 assertEquals("value of object: " + i + " was: " + actual, expected, actual); 117 } 118 catch (IOException e) { 119 e.printStackTrace(); 120 fail("Failed to parse boolean: " + i + " out of: " + count + " due to: " + e); 121 } 122 } 123 int marker = dis.readInt(); 124 assertEquals("Marker int when unmarshalling: " + count + " booleans", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker)); 125 126 try { 128 byte value = dis.readByte(); 129 fail("Should have reached the end of the stream"); 130 } 131 catch (IOException e) { 132 } 134 } 135 136 protected void setUp() throws Exception { 137 super.setUp(); 138 openWireformat = createOpenWireFormat(); 139 } 140 141 protected OpenWireFormat createOpenWireFormat() { 142 OpenWireFormat wf = new OpenWireFormat(); 143 wf.setCacheEnabled(true); 144 wf.setStackTraceEnabled(false); 145 wf.setVersion(1); 146 return wf; 147 } 148 149 } 150 | Popular Tags |