KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > test > TestUtils


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.test;
22
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import java.text.DateFormat JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30
31 import java.util.Date JavaDoc;
32 import java.util.Calendar JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import com.methodhead.persistable.ConnectionSingleton;
36
37 import junit.framework.TestCase;
38
39 import org.apache.log4j.BasicConfigurator;
40 import org.apache.log4j.Level;
41 import org.apache.log4j.Logger;
42 import org.apache.log4j.PropertyConfigurator;
43
44 /**
45  * Provides methods for use in unit tests that use MHF.
46  */

47 public class TestUtils {
48
49   // constructors /////////////////////////////////////////////////////////////
50

51   // constants ////////////////////////////////////////////////////////////////
52

53   // classes //////////////////////////////////////////////////////////////////
54

55   // methods //////////////////////////////////////////////////////////////////
56

57   /**
58    * Sets up a <tt>FileAppender</tt> with a <tt>SimpleLayout</tt> with the
59    * specified <tt>level</tt>.
60    */

61   public static void setLogLevel( Level level ) {
62     BasicConfigurator.resetConfiguration();
63
64     Properties JavaDoc props = new Properties JavaDoc();
65     props.put(
66       "log4j.appender.default",
67       "org.apache.log4j.FileAppender" );
68     props.put(
69       "log4j.appender.default.file",
70       "log.txt" );
71     props.put(
72       "log4j.appender.default.layout",
73       "org.apache.log4j.SimpleLayout" );
74     props.put(
75       "log4j.rootLogger",
76       level.toString() + ", default" );
77
78     PropertyConfigurator.configure( props );
79   }
80
81   /**
82    * Initializes the Log4j system. Logging information will be written to
83    * <tt>log.txt</tt> in the current working directory.
84    */

85   public static void initLogger() {
86       //
87
// basic configuration
88
//
89
Properties JavaDoc props = new Properties JavaDoc();
90       props.put( "log4j.appender.default", "org.apache.log4j.FileAppender" );
91       props.put( "log4j.appender.default.file", "log.txt" );
92       props.put( "log4j.appender.default.layout", "org.apache.log4j.PatternLayout" );
93       props.put( "log4j.appender.default.layout.ConversionPattern", "%d %-5p %c - %m%n" );
94       props.put( "log4j.rootLogger", Level.ERROR.toString() + ", default" );
95
96       //
97
// look for log4j.properties in the current directory
98
//
99
File JavaDoc loggerPropertiesFile = new File JavaDoc( "log4j.properties" );
100
101       if ( loggerPropertiesFile.exists() ) {
102         try {
103           InputStream JavaDoc in = new FileInputStream JavaDoc( loggerPropertiesFile );
104           props.load( in );
105           in.close();
106         }
107         catch ( IOException JavaDoc e ) {
108           System.out.println( "TestUtils.initLogger(): Couldn't read log4j.properties." );
109         }
110       }
111  
112       //
113
// configure the logger
114
//
115
PropertyConfigurator.configure( props );
116   }
117
118   /**
119    * Initializes a {@link com.methodhead.persistable.ConnectionSingleton
120    * ConnectionSingleton}. Database connection properties are expected to be
121    * in <tt>db.properties</tt> in the current working directory.
122    */

123   public static void initDb() {
124
125     //
126
// init connection singleton
127
//
128
Properties JavaDoc dbProps = null;
129     try {
130       InputStream JavaDoc in =
131         new FileInputStream JavaDoc( "db.properties" );
132
133       dbProps = new Properties JavaDoc();
134       dbProps.load( in );
135
136       in.close();
137
138       if ( !ConnectionSingleton.init( dbProps ) ) {
139         Logger.getLogger( "DbTestCase" ).error(
140           "Couldn't init connection singleton with properties " + dbProps );
141       }
142     }
143     catch ( Exception JavaDoc e ) {
144       // do nothing?
145
}
146   }
147
148   /**
149    * Compares two dates for equality to second precision.
150    */

151   public static boolean datesEqual(
152     Date JavaDoc expected,
153     Date JavaDoc actual ) {
154
155     DateFormat JavaDoc format = new SimpleDateFormat JavaDoc( "yyyy-MM-dd HH:mm:ss" );
156
157     String JavaDoc e = "null";
158     if ( expected != null )
159       e = format.format( expected );
160
161     String JavaDoc a = "null";
162     if ( actual != null )
163       a = format.format( actual );
164
165     if ( !e.equals( a ) )
166       return false;
167
168     return true;
169   }
170
171   /**
172    * Returns the current date (not unit tested). The current date can be set
173    * explicity using {@link #setCurrentDate setCurrentDate()}.
174    */

175   public static Date JavaDoc getCurrentDate() {
176     if ( currentDate_ != null )
177       return currentDate_;
178
179     return new Date JavaDoc();
180   }
181
182   /**
183    * Returns a calendar set to the current date. The current date can be set
184    * explicity using {@link #setCurrentDate setCurrentDate()}.
185    */

186   public static Calendar JavaDoc getCurrentCalendar() {
187     Calendar JavaDoc cal = Calendar.getInstance();
188
189     if ( currentDate_ != null )
190       cal.setTime( currentDate_ );
191
192     return cal;
193   }
194
195   /**
196    * Sets the current date for testing (not unit tested).
197    */

198   public static void setCurrentDate(
199     Date JavaDoc currentDate ) {
200     currentDate_ = currentDate;
201   }
202
203   // properties ///////////////////////////////////////////////////////////////
204

205   // attributes ///////////////////////////////////////////////////////////////
206

207   private static Date JavaDoc currentDate_ = null;
208 }
209
Popular Tags