You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by el...@apache.org on 2011/03/17 23:28:01 UTC

svn commit: r1082736 - in /mina/branches/2.0.3/mina-core/src: main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java test/java/org/apache/mina/core/buffer/IoBufferTest.java

Author: elecharny
Date: Thu Mar 17 22:28:01 2011
New Revision: 1082736

URL: http://svn.apache.org/viewvc?rev=1082736&view=rev
Log:
Applied the porposed patch from DIRMINA-627. Should also fix DIRMINA-822/DIRMINA-824

Modified:
    mina/branches/2.0.3/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java
    mina/branches/2.0.3/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java

Modified: mina/branches/2.0.3/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java
URL: http://svn.apache.org/viewvc/mina/branches/2.0.3/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java?rev=1082736&r1=1082735&r2=1082736&view=diff
==============================================================================
--- mina/branches/2.0.3/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java (original)
+++ mina/branches/2.0.3/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java Thu Mar 17 22:28:01 2011
@@ -26,6 +26,7 @@ import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.OutputStream;
+import java.io.Serializable;
 import java.io.StreamCorruptedException;
 import java.nio.BufferOverflowException;
 import java.nio.BufferUnderflowException;
@@ -1955,9 +1956,9 @@ public abstract class AbstractIoBuffer e
                         throw new EOFException();
                     }
                     switch (type) {
-                    case 0: // Primitive types
+                    case 0: // NON-Serializable class or Primitive types
                         return super.readClassDescriptor();
-                    case 1: // Non-primitive types
+                    case 1: // Serializable class
                         String className = readUTF();
                         Class<?> clazz = Class.forName(className, true,
                                 classLoader);
@@ -1999,13 +2000,20 @@ public abstract class AbstractIoBuffer e
                 @Override
                 protected void writeClassDescriptor(ObjectStreamClass desc)
                         throws IOException {
-                    if (desc.forClass().isPrimitive()) {
+                    try {
+                        Class<?> clz = Class.forName(desc.getName());
+                        if (!Serializable.class.isAssignableFrom(clz)) { // NON-Serializable class
+                          write(0);
+                          super.writeClassDescriptor(desc);
+                        } else { // Serializable class
+                          write(1);
+                          writeUTF(desc.getName());
+                        }
+                      }
+                      catch (ClassNotFoundException ex) { // Primitive types
                         write(0);
                         super.writeClassDescriptor(desc);
-                    } else {
-                        write(1);
-                        writeUTF(desc.getName());
-                    }
+                      }
                 }
             };
             out.writeObject(o);

Modified: mina/branches/2.0.3/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java
URL: http://svn.apache.org/viewvc/mina/branches/2.0.3/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java?rev=1082736&r1=1082735&r2=1082736&view=diff
==============================================================================
--- mina/branches/2.0.3/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java (original)
+++ mina/branches/2.0.3/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferTest.java Thu Mar 17 22:28:01 2011
@@ -21,9 +21,14 @@ package org.apache.mina.core.buffer;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
 
 import org.junit.Test;
 
@@ -33,6 +38,13 @@ import org.junit.Test;
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
 public class IoBufferTest {
+
+    private static interface NonserializableInterface {
+    }
+
+    public static class NonserializableClass {
+    }
+
     @Test
     public void testNormalizeCapacity() {
         // A few sanity checks
@@ -72,7 +84,7 @@ public class IoBufferTest {
         }
 
         long time2 = System.currentTimeMillis();
-        System.out.println("Time for performance test 1: " + (time2 - time) + "ms");
+        //System.out.println("Time for performance test 1: " + (time2 - time) + "ms");
 
         // The second performance test measures the time to normalize integers
         // from Integer.MAX_VALUE to Integer.MAX_VALUE - 2^27 (it tests 2^27
@@ -89,7 +101,7 @@ public class IoBufferTest {
         }
 
         time2 = System.currentTimeMillis();
-        System.out.println("Time for performance test 2: " + (time2 - time) + "ms");
+        //System.out.println("Time for performance test 2: " + (time2 - time) + "ms");
     }
     
     @Test 
@@ -154,4 +166,54 @@ public class IoBufferTest {
         }
 
     }
+
+    @Test
+    public void testObjectSerialization() throws Exception {
+        IoBuffer buf = IoBuffer.allocate(16);
+        buf.setAutoExpand(true);
+        List<Object> o = new ArrayList<Object>();
+        o.add(new Date());
+        o.add(long.class);
+
+        // Test writing an object.
+        buf.putObject(o);
+
+        // Test reading an object.
+        buf.clear();
+        Object o2 = buf.getObject();
+        assertEquals(o, o2);
+
+        // This assertion is just to make sure that deserialization occurred.
+        assertNotSame(o, o2);
+    }
+    
+    @Test
+    public void testNonserializableClass() throws Exception {
+        Class<?> c = NonserializableClass.class;
+
+        IoBuffer buffer = IoBuffer.allocate(16);
+        buffer.setAutoExpand(true);
+        buffer.putObject(c);
+
+        buffer.flip();
+        Object o = buffer.getObject();
+
+        assertEquals(c, o);
+        assertSame(c, o);
+    }
+    
+    @Test
+    public void testNonserializableInterface() throws Exception {
+        Class<?> c = NonserializableInterface.class;
+
+        IoBuffer buffer = IoBuffer.allocate(16);
+        buffer.setAutoExpand(true);
+        buffer.putObject(c);
+
+        buffer.flip();
+        Object o = buffer.getObject();
+
+        assertEquals(c, o);
+        assertSame(c, o);
+    }
 }