1   /*
2    *  TestPersist.java
3    *
4    *  Copyright (c) 1998-2005, 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.99 2005/01/11 13:51:36 ian Exp $
14   */
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  /** Persistence test class
33    */
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    /** Debug flag */
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    /* cached properties of the original transient document that will be
48       compared with the DB copies
49     */
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 = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";
62    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    /** Construction */
77    public TestPersist(String name) throws GateException { super(name); }
78  
79    /** Fixture set up */
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    } // setUp
89  
90    /** Put things back as they should be after running tests
91      * (reinitialise the CREOLE register).
92      */
93    public void tearDown() throws Exception {
94    } // tearDown
95  
96    /** Test resource save and restore */
97    public void testSaveRestore() throws Exception {
98      File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
99      storageDir.delete(); // get rid of the temp file
100     storageDir.mkdir(); // create an empty dir of same name
101 
102     SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString());
103     sds.create();
104     sds.open();
105 
106     // create a document
107     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     // check that we can't save a resource without adopting it
117     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     // check that we can't adopt a resource that's stored somewhere else
122     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     // save the document
130     Document persDoc = (Document) sds.adopt(doc,null);
131     sds.sync(persDoc);
132     Object lrPersistenceId = persDoc.getLRPersistenceId();
133 
134     // test the getLrTypes method
135     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     // test the getLrNames method
143     Iterator iter = sds.getLrNames("gate.corpora.DocumentImpl").iterator();
144     String name = (String) iter.next();
145     assertEquals(name, "Alicia Tonbridge, a Document");
146 
147     // read the document back
148     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       // delete the datastore
163       sds.delete();
164     }
165   } // testSaveRestore()
166 
167   /** Simple test */
168   public void testSimple() throws Exception {
169     // create a temporary directory; because File.createTempFile actually
170     // writes the bloody thing, we need to delete it from disk before calling
171     // DataStore.create
172     File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
173     storageDir.delete();
174 
175     // create and open a serial data store
176     DataStore sds = Factory.createDataStore(
177       "gate.persist.SerialDataStore", storageDir.toURL().toString()
178     );
179 
180     // check we can get empty lists from empty data stores
181     List lrTypes = sds.getLrTypes();
182 
183     // create a document with some annotations / features on it
184     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     // save the document
192     Document persDoc = (Document) sds.adopt(doc,null);
193     sds.sync(persDoc);
194 
195     // remember the persistence ID for reading back
196     // (in the normal case these ids are obtained by DataStore.getLrIds(type))
197     Object lrPersistenceId = persDoc.getLRPersistenceId();
198 
199     // read the document back
200     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     //parameters should be different
207     // check that the version we read back matches the original
208     assertTrue(TestEqual.documentsEqual(persDoc, doc2));
209 
210     // delete the datastore
211     sds.delete();
212   } // testSimple()
213 
214   /** Test multiple LRs */
215   public void testMultipleLrs() throws Exception {
216     // create a temporary directory; because File.createTempFile actually
217     // writes the bloody thing, we need to delete it from disk before calling
218     // DataStore.create
219     File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
220     storageDir.delete();
221 
222     // create and open a serial data store
223     SerialDataStore sds = new SerialDataStore(storageDir.toURL().toString());
224     sds.create();
225     sds.open();
226 
227     // create a document with some annotations / features on it
228     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     // create another document with some annotations / features on it
236     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     // create a corpus with the documents
244     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     // read the documents back
252     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     } // for each LR ID
265 
266     if (DEBUG) System.out.println("LRs on disk" + lrsFromDisk);
267 
268     // check that the versions we read back match the originals
269     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 //    assertTrue("doc2 from disk not equal to memory version", doc2.equals(diskDoc2));
298 
299     // delete the datastore
300     sds.delete();
301   } // testMultipleLrs()
302 
303   /** Test LR deletion */
304   public void testDelete() throws Exception {
305     // create a temporary directory; because File.createTempFile actually
306     // writes the bloody thing, we need to delete it from disk before calling
307     // DataStore.create
308     File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
309     if (DEBUG) Out.prln("Corpus stored to: " + storageDir.getAbsolutePath());
310     storageDir.delete();
311 
312     // create and open a serial data store
313     SerialDataStore sds = new SerialDataStore();
314     sds.setStorageUrl(storageDir.toURL().toString());
315     sds.create();
316     sds.open();
317 
318     // create a document with some annotations / features on it
319     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     // save the document
327     Document persDoc = (Document) sds.adopt(doc,null);
328     sds.sync(persDoc);
329 
330     // remember the persistence ID for reading back
331     // (in the normal case these ids are obtained by DataStore.getLrIds(type))
332     Object lrPersistenceId = persDoc.getLRPersistenceId();
333 
334     // delete document back
335     sds.delete("gate.corpora.DocumentImpl", lrPersistenceId);
336 
337     // check that there are no LRs left in the DS
338     assertTrue(sds.getLrIds("gate.corpora.DocumentImpl").size() == 0);
339 
340     // delete the datastore
341     sds.delete();
342   } // testDelete()
343 
344 
345 
346 
347   /** Test the DS register. */
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     // create a temporary directory; because File.createTempFile actually
354     // writes the bloody thing, we need to delete it from disk before calling
355     // DataStore.create
356     File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
357     storageDir.delete();
358 
359     // create and open a serial data store
360     DataStore sds = Factory.createDataStore(
361       "gate.persist.SerialDataStore", storageDir.toURL().toString()
362     );
363 
364     // create a document with some annotations / features on it
365     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     // save the document
373     Document persDoc = (Document) sds.adopt(doc,null);
374     sds.sync(persDoc);
375 
376     // DSR should have one member
377     assertTrue("DSR has wrong number elements (expected 1): " + dsr.size(),
378                dsr.size() == 1);
379 
380     // create and open another serial data store
381     storageDir = File.createTempFile("TestPersist__", "__StorageDir");
382     storageDir.delete();
383     DataStore sds2 = Factory.createDataStore(
384       "gate.persist.SerialDataStore", storageDir.toURL().toString()
385     );
386 
387     // DSR should have two members
388     assertTrue("DSR has wrong number elements: " + dsr.size(), dsr.size() == 2);
389 
390     // peek at the DSR members
391     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     // delete the datastores
400     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   } // testDSR()
411 
412 
413 
414   /** Test suite routine for the test runner */
415   public static Test suite() {
416     return new TestSuite(TestPersist.class);
417   } // suite
418 
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("FLOAT feature",new Double(101.102d));
434     doc.getFeatures().put("ASCII feature",ASCII_STRING);
435     doc.getFeatures().put("UNICODE feature",UNICODE_STRING);
436 
437     //create a complex feature - array of strings
438     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 //    fm.put("FLOAT feature ZZZ",new Double(101.102d));
447 //    fm.put("ASCII feature",ASCII_STRING);
448 //      fm.put("INT feature",new Integer(1212));
449 //    fm.put("UNICODE feature",UNICODE_STRING);
450     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     // create another document with some annotations / features on it
473     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     //create corpus
482     Corpus corp = Factory.newCorpus("My test corpus");
483     //add docs
484     corp.add(doc1);
485     corp.add(doc2);
486     //add features
487     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   /** Test the DS register. */
530   private void _testDB_UseCase01() throws Exception {
531 ///Err.prln("Use case 01 started...");
532     //descr: create a document in the DB
533 
534 
535     //1. open data storage
536     DatabaseDataStore ds = this._createDS();
537     Assert.assertNotNull(ds);
538     ds.setStorageUrl(TestPersist.JDBC_URL);
539     ds.open();
540 
541     //2. get test document
542     Document transDoc = createTestDocument();
543     Assert.assertNotNull(transDoc);
544 
545     //3. get security factory & login
546     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     //4. create security settings for doc
561     SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
562 
563     //5 set DS session
564     ds.setSession(usrSession);
565 
566     //6. cache the transient document properties for comparison
567     /// ...since it will be cleanup upon adoption from the datastore.
568     //... We'll need the cached values for the comparison only (asserts)
569     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     //7. try adding doc to data store
591     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     //8.close
602     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 ///Err.prln("Use case 02 started...");
613     //read a document
614     //use the one created in UC01
615     LanguageResource lr = null;
616 
617     //1. open data storage
618     DatabaseDataStore ds = this._createDS();
619     Assert.assertNotNull(ds);
620     ds.setStorageUrl(TestPersist.JDBC_URL);
621     ds.open();
622 
623     //3. get security factory & login
624     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     //4. create security settings for doc
639     SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
640 
641     //4.5 set DS session
642     ds.setSession(usrSession);
643 
644     //2. read LR
645 ///Err.println(">>>");
646     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 ///Err.println("<<<");
651     //3. check name
652     String name = lr.getName();
653     Assert.assertNotNull(name);
654     Assert.assertEquals(name,sample_name);
655 
656     //4. check features
657     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     //6. URL
672     DatabaseDocumentImpl dbDoc = (DatabaseDocumentImpl)lr;
673     Assert.assertEquals(dbDoc.getSourceUrl(),sample_sourceURL);
674 
675     //5.start/end
676     Assert.assertEquals(dbDoc.getSourceUrlStartOffset(),sample_startOffset);
677     Assert.assertEquals(dbDoc.getSourceUrlEndOffset(),sample_endOffset);
678 
679     //6.markupAware
680     Assert.assertEquals(dbDoc.getMarkupAware(),sample_markupAware);
681 
682     //7. content
683     DocumentContent cont = dbDoc.getContent();
684     Assert.assertEquals(cont,sample_content);
685 
686     //8. access the content again and assure it's not read from the DB twice
687     Assert.assertEquals(cont,sample_content);
688 
689     //9. encoding
690     String encNew = (String)dbDoc.
691       getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
692     String encOld = sample_encoding;
693     Assert.assertEquals(encNew,encOld);
694 
695     //10. default annotations
696 ///System.out.println("GETTING default ANNOTATIONS...");
697     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     //10. iterate named annotations
706     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 /*
717     //10. iterate named annotations
718     Map namedOld = this.sampleDoc.getNamedAnnotationSets();
719     Iterator itOld = namedOld.keySet().iterator();
720     while (itOld.hasNext()) {
721       String asetName = (String)itOld.next();
722       AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName);
723       AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName);
724       Assert.assertNotNull(asetNew);
725       Assert.assertTrue(asetNew.size() == asetOld.size());
726       Assert.assertEquals(asetNew,asetOld);
727     }
728 */
729 
730     //11. ALL named annotation (ensure nothing is read from DB twice)
731     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     //close
747     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 ///Err.prln("Use case 03 started...");
762     //sync a document
763     LanguageResource lr = null;
764 
765     //0. get security factory & login
766     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     //1. open data storage
781     DatabaseDataStore ds = this._createDS();
782     Assert.assertNotNull(ds);
783     ds.setStorageUrl(TestPersist.JDBC_URL);
784     ds.open();
785 
786     //1.5 set DS session
787     ds.setSession(usrSession);
788 
789     if (DEBUG) Out.prln("ID " + sampleDoc_lrID);
790     //2. read LR
791     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     //2.5 get exclusive lock
799     if (false == ds.lockLr(lr)) {
800       throw new PersistenceException("document is locked by another user");
801     }
802 
803     //3. change name
804     String oldName = dbDoc.getName();
805     String newName = oldName + "__UPD";
806     dbDoc.setName(newName);
807     dbDoc.sync();
808 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
809     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     //4. change features
821     FeatureMap fm = dbDoc.getFeatures();
822     Iterator keys = fm.keySet().iterator();
823 
824     //4.1 change the value of the first feature
825     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 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
843     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     //6. URL
854     URL docURL = dbDoc.getSourceUrl();
855     URL newURL = null;
856     newURL = new URL(docURL.toString()+".UPDATED");
857     dbDoc.setSourceUrl(newURL);
858     dbDoc.sync();
859 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
860     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     //5.start/end
871     Long newStart = new Long(123);
872     Long newEnd = new Long(789);
873     dbDoc.setSourceUrlStartOffset(newStart);
874     dbDoc.setSourceUrlEndOffset(newEnd);
875     dbDoc.sync();
876 
877 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
878     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     //6.markupAware
893     Boolean oldMA = dbDoc.getMarkupAware();
894     Boolean newMA = oldMA.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
895     dbDoc.setMarkupAware(newMA);
896     dbDoc.sync();
897 
898 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
899     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     //7. content
912     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 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
918     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     //8. encoding
930     String encOld = (String)dbDoc.
931       getParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME);
932     dbDoc.setParameterValue(Document.DOCUMENT_ENCODING_PARAMETER_NAME,"XXX");
933     dbDoc.sync();
934 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
935     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     //9. add annotations
949     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 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
961     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 //--    Assert.assertEquals(doc2Set,dbDocSet);
969 
970     Factory.deleteResource(doc2);
971     doc2 = null;
972 
973 
974     //9.1. change+add annotations
975     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 //--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
988     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/*
1001    //10. iterate named annotations
1002    Map namedOld = ((DocumentImpl)this.uc01_LR).getNamedAnnotationSets();
1003    Iterator itOld = namedOld.keySet().iterator();
1004    while (itOld.hasNext()) {
1005      String asetName = (String)itOld.next();
1006      AnnotationSet asetOld = (AnnotationSet)namedOld.get(asetName);
1007      AnnotationSet asetNew = (AnnotationSet)dbDoc.getAnnotations(asetName);
1008      Assert.assertNotNull(asetNew);
1009      Assert.assertEquals(asetNew,asetOld);
1010//      Features fmNew = asetNew.getFea
1011    }
1012*/
1013
1014    //11. add a new ann-set
1015    String dummySetName = "--NO--SUCH--SET--";
1016    AnnotationSet aset = dbDoc.getAnnotations(dummySetName);
1017    aset.addAll(dbDoc.getAnnotations());
1018    dbDoc.sync();
1019
1020//--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
1021    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//maps aren't equal since removing the equals impementations
1036//    Assert.assertEquals(doc2.getNamedAnnotationSets(),dbDoc.getNamedAnnotationSets());
1037
1038    Factory.deleteResource(doc2);
1039    doc2 = null;
1040
1041    //12. remove aset
1042    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//--    doc2 = (Document)ds.getLr(DBHelper.DOCUMENT_CLASS,sampleDoc_lrID);
1048    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    //13. unlock
1059    ds.unlockLr(lr);
1060    ds.sync(lr);
1061
1062    //close
1063    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///Err.prln("Use case 04 started...");
1077    //delete a document
1078    LanguageResource lr = null;
1079
1080    //0. get security factory & login
1081    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    //1. open data storage
1096    DatabaseDataStore ds = this._createDS();
1097    Assert.assertNotNull(ds);
1098    ds.setStorageUrl(TestPersist.JDBC_URL);
1099    ds.open();
1100    ds.setSession(usrSession);
1101
1102    //2. read LR
1103    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    //2.5 get exclusive lock
1109    if (false == ds.lockLr(lr)) {
1110      throw new PersistenceException("document is locked by another user");
1111    }
1112
1113    //3. try to delete it
1114    ds.delete(DBHelper.DOCUMENT_CLASS,lr.getLRPersistenceId());
1115
1116    //no need to unlock
1117
1118    //close
1119    ds.close();
1120    ac.close();
1121
1122    if(DEBUG) {
1123      Err.prln("Use case 04 passed...");
1124    }
1125
1126  }
1127
1128
1129  /** Test the DS register. */
1130  private void _testDB_UseCase101() throws Exception {
1131///Err.prln("Use case 101 started...");
1132    //descr : create a corpus
1133
1134    //0. get security factory & login
1135    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    //1. open data storage
1150    DatabaseDataStore ds = this._createDS();
1151    Assert.assertNotNull(ds);
1152    ds.setStorageUrl(TestPersist.JDBC_URL);
1153    ds.open();
1154    ds.setSession(usrSession);
1155
1156    //2. get test document
1157    Corpus corp = createTestCorpus();
1158    Assert.assertNotNull(corp);
1159
1160    //4. create security settings for doc
1161    SecurityInfo si = new SecurityInfo(SecurityInfo.ACCESS_WR_GW,usr,grp);
1162
1163    //5. try adding corpus to data store
1164    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    //6.close
1173    ac.close();
1174    ds.close();
1175
1176    if(DEBUG) {
1177      Err.prln("Use case 101 passed...");
1178    }
1179
1180  }
1181
1182
1183
1184  /** Test the DS register. */
1185  private void _testDB_UseCase102() throws Exception {
1186    //read a corpus
1187///Err.prln("Use case 102 started...");
1188    LanguageResource lr = null;
1189
1190    //0. get security factory & login
1191    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    //1. open data storage
1206    DatabaseDataStore ds = this._createDS();
1207    Assert.assertNotNull(ds);
1208    ds.setStorageUrl(TestPersist.JDBC_URL);
1209    ds.open();
1210    ds.setSession(usrSession);
1211
1212    //2. read LR
1213    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    //3. check name
1219    String name = lr.getName();
1220    Assert.assertNotNull(name);
1221    Assert.assertEquals(name,sampleCorpus.getName());
1222
1223    //4. check features
1224    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    //close
1240    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///Err.prln("Use case 103 started...");
1251    //sync a corpus
1252    LanguageResource lr = null;
1253
1254    //0. get security factory & login
1255    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    //1. open data storage
1270    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    //2. read LR
1279    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    //3. change name
1288    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    //4. change features
1297    FeatureMap fm = dbCorp.getFeatures();
1298    Iterator keys = fm.keySet().iterator();
1299
1300    //4.1 change the value of the first feature
1301    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    //close
1323    ds.close();
1324
1325    if(DEBUG) {
1326      Err.prln("Use case 103 passed...");
1327    }
1328
1329}
1330
1331//  public void testOracle_01() throws Exception {
1332//
1333//    if (DEBUG)
1334//      System.out.println(">> 01");
1335//
1336//    prepareDB("oracle");
1337//    _testDB_UseCase01();
1338//
1339//    if (DEBUG)
1340//      System.out.println("<< 01");
1341//  }
1342//
1343//  public void testOracle_02() throws Exception {
1344//
1345//    if (DEBUG)
1346//      System.out.println(">> 02");
1347//
1348//    prepareDB("oracle");
1349//    _testDB_UseCase02();
1350//
1351//    if (DEBUG)
1352//      System.out.println("<< 02");
1353//  }
1354//
1355//  public void testOracle_03() throws Exception {
1356//    if (DEBUG)
1357//      System.out.println(">> 03");
1358//
1359//    prepareDB("oracle");
1360//    _testDB_UseCase03();
1361//
1362//    if (DEBUG)
1363//      System.out.println("<< 03");
1364//  }
1365//
1366//  public void testOracle_04() throws Exception {
1367//    if (DEBUG)
1368//      System.out.println(">> 04");
1369//
1370//    prepareDB("oracle");
1371//    _testDB_UseCase04();
1372//
1373//    if (DEBUG)
1374//      System.out.println("<< 04");
1375//  }
1376//
1377//  public void testOracle_101() throws Exception {
1378//    if (DEBUG)
1379//      System.out.println(">> 101");
1380//
1381//    prepareDB("oracle");
1382//    _testDB_UseCase101();
1383//
1384//    if (DEBUG)
1385//      System.out.println("<< 101");
1386//  }
1387//
1388//  public void testOracle_102() throws Exception {
1389//    if (DEBUG)
1390//      System.out.println(">> 102");
1391//
1392//    prepareDB("oracle");
1393//    _testDB_UseCase102();
1394//
1395//    if (DEBUG)
1396//      System.out.println("<< 102");
1397//  }
1398//
1399//  public void testOracle_103() throws Exception {
1400//    if (DEBUG)
1401//      System.out.println(">> 103");
1402//
1403//    prepareDB("oracle");
1404//    _testDB_UseCase103();
1405//
1406//    if (DEBUG)
1407//      System.out.println("<< 103");
1408//  }
1409
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//-System.setProperty(Gate.GATE_CONFIG_PROPERTY,"y:/gate.xml")    ;
1458      Gate.setLocalWebServer(false);
1459      Gate.setNetConnected(false);
1460      Gate.init();
1461
1462
1463      TestPersist test = new TestPersist("");
1464
1465/*
1466      long timeStart = 0;
1467      timeStart = System.currentTimeMillis();
1468      int size = 512*1024;
1469//      test.testOracleLOB(size,3);
1470      test.testPostgresLOB(size,3);
1471      System.out.println("time: ["+ (System.currentTimeMillis()-timeStart) +"]");
1472
1473      if (true) {
1474        throw new RuntimeException();
1475      }
1476*/
1477
1478      /* oracle */
1479
1480//      test.setUp();
1481//      test.testOracle_01();
1482//      test.tearDown();
1483//
1484//      test.setUp();
1485//      test.testOracle_02();
1486//      test.tearDown();
1487//
1488//      test.setUp();
1489//      test.testOracle_03();
1490//      test.tearDown();
1491//
1492//      test.setUp();
1493//      test.testOracle_04();
1494//      test.tearDown();
1495//
1496//      test.setUp();
1497//      test.testOracle_101();
1498//      test.tearDown();
1499//
1500//      test.setUp();
1501//      test.testOracle_102();
1502//      test.tearDown();
1503//
1504//      test.setUp();
1505//      test.testOracle_103();
1506//      test.tearDown();
1507
1508
1509      /* postgres */
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.testSaveRestore();
1553      test.tearDown();
1554
1555      test.setUp();
1556      test.testSimple();
1557      test.tearDown();
1558
1559      //I put this last because its failure is dependent on the gc() and
1560      //there's nothing I can do about it. Maybe I'll remove this from the
1561      //test
1562      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/*
1575  public void testPostgresLOB(int size, int count) throws Exception {
1576
1577    byte[] buffer = new byte[size];
1578    String url = "jdbc:postgresql://192.168.128.208:5432/gate09?user=gateuser&password=gate";
1579//    ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
1580
1581    try {
1582      Connection conn = DBHelper.connect(url);
1583      conn.setAutoCommit(false);
1584      PreparedStatement pstmt = conn.prepareStatement("insert into lob_test values(?)");
1585
1586      for (int i =0; i< count; i++) {
1587//        bais.reset();
1588//        pstmt.setBinaryStream(1,bais,buffer.length);
1589        pstmt.setBytes(1,buffer);
1590        pstmt.executeUpdate();
1591        conn.commit();
1592      }
1593    }
1594    catch(Exception e) {
1595      e.printStackTrace();
1596    }
1597
1598
1599  }
1600
1601  public void testOracleLOB(int size,int count) throws Exception {
1602    byte[] buffer = new byte[size];
1603    String url = "jdbc:oracle:thin:GATEUSER/gate@192.168.128.208:1521:gate07";
1604    ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
1605
1606    CallableStatement cstmt = null;
1607    PreparedStatement pstmt = null;
1608    ResultSet rs = null;
1609    Blob blobValue = null;
1610
1611    try {
1612      Connection conn = DBHelper.connect(url);
1613      conn.setAutoCommit(false);
1614      cstmt = conn.prepareCall("{ call gateadmin.create_lob(?) }");
1615
1616
1617      for (int i =0; i< count; i++) {
1618
1619        cstmt.registerOutParameter(1,java.sql.Types.BIGINT);
1620        cstmt.execute();
1621        long blobID = cstmt.getLong(1);
1622
1623        pstmt = conn.prepareStatement("select blob_value from gateadmin.lob_test where id=?");
1624        pstmt.setLong(1,blobID);
1625        pstmt.execute();
1626        rs = pstmt.getResultSet();
1627        rs.next();
1628
1629        blobValue = rs.getBlob(1);
1630        BLOB oraBlob = (BLOB)blobValue;
1631        OutputStream output = oraBlob.getBinaryOutputStream();
1632        output.write(buffer,0,buffer.length);
1633        output.close();
1634
1635        conn.commit();
1636      }
1637
1638    }
1639    catch(Exception e) {
1640      e.printStackTrace();
1641    }
1642
1643  }
1644
1645*/
1646
1647
1648/*
1649  public void testPostgres01() throws Exception {
1650
1651    String url = "jdbc:postgresql://192.168.128.208:5432/gate09";
1652    try {
1653
1654      Connection c = DBHelper.connect(url,"gateuser","gate");
1655      c.setAutoCommit(false);
1656
1657      Object src = new Long(1234);
1658
1659      PreparedStatement pstmt = c.prepareStatement("insert into test3 values (nextval('seq3'), ?)");
1660      Object o = new Object();
1661
1662      ByteArrayOutputStream baos = new ByteArrayOutputStream();
1663      ObjectOutputStream oos = new ObjectOutputStream(baos);
1664      oos.writeObject(src);
1665      oos.flush();
1666      oos.close();
1667      baos.close();
1668
1669      byte[] buff = baos.toByteArray();
1670System.out.println(buff.length);
1671      ByteArrayInputStream bais = new ByteArrayInputStream(buff);
1672
1673      pstmt.setBinaryStream(1,bais,buff.length);
1674      pstmt.execute();
1675bais.close();
1676      c.commit();
1677      bais.close();
1678
1679      PreparedStatement pstmt2 = c.prepareStatement("select blob from test3 where id = (select max(id) from test3)");
1680      pstmt2.execute();
1681      ResultSet rs = pstmt2.getResultSet();
1682      if (false == rs.next()) {
1683        throw new Exception("empty result set");
1684      }
1685
1686      InputStream is = rs.getBinaryStream("blob");
1687      ObjectInputStream ois = new ObjectInputStream(is);
1688      Object result = ois.readObject();
1689System.out.println(result);
1690      ois.close();
1691      is.close();
1692
1693      rs.close();
1694      pstmt2.close();
1695
1696      c.commit();
1697
1698    }
1699    catch(SQLException e) {
1700System.out.println(e.getErrorCode());
1701      e.printStackTrace();
1702    }
1703
1704  }
1705*/
1706
1707} // class TestPersist
1708
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