1 /*
   2  * Copyright (c) 2006, 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
  28  * @compile -XDignore.symbol.file ClassLoaderTest.java MyTransform.java
  29  * @run main 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.classes", "./");
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         Transform.init();
  47         File file = new File(BASE);
  48         URL[] urls = new URL[1];
  49         urls[0] = file.toURI().toURL();
  50         URLClassLoader ucl = new URLClassLoader(urls);
  51         Class c = ucl.loadClass("MyTransform");
  52         Constructor cons = c.getConstructor();
  53 
  54         Thread curThread = Thread.currentThread();
  55         ClassLoader ctxLoader = curThread.getContextClassLoader();
  56         ClassLoader loader = MyTransform.class.getClassLoader();
  57         try {
  58             // In agentvm mode, the class MyTransform is loaded by the child of
  59             // context ClassLoader of this thread. Replace context ClassLoader
  60             // with its child to avoid ClassNotFoundException thrown from
  61             // Transform.register(String, String) method.
  62             curThread.setContextClassLoader(loader);
  63             Object o = cons.newInstance();
  64             // Apache code swallows the ClassNotFoundExc, so we need to
  65             // check if the Transform has already been registered by registering
  66             // it again and catching an AlgorithmAlreadyRegisteredExc
  67             Transform.register(MyTransform.URI, "MyTransform");
  68             throw new Exception("ClassLoaderTest failed");
  69         } catch (AlgorithmAlreadyRegisteredException e) {
  70             // test passed
  71         } finally {
  72             curThread.setContextClassLoader(ctxLoader);
  73         }
  74     }
  75 }