1 21 package com.db4o.test; 22 23 import java.lang.reflect.*; 24 import java.util.*; 25 26 import com.db4o.*; 27 import com.db4o.config.*; 28 29 39 public class AllTests extends AllTestsConfAll implements Runnable { 40 41 public static void main(String [] args) { 42 if(args!=null&&args.length>0&&args[0].startsWith("-")) { 43 boolean solo=false; 44 boolean cs=false; 45 if("-solo".equals(args[0])||"-full".equals(args[0])) { 46 solo=true; 47 } 48 if("-cs".equals(args[0])||"-full".equals(args[0])) { 49 cs=true; 50 } 51 AllTests.run(solo, cs, testCasesFromArgs(args,1)); 52 return; 53 } 54 if(args!=null&&args.length==1&&args[0].equals("*")) { 55 new AllTests(new String []{}).runWithException(); 56 return; 57 } 58 new AllTests(args).run(); 59 } 60 61 public AllTests(String [] testcasenames) { 62 63 Configuration conf = Db4o.configure(); 64 conf.messageLevel(-1); 65 66 68 71 87 88 if(testcasenames!=null&&testcasenames.length>0) { 89 _testCases=testCasesFromArgs(testcasenames); 90 } else{ 91 testCasesFromTestSuites(); 92 } 93 94 Test.currentRunner = this; 95 } 96 97 public void run() { 98 printOutResult(runResult()); 99 } 100 101 public void runWithException() { 102 TestResult result = runResult(); 103 printOutResult(result); 104 if(result.numFailures()>0) { 105 throw new RuntimeException ("db4o regression test failure: "+result); 106 } 107 } 108 109 private void printOutResult(TestResult result) { 110 System.out.println("\n\nAllTests completed.\nAssertions: " 111 + result.numAssertions() + "\nTime: " + result.timeTaken() + "ms"); 112 if (result.numFailures() == 0) { 113 System.out.println("No errors detected.\n"); 114 } else { 115 System.out 116 .println("" + result.numFailures() + " ERRORS DETECTED !!!.\n"); 117 } 118 } 119 120 public TestResult runResult() { 121 logConfiguration(); 122 123 Test.beginTesting(); 124 125 long time = System.currentTimeMillis(); 126 127 if (DELETE_FILE) { 128 Test.delete(); 129 } 130 131 configure(); 132 133 for (Test.run = 1; Test.run <= RUNS; Test.run++) { 134 System.out.println("\ncom.db4o.test.AllTests run " + Test.run 135 + " from " + RUNS + "\n"); 136 if (SOLO) { 137 Test.runServer = false; 138 Test.clientServer = false; 139 runTests(); 140 } 141 if (CLIENT_SERVER) { 142 Test.runServer = !REMOTE_SERVER; 143 Test.clientServer = true; 144 runTests(); 145 } 146 Test.end(); 147 } 148 time = System.currentTimeMillis() - time; 149 TestResult result=new TestResult(Test.assertionCount,Test.errorCount,time); 150 return result; 151 } 152 153 protected void configure() { 154 for (int i = 0; i < _testCases.length; i++) { 155 Object toTest = newInstance(_testCases[i]); 156 runMethod(toTest, "configure"); 157 } 158 } 159 160 private void runTests() { 161 String cs = Test.clientServer ? "C/S" : "SOLO"; 162 for (int i = 0; i < _testCases.length; i++) { 163 System.out.println(cs + " testing " + _testCases[i].getName()); 164 Object toTest = newInstance(_testCases[i]); 165 Test.open(); 166 if (!runStoreOne(toTest)) { 167 runMethod(toTest, "store"); 168 } 169 Test.commit(); 170 Test.close(); 171 Test.open(); 172 toTest = newInstance(_testCases[i]); 173 runTestOne(toTest); 174 toTest = newInstance(_testCases[i]); 175 Method[] methods = _testCases[i].getDeclaredMethods(); 176 for (int j = 0; j < methods.length; j++) { 177 Method method = methods[j]; 178 String methodName = method.getName(); 179 if (!methodName.equals("testOne")) { 180 if (method.getName().indexOf("test") == 0) { 181 try { 182 method.invoke(toTest, (Object [])null); 183 } catch (Exception e) { 184 Test.errorCount++; 185 e.printStackTrace(); 186 } 187 } 188 } 189 } 190 Test.close(); 191 } 192 } 193 194 private Object newInstance(Class clazz) { 195 try { 196 return clazz.newInstance(); 197 } catch (InstantiationException e) { 198 } catch (IllegalAccessException e) { 199 } 200 System.out.println("Instantiation failed. Class:" + clazz.getName()); 201 System.out.println("The class needs a #newInstance() constructor for the test framework."); 202 new Exception ().printStackTrace(); 203 return null; 204 } 205 206 private void runMethod(Object onObject, String methodName) { 207 try { 208 Method method = onObject.getClass().getDeclaredMethod(methodName, 209 (Class [])null); 210 if (method != null) { 211 try { 212 method.invoke(onObject, (Object [])null); 213 } catch (Exception e1) { 214 e1.printStackTrace(); 215 } 216 } 217 } catch (Exception e) { 218 } 219 } 220 221 private boolean runStoreOne(Object onObject) { 222 try { 223 Method method = onObject.getClass().getDeclaredMethod("storeOne", 224 (Class [])null); 225 if (method != null) { 226 try { 227 Test.deleteAllInstances(onObject); 228 method.invoke(onObject, (Object [])null); 229 Test.store(onObject); 230 return true; 231 } catch (Exception e2) { 232 e2.printStackTrace(); 233 } 234 } 235 } catch (Exception e) { 236 } 237 return false; 238 } 239 240 private boolean runTestOne(Object onObject) { 241 try { 242 Method method = onObject.getClass().getDeclaredMethod("testOne", 243 (Class [])null); 244 if (method != null) { 245 try { 246 onObject = Test.getOne(onObject); 247 method.invoke(onObject, (Object [])null); 248 return true; 249 } catch (Exception e2) { 250 e2.printStackTrace(); 251 } 252 } 253 } catch (Exception e) { 254 } 255 return false; 256 } 257 258 public static void run(Class clazz){ 259 run(new Class []{clazz}); 260 } 261 262 public static void run(TestSuite suite){ 263 run(suite.tests()); 264 } 265 266 public static void run(Class [] classes){ 267 run(true, true, classes); 268 } 269 270 public static void runSolo(Class clazz){ 271 runSolo(new Class []{clazz}); 272 } 273 274 public static void runSolo(TestSuite suite){ 275 runSolo(suite.tests()); 276 } 277 278 public static void runSolo(Class [] classes){ 279 run(true, false, classes); 280 } 281 282 public static void runClientServer(Class clazz){ 283 runClientServer(new Class []{clazz}); 284 } 285 286 public static void runClientServer(TestSuite suite){ 287 runClientServer(suite.tests()); 288 } 289 290 public static void runClientServer(Class [] classes){ 291 run(false, true, classes); 292 } 293 294 public static void run(boolean solo, boolean clientServer, Class [] classes){ 295 AllTests allTests = new AllTests(); 296 allTests._testCases=classes; 297 allTests.SOLO = solo; 298 allTests.CLIENT_SERVER = clientServer; 299 allTests.run(); 300 } 301 302 protected void logConfiguration() { 303 System.out.println("Running " + getClass().getName() + " against\n" 304 + Db4o.version() + "\n"); 305 System.out.println("Using " + TEST_CONFIGURATION 306 + ".\n"); 307 System.out.println("SERVER_HOSTNAME: " + SERVER_HOSTNAME); 308 System.out.println("SERVER_PORT: " + SERVER_PORT); 309 System.out.println("FILE_SERVER: " + FILE_SERVER); 310 if(MEMORY_FILE) { 311 System.out.println("MEMORY_FILE !!!"); 312 }else { 313 System.out.println("FILE_SOLO: " + FILE_SOLO); 314 } 315 System.out.println("DELETE_FILE: " + DELETE_FILE); 316 System.out.println("BLOB_PATH: " + BLOB_PATH + "\n"); 317 318 } 319 320 public AllTests() { 321 this(null); 322 } 323 324 private void testCasesFromTestSuites() { 325 _testCases=new Class [0]; 326 _testSuites = new Vector(); 327 328 addTestSuites(this); 329 330 Enumeration e = _testSuites.elements(); 331 while (e.hasMoreElements()) { 332 TestSuite suite = (TestSuite)e.nextElement(); 333 _testCases = concat(_testCases, suite.tests()); 334 } 335 } 336 337 private Class [] concat(Class [] a, Class [] b) { 338 Class [] result = new Class [a.length + b.length]; 339 System.arraycopy(a,0, result,0 , a.length); 340 System.arraycopy(b,0, result,a.length, b.length); 341 return result; 342 } 343 344 private static Class [] testCasesFromArgs(String [] testcasenames) { 345 return testCasesFromArgs(testcasenames,0); 346 } 347 348 private static Class [] testCasesFromArgs(String [] testcasenames,int offset) { 349 Class [] testCases=new Class [testcasenames.length-offset]; 350 for (int testidx = offset; testidx < testcasenames.length; testidx++) { 351 try { 352 testCases[testidx-offset]=Class.forName(testcasenames[testidx]); 353 } catch (ClassNotFoundException e) { 354 System.err.println("Test case class not found: "+testcasenames[testidx]); 355 e.printStackTrace(); 356 System.exit(0); 357 } 358 } 359 return testCases; 360 } 361 362 363 private Class [] _testCases; 364 365 } | Popular Tags |