DateTimeCustomConfigTestCase xref

    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.simple;
    19
    20  import java.text.DateFormat;
    21  import java.text.SimpleDateFormat;
    22  import java.util.Date;
    23
    24  import junit.framework.Test;
    25
    26  import org.apache.commons.logging.PathableClassLoader;
    27  import org.apache.commons.logging.PathableTestSuite;
    28
    29
    30  /**
    31   * Tests custom date time format configuration
    32   */
    33  public class DateTimeCustomConfigTestCase extends CustomConfigTestCase {
    34
    35      // ----------------------------------------------------------- Constructors
    36
    37      /**
    38       * Return the tests included in this test suite.
    39       * <p>
    40       * We need to use a PathableClassLoader here because the SimpleLog class
    41       * is a pile of junk and chock-full of static variables. Any other test
    42       * (like simple.CustomConfigTestCase) that has used the SimpleLog class
    43       * will already have caused it to do once-only initialisation that we
    44       * can't reset, even by calling LogFactory.releaseAll, because of those
    45       * ugly statics. The only clean solution is to load a clean copy of
    46       * commons-logging including SimpleLog via a nice clean classloader.
    47       * Or we could fix SimpleLog to be sane...
    48       */
    49      public static Test suite() throws Exception {
    50          Class thisClass = DateTimeCustomConfigTestCase.class;
    51
    52          PathableClassLoader loader = new PathableClassLoader(null);
    53          loader.useExplicitLoader("junit.", Test.class.getClassLoader());
    54          loader.addLogicalLib("testclasses");
    55          loader.addLogicalLib("commons-logging");
    56
    57          Class testClass = loader.loadClass(thisClass.getName());
    58          return new PathableTestSuite(testClass, loader);
    59      }
    60
    61
    62      /**
    63       * Set up system properties required by this unit test. Here, we
    64       * set up the props defined in the parent class setProperties method,
    65       * and add a few to configure the SimpleLog class date/time output.
    66       */
    67      public void setProperties() {
    68          super.setProperties();
    69
    70          System.setProperty(
    71              "org.apache.commons.logging.simplelog.dateTimeFormat",
    72              "dd.mm.yyyy");
    73          System.setProperty(
    74              "org.apache.commons.logging.simplelog.showdatetime",
    75              "true");
    76      }
    77
    78      /**
    79       * Set up instance variables required by this test case.
    80       */
    81      public void setUp() throws Exception {
    82          super.setUp();
    83      }
    84
    85
    86      // ----------------------------------------------------------- Methods
    87
    88      /** Checks that the date time format has been successfully set */
    89      protected void checkDecoratedDateTime() {
    90          assertEquals("Expected date format to be set", "dd.mm.yyyy",
    91                       ((DecoratedSimpleLog) log).getDateTimeFormat());
    92
    93          // try the formatter
    94          Date now = new Date();
    95          DateFormat formatter = ((DecoratedSimpleLog) log).getDateTimeFormatter();
    96          SimpleDateFormat sampleFormatter = new SimpleDateFormat("dd.mm.yyyy");
    97          assertEquals("Date should be formatters to pattern dd.mm.yyyy", sampleFormatter.format(now), formatter.format(now));
    98      }
    99
    100     /** Hook for subclassses */
    101     protected void checkShowDateTime() {
    102         assertTrue(((DecoratedSimpleLog) log).getShowDateTime());
    103     }
    104
    105 }