|
TestPersist |
|
1 /* 2 * TestPersist.java 3 * 4 * Copyright (c) 1998-2001, The University of Sheffield. 5 * 6 * This file is part of GATE (see http://gate.ac.uk/), and is free 7 * software, licenced under the GNU Library General Public License, 8 * Version 2, June 1991 (in the distribution as file licence.html, 9 * and also available at http://gate.ac.uk/gate/licence.html). 10 * 11 * Hamish Cunningham, 19/Jan/01 12 * 13 * $Id: TestPersist.java,v 1.89 2003/01/27 11:06:32 valyt Exp $ 14 */ 15 16 package gate.persist; 17 18 import java.util.*; 19 import java.io.*; 20 import java.net.*; 21 import java.beans.*; 22 import java.lang.reflect.*; 23 import junit.framework.*; 24 import junit.framework.Test; 25 import java.sql.*; 26 27 import oracle.sql.*; 28 //import oracle.jdbc.driver.*; 29 30 import gate.*; 31 import gate.event.DatastoreListener; 32 import gate.util.*; 33 import gate.corpora.*; 34 import gate.security.*; 35 36 /** Persistence test class 37 */ 38 public class TestPersist extends TestCase 39 { 40 private static String JDBC_URL_1; 41 private static String JDBC_URL_2; 42 private static String JDBC_URL; 43 44 /** Debug flag */ 45 private static final boolean DEBUG = false; 46 private static Long sampleDoc_lrID = null; 47 private static Long sampleCorpus_lrID = null; 48 private static Document sampleDoc = null; 49 private static Corpus sampleCorpus = null; 50 private static int dbType; 51 52 // private static final String UNICODE_STRING = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217"; 53 private static final String UNICODE_STRING = "\u0915\u0932\u094d\u0907\u0928\u0643\u0637\u0628\u041a\u0430\u043b\u0438\u043d\u0430 Kalina"; 54 private static final String ASCII_STRING = "Never mistake motion for action (Ernest Hemingway)"; 55 56 private final String VERY_LONG_STRING = 57 "The memory of Father came back to her. Ever since she had seen him retreat from those "+ 58 "twelve-year-old boys she often imagined him in this situation: he is on a sinking ship; "+ 59 "there are only a few lifeboats and there isn't enough room for everyone; there is a "+ 60 "furious stampede on the deck. At first Father rushes along with the others, but when he "+ 61 "sees how they push and shove, ready to trample each other under foot, and a wild-eyed "+ 62 "woman strikes him with her fist because he is in her way, he suddenly stops and steps "+ 63 "aside. And in the end he merely watches the overloaded lifeboats as they are slowly "+ 64 "lowered amid shouts and curses, towards the raging waves. "+ 65 "What name to give to that attitude? Cowardice? No. Cowards are afraid of dying and will "+ 66 "fight to survive. Nobility? Undoubtedly, if he had acted out of regard for his fellows. "+ 67 "But Agnes did not believe this was the motive. What was it then? She couldn't say. Only "+ 68 "one thing seemed certain: on a sinking ship where it was necessary to fight in order to "+ 69 "boat a lifeboat, Father would have been condemned in advance. "+ 70 "Yes, that much was certain. The question that arises is htis: had Father hated the people "+ 71 "on the ship, just as she now hates the motorcyclist and the man who mocked her because "+ 72 "she covered her ears? No, Agnes cannot imagine that Father was capable of hatred. "+ 73 "Hate traps us by binding us too tightly to our adversary. This is the obscenity of war: "+ 74 "the intimacy of mutually shed blood, the lascivious of two soldiers who, eye to eye, "+ 75 "bayonet each other. Agnes was sure: it is precisely this kind of intimacy that her "+ 76 "father found repugnant.The melee on the ship filled him with such disgust that he "+ 77 "preferred to drown. The physical contact with people who struck and trampled and killed "+ 78 "one another seemed far worse to him than a solitary death in the purity of the waters. "+ 79 "[p.25-26] "+ 80 "In our world, where there are more and more faces, more and more alike, it is difficult "+ 81 "for an individual to reinforce the originality of the self and to become convinced of "+ 82 "its inimitatable uniqueness. There are two methods for cultivating the uniqueness of the "+ 83 "self: the method of addition and the method of subtraction. Agnes subtracts from herself "+ 84 "everything that is exterior and borrowed, in order to come closer to her sheer essence "+ 85 "(even with the risk that zero lurks at the bottom of subtarction). Laura's method is "+ 86 "precisely the opposite: in order to make herself even more visible, perceivable, seizable, "+ 87 "sizeable, she keeps adding to it more and more attributes and she attemptes to identify "+ 88 "herself with them (with the risk that the essence of the self may be buried by the "+ 89 "additional attributes). "+ 90 "Let's take her car as an example. After her divorce, Laura remained alone in a large "+ 91 "apartment and felt lonely. She longed for a pet to share her solitude. First she thought "+ 92 "of a dog, but soon realized that a dog needed a kind of care she would be unable to "+ 93 "provide. And she got a cat. It was a big Siamese cat, beautiful and wicked. As she "+ 94 "lived with her car and regaled her friends with stories about it, the animal that she "+ 95 "picked more or less by accident, without any special conviction (after all, her first "+ 96 "choice was a dog!), took on an evr growing significance: she began to lavish praise "+ 97 "on her pet and forced everyone to admire it. She saw in the cat a superb independence, "+ 98 "pride, freedon of action and constancy of charm (so different from human charm, which "+ 99 "is always spoiled by moments of clumsiness and unattractiveness); in the cat, she saw "+ 100 "her paradigm; in the cat she saw herself. "+ 101 " "+ 102 "The method of addition is quite charming if it involves adding to the self such "+ 103 "things as a cat, a dog, roast pork, love of the sea or of cold showers. But the matter "+ 104 "becomes less idyllic if a person decides to add love for communism, for the homeland, "+ 105 "for Mussolini, for Roman Catolicism or atheism, for facism or antifacism. In both cases "+ 106 "the method remains exactly the same: a person stubbornly defending the superiority of "+ 107 "cats over other animals is doing basically the same as the one who maintains that "+ 108 "Mussolini was the sole saviour of Italy: he is proud of this attribute of the self and "+ 109 "he tries to make this attribute (a cat or Mussolini) acknowledged or loved by everyone. "+ 110 "Here is that strange paradox to which all people cultivating the self by way of the "+ 111 "addition method are subject: they use addition in order to create unique, inimitable "+ 112 "self, yet because they automatically become prpagandists for the added attributes, they are "+ 113 "actually doing everything in their power to make as many others as possible similar "+ 114 "to themselves; as a result, their uniqueness (so painfully gained) quickly begins to disappear. "+ 115 "We may ask ourselves why a person who loves a cat (or Mussolini) is not satisfied to "+ 116 "keep his love to himself, and wants to force it on others. Let's seek the answer by "+ 117 "recalling the young woman in the sauna, who belligerently asserted that she loved "+ 118 "cold showers. She thereby managed to differentiate herself at once from one half of the "+ 119 "human race, namely the half that prefers hot showers. Unfortunately, that other half "+ 120 "resembled her all the more. Alas, how sad! Many people, few ideas, so how are we "+ 121 "differentiate ourselves from each other? The young woman knew only one way of overcoming "+ 122 "the disadvantage of her similarity to that enourmous throng devoted to cold showers: "+ 123 "she had to proclaim her credo \"I adore cold showers!\" as soon as she appeared in the "+ 124 "door of the sauna and to proclaim it with such fervour as to make the millions of other "+ 125 "women who also enjoy cold showers seem like pale imitations of herself. Let me put it "+ 126 "another way: a mere (simple and innocent) love for showers can become an attribute "+ 127 "of the self only on condition that we let the world we are ready to fight for it. "+ 128 "The one who chooses as an atrtibyte of the self a love for Mussolini becomes a "+ 129 "political warrior, while the partisan of cats, music or antique furniture bestows "+ 130 "gifts on his surroundings. "+ 131 "[p.111-113]"; 132 133 /** Construction */ 134 public TestPersist(String name) throws GateException { super(name); } 135 136 /** Fixture set up */ 137 public void setUp() throws Exception { 138 if (! Gate.getDataStoreRegister().getConfigData().containsKey("url-test")) 139 throw new GateRuntimeException("DB URL not configured in gate.xml"); 140 else 141 JDBC_URL_1 = 142 (String) Gate.getDataStoreRegister().getConfigData().get("url-test"); 143 JDBC_URL_2 = 144 (String) Gate.getDataStoreRegister().getConfigData().get("url-test1"); 145 } // setUp 146 147 /** Put things back as they should be after running tests 148 * (reinitialise the CREOLE register). 149 */ 150 public void tearDown() throws Exception { 151 } // tearDown 152 153 /** Test resource save and restore */ 154 public void testSaveRestore() throws Exception { 155 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 156 storageDir.delete(); // get rid of the temp file 157 storageDir.mkdir(); // create an empty dir of same name 158 159 SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString()); 160 sds.create(); 161 sds.open(); 162 163 // create a document 164 String server = TestDocument.getTestServerName(); 165 assertNotNull(server); 166 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 167 assertNotNull(doc); 168 doc.getFeatures().put("hi there", new Integer(23232)); 169 doc.getAnnotations().add( 170 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap() 171 ); 172 173 // check that we can't save a resource without adopting it 174 boolean cannotSync = false; 175 try { sds.sync(doc); } catch(PersistenceException e) { cannotSync=true; } 176 if(! cannotSync) assertTrue("doc synced ok before adoption", false); 177 178 // check that we can't adopt a resource that's stored somewhere else 179 doc.setDataStore(new SerialDataStore(new File("z:\\").toURL().toString())); 180 try { sds.adopt(doc,null); } catch(PersistenceException e) { cannotSync=true; } 181 if(! cannotSync) 182 assertTrue("doc adopted but in other datastore already", false); 183 doc.setDataStore(null); 184 doc.setName("Alicia Tonbridge, a Document"); 185 186 // save the document 187 Document persDoc = (Document) sds.adopt(doc,null); 188 sds.sync(persDoc); 189 Object lrPersistenceId = persDoc.getLRPersistenceId(); 190 191 // test the getLrTypes method 192 List lrTypes = sds.getLrTypes(); 193 assertTrue("wrong number of types in SDS", lrTypes.size() == 1); 194 assertTrue( 195 "wrong type LR in SDS", 196 lrTypes.get(0).equals("gate.corpora.DocumentImpl") 197 ); 198 199 // test the getLrNames method 200 Iterator iter = sds.getLrNames("gate.corpora.DocumentImpl").iterator(); 201 String name = (String) iter.next(); 202 assertEquals(name, "Alicia Tonbridge, a Document"); 203 204 // read the document back 205 FeatureMap features = Factory.newFeatureMap(); 206 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId); 207 features.put(DataStore.DATASTORE_FEATURE_NAME, sds); 208 Document doc2 = 209 (Document) Factory.createResource("gate.corpora.DocumentImpl", features); 210 Document doc3 = 211 (Document) sds.getLr("gate.corpora.DocumentImpl", lrPersistenceId); 212 213 try{ 214 boolean value = TestEqual.documentsEqual(doc3, doc2); 215 assertTrue(TestEqual.message, value); 216 value = TestEqual.documentsEqual(persDoc, doc2); 217 assertTrue(TestEqual.message, value); 218 }finally{ 219 // delete the datastore 220 sds.delete(); 221 } 222 } // testSaveRestore() 223 224 /** Simple test */ 225 public void testSimple() throws Exception { 226 // create a temporary directory; because File.createTempFile actually 227 // writes the bloody thing, we need to delete it from disk before calling 228 // DataStore.create 229 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 230 storageDir.delete(); 231 232 // create and open a serial data store 233 DataStore sds = Factory.createDataStore( 234 "gate.persist.SerialDataStore", storageDir.toURL().toString() 235 ); 236 237 // check we can get empty lists from empty data stores 238 List lrTypes = sds.getLrTypes(); 239 240 // create a document with some annotations / features on it 241 String server = TestDocument.getTestServerName(); 242 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 243 doc.getFeatures().put("hi there", new Integer(23232)); 244 doc.getAnnotations().add( 245 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 246 ); 247 248 // save the document 249 Document persDoc = (Document) sds.adopt(doc,null); 250 sds.sync(persDoc); 251 252 // remember the persistence ID for reading back 253 // (in the normal case these ids are obtained by DataStore.getLrIds(type)) 254 Object lrPersistenceId = persDoc.getLRPersistenceId(); 255 256 // read the document back 257 FeatureMap features = Factory.newFeatureMap(); 258 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId); 259 features.put(DataStore.DATASTORE_FEATURE_NAME, sds); 260 Document doc2 = 261 (Document) Factory.createResource("gate.corpora.DocumentImpl", features); 262 263 //parameters should be different 264 // check that the version we read back matches the original 265 assertTrue(TestEqual.documentsEqual(persDoc, doc2)); 266 267 // delete the datastore 268 sds.delete(); 269 } // testSimple() 270 271 /** Test multiple LRs */ 272 public void testMultipleLrs() throws Exception { 273 // create a temporary directory; because File.createTempFile actually 274 // writes the bloody thing, we need to delete it from disk before calling 275 // DataStore.create 276 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 277 storageDir.delete(); 278 279 // create and open a serial data store 280 SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString()); 281 sds.create(); 282 sds.open(); 283 284 // create a document with some annotations / features on it 285 String server = TestDocument.getTestServerName(); 286 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 287 doc.getFeatures().put("hi there", new Integer(23232)); 288 doc.getAnnotations().add( 289 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 290 ); 291 292 // create another document with some annotations / features on it 293 Document doc2 = 294 Factory.newDocument(new URL(server + "tests/html/test1.htm")); 295 doc.getFeatures().put("hi there again", new Integer(23232)); 296 doc.getAnnotations().add( 297 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap() 298 ); 299 300 // create a corpus with the documents 301 Corpus corp = Factory.newCorpus("Hamish test corpus"); 302 corp.add(doc); 303 corp.add(doc2); 304 LanguageResource persCorpus = sds.adopt(corp,null); 305 sds.sync(persCorpus); 306 307 308 // read the documents back 309 ArrayList lrsFromDisk = new ArrayList(); 310 List lrIds = sds.getLrIds("gate.corpora.SerialCorpusImpl"); 311 312 Iterator idsIter = lrIds.iterator(); 313 while(idsIter.hasNext()) { 314 String lrId = (String) idsIter.next(); 315 FeatureMap features = Factory.newFeatureMap(); 316 features.put(DataStore.DATASTORE_FEATURE_NAME, sds); 317 features.put(DataStore.LR_ID_FEATURE_NAME, lrId); 318 Resource lr = Factory.createResource( "gate.corpora.SerialCorpusImpl", 319 features); 320 lrsFromDisk.add(lr); 321 } // for each LR ID 322 323 if (DEBUG) System.out.println("LRs on disk" + lrsFromDisk); 324 325 // check that the versions we read back match the originals 326 Corpus diskCorp = (Corpus) lrsFromDisk.get(0); 327 328 Document diskDoc = (Document) diskCorp.get(0); 329 330 if (DEBUG) Out.prln("Documents in corpus: " + corp.getDocumentNames()); 331 assertTrue("corp name != mem name", corp.getName().equals(diskCorp.getName())); 332 if (DEBUG) Out.prln("Memory features " + corp.getFeatures()); 333 if (DEBUG) Out.prln("Disk features " + diskCorp.getFeatures()); 334 assertTrue("corp feat != mem feat", 335 corp.getFeatures().equals(diskCorp.getFeatures())); 336 if (DEBUG) 337 Out.prln("Annotations in doc: " + diskDoc.getAnnotations()); 338 assertTrue("doc annotations from disk not equal to memory version", 339 TestEqual.annotationSetsEqual(doc.getAnnotations(), 340 diskDoc.getAnnotations())); 341 342 assertTrue("doc from disk not equal to memory version", 343 TestEqual.documentsEqual(doc, diskDoc)); 344 345 Iterator corpusIter = diskCorp.iterator(); 346 while(corpusIter.hasNext()){ 347 if (DEBUG) 348 Out.prln(((Document) corpusIter.next()).getName()); 349 else 350 corpusIter.next(); 351 } 352 353 354 // assertTrue("doc2 from disk not equal to memory version", doc2.equals(diskDoc2)); 355 356 // delete the datastore 357 sds.delete(); 358 } // testMultipleLrs() 359 360 /** Test LR deletion */ 361 public void testDelete() throws Exception { 362 // create a temporary directory; because File.createTempFile actually 363 // writes the bloody thing, we need to delete it from disk before calling 364 // DataStore.create 365 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 366 if (DEBUG) Out.prln("Corpus stored to: " + storageDir.getAbsolutePath()); 367 storageDir.delete(); 368 369 // create and open a serial data store 370 SerialDataStore sds = new SerialDataStore(); 371 sds.setStorageUrl(storageDir.toURL().toString()); 372 sds.create(); 373 sds.open(); 374 375 // create a document with some annotations / features on it 376 String server = TestDocument.getTestServerName(); 377 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 378 doc.getFeatures().put("hi there", new Integer(23232)); 379 doc.getAnnotations().add( 380 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 381 ); 382 383 // save the document 384 Document persDoc = (Document) sds.adopt(doc,null); 385 sds.sync(persDoc); 386 387 // remember the persistence ID for reading back 388 // (in the normal case these ids are obtained by DataStore.getLrIds(type)) 389 Object lrPersistenceId = persDoc.getLRPersistenceId(); 390 391 // delete document back 392 sds.delete("gate.corpora.DocumentImpl", lrPersistenceId); 393 394 // check that there are no LRs left in the DS 395 assertTrue(sds.getLrIds("gate.corpora.DocumentImpl").size() == 0); 396 397 // delete the datastore 398 sds.delete(); 399 } // testDelete() 400 401 402 403 404 /** Test the DS register. */ 405 public void testDSR() throws Exception { 406 DataStoreRegister dsr = Gate.getDataStoreRegister(); 407 assertTrue("DSR has wrong number elements (not 0): " + dsr.size(), 408 dsr.size() == 0); 409 410 // create a temporary directory; because File.createTempFile actually 411 // writes the bloody thing, we need to delete it from disk before calling 412 // DataStore.create 413 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 414 storageDir.delete(); 415 416 // create and open a serial data store 417 DataStore sds = Factory.createDataStore( 418 "gate.persist.SerialDataStore", storageDir.toURL().toString() 419 ); 420 421 // create a document with some annotations / features on it 422 String server = TestDocument.getTestServerName(); 423 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 424 doc.getFeatures().put("hi there", new Integer(23232)); 425 doc.getAnnotations().add( 426 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 427 ); 428 429 // save the document 430 Document persDoc = (Document) sds.adopt(doc,null); 431 sds.sync(persDoc); 432 433 // DSR should have one member 434 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(), 435 dsr.size() == 1); 436 437 // create and open another serial data store 438 storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 439 storageDir.delete(); 440 DataStore sds2 = Factory.createDataStore( 441 "gate.persist.SerialDataStore", storageDir.toURL().toString() 442 ); 443 444 // DSR should have two members 445 assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 2); 446 447 // peek at the DSR members 448 Iterator dsrIter = dsr.iterator(); 449 while(dsrIter.hasNext()) { 450 DataStore ds = (DataStore) dsrIter.next(); 451 assertNotNull("null ds in ds reg", ds); 452 if(DEBUG) 453 Out.prln(ds); 454 } 455 456 // delete the datastores 457 sds.close(); 458 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(), 459 dsr.size() == 1); 460 sds.delete(); 461 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(), 462 dsr.size() == 1); 463 sds2.delete(); 464 assertTrue("DSR has wrong number elements (expected 0): " + dsr.size(), 465 dsr.size() == 0); 466 467 } // testDSR() 468 469 470 471 /** Test suite routine for the test runner */ 472 public static Test suite() { 473 return new TestSuite(TestPersist.class); 474 } // suite 475 476 477 private Document createTestDocument() 478 throws Exception { 479 480 String server = TestDocument.getTestServerName(); 481 assertNotNull(server); 482 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 483 assertNotNull(doc); 484 485 doc.getFeatures().put("hi there", new Integer(23232)); 486 doc.getFeatures().put("LONG STRING feature", this.VERY_LONG_STRING); 487 doc.getFeatures().put("NULL feature",null); 488 doc.getFeatures().put("BINARY feature",new Dummy(101,"101",true,101.101f)); 489 doc.getFeatures().put("LONG feature",new Long(101)); 490 // doc.getFeatures().put("FLOAT feature",new Double(101.102d)); 491 doc.getFeatures().put("ASCII feature",ASCII_STRING); 492 doc.getFeatures().put("UNICODE feature",UNICODE_STRING); 493 494 //create a complex feature - array of strings 495 Vector complexFeature = new Vector(); 496 complexFeature.add("string 1"); 497 complexFeature.add("string 2"); 498 complexFeature.add("string 3"); 499 complexFeature.add("string 4"); 500 complexFeature.add("string 5"); 501 doc.getFeatures().put("complex feature",complexFeature); 502 FeatureMap fm = Factory.newFeatureMap(); 503 // fm.put("FLOAT feature ZZZ",new Double(101.102d)); 504 // fm.put("ASCII feature",ASCII_STRING); 505 // fm.put("INT feature",new Integer(1212)); 506 // fm.put("UNICODE feature",UNICODE_STRING); 507 doc.getAnnotations().add( 508 new Long(0), new Long(20), "thingymajig", fm); 509 doc.setName("DB test Document---"); 510 511 return doc; 512 } 513 514 515 private Corpus createTestCorpus() 516 throws Exception { 517 518 String server = TestDocument.getTestServerName(); 519 assertNotNull(server); 520 Document doc1 = Factory.newDocument(new URL(server + "tests/doc0.html")); 521 assertNotNull(doc1); 522 523 doc1.getFeatures().put("hi there", new Integer(23232)); 524 doc1.getAnnotations().add( 525 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap() 526 ); 527 doc1.setName("DB test Document1"); 528 529 // create another document with some annotations / features on it 530 Document doc2 = 531 Factory.newDocument(new URL(server + "tests/html/test1.htm")); 532 doc2.getFeatures().put("hi there again", new Integer(23232)); 533 doc2.getAnnotations().add( 534 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap() 535 ); 536 doc2.setName("DB test Document2"); 537 538 //create corpus 539 Corpus corp = Factory.newCorpus("My test corpus"); 540 //add docs 541 corp.add(doc1); 542 corp.add(doc2); 543 //add features 544 corp.getFeatures().put("my STRING feature ", new String("string string")); 545 corp.getFeatures().put("my BOOL feature ", new Boolean("false")); 546 corp.getFeatures().put("my INT feature ", new Integer("1234")); 547 corp.getFeatures().put("my LONG feature ", new Long("123456789")); 548 corp.getFeatures().put("my LONG STRING feature", this.VERY_LONG_STRING); 549 corp.getFeatures().put("my NULL feature", null); 550 corp.getFeatures().put("my BINARY feature",new Dummy(101,"101",true,101.101f)); 551 return corp; 552 } 553 554 private DatabaseDataStore _createDS() { 555 556 DatabaseDataStore ds = null; 557 if (this.dbType == DBHelper.ORACLE_DB) { 558 ds = new OracleDataStore(); 559 } 560 else if (this.dbType == DBHelper.POSTGRES_DB) { 561 ds = new PostgresDataStore(); 562 } 563 else { 564 throw new IllegalArgumentException(); 565 } 566 567 Assert.assertNotNull(ds); 568 return ds; 569 } 570 571 private void prepareDB(String db) { 572 573 if (this.JDBC_URL_1.indexOf(db) > 0 ) { 574 this.JDBC_URL = this.JDBC_URL_1; 575 } 576 else { 577 this.JDBC_URL = this.JDBC_URL_2; 578 } 579 580 Assert.assertNotNull("jdbc url not set for Oracle or Postgres",this.JDBC_URL); 581 582 this.dbType = DBHelper.getDatabaseType(JDBC_URL); 583 } 584 585 586 /** Test the DS register. */ 587 private void _testDB_UseCase01() throws Exception { 588 ///Err.prln("Use case 01 started..."); 589 //descr: create a document in the DB 590 591 592 //1. open data storage 593 DatabaseDataStore ds = this._createDS(); 594 Assert.assertNotNull(ds); 595 ds.setStorageUrl(this.JDBC_URL); 596 ds.open(); 597 598 //2. get test document 599 Document doc = createTestDocument(); 600 Assert.assertNotNull(doc); 601 602 //3. get security factory & login 603 AccessController ac = Factory.createAccessController(this.JDBC_URL); 604 ac.open(); 605 Assert.assertNotNull(ac); 606 607 User usr = ac.findUser("kalina"); 608 Assert.assertNotNull(usr); 609 610 Group grp = (Group)usr.getGroups().get(0); 611 Assert.assertNotNull(grp); 612 613 Session usrSession = ac.login("kalina","sesame",grp.getID()); 614 Assert.assertNotNull(usrSession); 615 Assert.assertTrue(ac.isValidSession(usrSession)); 616 617 //4. create security settings for doc 618 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp); 619 620 //4.5 set DS session 621 ds.setSession(usrSession); 622 623 //5. try adding doc to data store 624 LanguageResource lr = ds.adopt(doc,si); 625 626 Assert.assertTrue(lr instanceof DatabaseDocumentImpl); 627 Assert.assertNotNull(lr.getDataStore()); 628 Assert.assertTrue(lr.getDataStore() instanceof DatabaseDataStore); 629 Assert.assertEquals(doc.getAnnotations(), ((DatabaseDocumentImpl)lr).getAnnotations()); 630 631 sampleDoc_lrID = (Long)lr.getLRPersistenceId(); 632 if (DEBUG) Out.prln("lr id: " + this.sampleDoc_lrID); 633 // this.sampleDoc = lr; 634 sampleDoc = doc; 635 //System.out.println("adopted doc:name=["+((Document)lr).getName()+"], lr_id=["+((Document)lr).getLRPersistenceId()+"]"); 636 //6.close 637 ac.close(); 638 ds.close(); 639 640 if(DEBUG) { 641 Err.prln("Use case 01 passed..."); 642 } 643 } 644 645 646 private void _testDB_UseCase02() throws Exception { 647 ///Err.prln("Use case 02 started..."); 648 //read a document 649 //use the one created in UC01 650 LanguageResource lr = null; 651 652 //1. open data storage 653 DatabaseDataStore ds = this._createDS(); 654 Assert.assertNotNull(ds); 655 ds.setStorageUrl(this.JDBC_URL); 656 ds.open(); 657 658 //3. get security factory & login 659 AccessController ac = Factory.createAccessController(this.JDBC_URL); 660 Assert.assertNotNull(ac); 661 ac.open(); 662 663 User usr = ac.findUser("kalina"); 664 Assert.assertNotNull(usr); 665 666 Group grp = (Group)usr.getGroups().get(0); 667 Assert.assertNotNull(grp); 668 669 Session usrSession = ac.login("kalina","sesame",grp.getID()); 670 Assert.assertNotNull(usrSession); 671 Assert.assertTrue(ac.isValidSession(usrSession)); 672 673 //4. create security settings for doc 674 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp); 675 676 //4.5 set DS session 677 ds.setSession(usrSession); 678 679 //2. read LR 680 ///Err.println(">>>"); 681 FeatureMap params = Factory.newFeatureMap(); 682 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 683 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 684 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 685 ///Err.println("<<<"); 686 //3. check name 687 String name = lr.getName(); 688 Assert.assertNotNull(name); 689 Assert.assertEquals(name,sampleDoc.getName()); 690 691 //4. check features 692 FeatureMap fm = lr.getFeatures(); 693 FeatureMap fmOrig = sampleDoc.getFeatures(); 694 695 Assert.assertNotNull(fm); 696 Assert.assertNotNull(fmOrig); 697 Assert.assertTrue(fm.size() == fmOrig.size()); 698 699 Iterator keys = fm.keySet().iterator(); 700 701 while (keys.hasNext()) { 702 String currKey = (String)keys.next(); 703 Assert.assertTrue(fmOrig.containsKey(currKey)); 704 Assert.assertEquals(fm.get(currKey),fmOrig.get(currKey)); 705 } 706 707 //6. URL 708 DatabaseDocumentImpl dbDoc = (DatabaseDocumentImpl)lr; 709 Assert.assertEquals(dbDoc.getSourceUrl(),this.sampleDoc.getSourceUrl()); 710 711 //5.start/end 712 Assert.assertEquals(dbDoc.getSourceUrlStartOffset(),this.sampleDoc.getSourceUrlStartOffset()); 713 Assert.assertEquals(dbDoc.getSourceUrlEndOffset(),this.sampleDoc.getSourceUrlEndOffset()); 714 715 //6.markupAware 716 Assert.assertEquals(dbDoc.getMarkupAware(),this.sampleDoc.getMarkupAware()); 717 718 //7. content 719 DocumentContent cont = dbDoc.getContent(); 720 Assert.assertEquals(cont,this.sampleDoc.getContent()); 721 722 //8. access the content again and assure it's not read from the DB twice 723 Assert.assertEquals(cont,((Document)this.sampleDoc).getContent()); 724 725 //9. encoding 726 String encNew = (String)dbDoc. 727 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 728 String encOld = (String)((DocumentImpl)this.sampleDoc). 729 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 730 Assert.assertEquals(encNew,encOld); 731 732 //10. default annotations 733 ///System.out.println("GETTING default ANNOTATIONS..."); 734 AnnotationSet defaultNew = dbDoc.getAnnotations(); 735 AnnotationSet defaultOld = this.sampleDoc.getAnnotations(); 736 737 Assert.assertNotNull(defaultNew); 738 Assert.assertTrue(defaultNew.size() == defaultOld.size()); 739 Assert.assertEquals(defaultNew,defaultOld); 740 741 742 //10. iterate named annotations 743 Map namedOld = this.sampleDoc.getNamedAnnotationSets(); 744 Iterator itOld = namedOld.keySet().iterator(); 745 ///System.out.println("GETTING named ANNOTATIONS..."); 746 while (itOld.hasNext()) { 747 String asetName = (String)itOld.next(); 748 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName); 749 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName); 750 Assert.assertNotNull(asetNew); 751 ///System.out.println("aset_old, size=["+asetOld.size()+"]"); 752 ///System.out.println("aset_new, size=["+asetNew.size()+"]"); 753 ///System.out.println("old = >>" + asetOld +"<<"); 754 ///System.out.println("new = >>" + asetNew +"<<"); 755 Assert.assertTrue(asetNew.size() == asetOld.size()); 756 Assert.assertEquals(asetNew,asetOld); 757 } 758 759 760 //11. ALL named annotation (ensure nothing is read from DB twice) 761 Map namedNew = dbDoc.getNamedAnnotationSets(); 762 763 Assert.assertNotNull(namedNew); 764 Assert.assertTrue(namedNew.size() == namedOld.size()); 765 766 Iterator itNames = namedNew.keySet().iterator(); 767 while (itNames.hasNext()) { 768 String asetName = (String)itNames.next(); 769 AnnotationSet asetNew = (AnnotationSet)namedNew.get(asetName); 770 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName); 771 Assert.assertNotNull(asetNew); 772 Assert.assertNotNull(asetOld); 773 Assert.assertEquals(asetNew,asetOld); 774 } 775 776 //close 777 ds.removeDatastoreListener((DatastoreListener)lr); 778 lr = null; 779 780 ds.close(); 781 ac.close(); 782 783 if(DEBUG) { 784 Err.prln("Use case 02 passed..."); 785 } 786 787 } 788 789 790 private void _testDB_UseCase03() throws Exception { 791 ///Err.prln("Use case 03 started..."); 792 //sync a document 793 LanguageResource lr = null; 794 795 //0. get security factory & login 796 AccessController ac = Factory.createAccessController(this.JDBC_URL); 797 Assert.assertNotNull(ac); 798 ac.open(); 799 800 User usr = ac.findUser("kalina"); 801 Assert.assertNotNull(usr); 802 803 Group grp = (Group)usr.getGroups().get(0); 804 Assert.assertNotNull(grp); 805 806 Session usrSession = ac.login("kalina","sesame",grp.getID()); 807 Assert.assertNotNull(usrSession); 808 Assert.assertTrue(ac.isValidSession(usrSession)); 809 810 //1. open data storage 811 DatabaseDataStore ds = this._createDS(); 812 Assert.assertNotNull(ds); 813 ds.setStorageUrl(this.JDBC_URL); 814 ds.open(); 815 816 //1.5 set DS session 817 ds.setSession(usrSession); 818 819 if (DEBUG) Out.prln("ID " + sampleDoc_lrID); 820 //2. read LR 821 FeatureMap params = Factory.newFeatureMap(); 822 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 823 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 824 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 825 Document dbDoc = (Document)lr; 826 Document doc2 = null; 827 828 //2.5 get exclusive lock 829 if (false == ds.lockLr(lr)) { 830 throw new PersistenceException("document is locked by another user"); 831 } 832 833 //3. change name 834 String oldName = dbDoc.getName(); 835 String newName = oldName + "__UPD"; 836 dbDoc.setName(newName); 837 dbDoc.sync(); 838 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 839 params.clear(); 840 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 841 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 842 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 843 844 Assert.assertEquals(newName,dbDoc.getName()); 845 Assert.assertEquals(newName,doc2.getName()); 846 847 Factory.deleteResource(doc2); 848 doc2 = null; 849 850 //4. change features 851 FeatureMap fm = dbDoc.getFeatures(); 852 Iterator keys = fm.keySet().iterator(); 853 854 //4.1 change the value of the first feature 855 while(keys.hasNext()) { 856 String currKey = (String)keys.next(); 857 Object val = fm.get(currKey); 858 Object newVal = null; 859 if (val instanceof Long) { 860 newVal = new Long(101010101); 861 } 862 else if (val instanceof Integer) { 863 newVal = new Integer(2121212); 864 } 865 else if (val instanceof String) { 866 newVal = new String("UPD__").concat( (String)val).concat("__UPD"); 867 } 868 if (newVal != null) 869 fm.put(currKey,newVal); 870 } 871 dbDoc.sync(); 872 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 873 params.clear(); 874 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 875 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 876 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 877 878 Assert.assertEquals(fm,dbDoc.getFeatures()); 879 Assert.assertEquals(fm,doc2.getFeatures()); 880 Factory.deleteResource(doc2); 881 doc2 = null; 882 883 //6. URL 884 URL docURL = dbDoc.getSourceUrl(); 885 URL newURL = null; 886 newURL = new URL(docURL.toString()+".UPDATED"); 887 dbDoc.setSourceUrl(newURL); 888 dbDoc.sync(); 889 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 890 params.clear(); 891 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 892 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 893 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 894 895 Assert.assertEquals(newURL,dbDoc.getSourceUrl()); 896 Assert.assertEquals(newURL,doc2.getSourceUrl()); 897 Factory.deleteResource(doc2); 898 doc2 = null; 899 900 //5.start/end 901 Long newStart = new Long(123); 902 Long newEnd = new Long(789); 903 dbDoc.setSourceUrlStartOffset(newStart); 904 dbDoc.setSourceUrlEndOffset(newEnd); 905 dbDoc.sync(); 906 907 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 908 params.clear(); 909 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 910 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 911 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 912 913 Assert.assertEquals(newStart,dbDoc.getSourceUrlStartOffset()); 914 Assert.assertEquals(newStart,doc2.getSourceUrlStartOffset()); 915 Assert.assertEquals(newEnd,dbDoc.getSourceUrlEndOffset()); 916 Assert.assertEquals(newEnd,doc2.getSourceUrlEndOffset()); 917 918 Factory.deleteResource(doc2); 919 doc2 = null; 920 921 922 //6.markupAware 923 Boolean oldMA = dbDoc.getMarkupAware(); 924 Boolean newMA = oldMA.booleanValue() ? Boolean.FALSE : Boolean.TRUE; 925 dbDoc.setMarkupAware(newMA); 926 dbDoc.sync(); 927 928 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 929 params.clear(); 930 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 931 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 932 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 933 934 Assert.assertEquals(newMA,doc2.getMarkupAware()); 935 Assert.assertEquals(newMA,dbDoc.getMarkupAware()); 936 937 Factory.deleteResource(doc2); 938 doc2 = null; 939 940 941 //7. content 942 DocumentContent contOld = dbDoc.getContent(); 943 DocumentContent contNew = new DocumentContentImpl(new String("UPDATED__").concat(contOld.toString().concat("__UPDATED"))); 944 dbDoc.setContent(contNew); 945 dbDoc.sync(); 946 947 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 948 params.clear(); 949 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 950 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 951 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 952 953 Assert.assertEquals(contNew,dbDoc.getContent()); 954 Assert.assertEquals(contNew,doc2.getContent()); 955 956 Factory.deleteResource(doc2); 957 doc2 = null; 958 959 //8. encoding 960 String encOld = (String)dbDoc. 961 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 962 dbDoc.setParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME,"XXX"); 963 dbDoc.sync(); 964 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 965 params.clear(); 966 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 967 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 968 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 969 970 String encNew = (String)doc2. 971 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 972 Assert.assertEquals(encNew,encOld); 973 974 Factory.deleteResource(doc2); 975 doc2 = null; 976 977 978 //9. add annotations 979 AnnotationSet dbDocSet = dbDoc.getAnnotations("TEST SET"); 980 Assert.assertNotNull(dbDocSet); 981 982 FeatureMap fm1 = new SimpleFeatureMapImpl(); 983 fm1.put("string key","string value"); 984 985 Integer annInd = dbDocSet.add(new Long(0), new Long(10), 986 "TEST TYPE", 987 fm1); 988 989 dbDoc.sync(); 990 //-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 991 params.clear(); 992 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 993 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 994 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 995 996 AnnotationSet doc2Set = doc2.getAnnotations("TEST SET"); 997 Assert.assertTrue(dbDocSet.size() == doc2Set.size()); 998 //-- Assert.assertEquals(doc2Set,dbDocSet); 999 1000 Factory.deleteResource(doc2); 1001 doc2 = null; 1002 1003 1004 //9.1. change+add annotations 1005 Annotation dbDocAnn = dbDocSet.get(annInd); 1006 1007 FeatureMap fm2 = new SimpleFeatureMapImpl(); 1008 fm2.put("string2","uuuuuu"); 1009 fm2.put("int2",new Integer(98989898)); 1010 Integer newInd = dbDocSet.add(dbDocAnn.getStartNode().getOffset(), 1011 dbDocAnn.getEndNode().getOffset(), 1012 dbDocAnn.getType() + "__XX", 1013 fm2); 1014 Annotation dbDocAnnNew = dbDocSet.get(newInd); 1015 dbDoc.sync(); 1016 1017//-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 1018 params.clear(); 1019 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1020 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 1021 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 1022 1023 doc2Set = doc2.getAnnotations("TEST SET"); 1024 Assert.assertTrue(dbDocSet.size() == doc2Set.size()); 1025 Assert.assertTrue(TestEqual.annotationSetsEqual(doc2Set, dbDocSet)); 1026 Assert.assertTrue(doc2Set.contains(dbDocAnnNew)); 1027 1028 Factory.deleteResource(doc2); 1029 doc2 = null; 1030/* 1031 //10. iterate named annotations 1032 Map namedOld = ((DocumentImpl)this.uc01_LR).getNamedAnnotationSets(); 1033 Iterator itOld = namedOld.keySet().iterator(); 1034 while (itOld.hasNext()) { 1035 String asetName = (String)itOld.next(); 1036 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName); 1037 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName); 1038 Assert.assertNotNull(asetNew); 1039 Assert.assertEquals(asetNew,asetOld); 1040// Features fmNew = asetNew.getFea 1041 } 1042*/ 1043 1044 //11. add a new ann-set 1045 String dummySetName = "--NO--SUCH--SET--"; 1046 AnnotationSet aset = dbDoc.getAnnotations(dummySetName); 1047 aset.addAll(dbDoc.getAnnotations()); 1048 dbDoc.sync(); 1049 1050//-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 1051 params.clear(); 1052 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1053 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 1054 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 1055 1056 Assert.assertTrue(dbDoc.getNamedAnnotationSets().containsKey(dummySetName)); 1057 Assert.assertTrue(doc2.getNamedAnnotationSets().containsKey(dummySetName)); 1058 AnnotationSet copy1 = (AnnotationSet) 1059 dbDoc.getNamedAnnotationSets().get(dummySetName); 1060 AnnotationSet copy2 = (AnnotationSet) 1061 doc2.getNamedAnnotationSets().get(dummySetName); 1062 Assert.assertTrue(dbDoc.getNamedAnnotationSets().containsValue(aset)); 1063 Assert.assertTrue(TestEqual.annotationSetsEqual(copy1, copy2)); 1064 Assert.assertTrue(dbDoc.getNamedAnnotationSets().size() == doc2.getNamedAnnotationSets().size()); 1065//maps aren't equal since removing the equals impementations 1066// Assert.assertEquals(doc2.getNamedAnnotationSets(),dbDoc.getNamedAnnotationSets()); 1067 1068 Factory.deleteResource(doc2); 1069 doc2 = null; 1070 1071 //12. remove aset 1072 dbDoc.removeAnnotationSet(dummySetName); 1073 dbDoc.sync(); 1074 Assert.assertTrue(false == ((EventAwareDocument)dbDoc).getLoadedAnnotationSets().contains(dummySetName)); 1075 Assert.assertTrue(false == dbDoc.getNamedAnnotationSets().containsKey(dummySetName)); 1076 1077//-- doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 1078 params.clear(); 1079 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1080 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 1081 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 1082 1083 Assert.assertTrue(false == doc2.getNamedAnnotationSets().containsKey(dummySetName)); 1084 1085 Factory.deleteResource(doc2); 1086 doc2 = null; 1087 1088 //13. unlock 1089 ds.unlockLr(lr); 1090 ds.sync(lr); 1091 1092 //close 1093 Factory.deleteResource(dbDoc); 1094 dbDoc = null; 1095 1096 ac.close(); 1097 ds.close(); 1098 1099 if(DEBUG) { 1100 Err.prln("Use case 03 passed..."); 1101 } 1102 } 1103 1104 1105 private void _testDB_UseCase04() throws Exception { 1106///Err.prln("Use case 04 started..."); 1107 //delete a document 1108 LanguageResource lr = null; 1109 1110 //0. get security factory & login 1111 AccessController ac = Factory.createAccessController(this.JDBC_URL); 1112 Assert.assertNotNull(ac); 1113 ac.open(); 1114 1115 User usr = ac.findUser("kalina"); 1116 Assert.assertNotNull(usr); 1117 1118 Group grp = (Group)usr.getGroups().get(0); 1119 Assert.assertNotNull(grp); 1120 1121 Session usrSession = ac.login("kalina","sesame",grp.getID()); 1122 Assert.assertNotNull(usrSession); 1123 Assert.assertTrue(ac.isValidSession(usrSession)); 1124 1125 //1. open data storage 1126 DatabaseDataStore ds = this._createDS(); 1127 Assert.assertNotNull(ds); 1128 ds.setStorageUrl(this.JDBC_URL); 1129 ds.open(); 1130 ds.setSession(usrSession); 1131 1132 //2. read LR 1133 FeatureMap params = Factory.newFeatureMap(); 1134 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1135 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 1136 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 1137 1138 //2.5 get exclusive lock 1139 if (false == ds.lockLr(lr)) { 1140 throw new PersistenceException("document is locked by another user"); 1141 } 1142 1143 //3. try to delete it 1144 ds.delete(DBHelper.DOCUMENT_CLASS,lr.getLRPersistenceId()); 1145 1146 //no need to unlock 1147 1148 //close 1149 ds.close(); 1150 ac.close(); 1151 1152 if(DEBUG) { 1153 Err.prln("Use case 04 passed..."); 1154 } 1155 1156 } 1157 1158 1159 /** Test the DS register. */ 1160 private void _testDB_UseCase101() throws Exception { 1161///Err.prln("Use case 101 started..."); 1162 //descr : create a corpus 1163 1164 //0. get security factory & login 1165 AccessController ac = Factory.createAccessController(this.JDBC_URL); 1166 Assert.assertNotNull(ac); 1167 ac.open(); 1168 1169 User usr = ac.findUser("kalina"); 1170 Assert.assertNotNull(usr); 1171 1172 Group grp = (Group)usr.getGroups().get(0); 1173 Assert.assertNotNull(grp); 1174 1175 Session usrSession = ac.login("kalina","sesame",grp.getID()); 1176 Assert.assertNotNull(usrSession); 1177 Assert.assertTrue(ac.isValidSession(usrSession)); 1178 1179 //1. open data storage 1180 DatabaseDataStore ds = this._createDS(); 1181 Assert.assertNotNull(ds); 1182 ds.setStorageUrl(this.JDBC_URL); 1183 ds.open(); 1184 ds.setSession(usrSession); 1185 1186 //2. get test document 1187 Corpus corp = createTestCorpus(); 1188 Assert.assertNotNull(corp); 1189 1190 //4. create security settings for doc 1191 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp); 1192 1193 //5. try adding corpus to data store 1194 Corpus result = (Corpus)ds.adopt(corp,si); 1195 Assert.assertNotNull(result); 1196 Assert.assertTrue(result instanceof DatabaseCorpusImpl); 1197 Assert.assertNotNull(result.getLRPersistenceId()); 1198 1199 this.sampleCorpus = result; 1200 this.sampleCorpus_lrID = (Long)result.getLRPersistenceId(); 1201 1202 //6.close 1203 ac.close(); 1204 ds.close(); 1205 1206 if(DEBUG) { 1207 Err.prln("Use case 101 passed..."); 1208 } 1209 1210 } 1211 1212 1213 1214 /** Test the DS register. */ 1215 private void _testDB_UseCase102() throws Exception { 1216 //read a corpus 1217///Err.prln("Use case 102 started..."); 1218 LanguageResource lr = null; 1219 1220 //0. get security factory & login 1221 AccessController ac = Factory.createAccessController(this.JDBC_URL); 1222 Assert.assertNotNull(ac); 1223 ac.open(); 1224 1225 User usr = ac.findUser("kalina"); 1226 Assert.assertNotNull(usr); 1227 1228 Group grp = (Group)usr.getGroups().get(0); 1229 Assert.assertNotNull(grp); 1230 1231 Session usrSession = ac.login("kalina","sesame",grp.getID()); 1232 Assert.assertNotNull(usrSession); 1233 Assert.assertTrue(ac.isValidSession(usrSession)); 1234 1235 //1. open data storage 1236 DatabaseDataStore ds = this._createDS(); 1237 Assert.assertNotNull(ds); 1238 ds.setStorageUrl(this.JDBC_URL); 1239 ds.open(); 1240 ds.setSession(usrSession); 1241 1242 //2. read LR 1243 FeatureMap params = Factory.newFeatureMap(); 1244 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1245 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID); 1246 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params); 1247 1248 //3. check name 1249 String name = lr.getName(); 1250 Assert.assertNotNull(name); 1251 Assert.assertEquals(name,sampleCorpus.getName()); 1252 1253 //4. check features 1254 FeatureMap fm = lr.getFeatures(); 1255 FeatureMap fmOrig = sampleCorpus.getFeatures(); 1256 1257 Assert.assertNotNull(fm); 1258 Assert.assertNotNull(fmOrig); 1259 Assert.assertTrue(fm.size() == fmOrig.size()); 1260 1261 Iterator keys = fm.keySet().iterator(); 1262 1263 while (keys.hasNext()) { 1264 String currKey = (String)keys.next(); 1265 Assert.assertTrue(fmOrig.containsKey(currKey)); 1266 Assert.assertEquals(fm.get(currKey),fmOrig.get(currKey)); 1267 } 1268 1269 //close 1270 ds.close(); 1271 1272 if(DEBUG) { 1273 Err.prln("Use case 102 passed..."); 1274 } 1275 1276 } 1277 1278 1279 private void _testDB_UseCase103() throws Exception { 1280///Err.prln("Use case 103 started..."); 1281 //sync a corpus 1282 LanguageResource lr = null; 1283 1284 //0. get security factory & login 1285 AccessController ac = Factory.createAccessController(this.JDBC_URL); 1286 Assert.assertNotNull(ac); 1287 ac.open(); 1288 1289 User usr = ac.findUser("kalina"); 1290 Assert.assertNotNull(usr); 1291 1292 Group grp = (Group)usr.getGroups().get(0); 1293 Assert.assertNotNull(grp); 1294 1295 Session usrSession = ac.login("kalina","sesame",grp.getID()); 1296 Assert.assertNotNull(usrSession); 1297 Assert.assertTrue(ac.isValidSession(usrSession)); 1298 1299 //1. open data storage 1300 DatabaseDataStore ds = this._createDS(); 1301 Assert.assertNotNull(ds); 1302 ds.setStorageUrl(this.JDBC_URL); 1303 ds.open(); 1304 ds.setSession(usrSession); 1305 1306 if (DEBUG) Out.prln("ID " + sampleCorpus_lrID); 1307 1308 //2. read LR 1309 FeatureMap params = Factory.newFeatureMap(); 1310 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1311 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID); 1312 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params); 1313 1314 Corpus dbCorp = (Corpus)lr; 1315 Corpus corp2 = null; 1316 1317 //3. change name 1318 String oldName = dbCorp.getName(); 1319 String newName = oldName + "__UPD"; 1320 dbCorp.setName(newName); 1321 dbCorp.sync(); 1322 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID); 1323 Assert.assertEquals(newName,dbCorp.getName()); 1324 Assert.assertEquals(newName,corp2.getName()); 1325 1326 //4. change features 1327 FeatureMap fm = dbCorp.getFeatures(); 1328 Iterator keys = fm.keySet().iterator(); 1329 1330 //4.1 change the value of the first feature 1331 while(keys.hasNext()) { 1332 String currKey = (String)keys.next(); 1333 Object val = fm.get(currKey); 1334 Object newVal = null; 1335 if (val instanceof Long) { 1336 newVal = new Long(101010101); 1337 } 1338 else if (val instanceof Integer) { 1339 newVal = new Integer(2121212); 1340 } 1341 else if (val instanceof String) { 1342 newVal = new String("UPD__").concat( (String)val).concat("__UPD"); 1343 } 1344 if (newVal != null) 1345 fm.put(currKey,newVal); 1346 } 1347 dbCorp.sync(); 1348 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID); 1349 Assert.assertEquals(fm,dbCorp.getFeatures()); 1350 Assert.assertEquals(fm,corp2.getFeatures()); 1351 1352 //close 1353 ds.close(); 1354 1355 if(DEBUG) { 1356 Err.prln("Use case 103 passed..."); 1357 } 1358 1359} 1360 1361// public void testOracle_01() throws Exception { 1362// 1363// prepareDB("oracle"); 1364// _testDB_UseCase01(); 1365// } 1366// 1367// public void testOracle_02() throws Exception { 1368// 1369// prepareDB("oracle"); 1370// _testDB_UseCase02(); 1371// } 1372// 1373// public void testOracle_03() throws Exception { 1374// 1375// prepareDB("oracle"); 1376// _testDB_UseCase03(); 1377// } 1378// 1379// public void testOracle_04() throws Exception { 1380// 1381// prepareDB("oracle"); 1382// _testDB_UseCase04(); 1383// } 1384// 1385// public void testOracle_101() throws Exception { 1386// 1387// prepareDB("oracle"); 1388// _testDB_UseCase101(); 1389// } 1390// 1391// public void testOracle_102() throws Exception { 1392// 1393// prepareDB("oracle"); 1394// _testDB_UseCase102(); 1395// } 1396// 1397// public void testOracle_103() throws Exception { 1398// 1399// prepareDB("oracle"); 1400// _testDB_UseCase103(); 1401// } 1402 1403 public void testPostgres_01() throws Exception { 1404 1405 prepareDB("postgres"); 1406 _testDB_UseCase01(); 1407 } 1408 1409 public void testPostgres_02() throws Exception { 1410 1411 prepareDB("postgres"); 1412 _testDB_UseCase02(); 1413 } 1414 1415 public void testPostgres_03() throws Exception { 1416 1417 prepareDB("postgres"); 1418 _testDB_UseCase03(); 1419 } 1420 1421 public void testPostgres_04() throws Exception { 1422 1423 prepareDB("postgres"); 1424 _testDB_UseCase04(); 1425 } 1426 1427 public void testPostgres_101() throws Exception { 1428 1429 prepareDB("postgres"); 1430 _testDB_UseCase101(); 1431 } 1432 1433 public void testPostgres_102() throws Exception { 1434 1435 prepareDB("postgres"); 1436 _testDB_UseCase102(); 1437 } 1438 1439 public void testPostgres_103() throws Exception { 1440 1441 prepareDB("postgres"); 1442 _testDB_UseCase103(); 1443 } 1444 1445 1446 1447 public static void main(String[] args){ 1448 try{ 1449 1450System.setProperty(Gate.GATE_CONFIG_PROPERTY,"y:/gate.xml") ; 1451 Gate.setLocalWebServer(false); 1452 Gate.setNetConnected(false); 1453 Gate.init(); 1454 1455 1456 TestPersist test = new TestPersist(""); 1457 1458/* 1459 long timeStart = 0; 1460 timeStart = System.currentTimeMillis(); 1461 int size = 512*1024; 1462// test.testOracleLOB(size,3); 1463 test.testPostgresLOB(size,3); 1464 System.out.println("time: ["+ (System.currentTimeMillis()-timeStart) +"]"); 1465 1466 if (true) { 1467 throw new RuntimeException(); 1468 } 1469*/ 1470 1471 /* oracle */ 1472 1473// test.setUp(); 1474// test.testOracle_01(); 1475// test.tearDown(); 1476// 1477// test.setUp(); 1478// test.testOracle_02(); 1479// test.tearDown(); 1480// 1481// test.setUp(); 1482// test.testOracle_03(); 1483// test.tearDown(); 1484// 1485// test.setUp(); 1486// test.testOracle_04(); 1487// test.tearDown(); 1488// 1489// test.setUp(); 1490// test.testOracle_101(); 1491// test.tearDown(); 1492// 1493// test.setUp(); 1494// test.testOracle_102(); 1495// test.tearDown(); 1496// 1497// test.setUp(); 1498// test.testOracle_103(); 1499// test.tearDown(); 1500// 1501 1502 /* postgres */ 1503 1504 test.setUp(); 1505 test.testPostgres_01(); 1506 test.tearDown(); 1507 1508 test.setUp(); 1509 test.testPostgres_02(); 1510 test.tearDown(); 1511 1512 test.setUp(); 1513 test.testPostgres_03(); 1514 test.tearDown(); 1515 1516 test.setUp(); 1517 test.testPostgres_04(); 1518 test.tearDown(); 1519 1520 test.setUp(); 1521 test.testPostgres_101(); 1522 test.tearDown(); 1523 1524 test.setUp(); 1525 test.testPostgres_102(); 1526 test.tearDown(); 1527 1528 test.setUp(); 1529 test.testPostgres_103(); 1530 test.tearDown(); 1531 1532 test.setUp(); 1533 test.testDelete(); 1534 test.tearDown(); 1535 1536 test.setUp(); 1537 test.testDSR(); 1538 test.tearDown(); 1539 1540 test.setUp(); 1541 test.testMultipleLrs(); 1542 test.tearDown(); 1543 1544 test.setUp(); 1545// test.testSaveRestore(); 1546 test.tearDown(); 1547 1548 test.setUp(); 1549 test.testSimple(); 1550 test.tearDown(); 1551 1552 //I put this last because its failure is dependent on the gc() and 1553 //there's nothing I can do about it. Maybe I'll remove this from the 1554 //test 1555 test.setUp(); 1556 test.testMultipleLrs(); 1557 test.tearDown(); 1558 1559 if (DEBUG) { 1560 Err.println("done."); 1561 } 1562 }catch(Exception e){ 1563 e.printStackTrace(); 1564 } 1565 } 1566 1567/* 1568 public void testPostgresLOB(int size, int count) throws Exception { 1569 1570 byte[] buffer = new byte[size]; 1571 String url = "jdbc:postgresql://192.168.128.208:5432/gate09?user=gateuser&password=gate"; 1572// ByteArrayInputStream bais = new ByteArrayInputStream(buffer); 1573 1574 try { 1575 Connection conn = DBHelper.connect(url); 1576 conn.setAutoCommit(false); 1577 PreparedStatement pstmt = conn.prepareStatement("insert into lob_test values(?)"); 1578 1579 for (int i =0; i< count; i++) { 1580// bais.reset(); 1581// pstmt.setBinaryStream(1,bais,buffer.length); 1582 pstmt.setBytes(1,buffer); 1583 pstmt.executeUpdate(); 1584 conn.commit(); 1585 } 1586 } 1587 catch(Exception e) { 1588 e.printStackTrace(); 1589 } 1590 1591 1592 } 1593 1594 public void testOracleLOB(int size,int count) throws Exception { 1595 byte[] buffer = new byte[size]; 1596 String url = "jdbc:oracle:thin:GATEUSER/gate@192.168.128.208:1521:gate07"; 1597 ByteArrayInputStream bais = new ByteArrayInputStream(buffer); 1598 1599 CallableStatement cstmt = null; 1600 PreparedStatement pstmt = null; 1601 ResultSet rs = null; 1602 Blob blobValue = null; 1603 1604 try { 1605 Connection conn = DBHelper.connect(url); 1606 conn.setAutoCommit(false); 1607 cstmt = conn.prepareCall("{ call gateadmin.create_lob(?) }"); 1608 1609 1610 for (int i =0; i< count; i++) { 1611 1612 cstmt.registerOutParameter(1,java.sql.Types.BIGINT); 1613 cstmt.execute(); 1614 long blobID = cstmt.getLong(1); 1615 1616 pstmt = conn.prepareStatement("select blob_value from gateadmin.lob_test where id=?"); 1617 pstmt.setLong(1,blobID); 1618 pstmt.execute(); 1619 rs = pstmt.getResultSet(); 1620 rs.next(); 1621 1622 blobValue = rs.getBlob(1); 1623 BLOB oraBlob = (BLOB)blobValue; 1624 OutputStream output = oraBlob.getBinaryOutputStream(); 1625 output.write(buffer,0,buffer.length); 1626 output.close(); 1627 1628 conn.commit(); 1629 } 1630 1631 } 1632 catch(Exception e) { 1633 e.printStackTrace(); 1634 } 1635 1636 } 1637 1638*/ 1639 1640 1641/* 1642 public void testPostgres01() throws Exception { 1643 1644 String url = "jdbc:postgresql://192.168.128.208:5432/gate09"; 1645 try { 1646 1647 Connection c = DBHelper.connect(url,"gateuser","gate"); 1648 c.setAutoCommit(false); 1649 1650 Object src = new Long(1234); 1651 1652 PreparedStatement pstmt = c.prepareStatement("insert into test3 values (nextval('seq3'), ?)"); 1653 Object o = new Object(); 1654 1655 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1656 ObjectOutputStream oos = new ObjectOutputStream(baos); 1657 oos.writeObject(src); 1658 oos.flush(); 1659 oos.close(); 1660 baos.close(); 1661 1662 byte[] buff = baos.toByteArray(); 1663System.out.println(buff.length); 1664 ByteArrayInputStream bais = new ByteArrayInputStream(buff); 1665 1666 pstmt.setBinaryStream(1,bais,buff.length); 1667 pstmt.execute(); 1668bais.close(); 1669 c.commit(); 1670 bais.close(); 1671 1672 PreparedStatement pstmt2 = c.prepareStatement("select blob from test3 where id = (select max(id) from test3)"); 1673 pstmt2.execute(); 1674 ResultSet rs = pstmt2.getResultSet(); 1675 if (false == rs.next()) { 1676 throw new Exception("empty result set"); 1677 } 1678 1679 InputStream is = rs.getBinaryStream("blob"); 1680 ObjectInputStream ois = new ObjectInputStream(is); 1681 Object result = ois.readObject(); 1682System.out.println(result); 1683 ois.close(); 1684 is.close(); 1685 1686 rs.close(); 1687 pstmt2.close(); 1688 1689 c.commit(); 1690 1691 } 1692 catch(SQLException e) { 1693System.out.println(e.getErrorCode()); 1694 e.printStackTrace(); 1695 } 1696 1697 } 1698*/ 1699 1700} // class TestPersist 1701 1702 1703class Dummy implements Serializable { 1704 1705 static final long serialVersionUID = 3632609241787241900L; 1706 1707 public int intValue; 1708 public String stringValue; 1709 public boolean boolValue; 1710 public float floatValue; 1711 1712 1713 public Dummy(int _int, String _string, boolean _bool, float _float) { 1714 1715 this.intValue = _int; 1716 this.stringValue= _string; 1717 this.boolValue = _bool; 1718 this.floatValue = _float; 1719 } 1720 1721 public boolean equals(Object obj) { 1722 Dummy d2 = (Dummy)obj; 1723 1724 return this.intValue == d2.intValue && 1725 this.stringValue.equals(d2.stringValue) && 1726 this.boolValue == d2.boolValue && 1727 this.floatValue == d2.floatValue; 1728 } 1729 1730 public String toString() { 1731 return "Dummy: intV=["+this.intValue+"], stringV=["+this.stringValue+"], boolV=["+this.boolValue+"], floatV = ["+this.floatValue+"]"; 1732 } 1733 1734} 1735
|
TestPersist |
|