    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.logging.pathable;
    18
    19  import java.net.URL;
    20  import java.net.URLClassLoader;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestCase;
    24
    25  import org.apache.commons.logging.PathableClassLoader;
    26  import org.apache.commons.logging.PathableTestSuite;
    27
    28  /**
    29   * Tests for the PathableTestSuite class.
    30   */
    31
    32  public class GeneralTestCase extends TestCase {
    33
    34      /**
    35       * Set up a custom classloader hierarchy for this test case.
    36       */
    37      public static Test suite() throws Exception {
    38          Class thisClass = GeneralTestCase.class;
    39          ClassLoader thisClassLoader = thisClass.getClassLoader();
    40
    41          PathableClassLoader loader = new PathableClassLoader(null);
    42          loader.useExplicitLoader("junit.", thisClassLoader);
    43          loader.addLogicalLib("testclasses");
    44
    45          // reload this class via the child classloader
    46          Class testClass = loader.loadClass(thisClass.getName());
    47
    48          // and return our custom TestSuite class
    49          return new PathableTestSuite(testClass, loader);
    50      }
    51
    52      /**
    53       * Verify that a certain system property is not set, then set it.
    54       */
    55      private static void checkAndSetProperties() {
    56          String prop = System.getProperty("no.such.property");
    57          assertNull("no.such.property is unexpectedly defined", prop);
    58          System.setProperty("no.such.property", "dummy value");
    59          prop = System.getProperty("no.such.property");
    60          assertNotNull("no.such.property is unexpectedly undefined", prop);
    61      }
    62
    63      /**
    64       * Verify that when a test method modifies the system properties they are
    65       * reset before the next test is run.
    66       * <p>
    67       * This method works in conjunction with testResetProps2. There is no
    68       * way of knowing which test method junit will run first, but it doesn't
    69       * matter; whichever one of them runs first will modify the system properties.
    70       * If the PathableTestSuite isn't resetting the system properties then whichever
    71       * of them runs second will fail. Of course if other methods are run in-between
    72       * then those methods might also fail...
    73       */
    74      public void testResetProps1() {
    75          checkAndSetProperties();
    76      }
    77
    78      /**
    79       * See testResetProps1.
    80       */
    81      public void testResetProps2() {
    82          checkAndSetProperties();
    83      }
    84
    85      /**
    86       * Verify that the context classloader is a custom one, then reset it to
    87       * a non-custom one.
    88       */
    89      private static void checkAndSetContext() {
    90          ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
    91          assertEquals("ContextLoader is of unexpected type",
    92                  contextLoader.getClass().getName(),
    93                  PathableClassLoader.class.getName());
    94
    95          URL[] noUrls = new URL[0];
    96          Thread.currentThread().setContextClassLoader(new URLClassLoader(noUrls));
    97      }
    98
    99      /**
    100      * Verify that when a test method modifies the context classloader it is
    101      * reset before the next test is run.
    102      * <p>
    103      * This method works in conjunction with testResetContext2. There is no
    104      * way of knowing which test method junit will run first, but it doesn't
    105      * matter; whichever one of them runs first will modify the contextClassloader.
    106      * If the PathableTestSuite isn't resetting the contextClassLoader then whichever
    107      * of them runs second will fail. Of course if other methods are run in-between
    108      * then those methods might also fail...
    109      */
    110     public void testResetContext1() {
    111         checkAndSetContext();
    112     }
    113
    114     /**
    115      * See testResetContext1.
    116      */
    117     public void testResetContext2() {
    118         checkAndSetContext();
    119     }
    120 }