|
16 | 16 | */ |
17 | 17 | package org.apache.commons.beanutils2.bugs; |
18 | 18 |
|
| 19 | +import static org.junit.Assert.assertFalse; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.net.URL; |
| 24 | +import java.net.URLClassLoader; |
| 25 | +import java.nio.file.Path; |
| 26 | +import java.nio.file.Paths; |
| 27 | + |
| 28 | +import org.apache.commons.beanutils2.LoggerUtil; |
| 29 | +import org.apache.commons.logging.LogFactory; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
19 | 32 | /** |
20 | | - * Test class for {@code LoggerUtil}. |
| 33 | + * Test class for {@code LoggerUtil} |
21 | 34 | * https://issues.apache.org/jira/browse/BEANUTILS-566 |
22 | 35 | */ |
23 | 36 | public class Jira566TestCase { |
24 | 37 |
|
25 | | - public class CustomClassLoader extends ClassLoader { |
| 38 | + private static CustomURLClassLoader CUSTOM_CLASSLOADER; |
| 39 | + |
| 40 | + private static Boolean IS_CUSTOM_CLASSLOADER_INVOKED = false; |
| 41 | + |
| 42 | + private static class CustomURLClassLoader extends URLClassLoader { |
| 43 | + |
| 44 | + public CustomURLClassLoader(URL[] urls, ClassLoader parent) { |
| 45 | + super(urls, parent); |
| 46 | + } |
| 47 | + |
| 48 | + /* |
| 49 | + * Given the following default loadClass implementation steps (in order, per the JavaDoc): |
| 50 | + * 1) Invoke findLoadedClass(String) to check if the class has already been loaded. |
| 51 | + * 2) Invoke the loadClass method on the parent class loader. If the parent is null the class loader built-in to the virtual machine is used, instead. |
| 52 | + * 3) Invoke the findClass(String) method to find the class. |
| 53 | + * |
| 54 | + * If the name matches Jira566TestCase, then we find that class with this classloader instead via findClass. |
| 55 | + * Calling newInstance().getClass().getClassLoader() on the returned class object would an instance of CustomURLClassLoader. |
| 56 | + * Otherwise, if findLoadedClass or loadClass are used instead, then the original classloader is used (i.e jdk.internal.loader.ClassLoaders$AppClassLoader) |
| 57 | + * |
| 58 | + */ |
| 59 | + @Override |
| 60 | + public Class<?> loadClass(String name) throws ClassNotFoundException { |
| 61 | + if (name.equals("org.apache.commons.beanutils2.bugs.Jira566TestCase")) { |
| 62 | + // System.out.println("match: " + name); // debug |
| 63 | + return findClass(name); |
| 64 | + } else { |
| 65 | + // System.out.println("super: " + name); // debug |
| 66 | + IS_CUSTOM_CLASSLOADER_INVOKED = true; |
| 67 | + return super.loadClass(name); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public static void setup() throws Exception { |
| 75 | + Path targetPath = Paths.get(Jira566TestCase.class.getResource("/").toURI()).getParent(); |
| 76 | + ClassLoader parent = Jira566TestCase.class.getClassLoader(); |
| 77 | + CUSTOM_CLASSLOADER = new CustomURLClassLoader(new URL[] {new File(targetPath.toString(), "test-classes").toURI().toURL()}, parent); |
26 | 78 | } |
27 | 79 |
|
28 | 80 | /* |
29 | 81 | * Tests that context classloader is used |
30 | 82 | */ |
31 | | - // @Test |
32 | | - public void testClassLoaderUsed() throws Exception { |
33 | | - // TODO |
| 83 | + @Test |
| 84 | + public void testContextClassLoaderUsed() throws Exception { |
| 85 | + |
| 86 | + setup(); |
| 87 | + |
| 88 | + /* |
| 89 | + * Create an instance of this class (Jira566TestCase) so that it's classloader is CustomURLClassLoader |
| 90 | + */ |
| 91 | + Class<?> loadedClass = CUSTOM_CLASSLOADER.loadClass("org.apache.commons.beanutils2.bugs.Jira566TestCase"); |
| 92 | + Class<?> instance = loadedClass.newInstance().getClass(); |
| 93 | + |
| 94 | + IS_CUSTOM_CLASSLOADER_INVOKED = false; // reset to false |
| 95 | + |
| 96 | + /* |
| 97 | + * When the logger is created, the creation will go through CustomURLClassLoader#loadClass |
| 98 | + */ |
| 99 | + LoggerUtil.createLoggerwithContextClassLoader(instance); |
| 100 | + |
| 101 | + assertTrue("Context ClassLoader was not invoked", IS_CUSTOM_CLASSLOADER_INVOKED); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + * Tests that original classloader is used |
| 106 | + */ |
| 107 | + @Test |
| 108 | + public void testOriginalClassLoaderUsed() throws Exception { |
| 109 | + |
| 110 | + setup(); |
| 111 | + |
| 112 | + IS_CUSTOM_CLASSLOADER_INVOKED = false; // reset to false |
| 113 | + LogFactory.getLog(Jira566TestCase.class); |
| 114 | + |
| 115 | + assertFalse("Context ClassLoader was invoked when it should not have been", IS_CUSTOM_CLASSLOADER_INVOKED); |
34 | 116 | } |
35 | 117 | } |
0 commit comments