harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smish...@apache.org
Subject svn commit: r406887 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common: java/security/serialization/PermissionTest.java org/apache/harmony/security/support/SerializationTest.java
Date Tue, 16 May 2006 09:56:55 GMT
Author: smishura
Date: Tue May 16 02:56:52 2006
New Revision: 406887

URL: http://svn.apache.org/viewcvs?rev=406887&view=rev
Log:
Introducing SerializableAssert interface to compare (de)serialized objects + comparator for
Throwable objects

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/serialization/PermissionTest.java
  (contents, props changed)
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/support/SerializationTest.java
  (contents, props changed)

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/serialization/PermissionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/serialization/PermissionTest.java?rev=406887&r1=406886&r2=406887&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/serialization/PermissionTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/serialization/PermissionTest.java
Tue May 16 02:56:52 2006
@@ -21,6 +21,7 @@
 
 package java.security.serialization;
 
+import java.io.Serializable;
 import java.security.Permission;
 
 import org.apache.harmony.security.support.SerializationTest;
@@ -31,7 +32,8 @@
  * 
  */
 
-public class PermissionTest extends SerializationTest {
+public class PermissionTest extends SerializationTest implements
+        SerializationTest.SerializableAssert {
 
     /**
      * @see com.intel.drl.test.SerializationTest#getData()
@@ -41,7 +43,7 @@
                 new RealPermission("IYF&*%^sd 43") };
     }
 
-    protected void assertDeserialized(Object golden, Object test) {
+    public void assertDeserialized(Serializable golden, Serializable test) {
         assertSame(golden.getClass(), test.getClass());
         assertEquals(((Permission) golden).getName(), ((Permission) test)
                 .getName());

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/java/security/serialization/PermissionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/support/SerializationTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/support/SerializationTest.java?rev=406887&r1=406886&r2=406887&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/support/SerializationTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/support/SerializationTest.java
Tue May 16 02:56:52 2006
@@ -31,6 +31,8 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Method;
 
 import junit.framework.TestCase;
 
@@ -136,13 +138,18 @@
      * objects.
      */
     public void testSelf() throws Throwable {
+
+        SerializableAssert comparator = defineComparator();
+
         Object[] data = getData();
         for (int i = 0; i < data.length; i++) {
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
             putObjectToStream(data[i], bos);
             ByteArrayInputStream bis = new ByteArrayInputStream(bos
                 .toByteArray());
-            assertDeserialized(data[i], getObjectFromStream(bis));
+
+            comparator.assertDeserialized((Serializable) data[i],
+                    (Serializable) getObjectFromStream(bis));
         }
     }
 
@@ -151,11 +158,14 @@
      * compartibility with Reference Implementation.
      */
     public void testGolden() throws Throwable {
+        
+        SerializableAssert comparator = defineComparator();
+        
         Object[] data = getData();
         for (int i = 0; i < data.length; i++) {
-            assertDeserialized(data[i],
-                               getObjectFromStream(new FileInputStream(
-                                   getDataFile(i))));
+            comparator.assertDeserialized((Serializable) data[i],
+                    (Serializable) getObjectFromStream(new FileInputStream(
+                            getDataFile(i))));
         }
     }
 
@@ -214,5 +224,82 @@
         Object result = ois.readObject();
         ois.close();
         return result;
+    }
+    
+    /**
+     * Interface to compare (de)serialized objects
+     */
+    public interface SerializableAssert {
+        void assertDeserialized(Serializable reference, Serializable test);
+    }
+
+    // default comparator for a class that has equals(Object) method
+    private final static SerializableAssert DEFAULT_COMPARATOR = new SerializableAssert()
{
+        public void assertDeserialized(Serializable reference, Serializable test) {
+            TestCase.assertEquals(reference, test);
+        }
+    };
+
+    // for comparing java.lang.Throwable objects
+    private final static SerializableAssert THROWABLE_COMPARATOR = new SerializableAssert()
{
+        public void assertDeserialized(Serializable reference, Serializable test) {
+
+            Throwable refThr = (Throwable) reference;
+            Throwable tstThr = (Throwable) test;
+
+            // verify message
+            TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage());
+
+            // verify cause
+            if (refThr.getCause() == null) {
+                TestCase.assertNull(tstThr.getCause());
+            } else {
+                TestCase.assertNotNull(tstThr.getCause());
+
+                refThr = refThr.getCause();
+                tstThr = tstThr.getCause();
+
+                TestCase.assertEquals(refThr.getClass(), tstThr.getClass());
+                TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage());
+            }
+        }
+    };
+
+    private SerializableAssert defineComparator() throws Exception {
+
+        if (this instanceof SerializableAssert) {
+            return (SerializableAssert) this;
+        }
+
+        Object s[] = getData();
+        if (s == null || s.length == 0) {
+            // nothing to compare - OK with default comparator
+            return DEFAULT_COMPARATOR;
+
+        }
+
+        Method m = s[0].getClass().getMethod("equals", Object.class);
+
+        if (m.getDeclaringClass() != Object.class) {
+            // one of classes overrides Object.equals(Object) method
+            // use default comparator
+            return DEFAULT_COMPARATOR;
+        }
+
+        // TODO use generics to detect comparator
+        // instead of 'instanceof' for the first element
+        if(s[0] instanceof java.lang.Throwable){
+            return THROWABLE_COMPARATOR;
+        }
+
+        // TODO - throw new RuntimeException() if failed to detect comparator
+        // return stub comparator for a while
+        final SerializationTest thisTest = this;
+        return new SerializableAssert() {
+            public void assertDeserialized(Serializable reference,
+                    Serializable test) {
+                thisTest.assertDeserialized(reference, test);
+            }
+        };
     }
 }

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/org/apache/harmony/security/support/SerializationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message