1 18 19 package org.apache.jorphan.test; 20 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.lang.reflect.Method ; 25 import java.util.Iterator ; 26 import java.util.Properties ; 27 28 import junit.framework.TestCase; 29 import junit.framework.TestSuite; 30 import junit.textui.TestRunner; 31 32 import org.apache.jorphan.logging.LoggingManager; 33 import org.apache.jorphan.reflect.ClassFinder; 34 import org.apache.jorphan.util.JOrphanUtils; 35 import org.apache.log.Logger; 36 37 91 public final class AllTests 92 { 93 transient private static Logger log = LoggingManager.getLoggerForClass(); 94 95 98 private AllTests() 99 { 100 } 101 102 private static void logprop(String prop,boolean show) 103 { 104 String value = System.getProperty(prop); 105 log.info(prop+"="+value); 106 if (show) System.out.println(prop+"="+value); 107 } 108 109 private static void logprop(String prop) 110 { 111 logprop(prop,false); 112 } 113 114 125 public static void main(String [] args) 126 { 127 if (args.length < 1) 128 { 129 System.out.println( 130 "You must specify a comma-delimited list of paths to search " + 131 "for unit tests"); 132 System.exit(0); 133 } 134 initializeLogging(args); 135 initializeManager(args); 136 138 logprop("java.version",true); 139 logprop("java.vendor"); 140 logprop("java.home",true); 141 logprop("user.home"); 142 logprop("user.dir",true); 143 logprop("os.name"); 144 logprop("os.version"); 145 logprop("os.arch"); 146 logprop("java.class.version"); 147 String cp = System.getProperty("java.class.path"); 149 String cpe[]= JOrphanUtils.split(cp,java.io.File.pathSeparator); 150 StringBuffer sb = new StringBuffer (3000); 151 sb.append("java.class.path="); 152 for (int i=0;i<cpe.length;i++){ 153 sb.append("\n"); 154 sb.append(cpe[i]); 155 if (new java.io.File (cpe[i]).exists()) 156 { 157 sb.append(" - OK"); 158 } 159 else 160 { 161 sb.append(" - ??"); 162 } 163 } 164 log.info(sb.toString()); 165 166 175 System.out.println("+++++++++++"); 176 logprop("java.awt.headless",true); 177 logprop("java.awt.graphicsenv",true); 178 System.out.println("------------"); 191 System.out.println("Creating test suite"); 194 TestSuite suite = suite(args[0]); 195 System.out.println("Starting test run"); 196 197 TestRunner.run(suite); 204 System.out.println("+++++++++++"); 207 System.out.println("------------"); 221 System.exit(0); 223 } 224 225 230 protected static void initializeLogging(String [] args) 231 { 232 if (args.length >= 2) 233 { 234 Properties props = new Properties (); 235 try 236 { 237 System.out.println( 238 "Setting up logging props using file: " + args[1]); 239 props.load(new FileInputStream (args[1])); 240 LoggingManager.initializeLogging(props); 241 } 242 catch (FileNotFoundException e) 243 { 244 } 245 catch (IOException e) 246 { 247 } 248 } 249 } 250 251 257 protected static void initializeManager(String [] args) 258 { 259 if (args.length >= 3) 260 { 261 try 262 { 263 System.out.println( 264 "Using initializeProperties() from " + args[2]); 265 UnitTestManager um = 266 (UnitTestManager) Class.forName(args[2]).newInstance(); 267 System.out.println( 268 "Setting up initial properties using: " + args[1]); 269 um.initializeProperties(args[1]); 270 } 271 catch (Exception e) 272 { 273 System.out.println("Couldn't create: " + args[2]); 274 e.printStackTrace(); 275 } 276 } 277 } 278 279 285 public static TestSuite suite() 286 { 287 String args[] = { "../lib/ext", 288 "./jmetertest.properties", 289 "org.apache.jmeter.util.JMeterUtils" 290 }; 291 292 initializeManager(args); 293 return suite(args[0]); 294 } 295 296 301 private static TestSuite suite(String searchPaths) 302 { 303 TestSuite suite = new TestSuite(); 304 try 305 { 306 Iterator classes = 307 ClassFinder 308 .findClassesThatExtend( 309 JOrphanUtils.split(searchPaths, ","), 310 new Class [] { TestCase.class }, 311 true) 312 .iterator(); 313 while (classes.hasNext()) 314 { 315 String name = (String ) classes.next(); 316 try 317 { 318 326 327 Class clazz = Class.forName(name); 328 TestSuite t= null; 329 try 330 { 331 Method m = clazz.getMethod("suite", new Class [0]); 332 t = (TestSuite) m.invoke(clazz,null); 333 } 334 catch (NoSuchMethodException e) {} 340 if (t == null) 341 { 342 t= new TestSuite(clazz); 343 } 344 345 suite.addTest(t); 346 } 347 catch (Exception ex) 348 { 349 System.out.println("Error adding test for class "+name+" "+ex.toString()); 350 log.error("error adding test :", ex); 351 } 352 } 353 } 354 catch (IOException e) 355 { 356 log.error("", e); 357 } 358 catch (ClassNotFoundException e) 359 { 360 log.error("", e); 361 } 362 return suite; 363 } 364 } 365 | Popular Tags |