    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.servlet;
    19
    20  import junit.framework.Test;
    21  import junit.framework.TestCase;
    22
    23  import org.apache.commons.logging.PathableClassLoader;
    24  import org.apache.commons.logging.PathableTestSuite;
    25  import org.apache.commons.logging.impl.ServletContextCleaner;
    26
    27
    28  /**
    29   * Tests for ServletContextCleaner utility class.
    30   */
    31
    32  public class BasicServletTestCase extends TestCase {
    33
    34      /**
    35       * Return the tests included in this test suite.
    36       */
    37      public static Test suite() throws Exception {
    38          // LogFactory in parent
    39          // LogFactory in child (loads test)
    40          // LogFactory in tccl
    41          //
    42          // Having the test loaded via a loader above the tccl emulates the situation
    43          // where a web.xml file specifies ServletContextCleaner as a listener, and
    44          // that class is deployed via a shared classloader.
    45
    46          PathableClassLoader parent = new PathableClassLoader(null);
    47          parent.useExplicitLoader("junit.", Test.class.getClassLoader());
    48          parent.addLogicalLib("commons-logging");
    49          parent.addLogicalLib("servlet-api");
    50
    51          PathableClassLoader child = new PathableClassLoader(parent);
    52          child.setParentFirst(false);
    53          child.addLogicalLib("commons-logging");
    54          child.addLogicalLib("testclasses");
    55
    56          PathableClassLoader tccl = new PathableClassLoader(child);
    57          tccl.setParentFirst(false);
    58          tccl.addLogicalLib("commons-logging");
    59
    60          Class testClass = child.loadClass(BasicServletTestCase.class.getName());
    61          return new PathableTestSuite(testClass, tccl);
    62      }
    63
    64      /**
    65       * Test that calling ServletContextCleaner.contextDestroyed doesn't crash.
    66       * Testing anything else is rather difficult...
    67       */
    68      public void testBasics() {
    69          ServletContextCleaner scc = new ServletContextCleaner();
    70          scc.contextDestroyed(null);
    71      }
    72  }