1
15
16 package gate.persist;
17
18 import java.io.File;
19 import java.io.Serializable;
20 import java.net.URL;
21 import java.util.*;
22
23 import junit.framework.*;
24
25 import gate.*;
26 import gate.annotation.AnnotationSetImpl;
27 import gate.corpora.*;
28 import gate.event.DatastoreListener;
29 import gate.security.*;
30 import gate.util.*;
31
32
34 public class TestPersist extends TestCase
35 {
36 private static String JDBC_URL_1;
37 private static String JDBC_URL_2;
38 private static String JDBC_URL;
39
40
41 private static final boolean DEBUG = false;
42 private static Long sampleDoc_lrID = null;
43 private static Long sampleCorpus_lrID = null;
44 private static Corpus sampleCorpus = null;
45 private static int dbType;
46
47
50 private static AnnotationSet sample_defaultASet = null;
51 private static String sample_name = null;
52 private static FeatureMap sample_docFeatures = null;
53 private static URL sample_sourceURL = null;
54 private static Long sample_startOffset = null;
55 private static Long sample_endOffset = null;
56 private static Boolean sample_markupAware = null;
57 private static DocumentContent sample_content = null;
58 private static String sample_encoding = null;
59 private static Map sample_namedASets = null;
60
61 private static final String UNICODE_STRING = "\u0915\u0932\u094d\u0907\u0928\u0643\u0637\u0628\u041a\u0430\u043b\u0438\u043d\u0430 Kalina";
63 private static final String ASCII_STRING = "Never mistake motion for action (Ernest Hemingway)";
64
65 private final String VERY_LONG_STRING =
66 "The memory of Father came back to her. Ever since she had seen him retreat from those "+
67 "twelve-year-old boys she often imagined him in this situation: he is on a sinking ship; "+
68 "there are only a few lifeboats and there isn't enough room for everyone; there is a "+
69 "furious stampede on the deck. At first Father rushes along with the others, but when he "+
70 "sees how they push and shove, ready to trample each other under foot, and a wild-eyed "+
71 "woman strikes him with her fist because he is in her way, he suddenly stops and steps "+
72 "aside. And in the end he merely watches the overloaded lifeboats as they are slowly "+
73 "lowered amid shouts and curses, towards the raging waves. "+
74 "[p.111-113]";
75
76
77 public TestPersist(String name) throws GateException { super(name); }
78
79
80 public void setUp() throws Exception {
81 if (! DataStoreRegister.getConfigData().containsKey("url-test"))
82 throw new GateRuntimeException("DB URL not configured in gate.xml");
83 else
84 JDBC_URL_1 =
85 (String) DataStoreRegister.getConfigData().get("url-test");
86 JDBC_URL_2 =
87 (String) DataStoreRegister.getConfigData().get("url-test1");
88 }
90
93 public void tearDown() throws Exception {
94 }
96
97 public void testSaveRestore() throws Exception {
98 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
99 storageDir.delete(); storageDir.mkdir();
102 SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString());
103 sds.create();
104 sds.open();
105
106 String server = TestDocument.getTestServerName();
108 assertNotNull(server);
109 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
110 assertNotNull(doc);
111 doc.getFeatures().put("hi there", new Integer(23232));
112 doc.getAnnotations().add(
113 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap()
114 );
115
116 boolean cannotSync = false;
118 try { sds.sync(doc); } catch(PersistenceException e) { cannotSync=true; }
119 if(! cannotSync) assertTrue("doc synced ok before adoption", false);
120
121 doc.setDataStore(new SerialDataStore(new File("z:\\").toURL().toString()));
123 try { sds.adopt(doc,null); } catch(PersistenceException e) { cannotSync=true; }
124 if(! cannotSync)
125 assertTrue("doc adopted but in other datastore already", false);
126 doc.setDataStore(null);
127 doc.setName("Alicia Tonbridge, a Document");
128
129 Document persDoc = (Document) sds.adopt(doc,null);
131 sds.sync(persDoc);
132 Object lrPersistenceId = persDoc.getLRPersistenceId();
133
134 List lrTypes = sds.getLrTypes();
136 assertTrue("wrong number of types in SDS", lrTypes.size() == 1);
137 assertTrue(
138 "wrong type LR in SDS",
139 lrTypes.get(0).equals("gate.corpora.DocumentImpl")
140 );
141
142 Iterator iter = sds.getLrNames("gate.corpora.DocumentImpl").iterator();
144 String name = (String) iter.next();
145 assertEquals(name, "Alicia Tonbridge, a Document");
146
147 FeatureMap features = Factory.newFeatureMap();
149 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId);
150 features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
151 Document doc2 =
152 (Document) Factory.createResource("gate.corpora.DocumentImpl", features);
153 Document doc3 =
154 (Document) sds.getLr("gate.corpora.DocumentImpl", lrPersistenceId);
155
156 try{
157 boolean value = TestEqual.documentsEqual(doc3, doc2);
158 assertTrue(TestEqual.message, value);
159 value = TestEqual.documentsEqual(persDoc, doc2);
160 assertTrue(TestEqual.message, value);
161 }finally{
162 sds.delete();
164 }
165 }
167
168 public void testSimple() throws Exception {
169 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
173 storageDir.delete();
174
175 DataStore sds = Factory.createDataStore(
177 "gate.persist.SerialDataStore", storageDir.toURL().toString()
178 );
179
180 List lrTypes = sds.getLrTypes();
182
183 String server = TestDocument.getTestServerName();
185 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
186 doc.getFeatures().put("hi there", new Integer(23232));
187 doc.getAnnotations().add(
188 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
189 );
190
191 Document persDoc = (Document) sds.adopt(doc,null);
193 sds.sync(persDoc);
194
195 Object lrPersistenceId = persDoc.getLRPersistenceId();
198
199 FeatureMap features = Factory.newFeatureMap();
201 features.put(DataStore.LR_ID_FEATURE_NAME, lrPersistenceId);
202 features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
203 Document doc2 =
204 (Document) Factory.createResource("gate.corpora.DocumentImpl", features);
205
206 assertTrue(TestEqual.documentsEqual(persDoc, doc2));
209
210 sds.delete();
212 }
214
215 public void testMultipleLrs() throws Exception {
216 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
220 storageDir.delete();
221
222 SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString());
224 sds.create();
225 sds.open();
226
227 String server = TestDocument.getTestServerName();
229 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
230 doc.getFeatures().put("hi there", new Integer(23232));
231 doc.getAnnotations().add(
232 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
233 );
234
235 Document doc2 =
237 Factory.newDocument(new URL(server + "tests/html/test1.htm"));
238 doc.getFeatures().put("hi there again", new Integer(23232));
239 doc.getAnnotations().add(
240 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap()
241 );
242
243 Corpus corp = Factory.newCorpus("Hamish test corpus");
245 corp.add(doc);
246 corp.add(doc2);
247 LanguageResource persCorpus = sds.adopt(corp,null);
248 sds.sync(persCorpus);
249
250
251 ArrayList lrsFromDisk = new ArrayList();
253 List lrIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
254
255 Iterator idsIter = lrIds.iterator();
256 while(idsIter.hasNext()) {
257 String lrId = (String) idsIter.next();
258 FeatureMap features = Factory.newFeatureMap();
259 features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
260 features.put(DataStore.LR_ID_FEATURE_NAME, lrId);
261 Resource lr = Factory.createResource( "gate.corpora.SerialCorpusImpl",
262 features);
263 lrsFromDisk.add(lr);
264 }
266 if (DEBUG) System.out.println("LRs on disk" + lrsFromDisk);
267
268 Corpus diskCorp = (Corpus) lrsFromDisk.get(0);
270
271 Document diskDoc = (Document) diskCorp.get(0);
272
273 if (DEBUG) Out.prln("Documents in corpus: " + corp.getDocumentNames());
274 assertTrue("corp name != mem name", corp.getName().equals(diskCorp.getName()));
275 if (DEBUG) Out.prln("Memory features " + corp.getFeatures());
276 if (DEBUG) Out.prln("Disk features " + diskCorp.getFeatures());
277 assertTrue("corp feat != mem feat",
278 corp.getFeatures().equals(diskCorp.getFeatures()));
279 if (DEBUG)
280 Out.prln("Annotations in doc: " + diskDoc.getAnnotations());
281 assertTrue("doc annotations from disk not equal to memory version",
282 TestEqual.annotationSetsEqual(doc.getAnnotations(),
283 diskDoc.getAnnotations()));
284
285 assertTrue("doc from disk not equal to memory version",
286 TestEqual.documentsEqual(doc, diskDoc));
287
288 Iterator corpusIter = diskCorp.iterator();
289 while(corpusIter.hasNext()){
290 if (DEBUG)
291 Out.prln(((Document) corpusIter.next()).getName());
292 else
293 corpusIter.next();
294 }
295
296
297
299 sds.delete();
301 }
303
304 public void testDelete() throws Exception {
305 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
309 if (DEBUG) Out.prln("Corpus stored to: " + storageDir.getAbsolutePath());
310 storageDir.delete();
311
312 SerialDataStore sds = new SerialDataStore();
314 sds.setStorageUrl(storageDir.toURL().toString());
315 sds.create();
316 sds.open();
317
318 String server = TestDocument.getTestServerName();
320 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
321 doc.getFeatures().put("hi there", new Integer(23232));
322 doc.getAnnotations().add(
323 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
324 );
325
326 Document persDoc = (Document) sds.adopt(doc,null);
328 sds.sync(persDoc);
329
330 Object lrPersistenceId = persDoc.getLRPersistenceId();
333
334 sds.delete("gate.corpora.DocumentImpl", lrPersistenceId);
336
337 assertTrue(sds.getLrIds("gate.corpora.DocumentImpl").size() == 0);
339
340 sds.delete();
342 }
344
345
346
347
348 public void testDSR() throws Exception {
349 DataStoreRegister dsr = Gate.getDataStoreRegister();
350 assertTrue("DSR has wrong number elements (not 0): " + dsr.size(),
351 dsr.size() == 0);
352
353 File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
357 storageDir.delete();
358
359 DataStore sds = Factory.createDataStore(
361 "gate.persist.SerialDataStore", storageDir.toURL().toString()
362 );
363
364 String server = TestDocument.getTestServerName();
366 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
367 doc.getFeatures().put("hi there", new Integer(23232));
368 doc.getAnnotations().add(
369 new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap()
370 );
371
372 Document persDoc = (Document) sds.adopt(doc,null);
374 sds.sync(persDoc);
375
376 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(),
378 dsr.size() == 1);
379
380 storageDir = File.createTempFile("TestPersist__", "__StorageDir");
382 storageDir.delete();
383 DataStore sds2 = Factory.createDataStore(
384 "gate.persist.SerialDataStore", storageDir.toURL().toString()
385 );
386
387 assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 2);
389
390 Iterator dsrIter = dsr.iterator();
392 while(dsrIter.hasNext()) {
393 DataStore ds = (DataStore) dsrIter.next();
394 assertNotNull("null ds in ds reg", ds);
395 if(DEBUG)
396 Out.prln(ds);
397 }
398
399 sds.close();
401 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(),
402 dsr.size() == 1);
403 sds.delete();
404 assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(),
405 dsr.size() == 1);
406 sds2.delete();
407 assertTrue("DSR has wrong number elements (expected 0): " + dsr.size(),
408 dsr.size() == 0);
409
410 }
412
413
414
415 public static Test suite() {
416 return new TestSuite(TestPersist.class);
417 }
419
420 private Document createTestDocument()
421 throws Exception {
422
423 String server = TestDocument.getTestServerName();
424 assertNotNull(server);
425 Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
426 assertNotNull(doc);
427
428 doc.getFeatures().put("hi there", new Integer(23232));
429 doc.getFeatures().put("LONG STRING feature", this.VERY_LONG_STRING);
430 doc.getFeatures().put("NULL feature",null);
431 doc.getFeatures().put("BINARY feature",new Dummy(101,"101",true,101.101f));
432 doc.getFeatures().put("LONG feature",new Long(101));
433 doc.getFeatures().put("ASCII feature",ASCII_STRING);
435 doc.getFeatures().put("UNICODE feature",UNICODE_STRING);
436
437 Vector complexFeature = new Vector();
439 complexFeature.add("string 1");
440 complexFeature.add("string 2");
441 complexFeature.add("string 3");
442 complexFeature.add("string 4");
443 complexFeature.add("string 5");
444 doc.getFeatures().put("complex feature",complexFeature);
445 FeatureMap fm = Factory.newFeatureMap();
446 doc.getAnnotations().add(
451 new Long(0), new Long(20), "thingymajig", fm);
452 doc.setName("DB test Document---");
453
454 return doc;
455 }
456
457
458 private Corpus createTestCorpus()
459 throws Exception {
460
461 String server = TestDocument.getTestServerName();
462 assertNotNull(server);
463 Document doc1 = Factory.newDocument(new URL(server + "tests/doc0.html"));
464 assertNotNull(doc1);
465
466 doc1.getFeatures().put("hi there", new Integer(23232));
467 doc1.getAnnotations().add(
468 new Long(0), new Long(20), "thingymajig", Factory.newFeatureMap()
469 );
470 doc1.setName("DB test Document1");
471
472 Document doc2 =
474 Factory.newDocument(new URL(server + "tests/html/test1.htm"));
475 doc2.getFeatures().put("hi there again", new Integer(23232));
476 doc2.getAnnotations().add(
477 new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap()
478 );
479 doc2.setName("DB test Document2");
480
481 Corpus corp = Factory.newCorpus("My test corpus");
483 corp.add(doc1);
485 corp.add(doc2);
486 corp.getFeatures().put("my STRING feature ", new String("string string"));
488 corp.getFeatures().put("my BOOL feature ", new Boolean("false"));
489 corp.getFeatures().put("my INT feature ", new Integer("1234"));
490 corp.getFeatures().put("my LONG feature ", new Long("123456789"));
491 corp.getFeatures().put("my LONG STRING feature", this.VERY_LONG_STRING);
492 corp.getFeatures().put("my NULL feature", null);
493 corp.getFeatures().put("my BINARY feature",new Dummy(101,"101",true,101.101f));
494 return corp;
495 }
496
497 private DatabaseDataStore _createDS() {
498
499 DatabaseDataStore ds = null;
500 if (TestPersist.dbType == DBHelper.ORACLE_DB) {
501 ds = new OracleDataStore();
502 }
503 else if (TestPersist.dbType == DBHelper.POSTGRES_DB) {
504 ds = new PostgresDataStore();
505 }
506 else {
507 throw new IllegalArgumentException();
508 }
509
510 Assert.assertNotNull(ds);
511 return ds;
512 }
513
514 private void prepareDB(String db) {
515
516 if (TestPersist.JDBC_URL_1.indexOf(db) > 0 ) {
517 TestPersist.JDBC_URL = TestPersist.JDBC_URL_1;
518 }
519 else {
520 TestPersist.JDBC_URL = TestPersist.JDBC_URL_2;
521 }
522
523 Assert.assertNotNull("jdbc url not set for Oracle or Postgres",TestPersist.JDBC_URL);
524
525 TestPersist.dbType = DBHelper.getDatabaseType(JDBC_URL);
526 }
527
528
529
530 private void _testDB_UseCase01() throws Exception {
531
534
535 DatabaseDataStore ds = this._createDS();
537 Assert.assertNotNull(ds);
538 ds.setStorageUrl(TestPersist.JDBC_URL);
539 ds.open();
540
541 Document transDoc = createTestDocument();
543 Assert.assertNotNull(transDoc);
544
545 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
547 ac.open();
548 Assert.assertNotNull(ac);
549
550 User usr = ac.findUser("kalina");
551 Assert.assertNotNull(usr);
552
553 Group grp = (Group)usr.getGroups().get(0);
554 Assert.assertNotNull(grp);
555
556 Session usrSession = ac.login("kalina","sesame",grp.getID());
557 Assert.assertNotNull(usrSession);
558 Assert.assertTrue(ac.isValidSession(usrSession));
559
560 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
562
563 ds.setSession(usrSession);
565
566 sample_defaultASet = new AnnotationSetImpl(transDoc.getAnnotations());
570 sample_name = transDoc.getName();
571 sample_docFeatures = transDoc.getFeatures();
572 sample_sourceURL = transDoc.getSourceUrl();
573 sample_startOffset = transDoc.getSourceUrlStartOffset();
574 sample_endOffset = transDoc.getSourceUrlEndOffset();
575 sample_markupAware = transDoc.getMarkupAware();
576 sample_content = transDoc.getContent();
577 sample_encoding = (String)transDoc.getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
578
579 sample_namedASets = new HashMap();
580 Map transDocNamedSets = transDoc.getNamedAnnotationSets();
581 Iterator it = transDocNamedSets.keySet().iterator();
582 while (it.hasNext()) {
583 String asetName = (String)it.next();
584 AnnotationSet transAset = (AnnotationSet)transDocNamedSets.get(asetName);
585 AnnotationSet asetNew = new AnnotationSetImpl(transAset);
586 TestPersist.sample_namedASets.put(transAset.getName(),asetNew);
587 }
588
589
590 LanguageResource lr = ds.adopt(transDoc,si);
592
593 Assert.assertTrue(lr instanceof DatabaseDocumentImpl);
594 Assert.assertNotNull(lr.getDataStore());
595 Assert.assertTrue(lr.getDataStore() instanceof DatabaseDataStore);
596 Assert.assertEquals(sample_defaultASet, ((DatabaseDocumentImpl)lr).getAnnotations());
597
598 sampleDoc_lrID = (Long)lr.getLRPersistenceId();
599 if (DEBUG) Out.prln("lr id: " + TestPersist.sampleDoc_lrID);
600
601 ac.close();
603 ds.close();
604
605 if(DEBUG) {
606 Err.prln("Use case 01 passed...");
607 }
608 }
609
610
611 private void _testDB_UseCase02() throws Exception {
612 LanguageResource lr = null;
616
617 DatabaseDataStore ds = this._createDS();
619 Assert.assertNotNull(ds);
620 ds.setStorageUrl(TestPersist.JDBC_URL);
621 ds.open();
622
623 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
625 Assert.assertNotNull(ac);
626 ac.open();
627
628 User usr = ac.findUser("kalina");
629 Assert.assertNotNull(usr);
630
631 Group grp = (Group)usr.getGroups().get(0);
632 Assert.assertNotNull(grp);
633
634 Session usrSession = ac.login("kalina","sesame",grp.getID());
635 Assert.assertNotNull(usrSession);
636 Assert.assertTrue(ac.isValidSession(usrSession));
637
638 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
640
641 ds.setSession(usrSession);
643
644 FeatureMap params = Factory.newFeatureMap();
647 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
648 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
649 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
650 String name = lr.getName();
653 Assert.assertNotNull(name);
654 Assert.assertEquals(name,sample_name);
655
656 FeatureMap fm = lr.getFeatures();
658
659 Assert.assertNotNull(fm);
660 Assert.assertNotNull(sample_docFeatures);
661 Assert.assertTrue(fm.size() == sample_docFeatures.size());
662
663 Iterator keys = fm.keySet().iterator();
664
665 while (keys.hasNext()) {
666 String currKey = (String)keys.next();
667 Assert.assertTrue(sample_docFeatures.containsKey(currKey));
668 Assert.assertEquals(fm.get(currKey),sample_docFeatures.get(currKey));
669 }
670
671 DatabaseDocumentImpl dbDoc = (DatabaseDocumentImpl)lr;
673 Assert.assertEquals(dbDoc.getSourceUrl(),sample_sourceURL);
674
675 Assert.assertEquals(dbDoc.getSourceUrlStartOffset(),sample_startOffset);
677 Assert.assertEquals(dbDoc.getSourceUrlEndOffset(),sample_endOffset);
678
679 Assert.assertEquals(dbDoc.getMarkupAware(),sample_markupAware);
681
682 DocumentContent cont = dbDoc.getContent();
684 Assert.assertEquals(cont,sample_content);
685
686 Assert.assertEquals(cont,sample_content);
688
689 String encNew = (String)dbDoc.
691 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
692 String encOld = sample_encoding;
693 Assert.assertEquals(encNew,encOld);
694
695 AnnotationSet defaultNew = dbDoc.getAnnotations();
698 AnnotationSet defaultOld = sample_defaultASet;
699
700 Assert.assertNotNull(defaultNew);
701 Assert.assertTrue(defaultNew.size() == defaultOld.size());
702 Assert.assertEquals(defaultNew,defaultOld);
703
704
705 Iterator itOld = TestPersist.sample_namedASets.keySet().iterator();
707 while (itOld.hasNext()) {
708 String asetName = (String)itOld.next();
709 AnnotationSet asetOld = (AnnotationSet)sample_namedASets.get(asetName);
710 AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName);
711 Assert.assertNotNull(asetNew);
712 Assert.assertTrue(asetNew.size() == asetOld.size());
713 Assert.assertEquals(asetNew.get(),asetOld.get());
714 }
715
716
729
730 Map namedNew = dbDoc.getNamedAnnotationSets();
732
733 Assert.assertNotNull(namedNew);
734 Assert.assertTrue(namedNew.size() == TestPersist.sample_namedASets.size());
735
736 Iterator itNames = namedNew.keySet().iterator();
737 while (itNames.hasNext()) {
738 String asetName = (String)itNames.next();
739 AnnotationSet asetNew = (AnnotationSet)namedNew.get(asetName);
740 AnnotationSet asetOld = (AnnotationSet)sample_namedASets.get(asetName);
741 Assert.assertNotNull(asetNew);
742 Assert.assertNotNull(asetOld);
743 Assert.assertEquals(asetNew.get(),asetOld.get());
744 }
745
746 ds.removeDatastoreListener((DatastoreListener)lr);
748 lr = null;
749
750 ds.close();
751 ac.close();
752
753 if(DEBUG) {
754 Err.prln("Use case 02 passed...");
755 }
756
757 }
758
759
760 private void _testDB_UseCase03() throws Exception {
761 LanguageResource lr = null;
764
765 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
767 Assert.assertNotNull(ac);
768 ac.open();
769
770 User usr = ac.findUser("kalina");
771 Assert.assertNotNull(usr);
772
773 Group grp = (Group)usr.getGroups().get(0);
774 Assert.assertNotNull(grp);
775
776 Session usrSession = ac.login("kalina","sesame",grp.getID());
777 Assert.assertNotNull(usrSession);
778 Assert.assertTrue(ac.isValidSession(usrSession));
779
780 DatabaseDataStore ds = this._createDS();
782 Assert.assertNotNull(ds);
783 ds.setStorageUrl(TestPersist.JDBC_URL);
784 ds.open();
785
786 ds.setSession(usrSession);
788
789 if (DEBUG) Out.prln("ID " + sampleDoc_lrID);
790 FeatureMap params = Factory.newFeatureMap();
792 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
793 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
794 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
795 Document dbDoc = (Document)lr;
796 Document doc2 = null;
797
798 if (false == ds.lockLr(lr)) {
800 throw new PersistenceException("document is locked by another user");
801 }
802
803 String oldName = dbDoc.getName();
805 String newName = oldName + "__UPD";
806 dbDoc.setName(newName);
807 dbDoc.sync();
808 params.clear();
810 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
811 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
812 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
813
814 Assert.assertEquals(newName,dbDoc.getName());
815 Assert.assertEquals(newName,doc2.getName());
816
817 Factory.deleteResource(doc2);
818 doc2 = null;
819
820 FeatureMap fm = dbDoc.getFeatures();
822 Iterator keys = fm.keySet().iterator();
823
824 while(keys.hasNext()) {
826 String currKey = (String)keys.next();
827 Object val = fm.get(currKey);
828 Object newVal = null;
829 if (val instanceof Long) {
830 newVal = new Long(101010101);
831 }
832 else if (val instanceof Integer) {
833 newVal = new Integer(2121212);
834 }
835 else if (val instanceof String) {
836 newVal = new String("UPD__").concat( (String)val).concat("__UPD");
837 }
838 if (newVal != null)
839 fm.put(currKey,newVal);
840 }
841 dbDoc.sync();
842 params.clear();
844 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
845 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
846 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
847
848 Assert.assertEquals(fm,dbDoc.getFeatures());
849 Assert.assertEquals(fm,doc2.getFeatures());
850 Factory.deleteResource(doc2);
851 doc2 = null;
852
853 URL docURL = dbDoc.getSourceUrl();
855 URL newURL = null;
856 newURL = new URL(docURL.toString()+".UPDATED");
857 dbDoc.setSourceUrl(newURL);
858 dbDoc.sync();
859 params.clear();
861 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
862 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
863 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
864
865 Assert.assertEquals(newURL,dbDoc.getSourceUrl());
866 Assert.assertEquals(newURL,doc2.getSourceUrl());
867 Factory.deleteResource(doc2);
868 doc2 = null;
869
870 Long newStart = new Long(123);
872 Long newEnd = new Long(789);
873 dbDoc.setSourceUrlStartOffset(newStart);
874 dbDoc.setSourceUrlEndOffset(newEnd);
875 dbDoc.sync();
876
877 params.clear();
879 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
880 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
881 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
882
883 Assert.assertEquals(newStart,dbDoc.getSourceUrlStartOffset());
884 Assert.assertEquals(newStart,doc2.getSourceUrlStartOffset());
885 Assert.assertEquals(newEnd,dbDoc.getSourceUrlEndOffset());
886 Assert.assertEquals(newEnd,doc2.getSourceUrlEndOffset());
887
888 Factory.deleteResource(doc2);
889 doc2 = null;
890
891
892 Boolean oldMA = dbDoc.getMarkupAware();
894 Boolean newMA = oldMA.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
895 dbDoc.setMarkupAware(newMA);
896 dbDoc.sync();
897
898 params.clear();
900 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
901 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
902 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
903
904 Assert.assertEquals(newMA,doc2.getMarkupAware());
905 Assert.assertEquals(newMA,dbDoc.getMarkupAware());
906
907 Factory.deleteResource(doc2);
908 doc2 = null;
909
910
911 DocumentContent contOld = dbDoc.getContent();
913 DocumentContent contNew = new DocumentContentImpl(new String("UPDATED__").concat(contOld.toString().concat("__UPDATED")));
914 dbDoc.setContent(contNew);
915 dbDoc.sync();
916
917 params.clear();
919 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
920 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
921 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
922
923 Assert.assertEquals(contNew,dbDoc.getContent());
924 Assert.assertEquals(contNew,doc2.getContent());
925
926 Factory.deleteResource(doc2);
927 doc2 = null;
928
929 String encOld = (String)dbDoc.
931 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
932 dbDoc.setParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME,"XXX");
933 dbDoc.sync();
934 params.clear();
936 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
937 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
938 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
939
940 String encNew = (String)doc2.
941 getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
942 Assert.assertEquals(encNew,encOld);
943
944 Factory.deleteResource(doc2);
945 doc2 = null;
946
947
948 AnnotationSet dbDocSet = dbDoc.getAnnotations("TEST SET");
950 Assert.assertNotNull(dbDocSet);
951
952 FeatureMap fm1 = new SimpleFeatureMapImpl();
953 fm1.put("string key","string value");
954
955 Integer annInd = dbDocSet.add(new Long(0), new Long(10),
956 "TEST TYPE",
957 fm1);
958
959 dbDoc.sync();
960 params.clear();
962 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
963 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
964 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
965
966 AnnotationSet doc2Set = doc2.getAnnotations("TEST SET");
967 Assert.assertTrue(dbDocSet.size() == doc2Set.size());
968
970 Factory.deleteResource(doc2);
971 doc2 = null;
972
973
974 Annotation dbDocAnn = dbDocSet.get(annInd);
976
977 FeatureMap fm2 = new SimpleFeatureMapImpl();
978 fm2.put("string2","uuuuuu");
979 fm2.put("int2",new Integer(98989898));
980 Integer newInd = dbDocSet.add(dbDocAnn.getStartNode().getOffset(),
981 dbDocAnn.getEndNode().getOffset(),
982 dbDocAnn.getType() + "__XX",
983 fm2);
984 Annotation dbDocAnnNew = dbDocSet.get(newInd);
985 dbDoc.sync();
986
987 params.clear();
989 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
990 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
991 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
992
993 doc2Set = doc2.getAnnotations("TEST SET");
994 Assert.assertTrue(dbDocSet.size() == doc2Set.size());
995 Assert.assertTrue(TestEqual.annotationSetsEqual(doc2Set, dbDocSet));
996 Assert.assertTrue(doc2Set.contains(dbDocAnnNew));
997
998 Factory.deleteResource(doc2);
999 doc2 = null;
1000
1013
1014 String dummySetName = "--NO--SUCH--SET--";
1016 AnnotationSet aset = dbDoc.getAnnotations(dummySetName);
1017 aset.addAll(dbDoc.getAnnotations());
1018 dbDoc.sync();
1019
1020 params.clear();
1022 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1023 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
1024 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
1025
1026 Assert.assertTrue(dbDoc.getNamedAnnotationSets().containsKey(dummySetName));
1027 Assert.assertTrue(doc2.getNamedAnnotationSets().containsKey(dummySetName));
1028 AnnotationSet copy1 = (AnnotationSet)
1029 dbDoc.getNamedAnnotationSets().get(dummySetName);
1030 AnnotationSet copy2 = (AnnotationSet)
1031 doc2.getNamedAnnotationSets().get(dummySetName);
1032 Assert.assertTrue(dbDoc.getNamedAnnotationSets().containsValue(aset));
1033 Assert.assertTrue(TestEqual.annotationSetsEqual(copy1, copy2));
1034 Assert.assertTrue(dbDoc.getNamedAnnotationSets().size() == doc2.getNamedAnnotationSets().size());
1035
1038 Factory.deleteResource(doc2);
1039 doc2 = null;
1040
1041 dbDoc.removeAnnotationSet(dummySetName);
1043 dbDoc.sync();
1044 Assert.assertTrue(false == ((EventAwareDocument)dbDoc).getLoadedAnnotationSets().contains(dummySetName));
1045 Assert.assertTrue(false == dbDoc.getNamedAnnotationSets().containsKey(dummySetName));
1046
1047 params.clear();
1049 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1050 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
1051 doc2= (Document) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
1052
1053 Assert.assertTrue(false == doc2.getNamedAnnotationSets().containsKey(dummySetName));
1054
1055 Factory.deleteResource(doc2);
1056 doc2 = null;
1057
1058 ds.unlockLr(lr);
1060 ds.sync(lr);
1061
1062 Factory.deleteResource(dbDoc);
1064 dbDoc = null;
1065
1066 ac.close();
1067 ds.close();
1068
1069 if(DEBUG) {
1070 Err.prln("Use case 03 passed...");
1071 }
1072 }
1073
1074
1075 private void _testDB_UseCase04() throws Exception {
1076 LanguageResource lr = null;
1079
1080 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1082 Assert.assertNotNull(ac);
1083 ac.open();
1084
1085 User usr = ac.findUser("kalina");
1086 Assert.assertNotNull(usr);
1087
1088 Group grp = (Group)usr.getGroups().get(0);
1089 Assert.assertNotNull(grp);
1090
1091 Session usrSession = ac.login("kalina","sesame",grp.getID());
1092 Assert.assertNotNull(usrSession);
1093 Assert.assertTrue(ac.isValidSession(usrSession));
1094
1095 DatabaseDataStore ds = this._createDS();
1097 Assert.assertNotNull(ds);
1098 ds.setStorageUrl(TestPersist.JDBC_URL);
1099 ds.open();
1100 ds.setSession(usrSession);
1101
1102 FeatureMap params = Factory.newFeatureMap();
1104 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1105 params.put(DataStore.LR_ID_FEATURE_NAME, TestPersist.sampleDoc_lrID);
1106 lr = (LanguageResource) Factory.createResource(DBHelper.DOCUMENT_CLASS, params);
1107
1108 if (false == ds.lockLr(lr)) {
1110 throw new PersistenceException("document is locked by another user");
1111 }
1112
1113 ds.delete(DBHelper.DOCUMENT_CLASS,lr.getLRPersistenceId());
1115
1116
1118 ds.close();
1120 ac.close();
1121
1122 if(DEBUG) {
1123 Err.prln("Use case 04 passed...");
1124 }
1125
1126 }
1127
1128
1129
1130 private void _testDB_UseCase101() throws Exception {
1131
1134 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1136 Assert.assertNotNull(ac);
1137 ac.open();
1138
1139 User usr = ac.findUser("kalina");
1140 Assert.assertNotNull(usr);
1141
1142 Group grp = (Group)usr.getGroups().get(0);
1143 Assert.assertNotNull(grp);
1144
1145 Session usrSession = ac.login("kalina","sesame",grp.getID());
1146 Assert.assertNotNull(usrSession);
1147 Assert.assertTrue(ac.isValidSession(usrSession));
1148
1149 DatabaseDataStore ds = this._createDS();
1151 Assert.assertNotNull(ds);
1152 ds.setStorageUrl(TestPersist.JDBC_URL);
1153 ds.open();
1154 ds.setSession(usrSession);
1155
1156 Corpus corp = createTestCorpus();
1158 Assert.assertNotNull(corp);
1159
1160 SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
1162
1163 Corpus result = (Corpus)ds.adopt(corp,si);
1165 Assert.assertNotNull(result);
1166 Assert.assertTrue(result instanceof DatabaseCorpusImpl);
1167 Assert.assertNotNull(result.getLRPersistenceId());
1168
1169 TestPersist.sampleCorpus = result;
1170 TestPersist.sampleCorpus_lrID = (Long)result.getLRPersistenceId();
1171
1172 ac.close();
1174 ds.close();
1175
1176 if(DEBUG) {
1177 Err.prln("Use case 101 passed...");
1178 }
1179
1180 }
1181
1182
1183
1184
1185 private void _testDB_UseCase102() throws Exception {
1186 LanguageResource lr = null;
1189
1190 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1192 Assert.assertNotNull(ac);
1193 ac.open();
1194
1195 User usr = ac.findUser("kalina");
1196 Assert.assertNotNull(usr);
1197
1198 Group grp = (Group)usr.getGroups().get(0);
1199 Assert.assertNotNull(grp);
1200
1201 Session usrSession = ac.login("kalina","sesame",grp.getID());
1202 Assert.assertNotNull(usrSession);
1203 Assert.assertTrue(ac.isValidSession(usrSession));
1204
1205 DatabaseDataStore ds = this._createDS();
1207 Assert.assertNotNull(ds);
1208 ds.setStorageUrl(TestPersist.JDBC_URL);
1209 ds.open();
1210 ds.setSession(usrSession);
1211
1212 FeatureMap params = Factory.newFeatureMap();
1214 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1215 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID);
1216 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params);
1217
1218 String name = lr.getName();
1220 Assert.assertNotNull(name);
1221 Assert.assertEquals(name,sampleCorpus.getName());
1222
1223 FeatureMap fm = lr.getFeatures();
1225 FeatureMap fmOrig = sampleCorpus.getFeatures();
1226
1227 Assert.assertNotNull(fm);
1228 Assert.assertNotNull(fmOrig);
1229 Assert.assertTrue(fm.size() == fmOrig.size());
1230
1231 Iterator keys = fm.keySet().iterator();
1232
1233 while (keys.hasNext()) {
1234 String currKey = (String)keys.next();
1235 Assert.assertTrue(fmOrig.containsKey(currKey));
1236 Assert.assertEquals(fm.get(currKey),fmOrig.get(currKey));
1237 }
1238
1239 ds.close();
1241
1242 if(DEBUG) {
1243 Err.prln("Use case 102 passed...");
1244 }
1245
1246 }
1247
1248
1249 private void _testDB_UseCase103() throws Exception {
1250 LanguageResource lr = null;
1253
1254 AccessController ac = Factory.createAccessController(TestPersist.JDBC_URL);
1256 Assert.assertNotNull(ac);
1257 ac.open();
1258
1259 User usr = ac.findUser("kalina");
1260 Assert.assertNotNull(usr);
1261
1262 Group grp = (Group)usr.getGroups().get(0);
1263 Assert.assertNotNull(grp);
1264
1265 Session usrSession = ac.login("kalina","sesame",grp.getID());
1266 Assert.assertNotNull(usrSession);
1267 Assert.assertTrue(ac.isValidSession(usrSession));
1268
1269 DatabaseDataStore ds = this._createDS();
1271 Assert.assertNotNull(ds);
1272 ds.setStorageUrl(TestPersist.JDBC_URL);
1273 ds.open();
1274 ds.setSession(usrSession);
1275
1276 if (DEBUG) Out.prln("ID " + sampleCorpus_lrID);
1277
1278 FeatureMap params = Factory.newFeatureMap();
1280 params.put(DataStore.DATASTORE_FEATURE_NAME, ds);
1281 params.put(DataStore.LR_ID_FEATURE_NAME, sampleCorpus_lrID);
1282 lr = (LanguageResource) Factory.createResource(DBHelper.CORPUS_CLASS, params);
1283
1284 Corpus dbCorp = (Corpus)lr;
1285 Corpus corp2 = null;
1286
1287 String oldName = dbCorp.getName();
1289 String newName = oldName + "__UPD";
1290 dbCorp.setName(newName);
1291 dbCorp.sync();
1292 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID);
1293 Assert.assertEquals(newName,dbCorp.getName());
1294 Assert.assertEquals(newName,corp2.getName());
1295
1296 FeatureMap fm = dbCorp.getFeatures();
1298 Iterator keys = fm.keySet().iterator();
1299
1300 while(keys.hasNext()) {
1302 String currKey = (String)keys.next();
1303 Object val = fm.get(currKey);
1304 Object newVal = null;
1305 if (val instanceof Long) {
1306 newVal = new Long(101010101);
1307 }
1308 else if (val instanceof Integer) {
1309 newVal = new Integer(2121212);
1310 }
1311 else if (val instanceof String) {
1312 newVal = new String("UPD__").concat( (String)val).concat("__UPD");
1313 }
1314 if (newVal != null)
1315 fm.put(currKey,newVal);
1316 }
1317 dbCorp.sync();
1318 corp2 = (Corpus)ds.getLr(DBHelper.CORPUS_CLASS,sampleCorpus_lrID);
1319 Assert.assertEquals(fm,dbCorp.getFeatures());
1320 Assert.assertEquals(fm,corp2.getFeatures());
1321
1322 ds.close();
1324
1325 if(DEBUG) {
1326 Err.prln("Use case 103 passed...");
1327 }
1328
1329}
1330
1331
1410 public void testPostgres_01() throws Exception {
1411
1412 prepareDB("postgres");
1413 _testDB_UseCase01();
1414 }
1415
1416 public void testPostgres_02() throws Exception {
1417
1418 prepareDB("postgres");
1419 _testDB_UseCase02();
1420 }
1421
1422 public void testPostgres_03() throws Exception {
1423
1424 prepareDB("postgres");
1425 _testDB_UseCase03();
1426 }
1427
1428 public void testPostgres_04() throws Exception {
1429
1430 prepareDB("postgres");
1431 _testDB_UseCase04();
1432 }
1433
1434 public void testPostgres_101() throws Exception {
1435
1436 prepareDB("postgres");
1437 _testDB_UseCase101();
1438 }
1439
1440 public void testPostgres_102() throws Exception {
1441
1442 prepareDB("postgres");
1443 _testDB_UseCase102();
1444 }
1445
1446 public void testPostgres_103() throws Exception {
1447
1448 prepareDB("postgres");
1449 _testDB_UseCase103();
1450 }
1451
1452
1453
1454 public static void main(String[] args){
1455 try{
1456
1457 Gate.setLocalWebServer(false);
1459 Gate.setNetConnected(false);
1460 Gate.init();
1461
1462
1463 TestPersist test = new TestPersist("");
1464
1465
1477
1478
1479
1480
1508
1509
1510
1511 test.setUp();
1512 test.testPostgres_01();
1513 test.tearDown();
1514
1515 test.setUp();
1516 test.testPostgres_02();
1517 test.tearDown();
1518
1519 test.setUp();
1520 test.testPostgres_03();
1521 test.tearDown();
1522
1523 test.setUp();
1524 test.testPostgres_04();
1525 test.tearDown();
1526
1527 test.setUp();
1528 test.testPostgres_101();
1529 test.tearDown();
1530
1531 test.setUp();
1532 test.testPostgres_102();
1533 test.tearDown();
1534
1535 test.setUp();
1536 test.testPostgres_103();
1537 test.tearDown();
1538
1539 test.setUp();
1540 test.testDelete();
1541 test.tearDown();
1542
1543 test.setUp();
1544 test.testDSR();
1545 test.tearDown();
1546
1547 test.setUp();
1548 test.testMultipleLrs();
1549 test.tearDown();
1550
1551 test.setUp();
1552 test.tearDown();
1554
1555 test.setUp();
1556 test.testSimple();
1557 test.tearDown();
1558
1559 test.setUp();
1563 test.testMultipleLrs();
1564 test.tearDown();
1565
1566 if (DEBUG) {
1567 Err.println("done.");
1568 }
1569 }catch(Exception e){
1570 e.printStackTrace();
1571 }
1572 }
1573
1574
1646
1647
1648
1706
1707}
1709
1710class Dummy implements Serializable {
1711
1712 static final long serialVersionUID = 3632609241787241900L;
1713
1714 public int intValue;
1715 public String stringValue;
1716 public boolean boolValue;
1717 public float floatValue;
1718
1719
1720 public Dummy(int _int, String _string, boolean _bool, float _float) {
1721
1722 this.intValue = _int;
1723 this.stringValue= _string;
1724 this.boolValue = _bool;
1725 this.floatValue = _float;
1726 }
1727
1728 public boolean equals(Object obj) {
1729 Dummy d2 = (Dummy)obj;
1730
1731 return this.intValue == d2.intValue &&
1732 this.stringValue.equals(d2.stringValue) &&
1733 this.boolValue == d2.boolValue &&
1734 this.floatValue == d2.floatValue;
1735 }
1736
1737 public String toString() {
1738 return "Dummy: intV=["+this.intValue+"], stringV=["+this.stringValue+"], boolV=["+this.boolValue+"], floatV = ["+this.floatValue+"]";
1739 }
1740
1741}
1742