1 6 7 package com.hp.hpl.jena.db.test; 8 9 22 23 import com.hp.hpl.jena.db.*; 24 import com.hp.hpl.jena.db.impl.IRDBDriver; 25 26 27 import junit.framework.*; 28 29 import com.hp.hpl.jena.rdf.model.*; 30 import com.hp.hpl.jena.shared.*; 31 import com.hp.hpl.jena.vocabulary.DB; 32 33 public class TestConnection extends TestCase { 34 35 String DefModel = GraphRDB.DEFAULT; 36 37 public TestConnection( String name ) 38 { super( name ); } 39 40 public static TestSuite suite() 41 { return new TestSuite( TestConnection.class ); } 42 43 protected void setUp() throws java.lang.Exception { 44 } 45 46 protected void tearDown() throws java.lang.Exception { 47 } 48 49 private static void loadClass() 50 { 51 try { Class.forName(TestPackage.M_DBDRIVER_CLASS); } 52 catch (Exception e) { throw new JenaException( e ); } 53 } 54 55 public static IDBConnection makeTestConnection() 56 { 57 loadClass(); 58 return new DBConnection 59 ( 60 TestPackage.M_DB_URL, 61 TestPackage.M_DB_USER, 62 TestPackage.M_DB_PASSWD, 63 TestPackage.M_DB 64 ); 65 } 66 67 public static IDBConnection makeAndCleanTestConnection() 68 { 69 IDBConnection result = makeTestConnection(); 70 boolean tryClean = true; 71 boolean didClean = false; 72 boolean tryUnlock = true; 73 String err = null; 74 while ( tryClean && !didClean ) { 75 try { 76 result.cleanDB(); 77 didClean = true; 78 } catch (Exception e) { 79 err = err + "\n" + e; 80 if ( tryUnlock ) { 81 tryUnlock = false; 82 if ( result.getDriver().DBisLocked() ) 83 try { 84 result.getDriver().unlockDB(); 85 } catch ( Exception e1 ) { 86 err = err + "\n" + e1; 87 } 88 } else 89 tryClean = false; 90 } 91 } 92 if ( didClean == false ) 93 throw new JenaException("Failed to clean database.\n" + err); 94 return result; 95 } 96 97 109 110 public void testRecovery() throws java.lang.Exception { 111 IDBConnection conn = makeAndCleanTestConnection(); 112 ModelRDB m = null; 113 try { 114 m = ModelRDB.createModel(conn, "myName"); 115 m.close(); 116 } catch ( Exception e ) { 117 assertTrue(false); 118 } 119 try { 120 m = ModelRDB.createModel(conn, "myName"); 121 assertTrue(false); 122 } catch ( Exception e ) { 123 } 124 conn.close(); 125 } 126 127 128 public void testDBConnect() throws java.lang.Exception { 129 IDBConnection conn = makeTestConnection(); 130 conn.close(); 131 } 132 133 public void testBadConnection() throws java.lang.Exception { 134 140 try { 141 IDBConnection conn = new DBConnection( 142 TestPackage.M_DB_URL, 143 TestPackage.M_DB_USER, 144 TestPackage.M_DB_PASSWD, 145 "Bad DB"); 146 conn.cleanDB(); 147 assertTrue(false); } catch (Exception e) { 149 } 150 } 151 152 153 public void testConstructDefaultModel() throws java.lang.Exception { 154 IDBConnection conn = makeAndCleanTestConnection(); 155 ModelRDB m = ModelRDB.createModel(conn); 156 m.remove(); 157 conn.close(); 158 } 159 160 public void testConstructAndOpenDefaultModel() throws java.lang.Exception { 161 IDBConnection conn = makeAndCleanTestConnection(); 162 ModelRDB m = ModelRDB.createModel(conn); 163 m.close(); 164 ModelRDB m2 = ModelRDB.open(conn); 165 m2.remove(); 166 conn.close(); 167 } 168 169 public void testConstructNamedModel() throws java.lang.Exception { 170 IDBConnection conn = makeAndCleanTestConnection(); 171 ModelRDB m = ModelRDB.createModel(conn, "myName"); 172 m.remove(); 173 conn.close(); 174 } 175 176 public void testBadNamedModel() throws java.lang.Exception { 177 IDBConnection conn = makeAndCleanTestConnection(); 178 ModelRDB m = null; 179 try { 180 m = ModelRDB.createModel(conn, DefModel); 181 assertTrue(false); 182 } catch (Exception e) { 183 } 184 conn.close(); 185 } 186 187 public void testBadNamedFactoryModel() throws java.lang.Exception { 188 IDBConnection conn = makeAndCleanTestConnection(); 189 ModelMaker maker = ModelFactory.createModelRDBMaker(conn); 190 Model m = null; 191 try { 192 m = maker.createModel(DefModel); 193 assertTrue(false); 194 } catch (Exception e) { 195 } 196 conn.close(); 197 } 198 199 public void testReconstructDefaultModel() throws java.lang.Exception { 200 IDBConnection conn = makeAndCleanTestConnection(); 201 ModelRDB m = ModelRDB.createModel(conn); 202 m.remove(); 203 ModelRDB m1 = ModelRDB.createModel(conn); 204 m1.remove(); 205 conn.close(); 206 } 207 208 public void testReconstructNamedModel() throws java.lang.Exception { 209 IDBConnection conn = makeAndCleanTestConnection(); 210 ModelRDB m = ModelRDB.createModel(conn, "myName"); 211 m.remove(); 212 ModelRDB m1 = ModelRDB.createModel(conn, "myName"); 213 m1.remove(); 214 conn.close(); 215 } 216 217 218 public void testConstructAndOpenNamedModel() throws java.lang.Exception { 219 IDBConnection conn = makeAndCleanTestConnection(); 220 ModelRDB m = ModelRDB.createModel(conn, "myName"); 221 m.close(); 222 ModelRDB m2 = ModelRDB.open(conn, "myName"); 223 m2.remove(); 224 conn.close(); 225 } 226 227 public void testConstructParamaterizedModel() throws java.lang.Exception { 228 IDBConnection conn = makeAndCleanTestConnection(); 229 ModelRDB m = ModelRDB.createModel(conn, ModelRDB.getDefaultModelProperties(conn)); 230 m.remove(); 231 conn.close(); 232 } 233 234 public void testConstructAndOpenParamaterizedModel() throws java.lang.Exception { 235 IDBConnection conn = makeAndCleanTestConnection(); 236 ModelRDB m = ModelRDB.createModel(conn, ModelRDB.getDefaultModelProperties(conn)); 237 m.close(); 238 ModelRDB m2 = ModelRDB.open(conn); 239 m2.remove(); 240 conn.close(); 241 } 242 243 public void testConstructNamedParamaterizedModel() throws java.lang.Exception { 244 IDBConnection conn = makeAndCleanTestConnection(); 245 ModelRDB m = ModelRDB.createModel(conn, "myName", ModelRDB.getDefaultModelProperties(conn)); 246 m.remove(); 247 conn.close(); 248 } 249 250 public void testConstructAndOpenNamedParamaterizedModel() throws java.lang.Exception { 251 IDBConnection conn = makeAndCleanTestConnection(); 252 ModelRDB m = ModelRDB.createModel(conn, "myName", ModelRDB.getDefaultModelProperties(conn)); 253 m.close(); 254 ModelRDB m2 = ModelRDB.open(conn, "myName"); 255 m2.remove(); 256 conn.close(); 257 } 258 259 public void testOpenNamedNonExistentModel() throws java.lang.Exception { 260 IDBConnection conn = makeTestConnection(); 261 try { 262 ModelRDB m2 = ModelRDB.open(conn, "myName"); 263 m2.remove(); 264 conn.close(); 265 assertTrue("Successfully opened non-existent model", false); 266 } catch ( RDFRDBException e ) { 267 conn.close(); 268 } 269 } 270 271 public void testOpenUnnamedNonExistentModel() throws java.lang.Exception { 272 IDBConnection conn = makeTestConnection(); 273 try { 274 conn.cleanDB(); 275 ModelRDB m2 = ModelRDB.open(conn); 276 m2.remove(); 277 conn.close(); 278 assertTrue("Successfully opened unnamed non-existent model", false); 279 } catch ( RDFRDBException e ) { 280 conn.close(); 281 } 282 } 283 284 public void testCreateExistingModel() throws java.lang.Exception { 285 IDBConnection conn = makeAndCleanTestConnection(); 286 ModelRDB m = ModelRDB.createModel(conn, "myName", ModelRDB.getDefaultModelProperties(conn)); 287 try { 288 ModelRDB m2 = ModelRDB.createModel(conn, "myName", ModelRDB.getDefaultModelProperties(conn)); 289 m.remove(); m2.remove(); 290 conn.close(); 291 assertTrue("Successfully created pre-existing model", false); 292 } catch ( RDFRDBException e ) { 293 m.remove(); 294 conn.close(); 295 } 296 } 297 298 public void addToDBGraphProp ( Model model, Property prop, String val ) { 299 StmtIterator iter = model.listStatements( 301 new SimpleSelector(null, DB.graphName, (RDFNode) null)); 302 assertTrue(iter.hasNext()); 303 304 Statement stmt = iter.nextStatement(); 305 assertTrue(iter.hasNext() == false); 306 Resource graphURI = stmt.getSubject(); 307 Literal l = model.createLiteral(val); 308 Statement s = model.createStatement(graphURI,prop,l); 309 model.add(s); 310 assertTrue(model.contains(s)); 311 } 312 313 public void testConstructDefSchemaModel() throws java.lang.Exception { 314 IDBConnection conn = makeAndCleanTestConnection(); 315 conn.getDriver().setStoreWithModel(""); 316 ModelRDB m = ModelRDB.createModel(conn); 320 m.remove(); 321 conn.close(); 322 } 323 324 public void testConstructBadSchemaModel() throws java.lang.Exception { 325 IDBConnection conn = makeAndCleanTestConnection(); 326 conn.getDriver().setStoreWithModel(DefModel); 329 try { 330 ModelRDB m = ModelRDB.createModel(conn); 332 m.remove(); 333 assertFalse("Created model with non-existent schema",true); 334 } catch (RDFRDBException e) { 335 } 336 conn.getDriver().setStoreWithModel("MODEL_DOES_NOT_EXIST"); 337 try { 338 ModelRDB m = ModelRDB.createModel(conn); 340 m.remove(); 341 assertFalse("Created model with non-existent schema",true); 342 } catch (RDFRDBException e) { 343 } 344 conn.close(); 345 } 346 347 public void testConstructNamedModelDefSchema() throws java.lang.Exception { 348 IDBConnection conn = makeAndCleanTestConnection(); 350 conn.getDriver().setStoreWithModel(null); 353 ModelRDB m = ModelRDB.createModel(conn, "myName"); 355 m.remove(); 356 conn.close(); 357 } 358 359 public void testConstructNamedModelDefSchema1() throws java.lang.Exception { 360 IDBConnection conn = makeAndCleanTestConnection(); 364 ModelRDB mdef = ModelRDB.createModel(conn); 369 conn.getDriver().setStoreWithModel(DefModel); 370 ModelRDB m = ModelRDB.createModel(conn, "myName"); 371 mdef.remove(); m.remove(); 372 conn.close(); 373 } 374 375 public void testConstructNamedModelDefSchema2() throws java.lang.Exception { 376 IDBConnection conn = makeAndCleanTestConnection(); 379 ModelRDB mdef = ModelRDB.createModel(conn); 384 conn.getDriver().setStoreWithModel(null); 385 ModelRDB m = ModelRDB.createModel(conn, "myName"); 386 mdef.remove(); m.remove(); 387 conn.close(); 388 } 389 390 public void testConstructNamedModelSchema() throws java.lang.Exception { 391 IDBConnection conn = makeAndCleanTestConnection(); 393 ModelRDB m1 = ModelRDB.createModel(conn, "model1"); 395 conn.getDriver().setStoreWithModel("model1"); 399 ModelRDB m2 = ModelRDB.createModel(conn, "model2"); 400 m1.remove(); m2.remove(); 401 conn.close(); 402 } 403 404 public void testNamedPrefixedModel() throws java.lang.Exception { 405 IDBConnection conn = makeAndCleanTestConnection(); 406 IRDBDriver d = conn.getDriver(); 407 d.setTableNamePrefix("foo_"); 408 conn.cleanDB(); ModelRDB m = ModelRDB.createModel(conn, "myName"); 410 m.remove(); 411 conn.cleanDB(); 412 conn.close(); 413 } 414 415 public void testNamedPrefixedPersists() throws java.lang.Exception { 416 IDBConnection conn = makeTestConnection(); 417 IRDBDriver d = conn.getDriver(); 418 String pfx = "foo_"; 419 d.setTableNamePrefix(pfx); 420 conn.cleanDB(); ModelRDB m = ModelRDB.createModel(conn, "myName"); 422 m.close(); 423 conn.close(); 424 conn = makeTestConnection(); 425 d = conn.getDriver(); 426 d.setTableNamePrefix(pfx); 427 m = ModelRDB.open(conn, "myName"); 428 assertTrue(d.getTableNamePrefix().equalsIgnoreCase(pfx)); 429 conn.cleanDB(); 430 } 431 432 public void testNamedPrefixFailure() throws java.lang.Exception { 433 IDBConnection conn = makeAndCleanTestConnection(); 434 IRDBDriver d = conn.getDriver(); 435 String longPfx = 436 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 437 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 438 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 439 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 440 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 441 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 442 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 443 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 444 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 445 try { 446 d.setTableNamePrefix(longPfx); 447 assertTrue(false); } catch (Exception e) { 449 } 450 ModelRDB m = ModelRDB.createModel(conn); 451 try { 452 d.setTableNamePrefix("foo_"); 453 assertTrue(false); } catch (Exception e) { 455 } 456 m.close(); 457 conn.close(); 458 } 459 460 public void testDBMutex() { 461 IDBConnection conn = makeAndCleanTestConnection(); 462 IRDBDriver d = conn.getDriver(); 463 464 d.lockDB(); 465 try { 466 ModelRDB foo = ModelRDB.createModel(conn,"foo"); 467 assertTrue(false); } catch ( Exception e) { 469 } 470 471 d.unlockDB(); 472 473 if ( d.isDBFormatOK() ) 474 assertTrue(false); 476 if ( conn.containsModel("foo") ) 477 assertTrue(false); 478 479 if ( d.isDBFormatOK() ) 481 assertTrue(false); 483 ModelRDB foo = ModelRDB.createModel(conn,"foo"); 484 485 if ( d.isDBFormatOK() == false ) 486 assertTrue(false); 488 if ( conn.containsModel("foo") == false ) 489 assertTrue(false); 490 491 if ( conn.containsModel("bar") ) 492 assertTrue(false); 493 494 d.deleteTable(d.getSystemTableName(0)); 496 497 if ( d.isDBFormatOK() ) 498 assertTrue(false); 500 try { 501 conn.close(); 502 } catch ( Exception e) { 503 assertTrue(false); 504 } 505 506 conn = makeTestConnection(); 507 d = conn.getDriver(); 508 509 if ( conn.containsModel("foo") ) 510 assertTrue(false); 511 512 if ( d.isDBFormatOK() ) 513 assertTrue(false); 515 ModelRDB bar = ModelRDB.createModel(conn,"bar"); 517 518 if ( d.isDBFormatOK() == false ) 519 assertTrue(false); 521 if ( conn.containsModel("foo") ) 522 assertTrue(false); 523 524 if ( conn.containsModel("bar") == false ) 525 assertTrue(false); 526 527 bar.begin(); 528 529 try { 530 bar.remove(); assertTrue(false); 532 } catch ( Exception e) { 533 } 534 535 bar.abort(); 536 537 bar.remove(); 538 539 try { 540 conn.close(); 541 } catch ( Exception e) { 542 assertTrue(false); 543 } 544 } 545 546 public class syncOnCount { 548 private int count; 549 550 public syncOnCount () { count = 0; } 551 552 public synchronized boolean testCount ( int i ) { 553 return count >= i; 554 } 555 556 public synchronized void incCount() { 557 count++; 558 } 559 560 public void waitOnCount ( int cnt ) { 561 int i = 0; 562 for ( i=0; i<100; i++ ) 563 try { 564 if ( !testCount(cnt) ) { 565 Thread.yield(); 566 Thread.sleep(1000); 567 } 568 } catch ( Exception e ) { 569 throw new RuntimeException ("waitOnCount interrupted" + e); 570 } 571 assertTrue(testCount(cnt)); 572 } 573 574 } 575 576 syncOnCount s; 577 578 public void testConcurrentThread() { 579 class thread1 extends Thread { 580 syncOnCount s; 581 582 public thread1(syncOnCount sc) { 583 super("thread1"); 584 s = sc; 585 } 586 587 public void run() { 588 IDBConnection conn = makeAndCleanTestConnection(); 589 ModelRDB foo = ModelRDB.createModel(conn, "foo"); 590 s.incCount(); s.waitOnCount(2); 592 Resource u = foo.createResource("test#subject"); 593 Property p = foo.createProperty("test#predicate"); 594 Resource o = foo.createResource("test#object"); 595 Statement stmt = foo.createStatement(u, p, o); 596 assertFalse(foo.contains(stmt)); 597 s.incCount(); 598 s.waitOnCount(4); 599 assertTrue(foo.contains(stmt)); 600 foo.remove(stmt); 601 s.incCount(); 602 try { 603 conn.close(); 604 } catch (Exception e) { 605 assertTrue(false); 606 } 607 } 608 } 609 610 class thread2 extends Thread { 611 syncOnCount s; 612 613 public thread2(syncOnCount sc) { 614 super("thread2"); 615 s = sc; 616 } 617 618 public void run() { 619 s.waitOnCount(1); 620 IDBConnection conn = makeTestConnection(); 621 ModelRDB foo = ModelRDB.open(conn, "foo"); 622 foo.begin(); 623 Resource u = foo.createResource("test#subject"); 624 Property p = foo.createProperty("test#predicate"); 625 Resource o = foo.createResource("test#object"); 626 Statement stmt = foo.createStatement(u, p, o); 627 foo.add(stmt); 628 s.incCount(); 629 s.waitOnCount(3); 630 assertTrue(foo.contains(stmt)); 631 try { 632 foo.commit(); 633 } catch (Exception e) { 634 assertTrue(false); 635 } 636 s.incCount(); s.waitOnCount(5); assertFalse(foo.contains(stmt)); 639 try { 640 conn.close(); 641 } catch (Exception e) { 642 assertTrue(false); 643 } 644 } 645 } 646 647 syncOnCount s = new syncOnCount(); 648 Thread t1 = new thread1(s); 649 Thread t2 = new thread2(s); 650 t2.start(); 651 t1.start(); 652 try { 653 t1.join(); 654 t2.join(); 655 } catch (Exception e) { 656 assertTrue(false); 657 } 658 } 659 660 } 661 662 663 692 | Popular Tags |