|
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.75 2002/03/06 17:15:47 kalina 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 25 import gate.*; 26 import gate.util.*; 27 import gate.corpora.*; 28 import gate.security.*; 29 30 /** Persistence test class 31 */ 32 public class TestPersist extends TestCase 33 { 34 private static String JDBC_URL; 35 36 /** Debug flag */ 37 private static final boolean DEBUG = false; 38 private static Long sampleDoc_lrID = null; 39 private static Long sampleCorpus_lrID = null; 40 private static Document sampleDoc = null; 41 private static Corpus sampleCorpus = null; 42 43 private final String VERY_LONG_STRING = 44 "The memory of Father came back to her. Ever since she had seen him retreat from those "+ 45 "twelve-year-old boys she often imagined him in this situation: he is on a sinking ship; "+ 46 "there are only a few lifeboats and there isn't enough room for everyone; there is a "+ 47 "furious stampede on the deck. At first Father rushes along with the others, but when he "+ 48 "sees how they push and shove, ready to trample each other under foot, and a wild-eyed "+ 49 "woman strikes him with her fist because he is in her way, he suddenly stops and steps "+ 50 "aside. And in the end he merely watches the overloaded lifeboats as they are slowly "+ 51 "lowered amid shouts and curses, towards the raging waves. "+ 52 "What name to give to that attitude? Cowardice? No. Cowards are afraid of dying and will "+ 53 "fight to survive. Nobility? Undoubtedly, if he had acted out of regard for his fellows. "+ 54 "But Agnes did not believe this was the motive. What was it then? She couldn't say. Only "+ 55 "one thing seemed certain: on a sinking ship where it was necessary to fight in order to "+ 56 "boat a lifeboat, Father would have been condemned in advance. "+ 57 "Yes, that much was certain. The question that arises is htis: had Father hated the people "+ 58 "on the ship, just as she now hates the motorcyclist and the man who mocked her because "+ 59 "she covered her ears? No, Agnes cannot imagine that Father was capable of hatred. "+ 60 "Hate traps us by binding us too tightly to our adversary. This is the obscenity of war: "+ 61 "the intimacy of mutually shed blood, the lascivious of two soldiers who, eye to eye, "+ 62 "bayonet each other. Agnes was sure: it is precisely this kind of intimacy that her "+ 63 "father found repugnant.The melee on the ship filled him with such disgust that he "+ 64 "preferred to drown. The physical contact with people who struck and trampled and killed "+ 65 "one another seemed far worse to him than a solitary death in the purity of the waters. "+ 66 "[p.25-26] "+ 67 "In our world, where there are more and more faces, more and more alike, it is difficult "+ 68 "for an individual to reinforce the originality of the self and to become convinced of "+ 69 "its inimitatable uniqueness. There are two methods for cultivating the uniqueness of the "+ 70 "self: the method of addition and the method of subtraction. Agnes subtracts from herself "+ 71 "everything that is exterior and borrowed, in order to come closer to her sheer essence "+ 72 "(even with the risk that zero lurks at the bottom of subtarction). Laura's method is "+ 73 "precisely the opposite: in order to make herself even more visible, perceivable, seizable, "+ 74 "sizeable, she keeps adding to it more and more attributes and she attemptes to identify "+ 75 "herself with them (with the risk that the essence of the self may be buried by the "+ 76 "additional attributes). "+ 77 "Let's take her car as an example. After her divorce, Laura remained alone in a large "+ 78 "apartment and felt lonely. She longed for a pet to share her solitude. First she thought "+ 79 "of a dog, but soon realized that a dog needed a kind of care she would be unable to "+ 80 "provide. And she got a cat. It was a big Siamese cat, beautiful and wicked. As she "+ 81 "lived with her car and regaled her friends with stories about it, the animal that she "+ 82 "picked more or less by accident, without any special conviction (after all, her first "+ 83 "choice was a dog!), took on an evr growing significance: she began to lavish praise "+ 84 "on her pet and forced everyone to admire it. She saw in the cat a superb independence, "+ 85 "pride, freedon of action and constancy of charm (so different from human charm, which "+ 86 "is always spoiled by moments of clumsiness and unattractiveness); in the cat, she saw "+ 87 "her paradigm; in the cat she saw herself. "+ 88 " "+ 89 "The method of addition is quite charming if it involves adding to the self such "+ 90 "things as a cat, a dog, roast pork, love of the sea or of cold showers. But the matter "+ 91 "becomes less idyllic if a person decides to add love for communism, for the homeland, "+ 92 "for Mussolini, for Roman Catolicism or atheism, for facism or antifacism. In both cases "+ 93 "the method remains exactly the same: a person stubbornly defending the superiority of "+ 94 "cats over other animals is doing basically the same as the one who maintains that "+ 95 "Mussolini was the sole saviour of Italy: he is proud of this attribute of the self and "+ 96 "he tries to make this attribute (a cat or Mussolini) acknowledged or loved by everyone. "+ 97 "Here is that strange paradox to which all people cultivating the self by way of the "+ 98 "addition method are subject: they use addition in order to create unique, inimitable "+ 99 "self, yet because they automatically become prpagandists for the added attributes, they are "+ 100 "actually doing everything in their power to make as many others as possible similar "+ 101 "to themselves; as a result, their uniqueness (so painfully gained) quickly begins to disappear. "+ 102 "We may ask ourselves why a person who loves a cat (or Mussolini) is not satisfied to "+ 103 "keep his love to himself, and wants to force it on others. Let's seek the answer by "+ 104 "recalling the young woman in the sauna, who belligerently asserted that she loved "+ 105 "cold showers. She thereby managed to differentiate herself at once from one half of the "+ 106 "human race, namely the half that prefers hot showers. Unfortunately, that other half "+ 107 "resembled her all the more. Alas, how sad! Many people, few ideas, so how are we "+ 108 "differentiate ourselves from each other? The young woman knew only one way of overcoming "+ 109 "the disadvantage of her similarity to that enourmous throng devoted to cold showers: "+ 110 "she had to proclaim her credo \"I adore cold showers!\" as soon as she appeared in the "+ 111 "door of the sauna and to proclaim it with such fervour as to make the millions of other "+ 112 "women who also enjoy cold showers seem like pale imitations of herself. Let me put it "+ 113 "another way: a mere (simple and innocent) love for showers can become an attribute "+ 114 "of the self only on condition that we let the world we are ready to fight for it. "+ 115 "The one who chooses as an atrtibyte of the self a love for Mussolini becomes a "+ 116 "political warrior, while the partisan of cats, music or antique furniture bestows "+ 117 "gifts on his surroundings. "+ 118 "[p.111-113]"; 119 120 /** Construction */ 121 public TestPersist(String name) throws GateException { super(name); } 122 123 /** Fixture set up */ 124 public void setUp() throws Exception { 125 if (! Gate.getDataStoreRegister().getConfigData().containsKey("url-test")) 126 throw new GateRuntimeException("DB URL not configured in gate.xml"); 127 else 128 JDBC_URL = 129 (String) Gate.getDataStoreRegister().getConfigData().get("url-test"); 130 } // setUp 131 132 /** Put things back as they should be after running tests 133 * (reinitialise the CREOLE register). 134 */ 135 public void tearDown() throws Exception { 136 } // tearDown 137 138 /** Test resource save and restore */ 139 public void testSaveRestore() throws Exception { 140 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 141 storageDir.delete(); // get rid of the temp file 142 storageDir.mkdir(); // create an empty dir of same name 143 144 SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString()); 145 sds.create(); 146 sds.open(); 147 148 // create a document 149 String server = TestDocument.getTestServerName(); 150 assertNotNull(server); 151 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 152 assertNotNull(doc); 153 doc.getFeatures().put("hi there", new Integer(23232)); 154 doc.getAnnotations().add( 155 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap() 156 ); 157 158 // check that we can't save a resource without adopting it 159 boolean cannotSync = false; 160 try { sds.sync(doc); } catch(PersistenceException e) { cannotSync=true; } 161 if(! cannotSync) assertTrue("doc synced ok before adoption", false); 162 163 // check that we can't adopt a resource that's stored somewhere else 164 doc.setDataStore(new SerialDataStore(new File("z:\\").toURL().toString())); 165 try { sds.adopt(doc,null); } catch(PersistenceException e) { cannotSync=true; } 166 if(! cannotSync) 167 assertTrue("doc adopted but in other datastore already", false); 168 doc.setDataStore(null); 169 doc.setName("Alicia Tonbridge, a Document"); 170 171 // save the document 172 Document persDoc = (Document) sds.adopt(doc,null); 173 sds.sync(persDoc); 174 Object lrPersistenceId = persDoc.getLRPersistenceId(); 175 176 // test the getLrTypes method 177 List lrTypes = sds.getLrTypes(); 178 assertTrue("wrong number of types in SDS", lrTypes.size() == 1); 179 assertTrue( 180 "wrong type LR in SDS", 181 lrTypes.get(0).equals("gate.corpora.DocumentImpl") 182 ); 183 184 // test the getLrNames method 185 Iterator iter = sds.getLrNames("gate.corpora.DocumentImpl").iterator(); 186 String name = (String) iter.next(); 187 assertEquals(name, "Alicia Tonbridge, a Document"); 188 189 // read the document back 190 FeatureMap features = Factory.newFeatureMap(); 191 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId); 192 features.put(DataStore.DATASTORE_FEATURE_NAME, sds); 193 Document doc2 = 194 (Document) Factory.createResource("gate.corpora.DocumentImpl", features); 195 Document doc3 = 196 (Document) sds.getLr("gate.corpora.DocumentImpl", lrPersistenceId); 197 198 //clear the parameters value from features as they will be different 199 200 assertTrue(doc3.equals(doc2)); 201 assertTrue(persDoc.equals(doc2)); 202 203 // delete the datastore 204 sds.delete(); 205 } // testSaveRestore() 206 207 /** Simple test */ 208 public void testSimple() throws Exception { 209 // create a temporary directory; because File.createTempFile actually 210 // writes the bloody thing, we need to delete it from disk before calling 211 // DataStore.create 212 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 213 storageDir.delete(); 214 215 // create and open a serial data store 216 DataStore sds = Factory.createDataStore( 217 "gate.persist.SerialDataStore", storageDir.toURL().toString() 218 ); 219 220 // check we can get empty lists from empty data stores 221 List lrTypes = sds.getLrTypes(); 222 223 // create a document with some annotations / features on it 224 String server = TestDocument.getTestServerName(); 225 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 226 doc.getFeatures().put("hi there", new Integer(23232)); 227 doc.getAnnotations().add( 228 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 229 ); 230 231 // save the document 232 Document persDoc = (Document) sds.adopt(doc,null); 233 sds.sync(persDoc); 234 235 // remember the persistence ID for reading back 236 // (in the normal case these ids are obtained by DataStore.getLrIds(type)) 237 Object lrPersistenceId = persDoc.getLRPersistenceId(); 238 239 // read the document back 240 FeatureMap features = Factory.newFeatureMap(); 241 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId); 242 features.put(DataStore.DATASTORE_FEATURE_NAME, sds); 243 Document doc2 = 244 (Document) Factory.createResource("gate.corpora.DocumentImpl", features); 245 246 //parameters should be different 247 // check that the version we read back matches the original 248 assertTrue(persDoc.equals(doc2)); 249 250 // delete the datastore 251 sds.delete(); 252 } // testSimple() 253 254 /** Test multiple LRs */ 255 public void testMultipleLrs() throws Exception { 256 // create a temporary directory; because File.createTempFile actually 257 // writes the bloody thing, we need to delete it from disk before calling 258 // DataStore.create 259 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 260 storageDir.delete(); 261 262 // create and open a serial data store 263 SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString()); 264 sds.create(); 265 sds.open(); 266 267 // create a document with some annotations / features on it 268 String server = TestDocument.getTestServerName(); 269 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 270 doc.getFeatures().put("hi there", new Integer(23232)); 271 doc.getAnnotations().add( 272 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 273 ); 274 275 // create another document with some annotations / features on it 276 Document doc2 = 277 Factory.newDocument(new URL(server + "tests/html/test1.htm")); 278 doc.getFeatures().put("hi there again", new Integer(23232)); 279 doc.getAnnotations().add( 280 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap() 281 ); 282 283 // create a corpus with the documents 284 Corpus corp = Factory.newCorpus("Hamish test corpus"); 285 corp.add(doc); 286 corp.add(doc2); 287 LanguageResource persCorpus = sds.adopt(corp,null); 288 sds.sync(persCorpus); 289 290 291 // read the documents back 292 ArrayList lrsFromDisk = new ArrayList(); 293 List lrIds = sds.getLrIds("gate.corpora.SerialCorpusImpl"); 294 295 Iterator idsIter = lrIds.iterator(); 296 while(idsIter.hasNext()) { 297 String lrId = (String) idsIter.next(); 298 FeatureMap features = Factory.newFeatureMap(); 299 features.put(DataStore.DATASTORE_FEATURE_NAME, sds); 300 features.put(DataStore.LR_ID_FEATURE_NAME, lrId); 301 Resource lr = Factory.createResource( "gate.corpora.SerialCorpusImpl", 302 features); 303 lrsFromDisk.add(lr); 304 } // for each LR ID 305 306 if (DEBUG) System.out.println("LRs on disk" + lrsFromDisk); 307 308 // check that the versions we read back match the originals 309 Corpus diskCorp = (Corpus) lrsFromDisk.get(0); 310 311 Document diskDoc = (Document) diskCorp.get(0); 312 313 if (DEBUG) Out.prln("Documents in corpus: " + corp.getDocumentNames()); 314 assertTrue("corp name != mem name", corp.getName().equals(diskCorp.getName())); 315 if (DEBUG) Out.prln("Memory features " + corp.getFeatures()); 316 if (DEBUG) Out.prln("Disk features " + diskCorp.getFeatures()); 317 assertTrue("corp feat != mem feat", 318 corp.getFeatures().equals(diskCorp.getFeatures())); 319 if (DEBUG) 320 Out.prln("Annotations in doc: " + diskDoc.getAnnotations()); 321 assertTrue("doc annotations from disk not equal to memory version", 322 doc.getAnnotations().equals(diskDoc.getAnnotations())); 323 assertTrue("doc from disk not equal to memory version", 324 doc.equals(diskDoc)); 325 326 Iterator corpusIter = diskCorp.iterator(); 327 while(corpusIter.hasNext()){ 328 if (DEBUG) 329 Out.prln(((Document) corpusIter.next()).getName()); 330 else 331 corpusIter.next(); 332 } 333 334 335 // assertTrue("doc2 from disk not equal to memory version", doc2.equals(diskDoc2)); 336 337 // delete the datastore 338 sds.delete(); 339 } // testMultipleLrs() 340 341 /** Test LR deletion */ 342 public void testDelete() throws Exception { 343 // create a temporary directory; because File.createTempFile actually 344 // writes the bloody thing, we need to delete it from disk before calling 345 // DataStore.create 346 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 347 if (DEBUG) Out.prln("Corpus stored to: " + storageDir.getAbsolutePath()); 348 storageDir.delete(); 349 350 // create and open a serial data store 351 SerialDataStore sds = new SerialDataStore(); 352 sds.setStorageUrl(storageDir.toURL().toString()); 353 sds.create(); 354 sds.open(); 355 356 // create a document with some annotations / features on it 357 String server = TestDocument.getTestServerName(); 358 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 359 doc.getFeatures().put("hi there", new Integer(23232)); 360 doc.getAnnotations().add( 361 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 362 ); 363 364 // save the document 365 Document persDoc = (Document) sds.adopt(doc,null); 366 sds.sync(persDoc); 367 368 // remember the persistence ID for reading back 369 // (in the normal case these ids are obtained by DataStore.getLrIds(type)) 370 Object lrPersistenceId = persDoc.getLRPersistenceId(); 371 372 // delete document back 373 sds.delete("gate.corpora.DocumentImpl", lrPersistenceId); 374 375 // check that there are no LRs left in the DS 376 assertTrue(sds.getLrIds("gate.corpora.DocumentImpl").size() == 0); 377 378 // delete the datastore 379 sds.delete(); 380 } // testDelete() 381 382 383 384 385 /** Test the DS register. */ 386 public void testDSR() throws Exception { 387 DataStoreRegister dsr = Gate.getDataStoreRegister(); 388 assertTrue("DSR has wrong number elements (not 0): " + dsr.size(), 389 dsr.size() == 0); 390 391 // create a temporary directory; because File.createTempFile actually 392 // writes the bloody thing, we need to delete it from disk before calling 393 // DataStore.create 394 File storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 395 storageDir.delete(); 396 397 // create and open a serial data store 398 DataStore sds = Factory.createDataStore( 399 "gate.persist.SerialDataStore", storageDir.toURL().toString() 400 ); 401 402 // create a document with some annotations / features on it 403 String server = TestDocument.getTestServerName(); 404 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 405 doc.getFeatures().put("hi there", new Integer(23232)); 406 doc.getAnnotations().add( 407 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap() 408 ); 409 410 // save the document 411 Document persDoc = (Document) sds.adopt(doc,null); 412 sds.sync(persDoc); 413 414 // DSR should have one member 415 assertTrue("DSR has wrong number elements", dsr.size() == 1); 416 417 // create and open another serial data store 418 storageDir = File.createTempFile("TestPersist__", "__StorageDir"); 419 storageDir.delete(); 420 DataStore sds2 = Factory.createDataStore( 421 "gate.persist.SerialDataStore", storageDir.toURL().toString() 422 ); 423 424 // DSR should have two members 425 assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 2); 426 427 // peek at the DSR members 428 Iterator dsrIter = dsr.iterator(); 429 while(dsrIter.hasNext()) { 430 DataStore ds = (DataStore) dsrIter.next(); 431 assertNotNull("null ds in ds reg", ds); 432 if(DEBUG) 433 Out.prln(ds); 434 } 435 436 // delete the datastores 437 sds.close(); 438 assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 1); 439 sds.delete(); 440 assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 1); 441 sds2.delete(); 442 assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 0); 443 444 } // testDSR() 445 446 447 448 /** Test suite routine for the test runner */ 449 public static Test suite() { 450 return new TestSuite(TestPersist.class); 451 } // suite 452 453 454 private Document createTestDocument() 455 throws Exception { 456 457 String server = TestDocument.getTestServerName(); 458 assertNotNull(server); 459 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html")); 460 assertNotNull(doc); 461 462 doc.getFeatures().put("hi there", new Integer(23232)); 463 doc.getFeatures().put("LONG STRING", this.VERY_LONG_STRING); 464 doc.getFeatures().put("NULL feature",null); 465 doc.getFeatures().put("BINARY feature",new Dummy(101,"101",true,101.101f)); 466 467 //create a complex feature - array of strings 468 Vector complexFeature = new Vector(); 469 complexFeature.add("string 1"); 470 complexFeature.add("string 2"); 471 complexFeature.add("string 3"); 472 complexFeature.add("string 4"); 473 complexFeature.add("string 5"); 474 doc.getFeatures().put("complex feature",complexFeature); 475 476 doc.getAnnotations().add( 477 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap() 478 ); 479 doc.setName("DB test Document"); 480 481 return doc; 482 } 483 484 485 private Corpus createTestCorpus() 486 throws Exception { 487 488 String server = TestDocument.getTestServerName(); 489 assertNotNull(server); 490 Document doc1 = Factory.newDocument(new URL(server + "tests/doc0.html")); 491 assertNotNull(doc1); 492 493 doc1.getFeatures().put("hi there", new Integer(23232)); 494 doc1.getAnnotations().add( 495 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap() 496 ); 497 doc1.setName("DB test Document1"); 498 499 // create another document with some annotations / features on it 500 Document doc2 = 501 Factory.newDocument(new URL(server + "tests/html/test1.htm")); 502 doc2.getFeatures().put("hi there again", new Integer(23232)); 503 doc2.getAnnotations().add( 504 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap() 505 ); 506 doc2.setName("DB test Document2"); 507 508 //create corpus 509 Corpus corp = Factory.newCorpus("My test corpus"); 510 //add docs 511 corp.add(doc1); 512 corp.add(doc2); 513 //add features 514 corp.getFeatures().put("my STRING feature ", new String("string string")); 515 corp.getFeatures().put("my BOOL feature ", new Boolean("false")); 516 corp.getFeatures().put("my INT feature ", new Integer("1234")); 517 corp.getFeatures().put("my LONG feature ", new Long("123456789")); 518 corp.getFeatures().put("my LONG STRING feature", this.VERY_LONG_STRING); 519 corp.getFeatures().put("my NULL feature", null); 520 corp.getFeatures().put("my BINARY feature",new Dummy(101,"101",true,101.101f)); 521 return corp; 522 } 523 524 /** Test the DS register. */ 525 public void testDB_UseCase01() throws Exception { 526 ///Err.prln("Use case 01 started..."); 527 //descr: create a document in the DB 528 529 530 //1. open data storage 531 DatabaseDataStore ds = new OracleDataStore(); 532 Assert.assertNotNull(ds); 533 ds.setStorageUrl(this.JDBC_URL); 534 ds.open(); 535 536 //2. get test document 537 Document doc = createTestDocument(); 538 Assert.assertNotNull(doc); 539 540 //3. get security factory & login 541 AccessController ac = Factory.createAccessController(this.JDBC_URL); 542 ac.open(); 543 Assert.assertNotNull(ac); 544 545 User usr = ac.findUser("kalina"); 546 Assert.assertNotNull(usr); 547 548 Group grp = (Group)usr.getGroups().get(0); 549 Assert.assertNotNull(grp); 550 551 Session usrSession = ac.login("kalina","sesame",grp.getID()); 552 Assert.assertNotNull(usrSession); 553 Assert.assertTrue(ac.isValidSession(usrSession)); 554 555 //4. create security settings for doc 556 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp); 557 558 //4.5 set DS session 559 ds.setSession(usrSession); 560 561 //5. try adding doc to data store 562 LanguageResource lr = ds.adopt(doc,si); 563 564 Assert.assertTrue(lr instanceof DatabaseDocumentImpl); 565 Assert.assertNotNull(lr.getDataStore()); 566 Assert.assertTrue(lr.getDataStore() instanceof DatabaseDataStore); 567 568 sampleDoc_lrID = (Long)lr.getLRPersistenceId(); 569 if (DEBUG) Out.prln("lr id: " + this.sampleDoc_lrID); 570 // this.sampleDoc = lr; 571 sampleDoc = doc; 572 //System.out.println("adopted doc:name=["+((Document)lr).getName()+"], lr_id=["+((Document)lr).getLRPersistenceId()+"]"); 573 //6.close 574 ac.close(); 575 ds.close(); 576 577 if(DEBUG) { 578 Err.prln("Use case 01 passed..."); 579 } 580 } 581 582 583 public void testDB_UseCase02() throws Exception { 584 ///Err.prln("Use case 02 started..."); 585 //read a document 586 //use the one created in UC01 587 LanguageResource lr = null; 588 589 //1. open data storage 590 DatabaseDataStore ds = new OracleDataStore(); 591 Assert.assertNotNull(ds); 592 ds.setStorageUrl(this.JDBC_URL); 593 ds.open(); 594 595 //3. get security factory & login 596 AccessController ac = Factory.createAccessController(this.JDBC_URL); 597 Assert.assertNotNull(ac); 598 ac.open(); 599 600 User usr = ac.findUser("kalina"); 601 Assert.assertNotNull(usr); 602 603 Group grp = (Group)usr.getGroups().get(0); 604 Assert.assertNotNull(grp); 605 606 Session usrSession = ac.login("kalina","sesame",grp.getID()); 607 Assert.assertNotNull(usrSession); 608 Assert.assertTrue(ac.isValidSession(usrSession)); 609 610 //4. create security settings for doc 611 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp); 612 613 //4.5 set DS session 614 ds.setSession(usrSession); 615 616 //2. read LR 617 ///Err.println(">>>"); 618 FeatureMap params = Factory.newFeatureMap(); 619 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 620 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 621 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 622 ///Err.println("<<<"); 623 //3. check name 624 String name = lr.getName(); 625 Assert.assertNotNull(name); 626 Assert.assertEquals(name,sampleDoc.getName()); 627 628 //4. check features 629 FeatureMap fm = lr.getFeatures(); 630 FeatureMap fmOrig = sampleDoc.getFeatures(); 631 632 Assert.assertNotNull(fm); 633 Assert.assertNotNull(fmOrig); 634 Assert.assertTrue(fm.size() == fmOrig.size()); 635 636 Iterator keys = fm.keySet().iterator(); 637 638 while (keys.hasNext()) { 639 String currKey = (String)keys.next(); 640 Assert.assertTrue(fmOrig.containsKey(currKey)); 641 Assert.assertEquals(fm.get(currKey),fmOrig.get(currKey)); 642 } 643 644 //6. URL 645 DatabaseDocumentImpl dbDoc = (DatabaseDocumentImpl)lr; 646 Assert.assertEquals(dbDoc.getSourceUrl(),this.sampleDoc.getSourceUrl()); 647 648 //5.start/end 649 Assert.assertEquals(dbDoc.getSourceUrlStartOffset(),this.sampleDoc.getSourceUrlStartOffset()); 650 Assert.assertEquals(dbDoc.getSourceUrlEndOffset(),this.sampleDoc.getSourceUrlEndOffset()); 651 652 //6.markupAware 653 Assert.assertEquals(dbDoc.getMarkupAware(),this.sampleDoc.getMarkupAware()); 654 655 //7. content 656 DocumentContent cont = dbDoc.getContent(); 657 Assert.assertEquals(cont,this.sampleDoc.getContent()); 658 659 //8. access the content again and assure it's not read from the DB twice 660 Assert.assertEquals(cont,((Document)this.sampleDoc).getContent()); 661 662 //9. encoding 663 String encNew = (String)dbDoc. 664 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 665 String encOld = (String)((DocumentImpl)this.sampleDoc). 666 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 667 Assert.assertEquals(encNew,encOld); 668 669 //10. default annotations 670 ///System.out.println("GETTING default ANNOTATIONS..."); 671 AnnotationSet defaultNew = dbDoc.getAnnotations(); 672 AnnotationSet defaultOld = this.sampleDoc.getAnnotations(); 673 674 Assert.assertNotNull(defaultNew); 675 Assert.assertTrue(defaultNew.size() == defaultOld.size()); 676 Assert.assertEquals(defaultNew,defaultOld); 677 678 679 //10. iterate named annotations 680 Map namedOld = this.sampleDoc.getNamedAnnotationSets(); 681 Iterator itOld = namedOld.keySet().iterator(); 682 ///System.out.println("GETTING named ANNOTATIONS..."); 683 while (itOld.hasNext()) { 684 String asetName = (String)itOld.next(); 685 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName); 686 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName); 687 Assert.assertNotNull(asetNew); 688 ///System.out.println("aset_old, size=["+asetOld.size()+"]"); 689 ///System.out.println("aset_new, size=["+asetNew.size()+"]"); 690 ///System.out.println("old = >>" + asetOld +"<<"); 691 ///System.out.println("new = >>" + asetNew +"<<"); 692 Assert.assertTrue(asetNew.size() == asetOld.size()); 693 Assert.assertEquals(asetNew,asetOld); 694 } 695 696 697 //11. ALL named annotation (ensure nothing is read from DB twice) 698 Map namedNew = dbDoc.getNamedAnnotationSets(); 699 700 Assert.assertNotNull(namedNew); 701 Assert.assertTrue(namedNew.size() == namedOld.size()); 702 703 Iterator itNames = namedNew.keySet().iterator(); 704 while (itNames.hasNext()) { 705 String asetName = (String)itNames.next(); 706 AnnotationSet asetNew = (AnnotationSet)namedNew.get(asetName); 707 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName); 708 Assert.assertNotNull(asetNew); 709 Assert.assertNotNull(asetOld); 710 Assert.assertEquals(asetNew,asetOld); 711 } 712 713 //close 714 ds.close(); 715 ac.close(); 716 717 if(DEBUG) { 718 Err.prln("Use case 02 passed..."); 719 } 720 721 } 722 723 724 725 public void testDB_UseCase03() throws Exception { 726 ///Err.prln("Use case 03 started..."); 727 //sync a document 728 LanguageResource lr = null; 729 730 //0. get security factory & login 731 AccessController ac = Factory.createAccessController(this.JDBC_URL); 732 Assert.assertNotNull(ac); 733 ac.open(); 734 735 User usr = ac.findUser("kalina"); 736 Assert.assertNotNull(usr); 737 738 Group grp = (Group)usr.getGroups().get(0); 739 Assert.assertNotNull(grp); 740 741 Session usrSession = ac.login("kalina","sesame",grp.getID()); 742 Assert.assertNotNull(usrSession); 743 Assert.assertTrue(ac.isValidSession(usrSession)); 744 745 //1. open data storage 746 DatabaseDataStore ds = new OracleDataStore(); 747 Assert.assertNotNull(ds); 748 ds.setStorageUrl(this.JDBC_URL); 749 ds.open(); 750 751 //1.5 set DS session 752 ds.setSession(usrSession); 753 754 if (DEBUG) Out.prln("ID " + sampleDoc_lrID); 755 //2. read LR 756 FeatureMap params = Factory.newFeatureMap(); 757 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 758 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 759 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 760 Document dbDoc = (Document)lr; 761 Document doc2 = null; 762 763 //2.5 get exclusive lock 764 if (false == ds.lockLr(lr)) { 765 throw new PersistenceException("document is locked by another user"); 766 } 767 768 //3. change name 769 String oldName = dbDoc.getName(); 770 String newName = oldName + "__UPD"; 771 dbDoc.setName(newName); 772 dbDoc.sync(); 773 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 774 Assert.assertEquals(newName,dbDoc.getName()); 775 Assert.assertEquals(newName,doc2.getName()); 776 777 //4. change features 778 FeatureMap fm = dbDoc.getFeatures(); 779 Iterator keys = fm.keySet().iterator(); 780 781 //4.1 change the value of the first feature 782 while(keys.hasNext()) { 783 String currKey = (String)keys.next(); 784 Object val = fm.get(currKey); 785 Object newVal = null; 786 if (val instanceof Long) { 787 newVal = new Long(101010101); 788 } 789 else if (val instanceof Integer) { 790 newVal = new Integer(2121212); 791 } 792 else if (val instanceof String) { 793 newVal = new String("UPD__").concat( (String)val).concat("__UPD"); 794 } 795 if (newVal != null) 796 fm.put(currKey,newVal); 797 } 798 dbDoc.sync(); 799 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 800 Assert.assertEquals(fm,dbDoc.getFeatures()); 801 Assert.assertEquals(fm,doc2.getFeatures()); 802 803 //6. URL 804 URL docURL = dbDoc.getSourceUrl(); 805 URL newURL = null; 806 newURL = new URL(docURL.toString()+".UPDATED"); 807 dbDoc.setSourceUrl(newURL); 808 dbDoc.sync(); 809 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 810 Assert.assertEquals(newURL,dbDoc.getSourceUrl()); 811 Assert.assertEquals(newURL,doc2.getSourceUrl()); 812 813 //5.start/end 814 Long newStart = new Long(123); 815 Long newEnd = new Long(789); 816 dbDoc.setSourceUrlStartOffset(newStart); 817 dbDoc.setSourceUrlEndOffset(newEnd); 818 dbDoc.sync(); 819 820 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 821 Assert.assertEquals(newStart,dbDoc.getSourceUrlStartOffset()); 822 Assert.assertEquals(newStart,doc2.getSourceUrlStartOffset()); 823 Assert.assertEquals(newEnd,dbDoc.getSourceUrlEndOffset()); 824 Assert.assertEquals(newEnd,doc2.getSourceUrlEndOffset()); 825 826 827 //6.markupAware 828 Boolean oldMA = dbDoc.getMarkupAware(); 829 Boolean newMA = oldMA.booleanValue() ? Boolean.FALSE : Boolean.TRUE; 830 dbDoc.setMarkupAware(newMA); 831 dbDoc.sync(); 832 833 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 834 Assert.assertEquals(newMA,doc2.getMarkupAware()); 835 Assert.assertEquals(newMA,dbDoc.getMarkupAware()); 836 837 838 //7. content 839 DocumentContent contOld = dbDoc.getContent(); 840 DocumentContent contNew = new DocumentContentImpl(new String("UPDATED__").concat(contOld.toString().concat("__UPDATED"))); 841 dbDoc.setContent(contNew); 842 dbDoc.sync(); 843 844 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 845 Assert.assertEquals(contNew,dbDoc.getContent()); 846 Assert.assertEquals(contNew,doc2.getContent()); 847 848 //8. encoding 849 String encOld = (String)dbDoc. 850 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 851 dbDoc.setParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME,"XXX"); 852 dbDoc.sync(); 853 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 854 String encNew = (String)doc2. 855 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME); 856 Assert.assertEquals(encNew,encOld); 857 858 859 //9. add annotations 860 AnnotationSet dbDocSet = dbDoc.getAnnotations("TEST SET"); 861 Assert.assertNotNull(dbDocSet); 862 863 FeatureMap fm1 = new SimpleFeatureMapImpl(); 864 fm.put("string key","string value"); 865 866 Integer annInd = dbDocSet.add(new Long(0), new Long(10), 867 "TEST TYPE", 868 fm1); 869 870 dbDoc.sync(); 871 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 872 AnnotationSet doc2Set = doc2.getAnnotations("TEST SET"); 873 Assert.assertTrue(dbDocSet.size() == doc2Set.size()); 874 Assert.assertEquals(doc2Set,dbDocSet); 875 876 877 //9.1. change+add annotations 878 Annotation dbDocAnn = dbDocSet.get(annInd); 879 880 Integer newInd = dbDocSet.add(dbDocAnn.getStartNode().getOffset(), 881 dbDocAnn.getEndNode().getOffset(), 882 dbDocAnn.getType() + "__XX", 883 new SimpleFeatureMapImpl()); 884 Annotation dbDocAnnNew = dbDocSet.get(newInd); 885 dbDoc.sync(); 886 887 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 888 doc2Set = doc2.getAnnotations("TEST SET"); 889 Assert.assertTrue(dbDocSet.size() == doc2Set.size()); 890 Assert.assertEquals(doc2Set,dbDocSet); 891 Assert.assertTrue(doc2Set.contains(dbDocAnnNew)); 892 /* 893 //10. iterate named annotations 894 Map namedOld = ((DocumentImpl)this.uc01_LR).getNamedAnnotationSets(); 895 Iterator itOld = namedOld.keySet().iterator(); 896 while (itOld.hasNext()) { 897 String asetName = (String)itOld.next(); 898 AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName); 899 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName); 900 Assert.assertNotNull(asetNew); 901 Assert.assertEquals(asetNew,asetOld); 902 // Features fmNew = asetNew.getFea 903 } 904 */ 905 906 //11. add a new ann-set 907 String dummySetName = "--NO--SUCH--SET--"; 908 AnnotationSet aset = dbDoc.getAnnotations(dummySetName); 909 aset.addAll(dbDoc.getAnnotations()); 910 dbDoc.sync(); 911 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 912 913 Assert.assertTrue(dbDoc.getNamedAnnotationSets().size() == doc2.getNamedAnnotationSets().size()); 914 Assert.assertTrue(doc2.getNamedAnnotationSets().containsKey(dummySetName)); 915 Assert.assertTrue(doc2.getNamedAnnotationSets().equals(dbDoc.getNamedAnnotationSets())); 916 917 //12. remove aset 918 dbDoc.removeAnnotationSet(dummySetName); 919 dbDoc.sync(); 920 Assert.assertTrue(false == ((EventAwareDocument)dbDoc).getLoadedAnnotationSets().contains(dummySetName)); 921 Assert.assertTrue(false == dbDoc.getNamedAnnotationSets().containsKey(dummySetName)); 922 923 doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID); 924 Assert.assertTrue(false == doc2.getNamedAnnotationSets().containsKey(dummySetName)); 925 926 //13. unlock 927 ds.unlockLr(lr); 928 929 //close 930 ac.close(); 931 ds.close(); 932 933 if(DEBUG) { 934 Err.prln("Use case 03 passed..."); 935 } 936 } 937 938 939 public void testDB_UseCase04() throws Exception { 940 ///Err.prln("Use case 04 started..."); 941 //delete a document 942 LanguageResource lr = null; 943 944 //0. get security factory & login 945 AccessController ac = Factory.createAccessController(this.JDBC_URL); 946 Assert.assertNotNull(ac); 947 ac.open(); 948 949 User usr = ac.findUser("kalina"); 950 Assert.assertNotNull(usr); 951 952 Group grp = (Group)usr.getGroups().get(0); 953 Assert.assertNotNull(grp); 954 955 Session usrSession = ac.login("kalina","sesame",grp.getID()); 956 Assert.assertNotNull(usrSession); 957 Assert.assertTrue(ac.isValidSession(usrSession)); 958 959 //1. open data storage 960 DatabaseDataStore ds = new OracleDataStore(); 961 Assert.assertNotNull(ds); 962 ds.setStorageUrl(this.JDBC_URL); 963 ds.open(); 964 ds.setSession(usrSession); 965 966 //2. read LR 967 FeatureMap params = Factory.newFeatureMap(); 968 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 969 params.put(DataStore.LR_ID_FEATURE_NAME, this.sampleDoc_lrID); 970 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params); 971 972 //2.5 get exclusive lock 973 if (false == ds.lockLr(lr)) { 974 throw new PersistenceException("document is locked by another user"); 975 } 976 977 //3. try to delete it 978 ds.delete(DBHelper.DOCUMENT_CLASS,lr.getLRPersistenceId()); 979 980 //no need to unlock 981 982 //close 983 ds.close(); 984 ac.close(); 985 986 if(DEBUG) { 987 Err.prln("Use case 04 passed..."); 988 } 989 990 } 991 992 993 /** Test the DS register. */ 994 public void testDB_UseCase101() throws Exception { 995 ///Err.prln("Use case 101 started..."); 996 //descr : create a corpus 997 998 //0. get security factory & login 999 AccessController ac = Factory.createAccessController(this.JDBC_URL); 1000 Assert.assertNotNull(ac); 1001 ac.open(); 1002 1003 User usr = ac.findUser("kalina"); 1004 Assert.assertNotNull(usr); 1005 1006 Group grp = (Group)usr.getGroups().get(0); 1007 Assert.assertNotNull(grp); 1008 1009 Session usrSession = ac.login("kalina","sesame",grp.getID()); 1010 Assert.assertNotNull(usrSession); 1011 Assert.assertTrue(ac.isValidSession(usrSession)); 1012 1013 //1. open data storage 1014 DatabaseDataStore ds = new OracleDataStore(); 1015 Assert.assertNotNull(ds); 1016 ds.setStorageUrl(this.JDBC_URL); 1017 ds.open(); 1018 ds.setSession(usrSession); 1019 1020 //2. get test document 1021 Corpus corp = createTestCorpus(); 1022 Assert.assertNotNull(corp); 1023 1024 //4. create security settings for doc 1025 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp); 1026 1027 //5. try adding corpus to data store 1028 Corpus result = (Corpus)ds.adopt(corp,si); 1029 Assert.assertNotNull(result); 1030 Assert.assertTrue(result instanceof DatabaseCorpusImpl); 1031 Assert.assertNotNull(result.getLRPersistenceId()); 1032 1033 this.sampleCorpus = result; 1034 this.sampleCorpus_lrID = (Long)result.getLRPersistenceId(); 1035 1036 //6.close 1037 ac.close(); 1038 ds.close(); 1039 1040 if(DEBUG) { 1041 Err.prln("Use case 101 passed..."); 1042 } 1043 1044 } 1045 1046 1047 /** Test the DS register. */ 1048 public void testDB_UseCase102() throws Exception { 1049 //read a corpus 1050///Err.prln("Use case 102 started..."); 1051 LanguageResource lr = null; 1052 1053 //0. get security factory & login 1054 AccessController ac = Factory.createAccessController(this.JDBC_URL); 1055 Assert.assertNotNull(ac); 1056 ac.open(); 1057 1058 User usr = ac.findUser("kalina"); 1059 Assert.assertNotNull(usr); 1060 1061 Group grp = (Group)usr.getGroups().get(0); 1062 Assert.assertNotNull(grp); 1063 1064 Session usrSession = ac.login("kalina","sesame",grp.getID()); 1065 Assert.assertNotNull(usrSession); 1066 Assert.assertTrue(ac.isValidSession(usrSession)); 1067 1068 //1. open data storage 1069 DatabaseDataStore ds = new OracleDataStore(); 1070 Assert.assertNotNull(ds); 1071 ds.setStorageUrl(this.JDBC_URL); 1072 ds.open(); 1073 ds.setSession(usrSession); 1074 1075 //2. read LR 1076 FeatureMap params = Factory.newFeatureMap(); 1077 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1078 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID); 1079 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params); 1080 1081 //3. check name 1082 String name = lr.getName(); 1083 Assert.assertNotNull(name); 1084 Assert.assertEquals(name,sampleCorpus.getName()); 1085 1086 //4. check features 1087 FeatureMap fm = lr.getFeatures(); 1088 FeatureMap fmOrig = sampleCorpus.getFeatures(); 1089 1090 Assert.assertNotNull(fm); 1091 Assert.assertNotNull(fmOrig); 1092 Assert.assertTrue(fm.size() == fmOrig.size()); 1093 1094 Iterator keys = fm.keySet().iterator(); 1095 1096 while (keys.hasNext()) { 1097 String currKey = (String)keys.next(); 1098 Assert.assertTrue(fmOrig.containsKey(currKey)); 1099 Assert.assertEquals(fm.get(currKey),fmOrig.get(currKey)); 1100 } 1101 1102 //close 1103 ds.close(); 1104 1105 if(DEBUG) { 1106 Err.prln("Use case 102 passed..."); 1107 } 1108 1109 } 1110 1111 1112 public void testDB_UseCase103() throws Exception { 1113///Err.prln("Use case 103 started..."); 1114 //sync a corpus 1115 LanguageResource lr = null; 1116 1117 //0. get security factory & login 1118 AccessController ac = Factory.createAccessController(this.JDBC_URL); 1119 Assert.assertNotNull(ac); 1120 ac.open(); 1121 1122 User usr = ac.findUser("kalina"); 1123 Assert.assertNotNull(usr); 1124 1125 Group grp = (Group)usr.getGroups().get(0); 1126 Assert.assertNotNull(grp); 1127 1128 Session usrSession = ac.login("kalina","sesame",grp.getID()); 1129 Assert.assertNotNull(usrSession); 1130 Assert.assertTrue(ac.isValidSession(usrSession)); 1131 1132 //1. open data storage 1133 DatabaseDataStore ds = new OracleDataStore(); 1134 Assert.assertNotNull(ds); 1135 ds.setStorageUrl(this.JDBC_URL); 1136 ds.open(); 1137 ds.setSession(usrSession); 1138 1139 if (DEBUG) Out.prln("ID " + sampleCorpus_lrID); 1140 1141 //2. read LR 1142 FeatureMap params = Factory.newFeatureMap(); 1143 params.put(DataStore.DATASTORE_FEATURE_NAME, ds); 1144 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID); 1145 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params); 1146 1147 Corpus dbCorp = (Corpus)lr; 1148 Corpus corp2 = null; 1149 1150 //3. change name 1151 String oldName = dbCorp.getName(); 1152 String newName = oldName + "__UPD"; 1153 dbCorp.setName(newName); 1154 dbCorp.sync(); 1155 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID); 1156 Assert.assertEquals(newName,dbCorp.getName()); 1157 Assert.assertEquals(newName,corp2.getName()); 1158 1159 //4. change features 1160 FeatureMap fm = dbCorp.getFeatures(); 1161 Iterator keys = fm.keySet().iterator(); 1162 1163 //4.1 change the value of the first feature 1164 while(keys.hasNext()) { 1165 String currKey = (String)keys.next(); 1166 Object val = fm.get(currKey); 1167 Object newVal = null; 1168 if (val instanceof Long) { 1169 newVal = new Long(101010101); 1170 } 1171 else if (val instanceof Integer) { 1172 newVal = new Integer(2121212); 1173 } 1174 else if (val instanceof String) { 1175 newVal = new String("UPD__").concat( (String)val).concat("__UPD"); 1176 } 1177 if (newVal != null) 1178 fm.put(currKey,newVal); 1179 } 1180 dbCorp.sync(); 1181 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID); 1182 Assert.assertEquals(fm,dbCorp.getFeatures()); 1183 Assert.assertEquals(fm,corp2.getFeatures()); 1184 1185 //close 1186 ds.close(); 1187 1188 if(DEBUG) { 1189 Err.prln("Use case 103 passed..."); 1190 } 1191 1192} 1193 1194 1195 public static void main(String[] args){ 1196 try{ 1197///System.setProperty(Gate.GATE_CONFIG_PROPERTY,"y:/gate.xml") ; 1198 Gate.setLocalWebServer(false); 1199 Gate.setNetConnected(false); 1200 Gate.init(); 1201 1202 TestPersist test = new TestPersist(""); 1203 test.setUp(); 1204 test.testDelete(); 1205 test.tearDown(); 1206 1207 test.setUp(); 1208 test.testDSR(); 1209 test.tearDown(); 1210 1211 test.setUp(); 1212 test.testMultipleLrs(); 1213 test.tearDown(); 1214 1215 test.setUp(); 1216// test.testSaveRestore(); 1217 test.tearDown(); 1218 1219 test.setUp(); 1220 test.testSimple(); 1221 test.tearDown(); 1222 1223 //I put this last because its failure is dependent on the gc() and 1224 //there's nothing I can do about it. Maybe I'll remove this from the 1225 //test 1226 test.setUp(); 1227 test.testMultipleLrs(); 1228 test.tearDown(); 1229 1230 test.setUp(); 1231 test.testDB_UseCase01(); 1232 test.tearDown(); 1233/* 1234 test.setUp(); 1235 test.testDB_UseCase01(); 1236 test.tearDown(); 1237*/ 1238 test.setUp(); 1239 test.testDB_UseCase02(); 1240 test.tearDown(); 1241 1242 test.setUp(); 1243 test.testDB_UseCase03(); 1244 test.tearDown(); 1245 1246 test.setUp(); 1247 test.testDB_UseCase04(); 1248 test.tearDown(); 1249 1250 1251 test.setUp(); 1252 test.testDB_UseCase101(); 1253 test.tearDown(); 1254 1255 test.setUp(); 1256 test.testDB_UseCase102(); 1257 test.tearDown(); 1258 1259 test.setUp(); 1260 test.testDB_UseCase103(); 1261 test.tearDown(); 1262 1263 if (DEBUG) { 1264 Err.println("done."); 1265 } 1266 }catch(Exception e){ 1267 e.printStackTrace(); 1268 } 1269 } 1270} // class TestPersist 1271 1272 1273class Dummy implements Serializable { 1274 1275 static final long serialVersionUID = 3632609241787241900L; 1276 1277 public int intValue; 1278 public String stringValue; 1279 public boolean boolValue; 1280 public float floatValue; 1281 1282 1283 public Dummy(int _int, String _string, boolean _bool, float _float) { 1284 1285 this.intValue = _int; 1286 this.stringValue= _string; 1287 this.boolValue = _bool; 1288 this.floatValue = _float; 1289 } 1290 1291 public boolean equals(Object obj) { 1292 Dummy d2 = (Dummy)obj; 1293 1294 return this.intValue == d2.intValue && 1295 this.stringValue.equals(d2.stringValue) && 1296 this.boolValue == d2.boolValue && 1297 this.floatValue == d2.floatValue; 1298 } 1299 1300 public String toString() { 1301 return "Dummy: intV=["+this.intValue+"], stringV=["+this.stringValue+"], boolV=["+this.boolValue+"], floatV = ["+this.floatValue+"]"; 1302 } 1303}
|
TestPersist |
|