1
15
16 package gate.annotation;
17
18 import java.util.*;
19
20 import junit.framework.*;
21
22 import gate.*;
23 import gate.corpora.TestDocument;
24 import gate.util.Out;
25 import gate.util.SimpleFeatureMapImpl;
26
27
29 public class TestDatabaseAnnotationSet extends TestCase
30 {
31
32 private static final boolean DEBUG = false;
33
34
35 public TestDatabaseAnnotationSet(String name) { super(name); }
36
37
38 protected Document doc1;
39
40
41 protected AnnotationSet basicAS;
42
43
44 protected FeatureMap emptyFeatureMap;
45
46
47 public void setUp() throws Exception
48 {
49 String server = TestDocument.getTestServerName();
50 assertNotNull(server);
51 FeatureMap params = Factory.newFeatureMap();
52 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
53 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "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"); basicAS.get(new Long(0));
65 basicAS.add(new Long(10), new Long(20), "T1", fm); basicAS.add(new Long(10), new Long(20), "T2", fm); basicAS.add(new Long(10), new Long(20), "T3", fm); basicAS.add(new Long(10), new Long(20), "T1", fm);
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); basicAS.add(new Long(15), new Long(40), "T1", fm); basicAS.add(new Long(15), new Long(40), "T3", fm); basicAS.add(new Long(15), new Long(40), "T1", fm);
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); basicAS.add(new Long(15), new Long(40), "T1", fm); basicAS.add(new Long(15), new Long(40), "T1", fm);
90 }
93
94
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 }
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 basicAS.remove(ann);
126 }
127
128
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 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 }
153
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 }
168 i = 0;
169 while(i < annots.length) {
170 basicAS.add(annots[i++]);
171 assertEquals(i, basicAS.size());
172 }
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 }
219
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 }
263
264 public void testAnnotationSet() throws Exception {
265 FeatureMap params = Factory.newFeatureMap();
267 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
268 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "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 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 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 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 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 HashSet hs = new HashSet();
324 hs.add("test");
325 AnnotationSet fnSet = as.get("Token", hs);
326 assertEquals(fnSet.size(), 2);
327 fnSet = as.get(null, hs);
330 assertEquals(fnSet.size(), 4);
331
332
333 ((DatabaseAnnotationSetImpl) as).indexByType();
335 AnnotationSet tokenAnnots = as.get("Token");
336 assertEquals(tokenAnnots.size(), 2);
337
338 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 }
346 public static Test suite() {
347 return new TestSuite(TestAnnotation.class);
348 }
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 }
375