6905515: Test failed: java/beans/XMLEncoder/6329581/Test6329581.java
authormalenkov
Mon, 30 Nov 2009 18:26:52 +0300
changeset 4390 3d16ebf520b3
parent 4389 ad9f430e37c9
child 4391 b31fdb74b4a4
6905515: Test failed: java/beans/XMLEncoder/6329581/Test6329581.java Reviewed-by: peterz
jdk/test/java/beans/XMLEncoder/6329581/Test6329581.java
--- a/jdk/test/java/beans/XMLEncoder/6329581/Test6329581.java	Fri Nov 27 18:38:21 2009 +0300
+++ b/jdk/test/java/beans/XMLEncoder/6329581/Test6329581.java	Mon Nov 30 18:26:52 2009 +0300
@@ -33,43 +33,70 @@
 import java.beans.XMLEncoder;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.net.URL;
 import java.net.URLClassLoader;
 
-public class Test6329581 implements ExceptionListener {
+public class Test6329581 extends URLClassLoader implements ExceptionListener {
+    public static final class Bean {
+    }
 
     public static void main(String[] args) throws Exception {
-        ExceptionListener listener = new Test6329581();
-        // write bean to byte array
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        XMLEncoder encoder = new XMLEncoder(out);
-        encoder.setExceptionListener(listener);
-        encoder.writeObject(getClassLoader("beans.jar").loadClass("test.Bean").newInstance());
-        encoder.close();
-        // read bean from byte array
-        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-        XMLDecoder decoder = new XMLDecoder(in, null, listener, getClassLoader("beans.jar"));
-        Object object = decoder.readObject();
-        decoder.close();
+        new Test6329581().decode(new Test6329581().encode(Bean.class.getName()));
+    }
 
-        if (!object.getClass().getClassLoader().getClass().equals(URLClassLoader.class)) {
-            throw new Error("bean is loaded with unexpected class loader");
-        }
+    private Test6329581() {
+        super(new URL[] {
+                Test6329581.class.getProtectionDomain().getCodeSource().getLocation()
+        });
     }
 
-    private static ClassLoader getClassLoader(String name) throws Exception {
-        StringBuilder sb = new StringBuilder(256);
-        sb.append("file:");
-        sb.append(System.getProperty("test.src", "."));
-        sb.append(File.separatorChar);
-        sb.append(name);
-
-        URL[] url = { new URL(sb.toString()) };
-        return new URLClassLoader(url);
+    @Override
+    protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
+        Class c = findLoadedClass(name);
+        if (c == null) {
+            if (Bean.class.getName().equals(name)) {
+                c = findClass(name);
+            }
+            else try {
+                c = getParent().loadClass(name);
+            }
+            catch (ClassNotFoundException exception) {
+                c = findClass(name);
+            }
+        }
+        if (resolve) {
+            resolveClass(c);
+        }
+        return c;
     }
 
     public void exceptionThrown(Exception exception) {
         throw new Error("unexpected exception", exception);
     }
+
+    private void validate(Object object) {
+        if (!object.getClass().getClassLoader().equals(this)) {
+            throw new Error("Bean is loaded with unexpected class loader");
+        }
+    }
+
+    private byte[] encode(String name) throws Exception {
+        Object object = loadClass(name).newInstance();
+        validate(object);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        XMLEncoder encoder = new XMLEncoder(out);
+        encoder.setExceptionListener(this);
+        encoder.writeObject(object);
+        encoder.close();
+        return out.toByteArray();
+    }
+
+    private Object decode(byte[] array) {
+        ByteArrayInputStream in = new ByteArrayInputStream(array);
+        XMLDecoder decoder = new XMLDecoder(in, null, this, this);
+        Object object = decoder.readObject();
+        validate(object);
+        decoder.close();
+        return object;
+    }
 }