Test4676532.java revision 2362
84N/A/*
84N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
84N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
84N/A *
84N/A * This code is free software; you can redistribute it and/or modify it
84N/A * under the terms of the GNU General Public License version 2 only, as
84N/A * published by the Free Software Foundation.
84N/A *
84N/A * This code is distributed in the hope that it will be useful, but WITHOUT
84N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
84N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
84N/A * version 2 for more details (a copy is included in the LICENSE file that
84N/A * accompanied this code).
84N/A *
84N/A * You should have received a copy of the GNU General Public License version
84N/A * 2 along with this work; if not, write to the Free Software Foundation,
84N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
84N/A *
84N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
84N/A * or visit www.oracle.com if you need additional information or have any
84N/A * questions.
84N/A */
84N/A
84N/A/*
84N/A * @test
84N/A * @bug 4676532
84N/A * @summary Tests XMLDecoder.setClassLoader()
84N/A * @author Mark Davidson
84N/A */
84N/A
84N/Aimport java.beans.ExceptionListener;
84N/Aimport java.beans.XMLDecoder;
84N/Aimport java.io.ByteArrayInputStream;
84N/Aimport java.io.File;
84N/Aimport java.io.InputStream;
84N/Aimport java.net.URL;
84N/Aimport java.net.URLClassLoader;
84N/A
84N/A/**
84N/A * Tests the XMLDecoder with an alternative classloader.
84N/A * Specicifically, the URLClassLoader.
84N/A * The test.jar file should be in the same directory as the classes.
84N/A * The Encode class will create the test.xml test file.
84N/A */
84N/Apublic class Test4676532 {
84N/A private static final String DATA
84N/A = "<java>\n"
84N/A + " <object class=\"test.Test\">\n"
84N/A + " <void property=\"message\">\n"
84N/A + " <string>Hello, world</string>\n"
84N/A + " </void>\n"
84N/A + " </object>\n"
84N/A + "</java> ";
84N/A
84N/A public static void main(String[] args) throws Exception {
84N/A StringBuilder sb = new StringBuilder(256);
84N/A sb.append("file:");
84N/A sb.append(System.getProperty("test.src", "."));
84N/A sb.append(File.separatorChar);
84N/A sb.append("test.jar");
84N/A
84N/A URL[] url = {new URL(sb.toString())};
84N/A URLClassLoader cl = new URLClassLoader(url);
84N/A
84N/A Class type = cl.loadClass("test.Test");
84N/A if (type == null) {
84N/A throw new Error("could not find class test.Test");
84N/A }
84N/A
84N/A
84N/A InputStream stream = new ByteArrayInputStream(DATA.getBytes());
84N/A
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
}