1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.logging.simple;
18  
19  import java.text.DateFormat;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import junit.framework.Test;
24  
25  import org.apache.commons.logging.PathableClassLoader;
26  import org.apache.commons.logging.PathableTestSuite;
27  
28  
29  /***
30   * Tests custom date time format configuration
31   */
32  public class DateTimeCustomConfigTestCase extends CustomConfigTestCase {
33      
34      // ----------------------------------------------------------- Constructors
35  
36      /***
37       * Return the tests included in this test suite.
38       * <p>
39       * We need to use a PathableClassLoader here because the SimpleLog class
40       * is a pile of junk and chock-full of static variables. Any other test
41       * (like simple.CustomConfigTestCase) that has used the SimpleLog class
42       * will already have caused it to do once-only initialisation that we
43       * can't reset, even by calling LogFactory.releaseAll, because of those
44       * ugly statics. The only clean solution is to load a clean copy of
45       * commons-logging including SimpleLog via a nice clean classloader.
46       * Or we could fix SimpleLog to be sane...
47       */
48      public static Test suite() throws Exception {
49          Class thisClass = DateTimeCustomConfigTestCase.class;
50  
51          PathableClassLoader loader = new PathableClassLoader(null);
52          loader.useSystemLoader("junit.");
53          loader.addLogicalLib("testclasses");
54          loader.addLogicalLib("commons-logging");
55          
56          Class testClass = loader.loadClass(thisClass.getName());
57          return new PathableTestSuite(testClass, loader);
58      }
59  
60  
61      /***
62       * Set up system properties required by this unit test. Here, we
63       * set up the props defined in the parent class setProperties method, 
64       * and add a few to configure the SimpleLog class date/time output.
65       */
66      public void setProperties() {
67          super.setProperties();
68          
69          System.setProperty(
70              "org.apache.commons.logging.simplelog.dateTimeFormat",
71              "dd.mm.yyyy");
72          System.setProperty(
73              "org.apache.commons.logging.simplelog.showdatetime",
74              "true");
75      }
76  
77      /***
78       * Set up instance variables required by this test case.
79       */
80      public void setUp() throws Exception {
81          super.setUp();
82      }
83  
84  
85      // ----------------------------------------------------------- Methods
86  
87      /*** Checks that the date time format has been successfully set */
88      protected void checkDecoratedDateTime() {
89          assertEquals("Expected date format to be set", "dd.mm.yyyy",
90                       ((DecoratedSimpleLog) log).getDateTimeFormat());
91          
92          // try the formatter
93          Date now = new Date();
94          DateFormat formatter = ((DecoratedSimpleLog) log).getDateTimeFormatter(); 
95          SimpleDateFormat sampleFormatter = new SimpleDateFormat("dd.mm.yyyy");
96          assertEquals("Date should be formatters to pattern dd.mm.yyyy", sampleFormatter.format(now), formatter.format(now));
97      }
98      
99      /*** Hook for subclassses */
100     protected void checkShowDateTime() {
101         assertTrue(((DecoratedSimpleLog) log).getShowDateTime());
102     }
103     
104 }