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:
026: import java.text.DateFormat;
027: import java.text.SimpleDateFormat;
028:
029: import java.util.Date;
030: import java.util.Properties;
031:
032: import com.methodhead.persistable.ConnectionSingleton;
033:
034: import junit.framework.TestCase;
035:
036: import org.apache.log4j.BasicConfigurator;
037: import org.apache.log4j.Level;
038: import org.apache.log4j.Logger;
039: import org.apache.log4j.PropertyConfigurator;
040:
041: /**
042: * A test case that provides some convenience methods for database access,
043: * looking for configuration information in the file <tt>db.properties</tt> in
044: * the current working directory. The properties <tt>driver</tt> (e.g,
045: * <tt>org.postgresql.Driver</tt>), <tt>uri</tt> (e.g.,
046: * <tt>jdbc:postgresql:yourdatabase</tt>), <tt>user</tt>, and <tt>password</tt>
047: * are read.
048: */
049: public class DbTestCase extends TestCase {
050:
051: // constructors /////////////////////////////////////////////////////////////
052:
053: public DbTestCase(String name) {
054: super (name);
055: }
056:
057: // constants ////////////////////////////////////////////////////////////////
058:
059: // classes //////////////////////////////////////////////////////////////////
060:
061: // methods //////////////////////////////////////////////////////////////////
062:
063: /**
064: * Compares two dates for equality to second precision.
065: */
066: protected static void assertDatesEqual(Date expected, Date actual) {
067:
068: DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
069:
070: String e = "null";
071: if (expected != null)
072: e = format.format(expected);
073:
074: String a = "null";
075: if (actual != null)
076: a = format.format(actual);
077:
078: if (!e.equals(a))
079: fail("expected <" + e + "> but was:<" + a + ">");
080: }
081:
082: /**
083: * Sets up a <tt>FileAppender</tt> with a <tt>SimpleLayout</tt> with the
084: * specified <tt>level</tt>.
085: */
086: public static void setLogLevel(Level level) {
087: BasicConfigurator.resetConfiguration();
088:
089: Properties props = new Properties();
090: props.put("log4j.appender.default",
091: "org.apache.log4j.FileAppender");
092: props.put("log4j.appender.default.file", "log.txt");
093: props.put("log4j.appender.default.layout",
094: "org.apache.log4j.SimpleLayout");
095: props.put("log4j.rootLogger", level.toString() + ", default");
096:
097: PropertyConfigurator.configure(props);
098: }
099:
100: // properties ///////////////////////////////////////////////////////////////
101:
102: // attributes ///////////////////////////////////////////////////////////////
103:
104: static {
105: //
106: // init logger
107: //
108: BasicConfigurator.configure();
109: setLogLevel(Level.ERROR);
110:
111: //
112: // init connection singleton
113: //
114: Properties dbProps = null;
115: try {
116: InputStream in = new FileInputStream("db.properties");
117:
118: dbProps = new Properties();
119: dbProps.load(in);
120:
121: in.close();
122:
123: if (!ConnectionSingleton.init(dbProps)) {
124: Logger.getLogger("DbTestCase").error(
125: "Couldn't init connection singleton with properties "
126: + dbProps);
127: }
128: } catch (Exception e) {
129: // do nothing?
130: }
131: }
132: }
|