/** * Verifies that {@link Class#forName(String, boolean, ClassLoader)} used with * {@link ClassPathLoader} works as expected with named object arrays, while * {@link ClassLoader#loadClass(String)} throws ClassNotFoundException for named object arrays. */ @Test public void testForNameWithObjectArray() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testForNameWithObjectArray"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(false); String classToLoad = "[Ljava.lang.String;"; Class<?> clazz = null; clazz = dcl.forName(classToLoad); assertThat(clazz.getName()).isEqualTo(classToLoad); }
/** * Verifies that setting <tt>excludeThreadContextClassLoader</tt> to true will indeed exclude the * TCCL. */ @Test public void testExcludeTCCL() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testExcludeTCCL"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(true); String classToLoad = "com.nowhere.TestExcludeTCCL"; assertThatThrownBy(() -> { dcl.forName(classToLoad); }).isInstanceOf(ClassNotFoundException.class); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(new GeneratingClassLoader()); assertThatThrownBy(() -> { dcl.forName(classToLoad); }).isInstanceOf(ClassNotFoundException.class); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * Verifies that <tt>getResource</tt> will skip TCCL if <tt>excludeThreadContextClassLoader</tt> * has been set to true. */ @Test public void testGetResourceExcludeTCCL() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testGetResourceExcludeTCCL"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(true); String resourceToGet = "com/nowhere/testGetResourceExcludeTCCL.rsc"; assertThat(dcl.getResource(resourceToGet)).isNull(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // ensure that TCCL is only CL that can find this resource Thread.currentThread().setContextClassLoader(new GeneratingClassLoader()); assertThat(dcl.getResource(resourceToGet)).isNull(); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * Verifies that TCCL finds the class when {@link Class#forName(String, boolean, ClassLoader)} * uses {@link ClassPathLoader}. */ @Test public void testForNameWithTCCL() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testForNameWithTCCL"); final ClassPathLoader dcl = ClassPathLoader.createWithDefaults(false); final String classToLoad = "com.nowhere.TestForNameWithTCCL"; assertThatThrownBy(() -> { dcl.forName(classToLoad); }).isInstanceOf(ClassNotFoundException.class); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // ensure that TCCL is only CL that can find this class Thread.currentThread().setContextClassLoader(new GeneratingClassLoader()); Class<?> clazz = dcl.forName(classToLoad); assertThat(clazz).isNotNull(); Object instance = clazz.newInstance(); assertThat(instance).isNotNull(); assertThat(instance.getClass().getName()).isEqualTo(classToLoad); } finally { Thread.currentThread().setContextClassLoader(cl); } assertThatThrownBy(() -> { dcl.forName(classToLoad); }).isInstanceOf(ClassNotFoundException.class); }
/** * Verifies that <tt>getResourceAsStream</tt> will skip TCCL if * <tt>excludeThreadContextClassLoader</tt> has been set to true. */ @Test public void testGetResourceAsStreamExcludeTCCL() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testGetResourceAsStreamExcludeTCCL"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(true); String resourceToGet = "com/nowhere/testGetResourceAsStreamExcludeTCCL.rsc"; assertThat(dcl.getResourceAsStream(resourceToGet)).isNull(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // ensure that TCCL is only CL that can find this resource Thread.currentThread().setContextClassLoader(new GeneratingClassLoader()); assertThat(dcl.getResourceAsStream(resourceToGet)).isNull(); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * Verifies that the {@link BrokenClassLoader} is broken and always throws errors even when used * as a TCCL from {@link ClassPathLoader}. This is primarily a control which ensures that tests * depending on <tt>BrokenClassLoader</tt> are valid, but it also verifies that TCCL is included * by default by <tt>ClassPathLoader</tt>. */ @Test public void testBrokenTCCLThrowsErrors() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testBrokenTCCLThrowsErrors"); ClassPathLoader dcl = ClassPathLoader.createWithDefaults(false); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // set the TCCL to throw errors Thread.currentThread().setContextClassLoader(new BrokenClassLoader()); String classToLoad = "java.lang.String"; assertThatThrownBy(() -> { dcl.forName(classToLoad); }).isInstanceOf(BrokenError.class); String resourceToGet = "java/lang/String.class"; assertThatThrownBy(() -> { dcl.getResource(resourceToGet); }).isInstanceOf(BrokenError.class); assertThatThrownBy(() -> { dcl.getResourceAsStream(resourceToGet); }).isInstanceOf(BrokenError.class); } finally { Thread.currentThread().setContextClassLoader(cl); } }
/** * Verifies that the class classloader or system classloader will find the class or resource. * Parent is a {@link NullClassLoader} while the TCCL is an excluded {@link BrokenClassLoader}. */ @Test public void testEverythingWithDefaultLoader() throws Exception { System.out.println("\nStarting ClassPathLoaderTest#testEverythingWithDefaultLoader"); // create DCL such that parent cannot find anything ClassPathLoader dcl = ClassPathLoader.createWithDefaults(true); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { // set the TCCL to never find anything Thread.currentThread().setContextClassLoader(new BrokenClassLoader()); String classToLoad = "java.lang.String"; Class<?> clazz = dcl.forName(classToLoad); assertThat(clazz).isNotNull(); String resourceToGet = "java/lang/String.class"; URL url = dcl.getResource(resourceToGet); assertThat(url).isNotNull(); InputStream is = dcl.getResourceAsStream(resourceToGet); assertThat(is).isNotNull(); } finally { Thread.currentThread().setContextClassLoader(cl); } }