You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2006/03/02 17:03:18 UTC

svn commit: r382428 - in /incubator/activemq/trunk/activemq-core/src: main/java/org/apache/activemq/openwire/BooleanStream.java test/java/org/apache/activemq/openwire/BooleanStreamTest.java

Author: jstrachan
Date: Thu Mar  2 08:03:17 2006
New Revision: 382428

URL: http://svn.apache.org/viewcvs?rev=382428&view=rev
Log:
added test case for the BooleanStream together with the patch from Dave Gynn

Added:
    incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java   (with props)
Modified:
    incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/BooleanStream.java

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/BooleanStream.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/BooleanStream.java?rev=382428&r1=382427&r2=382428&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/BooleanStream.java (original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/BooleanStream.java Thu Mar  2 08:03:17 2006
@@ -66,7 +66,7 @@
             dataOut.writeByte(0xC0);
             dataOut.writeByte(arrayLimit);            
         } else {
-            dataOut.writeByte(0xE0);
+            dataOut.writeByte(0x80);
             dataOut.writeShort(arrayLimit);            
         }
         
@@ -77,7 +77,7 @@
     public void unmarshal(DataInputStream dataIn) throws IOException {
         
         arrayLimit = dataIn.readByte();
-        if( (arrayLimit & 0xE0)!=0 ) {
+        if( (arrayLimit & 0x80)!=0 ) {
             arrayLimit = dataIn.readShort();
         } else if ( (arrayLimit & 0xC0)!=0 ) {
             arrayLimit = (short)(dataIn.readByte() & 0xFF);

Added: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java?rev=382428&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java (added)
+++ incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java Thu Mar  2 08:03:17 2006
@@ -0,0 +1,135 @@
+/**
+ *
+ * Copyright 2005-2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.openwire;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ * @version $Revision$
+ */
+public class BooleanStreamTest extends TestCase {
+
+    protected OpenWireFormat openWireformat;
+    protected int endOfStreamMarker = 0x12345678;
+    int numberOfBytes = 8 * 50;
+
+    interface BooleanValueSet {
+        public boolean getBooleanValueFor(int index, int count);
+    }
+
+    public void testBooleanMarshallingUsingAllTrue() throws Exception {
+        testBooleanStream(numberOfBytes, new BooleanValueSet() {
+            public boolean getBooleanValueFor(int index, int count) {
+                return true;
+            }
+        });
+    }
+
+    public void testBooleanMarshallingUsingAllFalse() throws Exception {
+        testBooleanStream(numberOfBytes, new BooleanValueSet() {
+            public boolean getBooleanValueFor(int index, int count) {
+                return false;
+            }
+        });
+    }
+
+    public void testBooleanMarshallingUsingAlternateTrueFalse() throws Exception {
+        testBooleanStream(numberOfBytes, new BooleanValueSet() {
+            public boolean getBooleanValueFor(int index, int count) {
+                return (index & 1) == 0;
+            }
+        });
+    }
+
+    protected void testBooleanStream(int numberOfBytes, BooleanValueSet valueSet) throws Exception {
+        for (int i = 0; i < numberOfBytes; i++) {
+            assertMarshalBooleans(i, valueSet);
+        }
+    }
+
+    protected void assertMarshalBooleans(int count, BooleanValueSet valueSet) throws Exception {
+        BooleanStream bs = new BooleanStream();
+        for (int i = 0; i < count; i++) {
+            bs.writeBoolean(valueSet.getBooleanValueFor(i, count));
+        }
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        DataOutputStream ds = new DataOutputStream(buffer);
+        bs.marshal(ds);
+        ds.writeInt(endOfStreamMarker);
+
+        // now lets read from the stream
+        ds.close();
+
+        ByteArrayInputStream in = new ByteArrayInputStream(buffer.toByteArray());
+        DataInputStream dis = new DataInputStream(in);
+        bs = new BooleanStream();
+        try {
+            bs.unmarshal(dis);
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            fail("Failed to unmarshal: " + count + " booleans: " + e);
+        }
+
+        for (int i = 0; i < count; i++) {
+            boolean expected = valueSet.getBooleanValueFor(i, count);
+            // /System.out.println("Unmarshaling value: " + i + " = " + expected
+            // + " out of: " + count);
+
+            try {
+                boolean actual = bs.readBoolean();
+                assertEquals("value of object: " + i + " was: " + actual, expected, actual);
+            }
+            catch (IOException e) {
+                e.printStackTrace();
+                fail("Failed to parse boolean: " + i + " out of: " + count + " due to: " + e);
+            }
+        }
+        int marker = dis.readInt();
+        assertEquals("Marker int when unmarshalling: " + count + " booleans", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker));
+
+        // lets try read and we should get an exception
+        try {
+            byte value = dis.readByte();
+            fail("Should have reached the end of the stream");
+        }
+        catch (IOException e) {
+            // worked!
+        }
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        openWireformat = createOpenWireFormat();
+    }
+
+    protected OpenWireFormat createOpenWireFormat() {
+        OpenWireFormat wf = new OpenWireFormat();
+        wf.setCacheEnabled(true);
+        wf.setStackTraceEnabled(false);
+        wf.setVersion(1);
+        return wf;
+    }
+
+}

Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/BooleanStreamTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain