KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import java.text.DateFormat JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28
29 import java.util.Date JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import com.methodhead.persistable.ConnectionSingleton;
33
34 import junit.framework.TestCase;
35
36 import org.apache.log4j.BasicConfigurator;
37 import org.apache.log4j.Level;
38 import org.apache.log4j.Logger;
39 import org.apache.log4j.PropertyConfigurator;
40
41 /**
42  * A test case that provides some convenience methods for database access,
43  * looking for configuration information in the file <tt>db.properties</tt> in
44  * the current working directory. The properties <tt>driver</tt> (e.g,
45  * <tt>org.postgresql.Driver</tt>), <tt>uri</tt> (e.g.,
46  * <tt>jdbc:postgresql:yourdatabase</tt>), <tt>user</tt>, and <tt>password</tt>
47  * are read.
48  */

49 public class DbTestCase extends TestCase {
50
51   // constructors /////////////////////////////////////////////////////////////
52

53   public DbTestCase( String JavaDoc name ) {
54     super( name );
55   }
56
57   // constants ////////////////////////////////////////////////////////////////
58

59   // classes //////////////////////////////////////////////////////////////////
60

61   // methods //////////////////////////////////////////////////////////////////
62

63   /**
64    * Compares two dates for equality to second precision.
65    */

66   protected static void assertDatesEqual(
67     Date JavaDoc expected,
68     Date JavaDoc actual ) {
69
70     DateFormat JavaDoc format = new SimpleDateFormat JavaDoc( "yyyy-MM-dd HH:mm:ss" );
71
72     String JavaDoc e = "null";
73     if ( expected != null )
74       e = format.format( expected );
75
76     String JavaDoc a = "null";
77     if ( actual != null )
78       a = format.format( actual );
79
80     if ( !e.equals( a ) )
81       fail( "expected <" + e + "> but was:<" + a + ">" );
82   }
83
84   /**
85    * Sets up a <tt>FileAppender</tt> with a <tt>SimpleLayout</tt> with the
86    * specified <tt>level</tt>.
87    */

88   public static void setLogLevel( Level level ) {
89     BasicConfigurator.resetConfiguration();
90
91     Properties JavaDoc props = new Properties JavaDoc();
92     props.put(
93       "log4j.appender.default",
94       "org.apache.log4j.FileAppender" );
95     props.put(
96       "log4j.appender.default.file",
97       "log.txt" );
98     props.put(
99       "log4j.appender.default.layout",
100       "org.apache.log4j.SimpleLayout" );
101     props.put(
102       "log4j.rootLogger",
103       level.toString() + ", default" );
104
105     PropertyConfigurator.configure( props );
106   }
107
108   // properties ///////////////////////////////////////////////////////////////
109

110   // attributes ///////////////////////////////////////////////////////////////
111

112   static {
113     //
114
// init logger
115
//
116
BasicConfigurator.configure();
117     setLogLevel( Level.ERROR );
118
119     //
120
// init connection singleton
121
//
122
Properties JavaDoc dbProps = null;
123     try {
124       InputStream JavaDoc in =
125         new FileInputStream JavaDoc( "db.properties" );
126
127       dbProps = new Properties JavaDoc();
128       dbProps.load( in );
129
130       in.close();
131
132       if ( !ConnectionSingleton.init( dbProps ) ) {
133         Logger.getLogger( "DbTestCase" ).error(
134           "Couldn't init connection singleton with properties " + dbProps );
135       }
136     }
137     catch ( Exception JavaDoc e ) {
138       // do nothing?
139
}
140   }
141 }
142
Popular Tags