1 4 5 package org.objectweb.perseus.fos; 6 7 import junit.framework.Test; 8 import junit.framework.TestCase; 9 import junit.framework.TestSuite; 10 import junit.textui.TestRunner; 11 import org.objectweb.perseus.LogBoot; 12 import org.objectweb.perseus.fos.api.FosAccess; 13 import org.objectweb.perseus.fos.api.FosException; 14 import org.objectweb.perseus.fos.api.FosManager; 15 import org.objectweb.perseus.fos.api.FosStructure; 16 import org.objectweb.perseus.fos.api.FosTransaction; 17 import org.objectweb.perseus.fos.lib.FosTxContextFactory; 18 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.util.Properties ; 22 23 27 public class TestRecovery extends TestCase { 28 private static final int INITIALVALUE = 10; 29 private static final int NEWVALUE = 20; 30 private static final int NULLVALUE = 0; 31 private FosManager fm; 32 private static final String POBJDIR = "c1"; 33 private String [] pobjId = { 34 "obj1", "obj2", "obj3", "obj4", "obj5" 35 }; 36 private String dbDir; 37 private IntPObj[] pobjVal = { 38 new IntPObj(), 39 new IntPObj(), 40 new IntPObj(), 41 new IntPObj(), 42 new IntPObj() 43 }; 44 45 47 50 protected void setUp() { 51 try { 52 fm = new FosTxContextFactory(); 53 fm.setDbDir(dbDir); 54 fm.setMonologFactory(LogBoot.getLoggerFactory()); 55 fm.initialize(); 56 } catch (FosException fe) { 57 System.out.println("Error during setUp - 1"); 58 if (fe.getNestedException() != null) 59 fe.getNestedException().printStackTrace(); 60 fe.printStackTrace(); 61 System.exit(0); 62 } catch (Exception e) { 63 System.out.println("Error during setUp - 2"); 64 e.printStackTrace(); 65 System.exit(0); 66 } 67 } 68 69 72 protected void tearDown() { 73 } 74 75 77 80 public void testRecoverInitialized() { 81 try { 82 fm = new FosTxContextFactory(); 83 fm.setDbDir(dbDir); 84 85 fm.setMonologFactory(LogBoot.getLoggerFactory()); 86 fm.start(); 87 fm = new FosTxContextFactory(); 88 fm.setDbDir(dbDir); 89 90 fm.setMonologFactory(LogBoot.getLoggerFactory()); 91 fm.start(); 92 fm.stop(); 93 } catch (FosException fe) { 94 fe.printStackTrace(); 95 fail("FOS error."); 96 } catch (Exception e) { 97 e.printStackTrace(); 98 fail("Log configuration error."); 99 } 100 } 101 102 105 public void testRecoverCommitCreate() { 106 try { 107 fm = new FosTxContextFactory(); 108 fm.setDbDir(dbDir); 109 110 fm.setMonologFactory(LogBoot.getLoggerFactory()); 111 fm.start(); 112 FosTransaction ft = fm.createTxContext(); 113 ft.begin(); 114 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 115 ft.commit(); 116 fm = new FosTxContextFactory(); 117 fm.setDbDir(dbDir); 118 119 fm.setMonologFactory(LogBoot.getLoggerFactory()); 120 fm.start(); 121 ft.begin(); 122 boolean ex = ft.exist(POBJDIR, pobjId[0]); 123 ft.commit(); 124 fm.stop(); 125 if (!ex) 126 fail(); 127 } catch (FosException fe) { 128 fe.printStackTrace(); 129 fail("FOS error."); 130 } catch (Exception e) { 131 e.printStackTrace(); 132 fail("Log configuration error."); 133 } 134 } 135 136 139 public void testRecoverPrepareCreate() { 140 try { 141 fm = new FosTxContextFactory(); 142 fm.setDbDir(dbDir); 143 144 fm.setMonologFactory(LogBoot.getLoggerFactory()); 145 fm.start(); 146 FosTransaction ft = fm.createTxContext(); 147 ft.begin(); 148 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 149 ft.prepare(); 150 fm = new FosTxContextFactory(); 151 fm.setDbDir(dbDir); 152 153 fm.setMonologFactory(LogBoot.getLoggerFactory()); 154 fm.start(); 155 ft.begin(); 156 boolean ex = ft.exist(POBJDIR, pobjId[0]); 157 ft.commit(); 158 fm.stop(); 159 if (!ex) 160 fail(); 161 } catch (FosException fe) { 162 fe.printStackTrace(); 163 fail("FOS error."); 164 } catch (Exception e) { 165 e.printStackTrace(); 166 fail("Log configuration error."); 167 } 168 } 169 170 173 public void testRecoverRollbackCreate() { 174 try { 175 fm = new FosTxContextFactory(); 176 fm.setDbDir(dbDir); 177 178 fm.setMonologFactory(LogBoot.getLoggerFactory()); 179 fm.start(); 180 FosTransaction ft = fm.createTxContext(); 181 ft.begin(); 182 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 183 ft.rollback(); 184 fm = new FosTxContextFactory(); 185 fm.setDbDir(dbDir); 186 187 fm.setMonologFactory(LogBoot.getLoggerFactory()); 188 fm.start(); 189 ft.begin(); 190 boolean ex = ft.exist(POBJDIR, pobjId[0]); 191 ft.commit(); 192 fm.stop(); 193 if (ex) 194 fail(); 195 } catch (FosException fe) { 196 fe.printStackTrace(); 197 fail("FOS error."); 198 } catch (Exception e) { 199 e.printStackTrace(); 200 fail("Log configuration error."); 201 } 202 } 203 204 207 public void testRecoverCreate() { 208 try { 209 fm = new FosTxContextFactory(); 210 fm.setDbDir(dbDir); 211 212 fm.setMonologFactory(LogBoot.getLoggerFactory()); 213 fm.start(); 214 FosTransaction ft = fm.createTxContext(); 215 ft.begin(); 216 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 217 fm = new FosTxContextFactory(); 218 fm.setDbDir(dbDir); 219 220 fm.setMonologFactory(LogBoot.getLoggerFactory()); 221 fm.start(); 222 ft.begin(); 223 boolean ex = ft.exist(POBJDIR, pobjId[0]); 224 ft.commit(); 225 fm.stop(); 226 if (ex) 227 fail(); 228 } catch (FosException fe) { 229 fe.printStackTrace(); 230 fail("FOS error."); 231 } catch (Exception e) { 232 e.printStackTrace(); 233 fail("Log configuration error."); 234 } 235 } 236 237 240 public void testRecoverCommitDelete() { 241 try { 242 fm = new FosTxContextFactory(); 243 fm.setDbDir(dbDir); 244 245 fm.setMonologFactory(LogBoot.getLoggerFactory()); 246 fm.start(); 247 FosTransaction ft = fm.createTxContext(); 248 ft.begin(); 249 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 250 ft.commit(); 251 ft.begin(); 252 ft.delete(POBJDIR, pobjId[0]); 253 ft.commit(); 254 fm = new FosTxContextFactory(); 255 fm.setDbDir(dbDir); 256 257 fm.setMonologFactory(LogBoot.getLoggerFactory()); 258 fm.start(); 259 ft.begin(); 260 boolean ex = ft.exist(POBJDIR, pobjId[0]); 261 ft.commit(); 262 fm.stop(); 263 if (ex) 264 fail(); 265 } catch (FosException fe) { 266 fe.printStackTrace(); 267 fail("FOS error."); 268 } catch (Exception e) { 269 e.printStackTrace(); 270 fail("Log configuration error."); 271 } 272 } 273 274 277 public void testRecoverPrepareDelete() { 278 try { 279 fm = new FosTxContextFactory(); 280 fm.setDbDir(dbDir); 281 282 fm.setMonologFactory(LogBoot.getLoggerFactory()); 283 fm.start(); 284 FosTransaction ft = fm.createTxContext(); 285 ft.begin(); 286 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 287 ft.commit(); 288 ft.begin(); 289 ft.delete(POBJDIR, pobjId[0]); 290 ft.prepare(); 291 fm = new FosTxContextFactory(); 292 fm.setDbDir(dbDir); 293 294 fm.setMonologFactory(LogBoot.getLoggerFactory()); 295 fm.start(); 296 ft.begin(); 297 boolean ex = ft.exist(POBJDIR, pobjId[0]); 298 ft.commit(); 299 fm.stop(); 300 if (ex) 301 fail(); 302 } catch (FosException fe) { 303 fe.printStackTrace(); 304 fail("FOS error."); 305 } catch (Exception e) { 306 e.printStackTrace(); 307 fail("Log configuration error."); 308 } 309 } 310 311 314 public void testRecoverRollbackDelete() { 315 try { 316 fm = new FosTxContextFactory(); 317 fm.setDbDir(dbDir); 318 319 fm.setMonologFactory(LogBoot.getLoggerFactory()); 320 fm.start(); 321 FosTransaction ft = fm.createTxContext(); 322 ft.begin(); 323 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 324 ft.commit(); 325 ft.begin(); 326 ft.delete(POBJDIR, pobjId[0]); 327 ft.rollback(); 328 fm = new FosTxContextFactory(); 329 fm.setDbDir(dbDir); 330 331 fm.setMonologFactory(LogBoot.getLoggerFactory()); 332 fm.start(); 333 ft.begin(); 334 boolean ex = ft.exist(POBJDIR, pobjId[0]); 335 ft.commit(); 336 fm.stop(); 337 if (!ex) 338 fail(); 339 } catch (FosException fe) { 340 fe.printStackTrace(); 341 fail("FOS error."); 342 } catch (Exception e) { 343 e.printStackTrace(); 344 fail("Log configuration error."); 345 } 346 } 347 348 351 public void testRecoverDelete() { 352 try { 353 fm = new FosTxContextFactory(); 354 fm.setDbDir(dbDir); 355 356 fm.setMonologFactory(LogBoot.getLoggerFactory()); 357 fm.start(); 358 FosTransaction ft = fm.createTxContext(); 359 ft.begin(); 360 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 361 ft.commit(); 362 ft.begin(); 363 ft.delete(POBJDIR, pobjId[0]); 364 fm = new FosTxContextFactory(); 365 fm.setDbDir(dbDir); 366 367 fm.setMonologFactory(LogBoot.getLoggerFactory()); 368 fm.start(); 369 ft.begin(); 370 boolean ex = ft.exist(POBJDIR, pobjId[0]); 371 ft.commit(); 372 fm.stop(); 373 if (!ex) 374 fail(); 375 } catch (FosException fe) { 376 fe.printStackTrace(); 377 fail("FOS error."); 378 } catch (Exception e) { 379 e.printStackTrace(); 380 fail("Log configuration error."); 381 } 382 } 383 384 387 public void testRecoverCommitModify() { 388 try { 389 fm = new FosTxContextFactory(); 390 fm.setDbDir(dbDir); 391 392 fm.setMonologFactory(LogBoot.getLoggerFactory()); 393 fm.start(); 394 FosTransaction ft = fm.createTxContext(); 395 ft.begin(); 396 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 397 ft.commit(); 398 ft.begin(); 399 pobjVal[0].setValue(NEWVALUE); 400 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 401 ft.commit(); 402 fm = new FosTxContextFactory(); 403 fm.setDbDir(dbDir); 404 405 fm.setMonologFactory(LogBoot.getLoggerFactory()); 406 fm.start(); 407 pobjVal[0].setValue(NULLVALUE); 408 ft.begin(); 409 ft.read(POBJDIR, pobjId[0], pobjVal[0], null); 410 ft.commit(); 411 fm.stop(); 412 if (pobjVal[0].getValue() != NEWVALUE) 413 fail(); 414 } catch (FosException fe) { 415 fe.printStackTrace(); 416 fail("FOS error."); 417 } catch (Exception e) { 418 e.printStackTrace(); 419 fail("Log configuration error."); 420 } 421 } 422 423 426 public void testRecoverPrepareModify() { 427 try { 428 fm = new FosTxContextFactory(); 429 fm.setDbDir(dbDir); 430 431 fm.setMonologFactory(LogBoot.getLoggerFactory()); 432 fm.start(); 433 FosTransaction ft = fm.createTxContext(); 434 ft.begin(); 435 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 436 ft.commit(); 437 ft.begin(); 438 pobjVal[0].setValue(NEWVALUE); 439 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 440 ft.prepare(); 441 fm = new FosTxContextFactory(); 442 fm.setDbDir(dbDir); 443 444 fm.setMonologFactory(LogBoot.getLoggerFactory()); 445 fm.start(); 446 pobjVal[0].setValue(NULLVALUE); 447 ft.begin(); 448 ft.read(POBJDIR, pobjId[0], pobjVal[0], null); 449 ft.commit(); 450 fm.stop(); 451 if (pobjVal[0].getValue() != NEWVALUE) 452 fail(); 453 } catch (FosException fe) { 454 fe.printStackTrace(); 455 fail("FOS error."); 456 } catch (Exception e) { 457 e.printStackTrace(); 458 fail("Log configuration error."); 459 } 460 } 461 462 465 public void testRecoverRollbackModify() { 466 try { 467 fm = new FosTxContextFactory(); 468 fm.setDbDir(dbDir); 469 470 fm.setMonologFactory(LogBoot.getLoggerFactory()); 471 fm.start(); 472 FosTransaction ft = fm.createTxContext(); 473 ft.begin(); 474 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 475 ft.commit(); 476 ft.begin(); 477 pobjVal[0].setValue(NEWVALUE); 478 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 479 ft.rollback(); 480 fm = new FosTxContextFactory(); 481 fm.setDbDir(dbDir); 482 483 fm.setMonologFactory(LogBoot.getLoggerFactory()); 484 fm.start(); 485 pobjVal[0].setValue(NULLVALUE); 486 ft.begin(); 487 ft.read(POBJDIR, pobjId[0], pobjVal[0], null); 488 ft.commit(); 489 fm.stop(); 490 if (pobjVal[0].getValue() != INITIALVALUE) 491 fail(); 492 } catch (FosException fe) { 493 fe.printStackTrace(); 494 fail("FOS error."); 495 } catch (Exception e) { 496 e.printStackTrace(); 497 fail("Log configuration error."); 498 } 499 } 500 501 504 public void testRecoverModify() { 505 try { 506 fm = new FosTxContextFactory(); 507 fm.setDbDir(dbDir); 508 509 fm.setMonologFactory(LogBoot.getLoggerFactory()); 510 fm.start(); 511 FosTransaction ft = fm.createTxContext(); 512 ft.begin(); 513 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 514 ft.commit(); 515 ft.begin(); 516 pobjVal[0].setValue(NEWVALUE); 517 ft.write(POBJDIR, pobjId[0], pobjVal[0], null); 518 fm = new FosTxContextFactory(); 519 fm.setDbDir(dbDir); 520 521 fm.setMonologFactory(LogBoot.getLoggerFactory()); 522 fm.start(); 523 pobjVal[0].setValue(NULLVALUE); 524 ft.begin(); 525 ft.read(POBJDIR, pobjId[0], pobjVal[0], null); 526 ft.commit(); 527 fm.stop(); 528 if (pobjVal[0].getValue() != INITIALVALUE) 529 fail(); 530 } catch (FosException fe) { 531 fe.printStackTrace(); 532 fail("FOS error."); 533 } catch (Exception e) { 534 e.printStackTrace(); 535 fail("Log configuration error."); 536 } 537 } 538 539 541 545 public static Test suite() { 546 return new TestSuite(TestRecovery.class); 547 } 548 549 552 public static void main(String args[]) { 553 TestRunner.run(suite()); 554 } 555 556 559 public TestRecovery(String tn) throws Exception { 560 super(tn); 561 Properties prop = new Properties (); 562 prop.load(ClassLoader.getSystemResourceAsStream("owfos.properties")); 563 dbDir = prop.getProperty("dbdir", null); 564 if (dbDir == null) { 565 throw new Exception ( 566 "Cannot find the 'dbdir' property"); 567 } 568 } 569 570 573 class IntPObj implements FosStructure { 574 577 private int value = INITIALVALUE; 578 579 586 public void readFile(ObjectInputStream ois, FosAccess conn, Object ctxt) throws Exception { 587 value = ois.readInt(); 588 } 589 590 597 public void writeFile(ObjectOutputStream oos, FosAccess conn, Object ctxt) throws Exception { 598 oos.writeInt(value); 599 } 600 601 604 int getValue() { 605 return value; 606 } 607 608 611 void setValue(int val) { 612 value = val; 613 } 614 } 615 } | Popular Tags |