001 /*
002 * TestDatabaseAnnotationSet.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Kalina Bontcheva, 21/Oct/2001
013 *
014 * $Id: TestDatabaseAnnotationSet.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.annotation;
018
019 import java.util.*;
020
021 import junit.framework.*;
022
023 import gate.*;
024 import gate.corpora.TestDocument;
025 import gate.util.Out;
026 import gate.util.SimpleFeatureMapImpl;
027
028 /** Tests for the DatabaseAnnotationSet class
029 */
030 public class TestDatabaseAnnotationSet extends TestCase
031 {
032 /** Debug flag */
033 private static final boolean DEBUG = false;
034
035 /** Construction */
036 public TestDatabaseAnnotationSet(String name) { super(name); }
037
038 /** A document */
039 protected Document doc1;
040
041 /** An annotation set */
042 protected AnnotationSet basicAS;
043
044 /** An empty feature map */
045 protected FeatureMap emptyFeatureMap;
046
047 /** Fixture set up */
048 public void setUp() throws Exception
049 {
050 String server = TestDocument.getTestServerName();
051 assertNotNull(server);
052 FeatureMap params = Factory.newFeatureMap();
053 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
054 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
055 doc1 = (Document)Factory.createResource("gate.corpora.DocumentImpl",
056 params);
057
058 emptyFeatureMap = new SimpleFeatureMapImpl();
059
060 basicAS = new DatabaseAnnotationSetImpl(doc1);
061 FeatureMap fm = new SimpleFeatureMapImpl();
062
063 basicAS.get("T"); // to trigger type indexing
064 basicAS.get(new Long(0)); // trigger offset index (though add will too)
065
066 basicAS.add(new Long(10), new Long(20), "T1", fm); // 0
067 basicAS.add(new Long(10), new Long(20), "T2", fm); // 1
068 basicAS.add(new Long(10), new Long(20), "T3", fm); // 2
069 basicAS.add(new Long(10), new Long(20), "T1", fm); // 3
070
071 fm = new SimpleFeatureMapImpl();
072 fm.put("pos", "NN");
073 fm.put("author", "hamish");
074 fm.put("version", new Integer(1));
075
076 basicAS.add(new Long(10), new Long(20), "T1", fm); // 4
077 basicAS.add(new Long(15), new Long(40), "T1", fm); // 5
078 basicAS.add(new Long(15), new Long(40), "T3", fm); // 6
079 basicAS.add(new Long(15), new Long(40), "T1", fm); // 7
080
081 fm = new SimpleFeatureMapImpl();
082 fm.put("pos", "JJ");
083 fm.put("author", "the devil himself");
084 fm.put("version", new Long(44));
085 fm.put("created", "monday");
086
087 basicAS.add(new Long(15), new Long(40), "T3", fm); // 8
088 basicAS.add(new Long(15), new Long(40), "T1", fm); // 9
089 basicAS.add(new Long(15), new Long(40), "T1", fm); // 10
090
091 // Out.println(basicAS);
092 } // setUp
093
094
095 /** Test remove */
096 public void testRemove() {
097 AnnotationSet asBuf = basicAS.get("T1");
098 assertEquals(7, asBuf.size());
099 asBuf = basicAS.get(new Long(9));
100 assertEquals(5, asBuf.size());
101
102 basicAS.remove(basicAS.get(new Integer(0)));
103
104 assertEquals(10, basicAS.size());
105 assertEquals(10, ((DatabaseAnnotationSetImpl) basicAS).annotsById.size());
106
107 asBuf = basicAS.get("T1");
108 assertEquals(6, asBuf.size());
109
110 asBuf = basicAS.get(new Long(9));
111 assertEquals(4, asBuf.size());
112 assertEquals(null, basicAS.get(new Integer(0)));
113 basicAS.remove(basicAS.get(new Integer(8)));
114 assertEquals(9, basicAS.size());
115 basicAS.removeAll(basicAS);
116 assertEquals(null, basicAS.get());
117 assertEquals(null, basicAS.get("T1"));
118 assertEquals(null, basicAS.get(new Integer(0)));
119 } // testRemove()
120
121 public void testRemoveInexistant() throws Exception{
122 basicAS.add(new Long(0), new Long(10), "Foo", emptyFeatureMap);
123 Annotation ann = basicAS.get("Foo").iterator().next();
124 basicAS.remove(ann);
125 //the second remove should do nothing...
126 basicAS.remove(ann);
127 }
128
129 /** Test iterator remove */
130 /* This seems to be a bad idea - just testing order of hashset iterator, which
131 * isn't stable....
132 *
133 public void testIteratorRemove() {
134 AnnotationSet asBuf = basicAS.get("T1");
135 assertEquals(7, asBuf.size());
136 asBuf = basicAS.get(new Long(9));
137 assertEquals(5, asBuf.size());
138
139 // remove annotation with id 0; this is returned last by the
140 // iterator
141 Iterator iter = basicAS.iterator();
142 while(iter.hasNext())
143 iter.next();
144 iter.remove();
145
146 assertEquals(10, basicAS.size());
147 assertEquals(10, ((DatabaseAnnotationSetImpl) basicAS).annotsById.size());
148 asBuf = basicAS.get("T1");
149 assertEquals(6, asBuf.size());
150 asBuf = basicAS.get(new Long(9));
151 assertEquals(4, asBuf.size());
152 assertEquals(null, basicAS.get(new Integer(0)));
153 basicAS.remove(basicAS.get(new Integer(8)));
154
155 } // testIteratorRemove()
156 */
157
158 /** Test iterator */
159 public void testIterator() {
160 Iterator<Annotation> iter = basicAS.iterator();
161 Annotation[] annots = new Annotation[basicAS.size()];
162 int i = 0;
163
164 while(iter.hasNext()) {
165 Annotation a = iter.next();
166 annots[i++] = a;
167
168 assertTrue(basicAS.contains(a));
169 iter.remove();
170 assertTrue(!basicAS.contains(a));
171 } // while
172
173 i = 0;
174 while(i < annots.length) {
175 basicAS.add(annots[i++]);
176 assertEquals(i, basicAS.size());
177 } // while
178
179 AnnotationSet asBuf = basicAS.get("T1");
180 assertEquals(7, asBuf.size());
181 asBuf = basicAS.get(new Long(9));
182 assertEquals(5, asBuf.size());
183
184 AnnotationSet testAS = new DatabaseAnnotationSetImpl(doc1, "test");
185 testAS.add(basicAS.get(new Integer(1)));
186 testAS.add(basicAS.get(new Integer(4)));
187 testAS.add(basicAS.get(new Integer(5)));
188 testAS.add(basicAS.get(new Integer(0)));
189 Annotation ann = testAS.get(new Integer(0));
190 FeatureMap features = ann.getFeatures();
191 features.put("test", "test value");
192
193 Annotation ann1 = testAS.get(new Integer(4));
194 FeatureMap features1 = ann1.getFeatures();
195 features1.remove("pos");
196
197 FeatureMap newFeatures = Factory.newFeatureMap();
198 newFeatures.put("my test", "my value");
199 Annotation ann2 = testAS.get(new Integer(5));
200 ann2.setFeatures(newFeatures);
201 if (DEBUG) Out.prln("ann 2 features: " + ann2.getFeatures());
202
203 testAS.remove(basicAS.get(new Integer(0)));
204 if (DEBUG) Out.prln("Test AS is : " + testAS);
205
206 AnnotationSet fromTransientSet = new DatabaseAnnotationSetImpl(basicAS);
207 ann = fromTransientSet.get(new Integer(0));
208 features = ann.getFeatures();
209 features.put("test", "test value");
210
211 ann1 = fromTransientSet.get(new Integer(4));
212 features1 = ann1.getFeatures();
213 features1.remove("pos");
214
215 newFeatures = Factory.newFeatureMap();
216 newFeatures.put("my test", "my value");
217 ann2 = fromTransientSet.get(new Integer(5));
218 ann2.setFeatures(newFeatures);
219
220 if (DEBUG) Out.prln("From transient set is : " + fromTransientSet);
221
222 } // testIterator
223
224 /** Test Set methods */
225 public void testSetMethods() throws Exception {
226 basicAS.clear();
227 setUp();
228
229 Annotation a = basicAS.get(new Integer(6));
230 assertTrue(basicAS.contains(a));
231
232 Annotation[] annotArray =
233 (Annotation[]) basicAS.toArray(new Annotation[0]);
234 Object[] annotObjectArray = basicAS.toArray();
235 assertEquals(11, annotArray.length);
236 assertEquals(11, annotObjectArray.length);
237
238 SortedSet sortedAnnots = new TreeSet(basicAS);
239 annotArray = (Annotation[]) sortedAnnots.toArray(new Annotation[0]);
240 for(int i = 0; i<11; i++)
241 assertTrue( annotArray[i].getId().equals(new Integer(i)) );
242
243 Annotation a1 = basicAS.get(new Integer(3));
244 Annotation a2 = basicAS.get(new Integer(4));
245 Set a1a2 = new HashSet();
246 a1a2.add(a1);
247 a1a2.add(a2);
248 assertTrue(basicAS.contains(a1));
249 assertTrue(basicAS.containsAll(a1a2));
250 basicAS.removeAll(a1a2);
251
252 assertEquals(9, basicAS.size());
253 assertTrue(! basicAS.contains(a1));
254 assertTrue(! basicAS.containsAll(a1a2));
255
256 basicAS.addAll(a1a2);
257 assertTrue(basicAS.contains(a2));
258 assertTrue(basicAS.containsAll(a1a2));
259
260 assertTrue(basicAS.retainAll(a1a2));
261 assertTrue(basicAS.equals(a1a2));
262
263 basicAS.clear();
264 assertTrue(basicAS.isEmpty());
265
266 } // testSetMethods()
267
268 /** Test AnnotationSetImpl */
269 public void testAnnotationSet() throws Exception {
270 // constuct an empty AS
271 FeatureMap params = Factory.newFeatureMap();
272 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
273 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
274 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
275 params);
276
277 AnnotationSet as = new DatabaseAnnotationSetImpl(doc);
278 assertEquals(as.size(), 0);
279
280 // add some annotations
281 FeatureMap fm1 = Factory.newFeatureMap();
282 fm1.put("test", "my-value");
283 FeatureMap fm2 = Factory.newFeatureMap();
284 fm2.put("test", "my-value-different");
285 FeatureMap fm3 = Factory.newFeatureMap();
286 fm3.put("another test", "different my-value");
287
288 Integer newId;
289 newId =
290 as.add(new Long(0), new Long(10), "Token", fm1);
291 assertEquals(newId.intValue(), 0);
292 newId =
293 as.add(new Long(11), new Long(12), "Token", fm2);
294 assertEquals(newId.intValue(), 1);
295 assertEquals(as.size(), 2);
296 assertTrue(! as.isEmpty());
297 newId =
298 as.add(new Long(15), new Long(22), "Syntax", fm1);
299
300 // get by ID; remove; add(object)
301 Annotation a = as.get(new Integer(1));
302 as.remove(a);
303 assertEquals(as.size(), 2);
304 as.add(a);
305 assertEquals(as.size(), 3);
306
307 // iterate over the annotations
308 Iterator<Annotation> iter = as.iterator();
309 while(iter.hasNext()) {
310 a = iter.next();
311 if(a.getId().intValue() != 2)
312 assertEquals(a.getType(), "Token");
313 assertEquals(a.getFeatures().size(), 1);
314 }
315
316 // add some more
317 newId =
318 as.add(new Long(0), new Long(12), "Syntax", fm3);
319 assertEquals(newId.intValue(), 3);
320 newId =
321 as.add(new Long(14), new Long(22), "Syntax", fm1);
322 assertEquals(newId.intValue(), 4);
323 assertEquals(as.size(), 5);
324 newId =
325 as.add(new Long(15), new Long(22), "Syntax", new SimpleFeatureMapImpl());
326
327 //get by feature names
328 HashSet hs = new HashSet();
329 hs.add("test");
330 AnnotationSet fnSet = as.get("Token", hs);
331 assertEquals(fnSet.size(), 2);
332 //now try without a concrete type, just features
333 //we'll get some Syntax ones now too
334 fnSet = as.get(null, hs);
335 assertEquals(fnSet.size(), 4);
336
337
338 // indexing by type
339 ((DatabaseAnnotationSetImpl) as).indexByType();
340 AnnotationSet tokenAnnots = as.get("Token");
341 assertEquals(tokenAnnots.size(), 2);
342
343 // indexing by position
344 AnnotationSet annotsAfter10 = as.get(new Long(15));
345 if(annotsAfter10 == null)
346 fail("no annots found after offset 10");
347 assertEquals(annotsAfter10.size(), 2);
348
349 } // testAnnotationSet
350 /** Test suite routine for the test runner */
351 public static Test suite() {
352 return new TestSuite(TestAnnotation.class);
353 } // suite
354
355
356 public static void main(String[] args){
357
358 try{
359 Gate.init();
360 TestDatabaseAnnotationSet testAnnot = new TestDatabaseAnnotationSet("");
361 Out.prln("test set up");
362 testAnnot.setUp();
363 Out.prln("testIterator");
364 testAnnot.testIterator();
365 Out.prln("testAnnotationSet");
366 testAnnot.testAnnotationSet();
367 Out.prln("testRemove");
368 testAnnot.testRemove();
369 Out.prln("testInexistant");
370 testAnnot.testRemoveInexistant();
371 Out.prln("testSetMethods");
372 testAnnot.testSetMethods();
373 testAnnot.tearDown();
374 }catch(Throwable t){
375 t.printStackTrace();
376 }
377 }
378 } // class TestAnnotation
|