harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r562054 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/ test/java/org/apache/harmony/beans/tests/java/beans/ test/support/java/org/apache/harmony/beans/tests/support/mock/
Date Thu, 02 Aug 2007 09:37:06 GMT
Author: tonywu
Date: Thu Aug  2 02:37:05 2007
New Revision: 562054

URL: http://svn.apache.org/viewvc?view=rev&rev=562054
Log:
Fix bugs: 
1, Harmony throws StreamCorruptedException and exit while RI move on to create a new instance
when encounter an corrupt ser file
2, Harmony throws IOException while RI throws ClassNotFoundException

Added:
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.ser
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/NotExistBean.ser
  (with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.ser
  (with props)
    harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean2.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java?view=diff&rev=562054&r1=562053&r2=562054
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/Beans.java Thu
Aug  2 02:37:05 2007
@@ -161,34 +161,38 @@
 
 		ClassLoader loader = null;
 
-		String beanResourceName = getBeanResourceName(beanName);
+		// First try to load it from a serialization file.
+        String beanResourceName = getBeanResourceName(beanName);
 
 		InputStream is = (cls == null) ? ClassLoader
 				.getSystemResourceAsStream(beanResourceName) : cls
 				.getResourceAsStream(beanResourceName);
 
-		if (is != null) {
-			ObjectInputStream ois = (cls == null) ? new ObjectInputStream(is)
-					: new CustomizedObjectInputStream(is, cls);
-			result = ois.readObject();
+		IOException serializationException = null;
+        if (is != null) {
+            try{
+                ObjectInputStream ois = (cls == null) ? new ObjectInputStream(is)
+					    : new CustomizedObjectInputStream(is, cls);
+                result = ois.readObject();
+            }catch(IOException exception){
+                //Not loadable - remember this as we may throw it later.
+                serializationException = exception;
+            }
 		}
 
-		if (result == null) {
+		//If that did not work, try to instantiate it from the given classloader.
+        if (result == null) {
 			deserialized = false;
 			try {
 				loader = cls == null ? ClassLoader.getSystemClassLoader() : cls;
 				Class<?> c = Class.forName(beanName, true, loader);
-
-				try {
-					result = c.newInstance();
-
-				} catch (IllegalAccessException iae) {
-					throw new ClassNotFoundException(iae.getClass() + ": " //$NON-NLS-1$
-							+ iae.getMessage());
-				}
-			} catch (InstantiationException ie) {
-				throw new ClassNotFoundException(ie.getClass() + ": " //$NON-NLS-1$
-						+ ie.getMessage());
+				result = c.newInstance();
+			} catch (Exception e) {
+                if (serializationException != null) {
+                    throw serializationException;
+                }
+				throw new ClassNotFoundException(e.getClass() + ": " //$NON-NLS-1$
+						+ e.getMessage());
 			}
 		}
 

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java?view=diff&rev=562054&r1=562053&r2=562054
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/BeansTest.java
Thu Aug  2 02:37:05 2007
@@ -39,8 +39,10 @@
 import junit.textui.TestRunner;
 
 import org.apache.harmony.beans.tests.support.SampleBean;
+import org.apache.harmony.beans.tests.support.mock.CorruptedSerBean;
 import org.apache.harmony.beans.tests.support.mock.MockAppletInitializer;
 import org.apache.harmony.beans.tests.support.mock.MockJavaBean;
+import org.apache.harmony.beans.tests.support.mock.WrongSerBean;
 
 /**
  * Unit test for java.beans.Beans
@@ -345,7 +347,7 @@
         try {
             Beans.setDesignTime(true);
             assertTrue(Beans.isDesignTime());
-            
+
             Beans.setDesignTime(false);
             assertFalse(Beans.isDesignTime());
         } finally {
@@ -358,16 +360,16 @@
         try {
             Beans.setGuiAvailable(true);
             assertTrue(Beans.isGuiAvailable());
-            
+
             Beans.setGuiAvailable(false);
             assertFalse(Beans.isGuiAvailable());
         } finally {
             Beans.setGuiAvailable(value);
         }
     }
-    
+
     public void testIsGuiAvailableDefault() {
-        assertTrue("GUI is available by default", Beans.isGuiAvailable()); 
+        assertTrue("GUI is available by default", Beans.isGuiAvailable());
     }
 
     /**
@@ -427,7 +429,7 @@
         targetType = null;
         assertFalse(Beans.isInstanceOf(bean, targetType));
     }
-    
+
     public void test_instantiate_with_corrupted_serialization_file()
             throws Exception {
         final String BEANS_NAME = "TestBean";
@@ -436,33 +438,86 @@
         File file = new File(BEANS_NAME + ".ser");
         file.deleteOnExit();
         FileOutputStream fout = new FileOutputStream(file);
-        fout.close();       
+        fout.close();
 
         try {
             Beans.instantiate(null, BEANS_NAME);
-            fail("should throw IOException.");
-        } catch (IOException e) {
+            fail("should throw ClassNotFoundException.");
+        } catch (ClassNotFoundException e) {
             // expected
         }
     }
-    
-    //Regression for HARMONY-3777
-    public void test_instantiate_with_applet() throws Exception{
-		Applet applet = (Applet) Beans.instantiate(null, "java.applet.Applet");
-		assertNotNull(applet.getAppletContext());
-		assertTrue(applet.isActive());
-	}
 
-    /**
-     * 
+    // Regression for HARMONY-3777
+    public void test_instantiate_with_applet() throws Exception {
+        Applet applet = (Applet) Beans.instantiate(null, "java.applet.Applet");
+        assertNotNull(applet.getAppletContext());
+        assertTrue(applet.isActive());
+    }
+
+    /*
+     * Test instantiate a bean with corrupted .ser file. First failed to create
+     * an instance by deserialize from a corrupted .ser file, then successfully
+     * load the class and create an instance of it.
+     */
+    public void test_instantiate_withCorruptedSer() throws IOException,
+            ClassNotFoundException {
+        Object bean = Beans.instantiate(null,
+                "org.apache.harmony.beans.tests.support.mock.CorruptedSerBean");
+        assertTrue(bean instanceof CorruptedSerBean);
+    }
+
+    /*
+     * Test instantiate a bean with wrong .ser file, which means the definition
+     * of the class changes after .ser file is created. First failed to create
+     * an instance by deserialize from a corrupted .ser file, which will cause a
+     * InvalidClassException, then successfully load the class and create an
+     * instance of it.
+     */
+    public void test_instantiate_withWrongSer() throws IOException,
+            ClassNotFoundException {
+        Object bean = Beans.instantiate(null,
+                "org.apache.harmony.beans.tests.support.mock.WrongSerBean");
+        assertTrue(bean instanceof WrongSerBean);
+    }
+
+    /*
+     * Test instantiate a bean with wrong but not corrupted .ser file First
+     * failed to create an instance by deserialize from a wrong .ser file, which
+     * will cause a ClassNotFoundException.
+     */
+    public void test_instantiate_ClassNotFoundExceptionThrowing()
+            throws IOException {
+        ClassLoader loader = new WrongSerClassLoader();
+        try {
+            Beans
+                    .instantiate(loader,
+                            "org.apache.harmony.beans.tests.support.mock.WrongSerBean2");
+            fail("Should throw a ClassNotFoundException");
+        } catch (ClassNotFoundException ex) {
+            // expected
+        }
+    }
+
+    /*
+     * Test instantiate bean with corrupted .ser file and wrong class name. This
+     * will cause an IOException.
      */
+    public void test_instantiate_IOExceptionThrowing()
+            throws ClassNotFoundException {
+        ClassLoader loader = new CorruptedSerClassLoader();
+        try {
+            Beans.instantiate(loader, "NotExistBean2");
+            fail("Should throw a IOException");
+        } catch (IOException ex) {
+            // expected
+        }
+    }
+
     public static Test suite() {
         return new TestSuite(BeansTest.class);
     }
 
-    /**
-     * 
-     */
     public static void main(String[] args) {
         TestRunner.run(suite());
     }
@@ -529,4 +584,26 @@
             return getResource(MOCK_JAVA_BEAN2_SFILE);
         }
     }
+
+    /*
+     * A classloader for loading NotExistBean.ser, of which coresponding
+     * NotExistBean.java file is deleted.
+     */
+    private class WrongSerClassLoader extends ClassLoader {
+        @Override
+        protected URL findResource(String name) {
+            return getResource("org/apache/harmony/beans/tests/support/mock/NotExistBean.ser");
+        }
+    }
+
+    /*
+     * A classloader for loading corrupted .ser file.
+     */
+    private class CorruptedSerClassLoader extends ClassLoader {
+        @Override
+        protected URL findResource(String name) {
+            return getResource("org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.ser");
+        }
+    }
+
 }

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.java?view=auto&rev=562054
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.java
Thu Aug  2 02:37:05 2007
@@ -0,0 +1,5 @@
+package org.apache.harmony.beans.tests.support.mock;
+
+public class CorruptedSerBean {
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.ser?view=auto&rev=562054
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.ser
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/CorruptedSerBean.ser
Thu Aug  2 02:37:05 2007
@@ -0,0 +1 @@
+bad data.
\ No newline at end of file

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/NotExistBean.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/NotExistBean.ser?view=auto&rev=562054
==============================================================================
Binary file - no diff available.

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/NotExistBean.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.java?view=auto&rev=562054
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.java
Thu Aug  2 02:37:05 2007
@@ -0,0 +1,41 @@
+package org.apache.harmony.beans.tests.support.mock;
+
+import java.io.Serializable;
+
+/**
+ * A bean class which .ser file is wrong.
+ * Used for BeansTest.test_instantiate_withWrongSer()
+ */
+public class WrongSerBean implements Serializable{
+	private int intField;
+	private char charField;
+	private String str;
+	
+	public WrongSerBean(){
+		super();
+	}
+	
+	public WrongSerBean(int intField, char charField){
+		this.intField = intField;
+		this.charField = charField;
+		this.str = "test";
+	}
+
+	public char getCharField() {
+		return charField;
+	}
+
+	public void setCharField(char charField) {
+		this.charField = charField;
+	}
+
+	public int getIntField() {
+		return intField;
+	}
+
+	public void setIntField(int intField) {
+		this.intField = intField;
+	}
+	
+	
+}

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.ser
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.ser?view=auto&rev=562054
==============================================================================
Binary file - no diff available.

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean2.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean2.java?view=auto&rev=562054
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean2.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean2.java
Thu Aug  2 02:37:05 2007
@@ -0,0 +1,5 @@
+package org.apache.harmony.beans.tests.support.mock;
+
+public class WrongSerBean2 {
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/beans/src/test/support/java/org/apache/harmony/beans/tests/support/mock/WrongSerBean2.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message