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