1 4 5 package org.objectweb.util.monolog; 6 7 import junit.framework.TestCase; 8 import junit.textui.TestRunner; 9 import org.objectweb.util.monolog.api.Handler; 10 import org.objectweb.util.monolog.api.HandlerFactory; 11 import org.objectweb.util.monolog.api.LevelFactory; 12 import org.objectweb.util.monolog.api.LoggerFactory; 13 import org.objectweb.util.monolog.api.TopicalLogger; 14 15 import java.io.*; 16 import java.util.Vector ; 17 18 23 public class TestHelper extends TestCase { 24 28 public static final String [] SETTER_METHODS = {"setLoggerFactoryClassName", 29 }; 30 31 34 public static LoggerFactory lf = null; 35 36 39 public static HandlerFactory hf = null; 40 41 44 public static LevelFactory lef = null; 45 46 49 protected String fileName = null; 50 51 public TestHelper() { 52 super(""); 53 } 54 55 public TestHelper(String s) { 56 super(s); 57 } 58 59 protected void setUp() throws Exception { 60 if (lf == null) { 61 synchronized(TestHelper.class) { 62 lf = Monolog.getMonologFactory("monolog.properties"); 63 hf = (HandlerFactory) lf; 64 lef = (LevelFactory) lf; 65 } 66 } 67 } 68 69 73 public void setLoggerFactoryClassName(String lfcn) { 74 try { 75 lf = (LoggerFactory) Class.forName(lfcn).newInstance(); 76 } 77 catch (ClassCastException e) { 78 throw new UnsupportedOperationException ( 79 "The specified class is not a Logger factory: " + lfcn); 80 } 81 catch (Exception e) { 82 throw new UnsupportedOperationException ( 83 "Logger factory class is not availlable: " + lfcn); 84 } 85 if (lf instanceof HandlerFactory) { 86 hf = (HandlerFactory) lf; 87 } 88 if (lf instanceof LevelFactory) { 89 lef = (LevelFactory) lf; 90 } 91 } 92 93 96 public void setOutputFile(String filename) { 97 fileName = filename; 98 } 99 100 103 public static void run(Class c, String [] methods, String params[], 104 String lfcn, String fn) { 105 try { 106 TestRunner.run(getTestSuite(c, methods, params, lfcn, fn)); 107 } 108 catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 116 public static void run(Class c, String [] methods, 117 String params[], String lfcn) { 118 try { 119 TestRunner.run(getTestSuite(c, methods, params, lfcn)); 120 } 121 catch (Exception e) { 122 e.printStackTrace(); 123 } 124 } 125 126 129 public static TestSuite getTestSuite(Class c, 130 String [] methods, 131 String params[], 132 String lfcn, 133 String fn) { 134 Object [] myparams = new Object [params.length + 2]; 135 myparams[0] = lfcn; 136 myparams[1] = fn; 137 for (int i = 0; i < params.length; i++) { 138 myparams[i + 2] = params[i]; 139 } 140 String [] mymethods = new String [methods.length + 2]; 141 mymethods[0] = "setLoggerFactoryClassName"; 142 mymethods[1] = "setOutputFile"; 143 for (int i = 0; i < methods.length; i++) { 144 mymethods[i + 2] = methods[i]; 145 } 146 try { 147 return new TestSuite(c, mymethods, myparams); 148 } 149 catch (Exception e) { 150 e.printStackTrace(); 151 return null; 152 } 153 } 154 155 public static TestSuite getTestSuite(Class c, 156 String [] methods, 157 String params[], 158 String lfcn) { 159 Object [] myparams = new Object [params.length + 1]; 160 myparams[0] = lfcn; 161 for (int i = 0; i < params.length; i++) { 162 myparams[i + 1] = params[i]; 163 } 164 String [] mymethods = new String [methods.length + 1]; 165 mymethods[0] = "setLoggerFactoryClassName"; 166 for (int i = 0; i < methods.length; i++) { 167 mymethods[i + 1] = methods[i]; 168 } 169 try { 170 return new TestSuite(c, mymethods, myparams); 171 } 172 catch (Exception e) { 173 e.printStackTrace(); 174 return null; 175 } 176 } 177 178 public static void main(String [] args) { 179 if (args == null || args.length <= 1) { 180 System.out.println("Invalid argument"); 181 System.exit(1); 182 } 183 try { 184 File f = new File(args[0]); 185 f.delete(); 186 f.createNewFile(); 187 PrintStream ps = new PrintStream(new FileOutputStream(f)); 188 String [] required = new String [args.length - 1]; 189 for (int i = 0; i < required.length; i++) { 190 required[i] = args[i + 1]; 191 ps.println(required[i]); 192 } 193 ps.flush(); 194 ps.close(); 195 TestHelper to = new TestHelper(); 196 to.setOutputFile(args[0]); 197 to.assertInFileEquals("my message", required); 198 f.delete(); 199 } 200 catch (IOException e) { 201 e.printStackTrace(); 202 fail(e.getMessage()); 203 } 204 } 205 206 212 public void assertInFileEquals(String message, String required) { 213 String [] r = new String [1]; 214 r[0] = required; 215 assertInFileEquals(message, fileName, r); 216 } 217 218 225 public void assertInFileEquals(String message, String filename, 226 String required) { 227 String [] r = new String [1]; 228 r[0] = required; 229 assertInFileEquals(message, filename, r); 230 } 231 232 239 public void assertInFileEquals(String message, String [] required) { 240 String [] text = getFirstLines(fileName, required.length); 241 for (int i = 0; i < required.length; i++) { 243 debug("found : text[" + i + "]=" + text[i] + " text[i].length = " + text[i].length()); 244 debug("required : required[" + i + "]=" + required[i] + " required[i].length = " + required[i].length()); 245 assertEquals(message + " " + i, required[i], text[i]); 246 } 247 } 248 249 257 public void assertInFileEquals(String message, String filename, 258 String [] required) { 259 String [] text = getFirstLines(filename, required.length); 260 for (int i = 0; i < required.length; i++) { 262 debug("found : text[" + i + "]=" + text[i]); 263 debug("required : required[" + i + "]=" + required[i]); 264 assertEquals(message + " " + i, required[i], text[i]); 265 } 266 } 267 268 273 public String [] getLastLines(int linenumber) { 274 return getLastLines(fileName, linenumber); 275 } 276 277 282 public String [] getFirstLines(int linenumber) { 283 return getFirstLines(fileName, linenumber); 284 } 285 286 292 public String [] getFirstLines(String filename, int linenumber) { 293 if (filename==null || linenumber==0) 294 return(null); 295 Vector v = new Vector (linenumber); 296 try { 297 FileReader fr= new FileReader(filename); 298 LineNumberReader lnr = new LineNumberReader(fr); 299 String str = lnr.readLine(); 300 int i=0; 301 while(i<linenumber && str != null) { 302 v.addElement(str); 303 str = lnr.readLine(); 304 i++; 305 } 306 int size = v.size(); 307 String [] res = new String [size]; 308 for (int j = 0; j < size; j++) { 309 res[j] = (String ) v.elementAt(j); 310 } 311 return res; 312 } 313 catch (Exception e) { 314 e.printStackTrace(); 315 return null; 317 } 318 } 319 325 public String [] getLastLines(String filename, int linenumber) { 326 if (filename==null || linenumber==0) 327 return(null); 328 Vector v = new Vector (linenumber); 329 try { 330 FileReader fr= new FileReader(filename); 331 LineNumberReader lnr = new LineNumberReader(fr); 332 String str = lnr.readLine(); 333 int i=0; 334 while(str != null) { 335 v.addElement(str); 336 str = lnr.readLine(); 337 i++; 338 } 339 int size = v.size(); 340 String [] res = new String [size]; 341 for (int j = 0; j < size; j++) { 342 res[j] = (String ) v.elementAt(size-1-j); 343 } 344 return res; 345 } 346 catch (Exception e) { 347 e.printStackTrace(); 348 return null; 350 } 351 } 352 353 356 public void quietRootLogger() { 357 TopicalLogger l = (TopicalLogger) lf.getLogger("root"); 358 Handler[] hcs = l.getHandler(); 359 Handler hc = hf.createHandler("handlerOfroot", "file"); 360 hc.setAttribute(Handler.OUTPUT_ATTRIBUTE, "rootLogger.log"); 361 hc.setAttribute(Handler.PATTERN_ATTRIBUTE, "%m%n"); 362 hc.setAttribute("activation", lf); 363 try { 364 l.addHandler(hc); 365 for (int i = 0; i < hcs.length; i++) { 366 l.removeHandler(hcs[i]); 367 } 368 } 369 catch (Exception e) { 370 fail("error" + e.getMessage()); 371 } 372 l = null; 373 } 374 375 static boolean debug = 376 Boolean.valueOf(System.getProperty("monolog.test.debug")).booleanValue(); 377 378 381 protected void debug(String m) { 382 if (debug) 383 System.out.println(m); 384 } 385 386 private int next(int i, int max) { 387 return (i + 1 == max ? 0 : i + 1); 388 } 389 } 390 | Popular Tags |