1 /* 2 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @author Sean Mullan 27 * @bug 6461674 8009217 7147060 28 * @compile -XDignore.symbol.file ClassLoaderTest.java MyTransform.java 29 * @run main/othervm ClassLoaderTest 30 * @summary Ensure Transform.register works with transform implementations 31 * loaded by class loader other than system/boot class loader 32 */ 33 import java.io.File; 34 import java.lang.reflect.Constructor; 35 import java.net.URL; 36 import java.net.URLClassLoader; 37 import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException; 38 import com.sun.org.apache.xml.internal.security.transforms.Transform; 39 40 public class ClassLoaderTest { 41 42 private final static String BASE = System.getProperty("test.src", "./"); 43 44 public static void main(String[] args) throws Exception { 45 46 File file = new File(BASE); 47 URL[] urls = new URL[1]; 48 urls[0] = file.toURI().toURL(); 49 URLClassLoader ucl = new URLClassLoader(urls); 50 Class<?> c = ucl.loadClass("MyTransform"); 51 Constructor<?> cons = c.getConstructor(new Class[] {}); 52 Object o = cons.newInstance(); 53 // Apache code swallows the ClassNotFoundExc, so we need to 54 // check if the Transform has already been registered by registering 55 // it again and catching an AlgorithmAlreadyRegisteredExc 56 try { 57 Transform.register(MyTransform.URI, "MyTransform"); 58 throw new Exception("ClassLoaderTest failed"); 59 } catch (AlgorithmAlreadyRegisteredException e) { 60 // test passed 61 } 62 } 63 }