View Javadoc
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  
18  package org.apache.commons.logging.tccl.logfactory;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.commons.logging.PathableClassLoader;
25  import org.apache.commons.logging.PathableTestSuite;
26  
27  /**
28   * Verifies that if the TCCL is a sibling of the classloader with `commons-logging`
29   */
30  public class SiblingTcclTestCase extends TestCase {
31  
32      /**
33       * Return the tests included in this test suite.
34       */
35      public static Test suite() throws Exception {
36          // The classloader running the test has access to `commons-logging`
37          final PathableClassLoader classLoader = new PathableClassLoader(null);
38          classLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
39          classLoader.addLogicalLib("commons-logging");
40          classLoader.addLogicalLib("testclasses");
41  
42          // The TCCL has only access to `log4j-api` and `slf4j-api`
43          // See https://issues.apache.org/jira/browse/LOGGING-192
44          final PathableClassLoader tcclLoader = new PathableClassLoader(null);
45          tcclLoader.addLogicalLib("log4j-api");
46  
47          final Class<?> testClass = classLoader.loadClass(SiblingTcclTestCase.class.getName());
48          return new PathableTestSuite(testClass, tcclLoader);
49      }
50  
51      /**
52       * Sets up instance variables required by this test case.
53       */
54      @Override
55      public void setUp() throws Exception {
56          LogFactory.releaseAll();
57      }
58  
59      /**
60       * Tear down instance variables required by this test case.
61       */
62      @Override
63      public void tearDown() {
64          LogFactory.releaseAll();
65      }
66  
67      public void testFactoryLoading() {
68          // Loading the factory does not fail as in LOGGING-192
69          final LogFactory factory = LogFactory.getFactory();
70          // The selected implementation comes from this classloader
71          assertEquals(getClass().getClassLoader(), factory.getClass().getClassLoader());
72      }
73  }