1 21 package com.db4o.test; 22 23 import java.lang.reflect.*; 24 25 import com.db4o.*; 26 import com.db4o.config.*; 27 import com.db4o.ext.*; 28 import com.db4o.foundation.*; 29 import com.db4o.test.types.*; 30 31 35 public class Regression { 36 37 41 private static final boolean DEBUG = false; 42 43 44 private static final boolean PROFILE_ONLY = false; 45 46 47 private static final int RUNS = 1; 48 49 50 private static final boolean DEFAULT = true; 51 52 53 private static final boolean JDK2 = true; 54 55 56 protected static final int THREAD_RUNS = 300; 57 58 63 private static final boolean THREADS = false; 64 65 66 private static final int COMMIT_AFTER = 5; 67 68 69 private static final boolean LOG_CLASS_NAMES = true; 70 71 public static void main(String [] args) { 72 73 new java.io.File (FILE).delete(); 74 75 Db4o.configure().messageLevel(-1); 76 78 82 new Regression().run(); 83 } 84 85 public void run() { 86 if (isJDK2() && JDK2) { 87 Thread.currentThread().setName("JDK2 Regression Test"); 88 try { 89 Regression regression2 = 90 (Regression) Class.forName("com.db4o.test.test2.Regression2").newInstance(); 91 run1(regression2.testClasses()); 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 } 96 if (DEFAULT) { 97 Thread.currentThread().setName("Default Regression Test"); 98 run1(testClasses()); 99 } 100 101 if (THREADS) { 102 try { 103 threadsSharp = true; 104 new Thread (new Thread1(this)).start(); 105 Thread.sleep(100); 106 new Thread (new Thread2(this)).start(); 108 Thread.sleep(100); 109 new Thread (new Thread3(this)).start(); 110 111 do { 114 Cool.sleepIgnoringInterruption(300); 115 } while (threadsSharp); 116 117 } catch (Exception e) { 118 e.printStackTrace(); 119 } 120 } else { 121 completed(); 122 } 123 } 124 125 public void run1(RTestable[] clazzes) { 126 openedThreads++; 127 long time = System.currentTimeMillis(); 128 mainLoop(clazzes); 129 time = System.currentTimeMillis() - time; 130 System.out.println("\n" + Thread.currentThread().getName() + ": " + time + " ms."); 131 returnedThreads++; 132 if (returnedThreads >= openedThreads) { 133 if (errors.length() == 0) { 134 ObjectContainer con = open(); 135 int objectCount = con.get(null).size(); 136 closeAllButMemoryFile(con); 137 System.out.println( 138 PROFILE_ONLY 139 ? "Profile run completed." 140 : "Regression Test Passed. " + objectCount + " objects."); 141 } else { 142 System.out.println("!!! Regression Test Failed. !!!"); 143 } 144 System.out.println(Db4o.version()); 145 System.out.println(errors); 146 if (threadsSharp) { 147 if (openDelegate != null) { 148 openDelegate.completed(); 149 } 150 threadsSharp = false; 151 } 152 } 153 } 154 155 public void mainLoop(RTestable[] clazzes) { 156 int commitCounter = 0; 157 int run = 0; 158 ObjectContainer con = open(); 159 for (int k = 0; k < runs(); k++) { 160 run++; 161 System.out.println(Thread.currentThread().getName() + " Run:" + run); 162 closeFile = false; 163 for (int i = 0; i < 2; i++) { 164 for (int j = 0; j < clazzes.length; j++) { 165 if(! clazzes[j].jdk2() || isJDK2()){ 166 con = cycle(con, clazzes[j], run); 167 } 168 } 169 closeFile = true; 170 } 171 commitCounter++; 172 if (commitCounter > COMMIT_AFTER) { 173 con.commit(); 174 commitCounter = 0; 175 } 176 } 177 178 179 if(closeFile()){ 180 close(con); 181 con = open(); 182 } 183 184 StoredClass[] intClasses = con.ext().storedClasses(); 186 String [] noDuplicates = new String [intClasses.length]; 187 for (int i = 0; i < intClasses.length; i++) { 188 noDuplicates[i] = intClasses[i].getName(); 189 } 190 for (int i = 0; i < noDuplicates.length; i++) { 191 for (int j = i + 1; j < noDuplicates.length; j++) { 192 if (noDuplicates[i].equals(noDuplicates[j])) { 193 addError("Duplicate class definition: " + noDuplicates[i]); 194 } 195 } 196 } 197 closeAllButMemoryFile(con); 198 } 199 200 ObjectContainer cycle(ObjectContainer con, RTestable clazz, int a_run) { 201 if (LOG_CLASS_NAMES) { 202 System.out.println("Testing class: " + clazz.getClass().getName()); 203 } 204 if (clazz.jdk2() && (! isJDK2())) { 205 return con; 206 } 207 Object obj = clazz.newInstance(); 208 con = reOpen(con); 209 210 Object get = clazz.newInstance(); 212 con.deactivate(get, Integer.MAX_VALUE); 213 ObjectSet set = con.get(get); 214 while (set.hasNext()) { 215 con.delete(set.next()); 216 } 217 218 clazz.set(obj, 1); 220 con.set(obj); 221 specificTest(clazz, con, ONE); 222 con = reOpen(con); 223 224 227 233 compare(con, get, clazz, 1, 1); 235 236 clazz.set(get, 1); 238 compare(con, get, clazz, 1, 1); 239 con = reOpen(con); 240 241 for (int i = 0; i < 4; i++) { 243 obj = clazz.newInstance(); 244 clazz.set(obj, 1); 245 con.set(obj); 246 } 247 con = reOpen(con); 248 249 compare(con, get, clazz, 1, 5); 251 specificTest(clazz, con, FIVE); 252 con = reOpen(con); 253 254 set = con.get(get); 256 obj = set.next(); 257 con.delete(obj); 258 con = reOpen(con); 259 260 compare(con, get, clazz, 1, 4); 262 specificTest(clazz, con, DELETED); 263 con = reOpen(con); 264 265 set = con.get(get); 267 obj = set.next(); 268 clazz.set(obj, 2); 269 con.set(obj); 270 con = reOpen(con); 271 272 273 282 compare(con, get, clazz, 1, 3); 284 specificTest(clazz, con, SAME); 285 con = reOpen(con); 286 287 clazz.set(get, 2); 289 compare(con, get, clazz, 2, 1); 290 specificTest(clazz, con, UPDATED); 291 con = reOpen(con); 292 293 if (clazz.ver3()) { 294 295 set = con.get(get); 297 obj = set.next(); 298 clazz.set(obj, 3); 299 con.set(obj); 300 con = reOpen(con); 301 302 clazz.set(get, 1); 304 compare(con, get, clazz, 1, 3); 305 specificTest(clazz, con, SAME); 306 con = reOpen(con); 307 308 clazz.set(get, 3); 310 compare(con, get, clazz, 3, 1); 311 specificTest(clazz, con, UPDATED); 312 con = reOpen(con); 313 } 314 315 return con; 316 } 317 318 public void completed() { 319 } 321 322 public void compare(ObjectContainer con, Object get, RTestable clazz, int ver, int count) { 323 ObjectSet set = con.get(get); 324 if (!PROFILE_ONLY) { 325 set.reset(); 326 if (set.size() == count) { 327 while (set.hasNext()) { 328 Object res = set.next(); 329 clazz.compare(con, res, ver); 330 if (DEACTIVATE) { 331 con.deactivate(res, 1); 332 con.activate(res, Integer.MAX_VALUE); 333 clazz.compare(con, res, ver); 334 } 335 } 336 } else { 337 Regression.addError( 338 clazz.getClass().getName() 339 + ":offcount:expected" 340 + count 341 + ":actual:" 342 + set.size()); 343 } 344 } 345 } 346 347 void specificTest(Object clazz, ObjectContainer con, int step) { 348 String methodName = "specific"; 349 Class [] parameterClasses = { ObjectContainer.class, Integer.TYPE }; 350 try { 351 Method method = clazz.getClass().getMethod(methodName, parameterClasses); 352 if (method != null) { 353 method.invoke(clazz, new Object [] { con, new Integer (step)}); 354 } 355 } catch (Exception e) { 356 } 357 } 358 359 public ObjectContainer open() { 360 return openContainer(); 361 } 362 363 public static void configure() { 364 Configuration config = Db4o.configure(); 365 config.activationDepth(12); 367 ObjectClass oc = config.objectClass("com.db4o.test.DeepUpdate"); 368 oc.updateDepth(2); 369 oc = config.objectClass("com.db4o.test.CustomConstructor"); 370 oc.translate(new TCustomConstructor()); 371 oc = config.objectClass("com.db4o.test.Debug"); 372 oc.updateDepth(5); 373 } 374 375 public ObjectContainer openContainer() { 376 if (openDelegate != null) { 377 return openDelegate.openContainer(); 378 } 379 configure(); 380 return Db4o.openFile(FILE); 381 } 382 383 public void close(ObjectContainer con) { 384 if (openDelegate != null) { 385 openDelegate.close(con); 386 } 387 if(closeFile()){ 388 con.close(); 389 } 390 } 391 392 protected int runs() { 393 return RUNS; 394 } 395 396 protected void closeAllButMemoryFile(ObjectContainer con){ 397 if (openDelegate != null) { 398 openDelegate.closeAllButMemoryFile(con); 399 }else{ 400 con.close(); 401 } 402 } 403 404 protected boolean closeFile() { 405 return closeFile; 406 } 407 408 public ObjectContainer reOpen(ObjectContainer con) { 409 if (closeFile()) { 410 close(con); 411 return open(); 412 } 413 return con; 414 } 415 416 Object newInstance(Class a_class) { 417 try { 418 return a_class.newInstance(); 419 } catch (Throwable t) { 420 try { 421 Constructor[] constructors = a_class.getDeclaredConstructors(); 422 for (int i = 0; i < constructors.length; i++) { 423 try { 424 if(Platform4.canSetAccessible()){ 425 Platform4.setAccessible(constructors[i]); 426 } 427 428 Class [] pTypes = constructors[i].getParameterTypes(); 429 Object [] parms = new Object [constructors[i].getParameterTypes().length]; 430 for (int j = 0; j < parms.length; j++) { 431 for (int k = 0; k < simpleNullWrappers.length; k++) { 432 if (pTypes[j] == simpleClasses[k]) { 433 parms[j] = simpleNullWrappers[k]; 434 break; 435 } 436 } 437 } 438 Object res = constructors[i].newInstance(parms); 439 if (res != null) { 440 return res; 441 } 442 } catch (Exception exc) { 443 System.out.println(exc.getClass().getName()); 444 System.out.println(exc.getMessage()); 445 } 446 } 447 } catch (Exception ex) { 448 } 449 System.out.println("NewInstance failed:" + a_class.getName()); 450 return null; 451 } 452 } 453 454 boolean isJDK2(){ 455 return Platform4.jdk().ver() >= 2; 456 } 457 458 static String [] expectedErrors = 464 { 465 "1e3==null:com.db4o.test.types.ArrayTypedPrivate:oByte:", 466 "1e0==null:com.db4o.test.types.ArrayTypedPrivate:nByte:", 467 "1e3==null:com.db4o.test.types.ArrayTypedPublic:oByte:", 468 "1e0==null:com.db4o.test.types.ArrayTypedPublic:nByte:", 469 "com.db4o.test.types.Empty:offcount:expected3:actual:4", 470 "com.db4o.test.types.Empty:offcount:expected1:actual:4", 471 "f1==null:com.db4o.test.types.MasterMonster:ooo:nByte:", 472 "1e3==null:com.db4o.test.types.MasterMonster:ooo:oByte:", 473 "1e0==null:com.db4o.test.types.MasterMonster:ooo:nByte:", 474 "com.db4o.test.types.RecursiveTypedPrivate:offcount:expected1:actual:11", 475 "com.db4o.test.types.RecursiveUnTypedPrivate:offcount:expected1:actual:11", 476 "com.db4o.test.types.RecursiveTypedPublic:offcount:expected1:actual:11", 477 "com.db4o.test.types.RecursiveUnTypedPublic:offcount:expected1:actual:11", 478 "f1==null:com.db4o.test.types.TypedPrivate:nByte:", 479 "f1==null:com.db4o.test.types.TypedPublic:nByte:" }; 480 481 static Object [] simpleNullWrappers = 482 { 483 new Integer (0), 484 new Long (0), 485 new Character ((char) 0), 486 new Double (0), 487 new Float (0), 488 new Boolean (false), 489 new Short ((short) 0), 490 new Byte ((byte) 0)}; 491 492 static Class [] simpleClasses = 493 { 494 Integer.TYPE, 495 Long.TYPE, 496 Character.TYPE, 497 Double.TYPE, 498 Float.TYPE, 499 Boolean.TYPE, 500 Short.TYPE, 501 Byte.TYPE }; 502 503 public static final int ONE = 1; 504 public static final int FIVE = 5; 505 public static final int DELETED = 4; 506 public static final int SAME = 3; 507 public static final int UPDATED = 0; 508 509 public synchronized static void addError(String err) { 510 for (int i = 0; i < expectedErrors.length; i++) { 511 if (err.equals(expectedErrors[i])) { 512 return; 513 } 514 } 515 errors = errors + err + System.getProperty("line.separator"); 516 } 517 518 public static final boolean DEACTIVATE = false; 519 public static String FILE = "regression.yap"; 520 protected Regression openDelegate; 521 private static String errors = ""; 522 private static int openedThreads = 0; 523 private static int returnedThreads = 0; 524 private static boolean threadsSharp = false; 525 526 private boolean closeFile; 527 528 public static RTestable[] allClasses() { 529 return new RTestable[] { 530 new ArrayInObjectPrivate(), 531 new ArrayInObjectPublic(), 532 new ArrayMixedInObjectPrivate(), 533 new ArrayMixedInObjectPublic(), 534 new ArrayMixedTypedPrivate(), 535 new ArrayMixedTypedPublic(), 536 new ArrayNDimensionalPrivate(), 537 new ArrayNDimensionalPublic(), 538 new ArrayTypedPrivate(), 539 new ArrayTypedPublic(), 540 new ArrayUntypedPrivate(), 541 new ArrayUntypedPublic(), 542 new BiParentTypedPrivate(), 543 new BiParentTypedPublic(), 544 new BiParentUnTypedPrivate(), 545 new BiParentUnTypedPublic(), 546 new DeepUpdate(), 547 new Empty(), 548 new InterfacePrivate(), 549 new InterfacePublic(), 550 new ObjectSimplePrivate(), 551 new ObjectSimplePublic(), 552 new ParameterConstructor(0), 553 PrivateConstructor.construct(), 554 new RecursiveTypedPrivate(), 555 new RecursiveTypedPublic(), 556 new RecursiveUnTypedPrivate(), 557 new RecursiveUnTypedPublic(), 558 new RHashtable(), 559 new RProperties(), 560 new RStack(), 561 new RVector(), 562 new SelfReference(), 563 new TypedPrivate(), 564 new TypedPublic(), 565 new UntypedPrivate(), 566 new UntypedPublic() 567 }; 569 } 570 public RTestable[] testClasses() { 571 if (!DEBUG) { 572 return allClasses(); 573 } 574 return new RTestable[] { 575 new UntypedDebug() 576 }; 580 } 581 582 } 583 | Popular Tags |