Skip to content

Commit d1deea7

Browse files
committed
Create BEANUTILS-566 tests
1 parent 1f5d7b8 commit d1deea7

File tree

1 file changed

+108
-5
lines changed

1 file changed

+108
-5
lines changed

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

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

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import java.io.File;
24+
import java.net.URL;
25+
import java.net.URLClassLoader;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
29+
import org.apache.commons.beanutils2.LoggerUtil;
30+
import org.apache.commons.logging.LogFactory;
31+
import org.junit.jupiter.api.Test;
32+
1933
/**
20-
* Test class for {@code LoggerUtil}.
34+
* Test class for {@code LoggerUtil}
2135
* https://issues.apache.org/jira/browse/BEANUTILS-566
2236
*/
2337
public class Jira566TestCase {
2438

25-
public class CustomClassLoader extends ClassLoader {
39+
private static CustomURLClassLoader CUSTOM_CLASSLOADER;
40+
41+
private static Boolean IS_CUSTOM_CLASSLOADER_INVOKED = false;
42+
43+
private static class CustomURLClassLoader extends URLClassLoader {
44+
45+
public CustomURLClassLoader(URL[] urls, ClassLoader parent) {
46+
super(urls, parent);
47+
}
48+
49+
/*
50+
* Given the following default loadClass implementation steps (in order, per the JavaDoc):
51+
* 1) Invoke findLoadedClass(String) to check if the class has already been loaded.
52+
* 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.
53+
* 3) Invoke the findClass(String) method to find the class.
54+
*
55+
* If the name matches Jira566TestCase, then we find that class with this classloader instead via findClass.
56+
* Calling newInstance().getClass().getClassLoader() on the returned class object would an instance of CustomURLClassLoader.
57+
* Otherwise, if findLoadedClass or loadClass are used instead, then the original classloader is used (i.e jdk.internal.loader.ClassLoaders$AppClassLoader)
58+
*
59+
*/
60+
@Override
61+
public Class<?> loadClass(String name) throws ClassNotFoundException {
62+
if (name.equals("org.apache.commons.beanutils2.bugs.Jira566TestCase")) {
63+
// System.out.println("match: " + name); // debug
64+
return findClass(name);
65+
} else {
66+
// System.out.println("super: " + name); // debug
67+
IS_CUSTOM_CLASSLOADER_INVOKED = true;
68+
return super.loadClass(name);
69+
}
70+
}
71+
72+
}
73+
74+
75+
public static void setup() throws Exception {
76+
Path targetPath = Paths.get(Jira566TestCase.class.getResource("/").toURI()).getParent();
77+
ClassLoader parent = Jira566TestCase.class.getClassLoader();
78+
CUSTOM_CLASSLOADER = new CustomURLClassLoader(new URL[] {new File(targetPath.toString(), "test-classes").toURI().toURL()}, parent);
2679
}
2780

2881
/*
2982
* Tests that context classloader is used
3083
*/
31-
// @Test
32-
public void testClassLoaderUsed() throws Exception {
33-
// TODO
84+
@Test
85+
public void testContextClassLoaderUsed() throws Exception {
86+
87+
setup();
88+
89+
/*
90+
* Create an instance of this class (Jira566TestCase) so that it's classloader is CustomURLClassLoader
91+
*/
92+
Class<?> loadedClass = CUSTOM_CLASSLOADER.loadClass("org.apache.commons.beanutils2.bugs.Jira566TestCase");
93+
Class<?> instance = loadedClass.newInstance().getClass();
94+
95+
IS_CUSTOM_CLASSLOADER_INVOKED = false; // reset to false
96+
97+
/*
98+
* When the logger is created, the creation will go through CustomURLClassLoader#loadClass
99+
*/
100+
LoggerUtil.createLoggerwithContextClassLoader(instance);
101+
102+
assertTrue("Context ClassLoader was not invoked", IS_CUSTOM_CLASSLOADER_INVOKED);
103+
}
104+
105+
/*
106+
* Tests that original classloader is used
107+
*/
108+
@Test
109+
public void testOriginalClassLoaderUsed() throws Exception {
110+
111+
setup();
112+
113+
IS_CUSTOM_CLASSLOADER_INVOKED = false; // reset to false
114+
LogFactory.getLog(Jira566TestCase.class);
115+
116+
assertFalse("Context ClassLoader was invoked when it should not have been", IS_CUSTOM_CLASSLOADER_INVOKED);
117+
}
118+
119+
/*
120+
* The LoggerUtil.createLoggerwithContextClassLoader method swaps out the classloaders,
121+
* so this test checks to make it the original is set back.
122+
*/
123+
@Test
124+
public void testClassLoaderIsSetBackToOriginalInLoggerUtil() throws Exception {
125+
126+
ClassLoader original = Thread.currentThread().getContextClassLoader();
127+
try {
128+
Thread.currentThread().setContextClassLoader(CUSTOM_CLASSLOADER);
129+
LoggerUtil.createLoggerwithContextClassLoader(Jira566TestCase.class);
130+
131+
assertEquals("Classloader do not match!",Thread.currentThread().getContextClassLoader(), CUSTOM_CLASSLOADER);
132+
} finally {
133+
// set it back to avoid any problems in the other tests
134+
Thread.currentThread().setContextClassLoader(original);
135+
}
136+
34137
}
35138
}

0 commit comments

Comments
 (0)