|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.beanutils2.bugs; |
| 18 | + |
| 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 | + |
| 33 | +/** |
| 34 | + * Test class for {@code LoggerUtil} |
| 35 | + * https://issues.apache.org/jira/browse/BEANUTILS-566 |
| 36 | + */ |
| 37 | +public class Jira566TestCase { |
| 38 | + |
| 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); |
| 79 | + } |
| 80 | + |
| 81 | + /* |
| 82 | + * Tests that context classloader is used |
| 83 | + */ |
| 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 | + |
| 137 | + } |
| 138 | +} |
0 commit comments