| TestPersist |

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

| TestPersist |