1 20 21 package com.methodhead.test; 22 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 26 import java.text.DateFormat ; 27 import java.text.SimpleDateFormat ; 28 29 import java.util.Date ; 30 import java.util.Properties ; 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 49 public class DbTestCase extends TestCase { 50 51 53 public DbTestCase( String name ) { 54 super( name ); 55 } 56 57 59 61 63 66 protected static void assertDatesEqual( 67 Date expected, 68 Date actual ) { 69 70 DateFormat format = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ); 71 72 String e = "null"; 73 if ( expected != null ) 74 e = format.format( expected ); 75 76 String 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 88 public static void setLogLevel( Level level ) { 89 BasicConfigurator.resetConfiguration(); 90 91 Properties props = new Properties (); 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 110 112 static { 113 BasicConfigurator.configure(); 117 setLogLevel( Level.ERROR ); 118 119 Properties dbProps = null; 123 try { 124 InputStream in = 125 new FileInputStream ( "db.properties" ); 126 127 dbProps = new Properties (); 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 e ) { 138 } 140 } 141 } 142 | Popular Tags |