activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r383541 - in /incubator/activemq/trunk/activemq-core/src: main/java/org/apache/activemq/openwire/OpenWireFormat.java test/java/org/apache/activemq/openwire/MarshallingWithCachingTest.java
Date Mon, 06 Mar 2006 13:53:13 GMT
Author: jstrachan
Date: Mon Mar  6 05:53:11 2006
New Revision: 383541

URL: http://svn.apache.org/viewcvs?rev=383541&view=rev
Log:
added test case and patch for AMQ-617

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

Modified: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java?rev=383541&r1=383540&r2=383541&view=diff
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java
(original)
+++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java
Mon Mar  6 05:53:11 2006
@@ -365,7 +365,7 @@
     }
     
     public Short addToMarshallCache(Object o) {
-        Short index = new Short(nextMarshallCacheIndex++);
+        nextMarshallCacheIndex++;
         if( nextMarshallCacheIndex >= MARSHAL_CACHE_SIZE ) {
             nextMarshallCacheIndex=0;
         }
@@ -377,6 +377,8 @@
             marshallCacheMap.remove(marshallCache[lasMarshallCacheEvictionIndex]);
             marshallCache[lasMarshallCacheEvictionIndex]=null;
         }
+        marshallCache[nextMarshallCacheIndex] = (DataStructure) o;
+        Short index = new Short(nextMarshallCacheIndex);
         marshallCacheMap.put(o, index);
         return index;
     }

Added: incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/MarshallingWithCachingTest.java
URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/MarshallingWithCachingTest.java?rev=383541&view=auto
==============================================================================
--- incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/MarshallingWithCachingTest.java
(added)
+++ incubator/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/openwire/MarshallingWithCachingTest.java
Mon Mar  6 05:53:11 2006
@@ -0,0 +1,141 @@
+/**
+ *
+ * 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 org.apache.activemq.command.ActiveMQDestination;
+import org.apache.activemq.command.ActiveMQQueue;
+import org.apache.activemq.command.DataStructure;
+import org.apache.activemq.command.ProducerId;
+import org.apache.activemq.command.ProducerInfo;
+
+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 MarshallingWithCachingTest extends TestCase {
+
+    protected long commandCount = Short.MAX_VALUE;
+    protected String connectionId = "Cheese";
+    protected ActiveMQDestination destination = new ActiveMQQueue("Foo");
+    protected int endOfStreamMarker = 0x12345678;
+    
+    protected ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+    protected DataOutputStream ds = new DataOutputStream(buffer);
+    protected OpenWireFormat openWireformat;
+    protected long logGroup = 10000;
+    
+
+    
+    public void testReadAndWriteLotsOfCommands() throws Exception {
+        System.out.println("Marshalling: " + commandCount + " objects");
+        for (long i = 0; i < commandCount ; i++) {
+            logProgress("Marshalling", i);
+            DataStructure object = createDataStructure(i);
+            writeObject(object);
+        }
+        ds.writeInt(endOfStreamMarker);
+        
+        // now lets read from the stream
+        ds.close();
+        
+        System.out.println("Unmarshalling: " + commandCount + " objects");
+        
+        ByteArrayInputStream in = new ByteArrayInputStream(buffer.toByteArray());
+        DataInputStream dis = new DataInputStream(in);
+        for (long i = 0; i < commandCount ; i++) {
+            logProgress("Unmarshalling", i);
+            DataStructure command = null;
+            try {
+                command = (DataStructure) openWireformat.unmarshal(dis);
+            }
+            catch (Exception e) {
+                e.printStackTrace();
+                fail("Failed to unmarshal object: " + i + ". Reason: " + e);
+            }
+            assertDataStructureExpected(command, i);
+        }
+
+        int marker = dis.readInt();
+        assertEquals("Marker int", Integer.toHexString(endOfStreamMarker), Integer.toHexString(marker));
+        
+        // lets try read and we should get an exception
+        try {
+            dis.readByte();
+            fail("Should have reached the end of the stream");
+        }
+        catch (IOException e) {
+            // worked!
+        }
+    }
+
+    protected void logProgress(String message, long i) {
+        if (i % logGroup  == 0) {
+            System.out.println(message + " at object: " + i);
+        }
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        openWireformat = createOpenWireFormat();
+    }
+
+    protected OpenWireFormat createOpenWireFormat() {
+        OpenWireFormat wf = new OpenWireFormat();
+        wf.setCacheEnabled(true);
+        wf.setStackTraceEnabled(true);
+        wf.setVersion(1);
+        return wf;
+    }
+
+    private void writeObject(Object object) throws IOException {
+        openWireformat.marshal(object, ds);
+    }
+    
+    protected DataStructure createDataStructure(long index) {
+        ProducerId id = new ProducerId();
+        id.setConnectionId(connectionId);
+        id.setSessionId(index);
+        id.setValue(index);
+        
+        ProducerInfo object = new ProducerInfo();
+        object.setProducerId(id);
+        object.setDestination(destination);
+        return object;
+    }
+    
+    protected void assertDataStructureExpected(DataStructure object, long i) {
+        assertEquals("Type of object for index: " + i, ProducerInfo.class, object.getClass());
+        ProducerInfo command = (ProducerInfo) object;
+        
+        ProducerId id = command.getProducerId();
+        assertNotNull("ProducerID for object at index: " + i, id);
+        
+        assertEquals("connection ID in object: "+ i, connectionId, id.getConnectionId());
+        String actual = Long.toHexString(id.getValue());
+        String expected = Long.toHexString(i);
+        assertEquals("value of object: "+ i + " was: " + actual, expected, actual);
+        assertEquals("value of object: "+ i + " was: " + actual, i, id.getSessionId());
+    }
+}

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

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

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



Mime
View raw message