001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.test;
022:
023: import java.io.FileInputStream;
024: import java.io.InputStream;
025: import java.io.File;
026: import java.io.IOException;
027:
028: import java.text.DateFormat;
029: import java.text.SimpleDateFormat;
030:
031: import java.util.Date;
032: import java.util.Calendar;
033: import java.util.Properties;
034:
035: import com.methodhead.persistable.ConnectionSingleton;
036:
037: import junit.framework.TestCase;
038:
039: import org.apache.log4j.BasicConfigurator;
040: import org.apache.log4j.Level;
041: import org.apache.log4j.Logger;
042: import org.apache.log4j.PropertyConfigurator;
043:
044: /**
045: * Provides methods for use in unit tests that use MHF.
046: */
047: public class TestUtils {
048:
049: // constructors /////////////////////////////////////////////////////////////
050:
051: // constants ////////////////////////////////////////////////////////////////
052:
053: // classes //////////////////////////////////////////////////////////////////
054:
055: // methods //////////////////////////////////////////////////////////////////
056:
057: /**
058: * Sets up a <tt>FileAppender</tt> with a <tt>SimpleLayout</tt> with the
059: * specified <tt>level</tt>.
060: */
061: public static void setLogLevel(Level level) {
062: BasicConfigurator.resetConfiguration();
063:
064: Properties props = new Properties();
065: props.put("log4j.appender.default",
066: "org.apache.log4j.FileAppender");
067: props.put("log4j.appender.default.file", "log.txt");
068: props.put("log4j.appender.default.layout",
069: "org.apache.log4j.SimpleLayout");
070: props.put("log4j.rootLogger", level.toString() + ", default");
071:
072: PropertyConfigurator.configure(props);
073: }
074:
075: /**
076: * Initializes the Log4j system. Logging information will be written to
077: * <tt>log.txt</tt> in the current working directory.
078: */
079: public static void initLogger() {
080: //
081: // basic configuration
082: //
083: Properties props = new Properties();
084: props.put("log4j.appender.default",
085: "org.apache.log4j.FileAppender");
086: props.put("log4j.appender.default.file", "log.txt");
087: props.put("log4j.appender.default.layout",
088: "org.apache.log4j.PatternLayout");
089: props.put("log4j.appender.default.layout.ConversionPattern",
090: "%d %-5p %c - %m%n");
091: props.put("log4j.rootLogger", Level.ERROR.toString()
092: + ", default");
093:
094: //
095: // look for log4j.properties in the current directory
096: //
097: File loggerPropertiesFile = new File("log4j.properties");
098:
099: if (loggerPropertiesFile.exists()) {
100: try {
101: InputStream in = new FileInputStream(
102: loggerPropertiesFile);
103: props.load(in);
104: in.close();
105: } catch (IOException e) {
106: System.out
107: .println("TestUtils.initLogger(): Couldn't read log4j.properties.");
108: }
109: }
110:
111: //
112: // configure the logger
113: //
114: PropertyConfigurator.configure(props);
115: }
116:
117: /**
118: * Initializes a {@link com.methodhead.persistable.ConnectionSingleton
119: * ConnectionSingleton}. Database connection properties are expected to be
120: * in <tt>db.properties</tt> in the current working directory.
121: */
122: public static void initDb() {
123:
124: //
125: // init connection singleton
126: //
127: Properties dbProps = null;
128: try {
129: InputStream in = new FileInputStream("db.properties");
130:
131: dbProps = new Properties();
132: dbProps.load(in);
133:
134: in.close();
135:
136: if (!ConnectionSingleton.init(dbProps)) {
137: Logger.getLogger("DbTestCase").error(
138: "Couldn't init connection singleton with properties "
139: + dbProps);
140: }
141: } catch (Exception e) {
142: // do nothing?
143: }
144: }
145:
146: /**
147: * Compares two dates for equality to second precision.
148: */
149: public static boolean datesEqual(Date expected, Date actual) {
150:
151: DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
152:
153: String e = "null";
154: if (expected != null)
155: e = format.format(expected);
156:
157: String a = "null";
158: if (actual != null)
159: a = format.format(actual);
160:
161: if (!e.equals(a))
162: return false;
163:
164: return true;
165: }
166:
167: /**
168: * Returns the current date (not unit tested). The current date can be set
169: * explicity using {@link #setCurrentDate setCurrentDate()}.
170: */
171: public static Date getCurrentDate() {
172: if (currentDate_ != null)
173: return currentDate_;
174:
175: return new Date();
176: }
177:
178: /**
179: * Returns a calendar set to the current date. The current date can be set
180: * explicity using {@link #setCurrentDate setCurrentDate()}.
181: */
182: public static Calendar getCurrentCalendar() {
183: Calendar cal = Calendar.getInstance();
184:
185: if (currentDate_ != null)
186: cal.setTime(currentDate_);
187:
188: return cal;
189: }
190:
191: /**
192: * Sets the current date for testing (not unit tested).
193: */
194: public static void setCurrentDate(Date currentDate) {
195: currentDate_ = currentDate;
196: }
197:
198: // properties ///////////////////////////////////////////////////////////////
199:
200: // attributes ///////////////////////////////////////////////////////////////
201:
202: private static Date currentDate_ = null;
203: }
|