001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.logging.simple;
018:
019: import java.text.DateFormat;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022:
023: import junit.framework.Test;
024:
025: import org.apache.commons.logging.PathableClassLoader;
026: import org.apache.commons.logging.PathableTestSuite;
027:
028: /**
029: * Tests custom date time format configuration
030: */
031: public class DateTimeCustomConfigTestCase extends CustomConfigTestCase {
032:
033: // ----------------------------------------------------------- Constructors
034:
035: /**
036: * Return the tests included in this test suite.
037: * <p>
038: * We need to use a PathableClassLoader here because the SimpleLog class
039: * is a pile of junk and chock-full of static variables. Any other test
040: * (like simple.CustomConfigTestCase) that has used the SimpleLog class
041: * will already have caused it to do once-only initialisation that we
042: * can't reset, even by calling LogFactory.releaseAll, because of those
043: * ugly statics. The only clean solution is to load a clean copy of
044: * commons-logging including SimpleLog via a nice clean classloader.
045: * Or we could fix SimpleLog to be sane...
046: */
047: public static Test suite() throws Exception {
048: Class this Class = DateTimeCustomConfigTestCase.class;
049:
050: PathableClassLoader loader = new PathableClassLoader(null);
051: loader.useSystemLoader("junit.");
052: loader.addLogicalLib("testclasses");
053: loader.addLogicalLib("commons-logging");
054:
055: Class testClass = loader.loadClass(this Class.getName());
056: return new PathableTestSuite(testClass, loader);
057: }
058:
059: /**
060: * Set up system properties required by this unit test. Here, we
061: * set up the props defined in the parent class setProperties method,
062: * and add a few to configure the SimpleLog class date/time output.
063: */
064: public void setProperties() {
065: super .setProperties();
066:
067: System.setProperty(
068: "org.apache.commons.logging.simplelog.dateTimeFormat",
069: "dd.mm.yyyy");
070: System.setProperty(
071: "org.apache.commons.logging.simplelog.showdatetime",
072: "true");
073: }
074:
075: /**
076: * Set up instance variables required by this test case.
077: */
078: public void setUp() throws Exception {
079: super .setUp();
080: }
081:
082: // ----------------------------------------------------------- Methods
083:
084: /** Checks that the date time format has been successfully set */
085: protected void checkDecoratedDateTime() {
086: assertEquals("Expected date format to be set", "dd.mm.yyyy",
087: ((DecoratedSimpleLog) log).getDateTimeFormat());
088:
089: // try the formatter
090: Date now = new Date();
091: DateFormat formatter = ((DecoratedSimpleLog) log)
092: .getDateTimeFormatter();
093: SimpleDateFormat sampleFormatter = new SimpleDateFormat(
094: "dd.mm.yyyy");
095: assertEquals("Date should be formatters to pattern dd.mm.yyyy",
096: sampleFormatter.format(now), formatter.format(now));
097: }
098:
099: /** Hook for subclassses */
100: protected void checkShowDateTime() {
101: assertTrue(((DecoratedSimpleLog) log).getShowDateTime());
102: }
103:
104: }
|