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