1 20 21 package com.methodhead.test; 22 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 import java.io.File ; 26 import java.io.IOException ; 27 28 import java.text.DateFormat ; 29 import java.text.SimpleDateFormat ; 30 31 import java.util.Date ; 32 import java.util.Calendar ; 33 import java.util.Properties ; 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 47 public class TestUtils { 48 49 51 53 55 57 61 public static void setLogLevel( Level level ) { 62 BasicConfigurator.resetConfiguration(); 63 64 Properties props = new Properties (); 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 85 public static void initLogger() { 86 Properties props = new Properties (); 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 File loggerPropertiesFile = new File ( "log4j.properties" ); 100 101 if ( loggerPropertiesFile.exists() ) { 102 try { 103 InputStream in = new FileInputStream ( loggerPropertiesFile ); 104 props.load( in ); 105 in.close(); 106 } 107 catch ( IOException e ) { 108 System.out.println( "TestUtils.initLogger(): Couldn't read log4j.properties." ); 109 } 110 } 111 112 PropertyConfigurator.configure( props ); 116 } 117 118 123 public static void initDb() { 124 125 Properties dbProps = null; 129 try { 130 InputStream in = 131 new FileInputStream ( "db.properties" ); 132 133 dbProps = new Properties (); 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 e ) { 144 } 146 } 147 148 151 public static boolean datesEqual( 152 Date expected, 153 Date actual ) { 154 155 DateFormat format = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ); 156 157 String e = "null"; 158 if ( expected != null ) 159 e = format.format( expected ); 160 161 String 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 175 public static Date getCurrentDate() { 176 if ( currentDate_ != null ) 177 return currentDate_; 178 179 return new Date (); 180 } 181 182 186 public static Calendar getCurrentCalendar() { 187 Calendar cal = Calendar.getInstance(); 188 189 if ( currentDate_ != null ) 190 cal.setTime( currentDate_ ); 191 192 return cal; 193 } 194 195 198 public static void setCurrentDate( 199 Date currentDate ) { 200 currentDate_ = currentDate; 201 } 202 203 205 207 private static Date currentDate_ = null; 208 } 209 | Popular Tags |