Path: blob/master/test/jdk/java/beans/XMLDecoder/4676532/Test4676532.java
41153 views
/*1* Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 467653226* @summary Tests XMLDecoder.setClassLoader()27* @author Mark Davidson28*/2930import java.beans.ExceptionListener;31import java.beans.XMLDecoder;32import java.io.ByteArrayInputStream;33import java.io.File;34import java.io.InputStream;35import java.net.URL;36import java.net.URLClassLoader;3738/**39* Tests the XMLDecoder with an alternative classloader.40* Specicifically, the URLClassLoader.41* The test.jar file should be in the same directory as the classes.42* The Encode class will create the test.xml test file.43*/44public class Test4676532 {45private static final String DATA46= "<java>\n"47+ " <object class=\"test.Test\">\n"48+ " <void property=\"message\">\n"49+ " <string>Hello, world</string>\n"50+ " </void>\n"51+ " </object>\n"52+ "</java> ";5354public static void main(String[] args) throws Exception {55StringBuilder sb = new StringBuilder(256);56sb.append("file:");57sb.append(System.getProperty("test.src", "."));58sb.append(File.separatorChar);59sb.append("test.jar");6061URL[] url = {new URL(sb.toString())};62URLClassLoader cl = new URLClassLoader(url);6364Class type = cl.loadClass("test.Test");65if (type == null) {66throw new Error("could not find class test.Test");67}686970InputStream stream = new ByteArrayInputStream(DATA.getBytes());7172ExceptionListener el = new ExceptionListener() {73public void exceptionThrown(Exception exception) {74throw new Error("unexpected exception", exception);75}76};7778XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);79Object object = decoder.readObject();80decoder.close();8182if (!type.equals(object.getClass())) {83throw new Error("unexpected " + object.getClass());84}85}86}878889