Skip to content

Commit d8b1b55

Browse files
committed
Create BEANUTILS-566 tests
1 parent 1de3b80 commit d8b1b55

1 file changed

Lines changed: 87 additions & 5 deletions

File tree

src/test/java/org/apache/commons/beanutils2/bugs/Jira566TestCase.java

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,102 @@
1616
*/
1717
package org.apache.commons.beanutils2.bugs;
1818

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+
1932
/**
20-
* Test class for {@code LoggerUtil}.
33+
* Test class for {@code LoggerUtil}
2134
* https://issues.apache.org/jira/browse/BEANUTILS-566
2235
*/
2336
public class Jira566TestCase {
2437

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);
2678
}
2779

2880
/*
2981
* Tests that context classloader is used
3082
*/
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);
34116
}
35117
}

0 commit comments

Comments
 (0)