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