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