test/com/sun/org/apache/xml/internal/security/transforms/ClassLoaderTest.java

Print this page




  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.src", "./");
  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         Object o = cons.newInstance();
  54         // Apache code swallows the ClassNotFoundExc, so we need to
  55         // check if the Transform has already been registered by registering
  56         // it again and catching an AlgorithmAlreadyRegisteredExc
  57         try {
  58             Transform.register(MyTransform.URI, "MyTransform");
  59             throw new Exception("ClassLoaderTest failed");
  60         } catch (AlgorithmAlreadyRegisteredException e) {
  61             // test passed


  62         }
  63     }
  64 }


  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 }