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  import org.apache.commons.logging.impl.Log4jApiLogFactory;
27  
28  /**
29   * Verifies that if the TCCL contains only `commons-logging-adapters`, it can load a web-application specific
30   * logging backend.
31   */
32  public class AdaptersTcclTestCase extends TestCase {
33  
34      /**
35       * Return the tests included in this test suite.
36       */
37      public static Test suite() throws Exception {
38          // The classloader running the test has access to `commons-logging`
39          final PathableClassLoader classLoader = new PathableClassLoader(null);
40          classLoader.useExplicitLoader("junit.", Test.class.getClassLoader());
41          classLoader.addLogicalLib("commons-logging");
42          classLoader.addLogicalLib("testclasses");
43  
44          // The TCCL has access to both `commons-logging-adapters` and `log4j-api`
45          final PathableClassLoader tcclLoader = new PathableClassLoader(classLoader);
46          tcclLoader.addLogicalLib("commons-logging-adapters");
47          tcclLoader.addLogicalLib("log4j-api");
48          tcclLoader.setParentFirst(false);
49  
50          final Class<?> testClass = classLoader.loadClass(AdaptersTcclTestCase.class.getName());
51          return new PathableTestSuite(testClass, tcclLoader);
52      }
53  
54      /**
55       * Sets up instance variables required by this test case.
56       */
57      @Override
58      public void setUp() throws Exception {
59          LogFactory.releaseAll();
60      }
61  
62      /**
63       * Tear down instance variables required by this test case.
64       */
65      @Override
66      public void tearDown() {
67          LogFactory.releaseAll();
68      }
69  
70      public void testFactoryLoading() {
71          final LogFactory factory = LogFactory.getFactory();
72          // The implementation comes from the TCCL
73          assertEquals(Thread.currentThread().getContextClassLoader(), factory.getClass().getClassLoader());
74          // It uses the `log4j-api` only available in the TCCL
75          assertEquals(Log4jApiLogFactory.class.getName(), factory.getClass().getName());
76      }
77  }