| TestDatabaseAnnotationSet |

    1   /*
    2    *  TestDatabaseAnnotationSet.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   *  Kalina Bontcheva, 21/Oct/2001
    12   *
    13   *  $Id: TestDatabaseAnnotationSet.java,v 1.3 2001/10/30 12:45:33 valyt Exp $
    14   */
    15
    16  package gate.annotation;
    17
    18  import java.util.*;
    19  import java.io.*;
    20  import junit.framework.*;
    21  import java.net.*;
    22
    23  import gate.*;
    24  import gate.util.*;
    25  import gate.corpora.*;
    26
    27  /** Tests for the DatabaseAnnotationSet class
    28    */
    29  public class TestDatabaseAnnotationSet extends TestCase
    30  {
    31    /** Debug flag */
    32    private static final boolean DEBUG = false;
    33
    34    /** Construction */
    35    public TestDatabaseAnnotationSet(String name) { super(name); }
    36
    37    /** A document */
    38    protected Document doc1;
    39
    40    /** An annotation set */
    41    protected AnnotationSet basicAS;
    42
    43    /** An empty feature map */
    44    protected FeatureMap emptyFeatureMap;
    45
    46    /** Fixture set up */
    47    public void setUp() throws Exception
    48    {
    49      String server = TestDocument.getTestServerName();
    50      assertNotNull(server);
    51      FeatureMap params = Factory.newFeatureMap();
    52      params.put("sourceUrl", Gate.getUrl("tests/doc0.html"));
    53      params.put("markupAware", "false");
    54      doc1 = (Document)Factory.createResource("gate.corpora.DocumentImpl",
    55                                                      params);
    56
    57      emptyFeatureMap = new SimpleFeatureMapImpl();
    58
    59      basicAS = new DatabaseAnnotationSetImpl(doc1);
    60      FeatureMap fm = new SimpleFeatureMapImpl();
    61
    62      basicAS.get("T");          // to trigger type indexing
    63      basicAS.get(new Long(0));  // trigger offset index (though add will too)
    64
    65      basicAS.add(new Long(10), new Long(20), "T1", fm);    // 0
    66      basicAS.add(new Long(10), new Long(20), "T2", fm);    // 1
    67      basicAS.add(new Long(10), new Long(20), "T3", fm);    // 2
    68      basicAS.add(new Long(10), new Long(20), "T1", fm);    // 3
    69
    70      fm = new SimpleFeatureMapImpl();
    71      fm.put("pos", "NN");
    72      fm.put("author", "hamish");
    73      fm.put("version", new Integer(1));
    74
    75      basicAS.add(new Long(10), new Long(20), "T1", fm);    // 4
    76      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 5
    77      basicAS.add(new Long(15), new Long(40), "T3", fm);    // 6
    78      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 7
    79
    80      fm = new SimpleFeatureMapImpl();
    81      fm.put("pos", "JJ");
    82      fm.put("author", "the devil himself");
    83      fm.put("version", new Long(44));
    84      fm.put("created", "monday");
    85
    86      basicAS.add(new Long(15), new Long(40), "T3", fm);    // 8
    87      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 9
    88      basicAS.add(new Long(15), new Long(40), "T1", fm);    // 10
    89
    90      // Out.println(basicAS);
    91    } // setUp
    92
    93
    94    /** Test remove */
    95    public void testRemove() {
    96      AnnotationSet asBuf = basicAS.get("T1");
    97      assertEquals(7, asBuf.size());
    98      asBuf = basicAS.get(new Long(9));
    99      assertEquals(5, asBuf.size());
    100
    101     basicAS.remove(basicAS.get(new Integer(0)));
    102
    103     assertEquals(10, basicAS.size());
    104     assertEquals(10, ((DatabaseAnnotationSetImpl) basicAS).annotsById.size());
    105
    106     asBuf = basicAS.get("T1");
    107     assertEquals(6, asBuf.size());
    108
    109     asBuf = basicAS.get(new Long(9));
    110     assertEquals(4, asBuf.size());
    111     assertEquals(null, basicAS.get(new Integer(0)));
    112     basicAS.remove(basicAS.get(new Integer(8)));
    113     assertEquals(9, basicAS.size());
    114     basicAS.removeAll(basicAS);
    115     assertEquals(null, basicAS.get());
    116     assertEquals(null, basicAS.get("T1"));
    117     assertEquals(null, basicAS.get(new Integer(0)));
    118   } // testRemove()
    119
    120   public void testRemoveInexistant() throws Exception{
    121     basicAS.add(new Long(0), new Long(10), "Foo", emptyFeatureMap);
    122     Annotation ann = (Annotation)basicAS.get("Foo").iterator().next();
    123     basicAS.remove(ann);
    124     //the second remove should do nothing...
    125     basicAS.remove(ann);
    126   }
    127
    128   /** Test iterator remove */
    129   public void testIteratorRemove() {
    130     AnnotationSet asBuf = basicAS.get("T1");
    131     assertEquals(7, asBuf.size());
    132     asBuf = basicAS.get(new Long(9));
    133     assertEquals(5, asBuf.size());
    134
    135     // remove annotation with id 0; this is returned last by the
    136     // iterator
    137     Iterator iter = basicAS.iterator();
    138     while(iter.hasNext())
    139       iter.next();
    140     iter.remove();
    141
    142     assertEquals(10, basicAS.size());
    143     assertEquals(10, ((DatabaseAnnotationSetImpl) basicAS).annotsById.size());
    144     asBuf = basicAS.get("T1");
    145     assertEquals(6, asBuf.size());
    146     asBuf = basicAS.get(new Long(9));
    147     assertEquals(4, asBuf.size());
    148     assertEquals(null, basicAS.get(new Integer(0)));
    149     basicAS.remove(basicAS.get(new Integer(8)));
    150
    151   } // testIteratorRemove()
    152
    153   /** Test iterator */
    154   public void testIterator() {
    155     Iterator iter = basicAS.iterator();
    156     Annotation[] annots = new Annotation[basicAS.size()];
    157     int i = 0;
    158
    159     while(iter.hasNext()) {
    160       Annotation a = (Annotation) iter.next();
    161       annots[i++] = a;
    162
    163       assertTrue(basicAS.contains(a));
    164       iter.remove();
    165       assertTrue(!basicAS.contains(a));
    166     } // while
    167
    168     i = 0;
    169     while(i < annots.length) {
    170       basicAS.add(annots[i++]);
    171       assertEquals(i, basicAS.size());
    172     } // while
    173
    174     AnnotationSet asBuf = basicAS.get("T1");
    175     assertEquals(7, asBuf.size());
    176     asBuf = basicAS.get(new Long(9));
    177     assertEquals(5, asBuf.size());
    178
    179     AnnotationSet testAS = new DatabaseAnnotationSetImpl(doc1, "test");
    180     testAS.add(basicAS.get(new Integer(1)));
    181     testAS.add(basicAS.get(new Integer(4)));
    182     testAS.add(basicAS.get(new Integer(5)));
    183     testAS.add(basicAS.get(new Integer(0)));
    184     Annotation ann = testAS.get(new Integer(0));
    185     FeatureMap features = ann.getFeatures();
    186     features.put("test", "test value");
    187
    188     Annotation ann1 = testAS.get(new Integer(4));
    189     FeatureMap features1 = ann1.getFeatures();
    190     features1.remove("pos");
    191
    192     FeatureMap newFeatures = Factory.newFeatureMap();
    193     newFeatures.put("my test", "my value");
    194     Annotation ann2 = testAS.get(new Integer(5));
    195     ann2.setFeatures(newFeatures);
    196     if (DEBUG) Out.prln("ann 2 features: " + ann2.getFeatures());
    197
    198     testAS.remove(basicAS.get(new Integer(0)));
    199     if (DEBUG) Out.prln("Test AS is : " + testAS);
    200
    201     AnnotationSet fromTransientSet = new DatabaseAnnotationSetImpl(basicAS);
    202     ann = fromTransientSet.get(new Integer(0));
    203     features = ann.getFeatures();
    204     features.put("test", "test value");
    205
    206     ann1 = fromTransientSet.get(new Integer(4));
    207     features1 = ann1.getFeatures();
    208     features1.remove("pos");
    209
    210     newFeatures = Factory.newFeatureMap();
    211     newFeatures.put("my test", "my value");
    212     ann2 = fromTransientSet.get(new Integer(5));
    213     ann2.setFeatures(newFeatures);
    214
    215     if (DEBUG) Out.prln("From transient set is : " + fromTransientSet);
    216
    217   } // testIterator
    218
    219   /** Test Set methods */
    220   public void testSetMethods() throws Exception {
    221     basicAS.clear();
    222     setUp();
    223
    224     Annotation a = basicAS.get(new Integer(6));
    225     assertTrue(basicAS.contains(a));
    226
    227     Annotation[] annotArray =
    228       (Annotation[]) basicAS.toArray(new Annotation[0]);
    229     Object[] annotObjectArray = basicAS.toArray();
    230     assertEquals(11, annotArray.length);
    231     assertEquals(11, annotObjectArray.length);
    232
    233     SortedSet sortedAnnots = new TreeSet(basicAS);
    234     annotArray = (Annotation[]) sortedAnnots.toArray(new Annotation[0]);
    235     for(int i = 0; i<11; i++)
    236       assertTrue( annotArray[i].getId().equals(new Integer(i)) );
    237
    238     Annotation a1 = basicAS.get(new Integer(3));
    239     Annotation a2 = basicAS.get(new Integer(4));
    240     Set a1a2 = new HashSet();
    241     a1a2.add(a1);
    242     a1a2.add(a2);
    243     assertTrue(basicAS.contains(a1));
    244     assertTrue(basicAS.containsAll(a1a2));
    245     basicAS.removeAll(a1a2);
    246
    247     assertEquals(9, basicAS.size());
    248     assertTrue(! basicAS.contains(a1));
    249     assertTrue(! basicAS.containsAll(a1a2));
    250
    251     basicAS.addAll(a1a2);
    252     assertTrue(basicAS.contains(a2));
    253     assertTrue(basicAS.containsAll(a1a2));
    254
    255     assertTrue(basicAS.retainAll(a1a2));
    256     assertTrue(basicAS.equals(a1a2));
    257
    258     basicAS.clear();
    259     assertTrue(basicAS.isEmpty());
    260
    261   } // testSetMethods()
    262
    263   /** Test AnnotationSetImpl */
    264   public void testAnnotationSet() throws Exception {
    265     // constuct an empty AS
    266     FeatureMap params = Factory.newFeatureMap();
    267     params.put("sourceUrl", Gate.getUrl("tests/doc0.html"));
    268     params.put("markupAware", "false");
    269     Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
    270                                                     params);
    271
    272     AnnotationSet as = new DatabaseAnnotationSetImpl(doc);
    273     assertEquals(as.size(), 0);
    274
    275     // add some annotations
    276     FeatureMap fm1 = Factory.newFeatureMap();
    277     fm1.put("test", "my-value");
    278     FeatureMap fm2 = Factory.newFeatureMap();
    279     fm2.put("test", "my-value-different");
    280     FeatureMap fm3 = Factory.newFeatureMap();
    281     fm3.put("another test", "different my-value");
    282
    283     Integer newId;
    284     newId =
    285       as.add(new Long(0), new Long(10), "Token", fm1);
    286     assertEquals(newId.intValue(), 0);
    287     newId =
    288       as.add(new Long(11), new Long(12), "Token", fm2);
    289     assertEquals(newId.intValue(), 1);
    290     assertEquals(as.size(), 2);
    291     assertTrue(! as.isEmpty());
    292     newId =
    293       as.add(new Long(15), new Long(22), "Syntax", fm1);
    294
    295     // get by ID; remove; add(object)
    296     Annotation a = as.get(new Integer(1));
    297     as.remove(a);
    298     assertEquals(as.size(), 2);
    299     as.add(a);
    300     assertEquals(as.size(), 3);
    301
    302     // iterate over the annotations
    303     Iterator iter = as.iterator();
    304     while(iter.hasNext()) {
    305       a = (Annotation) iter.next();
    306       if(a.getId().intValue() != 2)
    307         assertEquals(a.getType(), "Token");
    308       assertEquals(a.getFeatures().size(), 1);
    309     }
    310
    311     // add some more
    312     newId =
    313       as.add(new Long(0), new Long(12), "Syntax", fm3);
    314     assertEquals(newId.intValue(), 3);
    315     newId =
    316       as.add(new Long(14), new Long(22), "Syntax", fm1);
    317     assertEquals(newId.intValue(), 4);
    318     assertEquals(as.size(), 5);
    319     newId =
    320       as.add(new Long(15), new Long(22), "Syntax", new SimpleFeatureMapImpl());
    321
    322     //get by feature names
    323     HashSet hs = new HashSet();
    324     hs.add("test");
    325     AnnotationSet fnSet = as.get("Token", hs);
    326     assertEquals(fnSet.size(), 2);
    327     //now try without a concrete type, just features
    328     //we'll get some Syntax ones now too
    329     fnSet = as.get(null, hs);
    330     assertEquals(fnSet.size(), 4);
    331
    332
    333     // indexing by type
    334     ((DatabaseAnnotationSetImpl) as).indexByType();
    335     AnnotationSet tokenAnnots = as.get("Token");
    336     assertEquals(tokenAnnots.size(), 2);
    337
    338     // indexing by position
    339     AnnotationSet annotsAfter10 = as.get(new Long(15));
    340     if(annotsAfter10 == null)
    341       fail("no annots found after offset 10");
    342     assertEquals(annotsAfter10.size(), 2);
    343
    344   } // testAnnotationSet
    345   /** Test suite routine for the test runner */
    346   public static Test suite() {
    347     return new TestSuite(TestAnnotation.class);
    348   } // suite
    349
    350
    351   public static void main(String[] args){
    352
    353     try{
    354       Gate.init();
    355       TestDatabaseAnnotationSet testAnnot = new TestDatabaseAnnotationSet("");
    356       Out.prln("test set up");
    357       testAnnot.setUp();
    358       Out.prln("testIterator");
    359       testAnnot.testIterator();
    360       Out.prln("testAnnotationSet");
    361       testAnnot.testAnnotationSet();
    362       Out.prln("testRemove");
    363       testAnnot.testRemove();
    364       Out.prln("testInexistant");
    365       testAnnot.testRemoveInexistant();
    366       Out.prln("testSetMethods");
    367       testAnnot.testSetMethods();
    368       testAnnot.tearDown();
    369     }catch(Throwable t){
    370       t.printStackTrace();
    371     }
    372   }
    373 } // class TestAnnotation
    374
    375

| TestDatabaseAnnotationSet |