1
15
16 package gate.creole;
17
18 import java.beans.*;
19 import java.lang.reflect.Method;
20 import java.net.URL;
21 import java.util.*;
22
23 import junit.framework.*;
24
25 import gate.*;
26 import gate.util.GateException;
27 import gate.util.Out;
28
29
31 public class TestCreole extends TestCase
32 {
33
34 private static final boolean DEBUG = false;
35
36
37 public TestCreole(String name) throws GateException { super(name); }
38
39
40 private CreoleRegister reg;
41
42
43 public void setUp() throws Exception {
44 Gate.init();
46
47 reg = Gate.getCreoleRegister();
49 reg.clear();
50
51 URL testUrl = Gate.getUrl("tests/");
53 reg.addDirectory(testUrl);
55 reg.registerDirectories();
56
57 if(DEBUG) {
58 Iterator iter = reg.values().iterator();
59 while(iter.hasNext()) Out.println(iter.next());
60 }
61 }
63
66 public void tearDown() throws Exception {
67 reg.clear();
68 Gate.init();
69 }
71
72 public void testInstanceLists() throws Exception {
73 List l;
75 CreoleRegister cr = Gate.getCreoleRegister();
76 Iterator iter;
77 ResourceData resData = null;
78 Resource res = null;
79 int numLrInstances = 0;
80
81 Set vrTypes = reg.getVrTypes();
83 Set prTypes = reg.getPrTypes();
84 Set lrTypes = reg.getLrTypes();
85
86 l = cr.getVrInstances();
88 if(! allAutoloaders(l))
89 fail(" non-autoloading resources already present (1)");
90 l = cr.getLrInstances();
91 numLrInstances = l.size();
92 if(! allAutoloaders(l))
93 fail(" non-autoloading resources already present (2)");
94 l = cr.getPrInstances();
95 if(! allAutoloaders(l))
96 fail(" non-autoloading resources already present (3)");
97
98 FeatureMap params = Factory.newFeatureMap();
100 params.put("features", Factory.newFeatureMap());
101 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html")
102 );
103 res = Factory.createResource("gate.corpora.DocumentImpl", params);
104
105 if(! (cr.getLrInstances().size() == numLrInstances + 1))
107 fail("wrong number of LRs");
108
109 params = Factory.newFeatureMap();
111 params.put("features", Factory.newFeatureMap());
112 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html")
113 );
114 res = Factory.createResource("gate.corpora.DocumentImpl", params);
115
116 if(! (cr.getLrInstances().size() == numLrInstances + 2))
118 fail("wrong number of LRs");
119
120 l = cr.getLrInstances("gate.corpora.DocumentImpl");
122 if(l.size() != 2)
123 fail("wrong number of documents");
124 }
126
127
128 public void testViews() throws Exception {
129 List smallViews1 =
130 reg.getSmallVRsForResource("gate.persist.SerialDataStore");
131 String className1 = new String("");
132 if (smallViews1!= null && smallViews1.size()>0)
133 className1 = (String)smallViews1.get(0);
134 assertTrue(
135 "Found "+className1+
136 " as small viewer for gate.persist.SerialDataStore, "+
137 "instead of gate.gui.SerialDatastoreViewer",
138 smallViews1.size() == 1 &&
139 "gate.gui.SerialDatastoreViewer".equals(className1)
140 );
141
142 List largeViews1 =
143 reg.getLargeVRsForResource("gate.Corpus");
144 assertTrue(
145 "Found "+largeViews1.size()+" wich are " +largeViews1 +
146 " as large viewers for gate.Corpus, "+
147 "instead of 2 which are [gate.gui.CorpusEditor, gate.gui.FeaturesEditor]",
148 largeViews1.size() == 2
149 );
150
151 List largeViews2 =
152 reg.getLargeVRsForResource("gate.Document");
153 assertTrue(
154 "Found "+largeViews2.size()+" wich are " +largeViews2 +
155 " as large viewers for gate.Document, "+
156 "instead of 2 which are [gate.gui.DocumentEditor, gate.gui.FeaturesEditor]",
157 largeViews2.size() == 2
158 );
159
160 List annotViews1 =
161 reg.getAnnotationVRs();
162 assertTrue(
163 "Found "+annotViews1.size()+" wich are " +annotViews1 +
164 " as annotation viewers for all types annotations, "+
165 "instead of 2 which are [gate.gui.SchemaAnnotationEditor,"+
166 " gate.gui.UnrestrictedAnnotationEditor]",
167 annotViews1.size() == 2
168 );
169 }
171
174 protected boolean allAutoloaders(List l) {
175 if(l != null) {
176 Resource res = null;
177 ResourceData resData = null;
178 CreoleRegister cr = Gate.getCreoleRegister();
179 Iterator iter = l.iterator();
180 while(iter.hasNext()) {
181 res = (Resource) iter.next();
182 if(DEBUG) Out.prln(res);
183 resData = (ResourceData) cr.get(res.getClass().getName());
184 if(DEBUG) Out.prln(resData);
185 if(! resData.isAutoLoading())
186 return false;
187 }
188 }
189
190 return true;
191 }
193
194 public void testDiscovery() throws Exception {
195
196 CreoleRegister reg = Gate.getCreoleRegister();
197 if(DEBUG) {
198 Iterator iter = reg.values().iterator();
199 while(iter.hasNext()) Out.println(iter.next());
200 }
201
202 ResourceData rd = (ResourceData)
203 reg.get("gate.creole.tokeniser.DefaultTokeniser");
204 assertNotNull("couldn't find unicode tok in register of resources", rd);
205 assertTrue(rd.getName().equals("ANNIE Unicode Tokeniser"));
206
207 String docFormatName = "gate.corpora.XmlDocumentFormat";
208 ResourceData xmlDocFormatRD = (ResourceData) reg.get(docFormatName);
209 assertTrue(xmlDocFormatRD.getName().equals("Sheffield XML Document Format"));
210 assertTrue(xmlDocFormatRD.isAutoLoading());
211 assertTrue(xmlDocFormatRD.getJarFileName().equals("ShefDocumentFormats.jar"));
212 }
214
215 public void testMetadata() throws Exception {
216
217 ResourceData pr1rd = (ResourceData) reg.get("testpkg.TestPR1");
219 ResourceData pr2rd = (ResourceData) reg.get("testpkg.TestPR2");
220 assertTrue(pr1rd != null & pr2rd != null);
221 assertTrue(pr2rd.getName().equals("Sheffield Test PR 2"));
222
223 assertTrue(pr1rd.getClassName().equals("testpkg.TestPR1"));
225 Iterator iter = pr1rd.getParameterList().getRuntimeParameters().iterator();
226 Iterator iter2 = null;
227 Parameter param = null;
228 while(iter.hasNext()) {
229 iter2 = ((List) iter.next()).iterator();
230 while(iter2.hasNext()) {
231 param = (Parameter) iter2.next();
232 if(param.typeName.equals("param0"))
233 break;
234 }
235 if(param.typeName.equals("param0"))
236 break;
237 }
238
239 assertTrue("param0 was null", param != null);
240 assertTrue(param.typeName.equals("java.lang.String"));
241 assertTrue(param.optional);
242 assertTrue(! param.runtime);
243 assertTrue(param.comment == null);
244 assertTrue(param.name.equals("thing"));
245
246 reg.clear();
247 }
249
250 public void testToolsAndPrivate() throws Exception {
251 ResourceData pr3rd = (ResourceData) reg.get("testpkg.PrintOutTokens");
252 assertTrue("couldn't get PR3", pr3rd != null);
253 assertTrue("PR3 not a tool", pr3rd.isTool());
254 if(DEBUG) Out.prln(pr3rd.getFeatures());
255
256 String docFormatName = "gate.corpora.XmlDocumentFormat";
257 ResourceData xmlDocFormatRD = (ResourceData) reg.get(docFormatName);
258 assertTrue("Xml doc format not PRIVATE", xmlDocFormatRD.isPrivate());
259 if(DEBUG) Out.prln(xmlDocFormatRD.getFeatures());
260
261 FeatureMap params = Factory.newFeatureMap();
263 params.put("features", Factory.newFeatureMap());
264 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
265 Resource res = Factory.createResource("gate.corpora.DocumentImpl", params);
266
267 List publics = reg.getPublicLrInstances();
268 List allLrs = reg.getLrInstances();
269
270 assertTrue(
271 "wrong number of public LR instances",
272 publics.size() == 1 && allLrs.size() == 3
273 );
274
275 if(DEBUG) {
276 Iterator iter = publics.iterator();
277 Out.prln("publics:");
278 while(iter.hasNext()) { Out.prln(iter.next()); }
279 iter = allLrs.iterator();
280 Out.prln("allLrs:");
281 while(iter.hasNext()) { Out.prln(iter.next()); }
282 }
283
284 }
286
287 public void testLoading() throws Exception {
288
289 assertTrue(
291 "wrong number of resources in the register: " + reg.size(),
292 reg.size() == 15
293 );
294 ResourceData pr1rd = (ResourceData) reg.get("testpkg.TestPR1");
295 ResourceData pr2rd = (ResourceData) reg.get("testpkg.TestPR2");
296 assertTrue("couldn't find PR1/PR2 res data", pr1rd != null && pr2rd != null);
297 assertTrue("wrong name on PR1", pr1rd.getName().equals("Sheffield Test PR 1"));
298
299 ProcessingResource pr1 = (ProcessingResource)
301 Factory.createResource("testpkg.TestPR1", Factory.newFeatureMap());
302 ProcessingResource pr2 = (ProcessingResource)
303 Factory.createResource("testpkg.TestPR2", Factory.newFeatureMap());
304
305 FeatureMap pr1features = pr1.getFeatures();
307 FeatureMap pr2features = pr2.getFeatures();
308 assertNotNull("PR1 features are null", pr1features);
309 assertTrue(
310 "PR2 got wrong features: " + pr2features,
311 pr2features != null || pr2features.size() != 1
312 );
313 pr1.execute();
314 pr2.execute();
315 assertTrue(
316 "PR1 feature not present",
317 pr1.getFeatures().get("I").equals("have been run, thankyou")
318 );
319 assertTrue(
320 "PR2 feature not present",
321 pr2.getFeatures().get("I").equals("am in a bad mood")
322 );
323
324 reg.clear();
325 }
327
328 public void testClassIndex() throws Exception {
329
330 ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
331 assertNotNull("couldn't find document res data", docRd);
332 assertTrue(
333 "doc res data has wrong class name",
334 docRd.getClassName().equals("gate.corpora.DocumentImpl")
335 );
336 assertTrue(
337 "doc res data has wrong interface name",
338 docRd.getInterfaceName().equals("gate.Document")
339 );
340
341 Class docClass = docRd.getResourceClass();
342 assertNotNull("couldn't get doc class", docClass);
343 LanguageResource docRes = (LanguageResource) docClass.newInstance();
344 assertTrue(
345 "instance of doc is wrong type",
346 docRes instanceof LanguageResource &&
347 docRes instanceof gate.Document
348 );
349
350 reg.clear();
351 }
353
354 public void testTypeLists() throws Exception {
355 Set vrs = reg.getVrTypes();
356 Set prs = reg.getPrTypes();
357 Set lrs = reg.getLrTypes();
358
359 assertTrue("wrong number vrs in reg: " + vrs.size(), vrs.size() == 7);
360 assertTrue("wrong number prs in reg: " + prs.size(), prs.size() == 5);
361 assertTrue("wrong number lrs in reg: " + lrs.size(), lrs.size() == 3);
362 }
364
365 public void testComments() throws Exception {
366
367 ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
368 assertNotNull("testComments: couldn't find document res data", docRd);
369 String comment = docRd.getComment();
370 assertTrue(
371 "testComments: incorrect or missing COMMENT on document",
372 comment != null && comment.equals("GATE document")
373 );
374 }
376
377 public void testParameterDefaults1() throws Exception {
378
379 ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
380 assertNotNull("Couldn: couldn't find document res data", docRd);
381 if(DEBUG) Out.prln(docRd.getParameterList().getInitimeParameters());
382 ParameterList paramList = docRd.getParameterList();
383 if(DEBUG) Out.prln(docRd);
384
385 Iterator iter = paramList.getRuntimeParameters().iterator();
387 assertTrue("Document has runtime params: " + paramList, ! iter.hasNext());
388
389 Parameter param = null;
391 iter = paramList.getInitimeParameters().iterator();
392 int paramDisjNumber = -1;
393 while(iter.hasNext()) {
394 List paramDisj = (List) iter.next();
395 Iterator iter2 = paramDisj.iterator();
396 paramDisjNumber++;
397
398 for(int i=0; iter2.hasNext(); i++) {
399 param = (Parameter) iter2.next();
400
401 switch(paramDisjNumber) {
402 case 0:
403 assertTrue(
404 "Doc param 0 wrong type: " + param.getTypeName(),
405 param.getTypeName().equals("java.lang.String")
406 );
407 assertTrue(
408 "Doc param 0 wrong name: " + param.getName(),
409 param.getName().equals("sourceUrlName")
410 );
411 Object defaultValue = param.calculateDefaultValue();
412 assertTrue(
413 "Doc param 0 default should be null but was: " + defaultValue,
414 defaultValue == null
415 );
416 break;
417 case 1:
418 assertTrue(
419 "Doc param 1 wrong name: " + param.getName(),
420 param.getName().equals(Document.DOCUMENT_ENCODING_PARAMETER_NAME)
421 );
422 break;
423 case 2:
424 assertTrue(
425 "Doc param 2 wrong name: " + param.getName(),
426 param.getName().equals(Document.DOCUMENT_START_OFFSET_PARAMETER_NAME)
427 );
428 break;
429 case 3:
430 assertTrue(
431 "Doc param 3 wrong name: " + param.getName(),
432 param.getName().equals(Document.DOCUMENT_END_OFFSET_PARAMETER_NAME)
433 );
434 break;
435 default:
436 fail("Doc has more than 4 params; 5th is: " + param);
437 } }
439 }
440
441 }
443
444 public void testParameterDefaults2() throws Exception {
445
446 ResourceData rd = (ResourceData) reg.get("testpkg.PrintOutTokens");
447 assertNotNull("Couldn't find testpkg.POT res data", rd);
448
449 Factory.newDocument(Gate.getUrl("tests/doc0.html"));
451
452 ParameterList paramList = rd.getParameterList();
453 if(DEBUG) Out.prln(rd);
454
455 Iterator iter = paramList.getInitimeParameters().iterator();
457 assertTrue("POT has initime params: " + paramList, ! iter.hasNext());
458
459 Parameter param = null;
461 iter = paramList.getRuntimeParameters().iterator();
462 int paramDisjNumber = -1;
463 while(iter.hasNext()) {
464 List paramDisj = (List) iter.next();
465 Iterator iter2 = paramDisj.iterator();
466 paramDisjNumber++;
467
468 for(int i=0; iter2.hasNext(); i++) {
469 param = (Parameter) iter2.next();
470
471 switch(paramDisjNumber) {
472 case 0:
473 assertTrue(
474 "POT param 0 wrong type: " + param.getTypeName(),
475 param.getTypeName().equals("gate.corpora.DocumentImpl")
476 );
477 assertTrue(
478 "POT param 0 wrong name: " + param.getName(),
479 param.getName().equals("document")
480 );
481 Object defaultValue = param.calculateDefaultValue();
482 assertTrue(
483 "POT param 0 default should be Document but is " +
484 defaultValue.getClass().getName(),
485 defaultValue instanceof Document
486 );
487 break;
488 default:
489 fail("POT has more than 1 param; 2nd is: " + param);
490 } }
492 }
493
494 }
496
497 public void testParamAsLists() throws Exception{
498 ResourceData rd = (ResourceData) reg.get("testpkg.TestPR3");
499 assertNotNull("Couldn: couldn't find testPR3 res data", rd);
500
501 ParameterList paramList = rd.getParameterList();
502 List runTime = paramList.getRuntimeParameters();
504 assertTrue("PR3 should have 4 runtime params: " + paramList, runTime.size()==4);
505 }
507
508 public void testParameters() throws Exception {
509
510 ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
511 assertNotNull("Couldn: couldn't find document res data", docRd);
512
513 ParameterList paramList = docRd.getParameterList();
514 if(DEBUG) Out.prln(docRd);
515
516 Iterator iter = paramList.getRuntimeParameters().iterator();
518 assertTrue("Document has runtime params: " + paramList, ! iter.hasNext());
519
520 Parameter param = null;
522 List initimeParams = paramList.getInitimeParameters();
523 int initimeLen = initimeParams.size();
524 assertTrue(
525 "initime params has wrong number of elements: " + initimeLen,
526 initimeLen == 4
527 );
528 iter = initimeParams.iterator();
529 int paramDisjNumber = -1;
530 while(iter.hasNext()) {
531 List paramDisj = (List) iter.next();
532 Iterator iter2 = paramDisj.iterator();
533 paramDisjNumber++;
534
535 int paramDisjLen = paramDisj.size();
536 assertTrue(
537 "param disj wrong length: " + paramDisjLen,
538 paramDisjLen == 1
539 );
540
541 for(int i=0; iter2.hasNext(); i++) {
542 param = (Parameter) iter2.next();
543
544 switch(paramDisjNumber) {
545 case 0:
546 assertTrue(
547 "Doc param 0 wrong type: " + param.getTypeName(),
548 param.getTypeName().equals("java.lang.String")
549 );
550 assertTrue(
551 "Doc param 0 wrong name: " + param.getName(),
552 param.getName().equals("sourceUrlName")
553 );
554 Object defaultValue = param.calculateDefaultValue();
555 assertTrue(
556 "Doc param 0 default should be null but was: " + defaultValue,
557 defaultValue == null
558 );
559 break;
560 case 1:
561 assertTrue(
562 "Doc param 1 wrong name: " + param.getName(),
563 param.getName().equals(Document.DOCUMENT_ENCODING_PARAMETER_NAME)
564 );
565 break;
566 case 2:
567 assertTrue(
568 "Doc param 2 wrong name: " + param.getName(),
569 param.getName().equals(Document.DOCUMENT_START_OFFSET_PARAMETER_NAME)
570 );
571 defaultValue = param.getDefaultValue();
572 break;
573 case 3:
574 assertTrue(
575 "Doc param 3 wrong name: " + param.getName(),
576 param.getName().equals(Document.DOCUMENT_END_OFFSET_PARAMETER_NAME)
577 );
578 break;
579 default:
580 fail("Doc has more than 4 params; 5th is: " + param);
581 } }
583 }
584
585 }
587
588 public void testDefaultRun() throws Exception {
589 ProcessingResource defaultPr = new AbstractProcessingResource() {
590 };
591 boolean gotExceptionAsExpected = false;
592 try {
593 defaultPr.execute();
594 } catch(ExecutionException e) {
595 gotExceptionAsExpected = true;
596 }
597
598 assertTrue("check should have thrown exception", gotExceptionAsExpected);
599 }
601
602 public void testArbitraryMetadata() throws Exception {
603
604 ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
605 assertNotNull("testArbitraryMetadata: couldn't find doc res data", docRd);
606 FeatureMap features = docRd.getFeatures();
607 String comment = (String) features.get("FUNKY-METADATA-THAING");
608 assertTrue(
609 "testArbitraryMetadata: incorrect FUNKY-METADATA-THAING on document",
610 comment != null && comment.equals("hubba hubba")
611 );
612 }
614
615 public void testIntrospection() throws Exception {
616 ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
618 assertNotNull("couldn't find document res data (2)", docRd);
619 Class resClass = docRd.getResourceClass();
620
621 BeanInfo docBeanInfo = Introspector.getBeanInfo(resClass, Object.class);
623 PropertyDescriptor[] propDescrs = docBeanInfo.getPropertyDescriptors();
624
625 Method setFeaturesMethod = null;
628 for(int i = 0; i<propDescrs.length; i++) {
629 Method getMethodDescr = null;
630 Method setMethodDescr = null;
631 Class propClass = null;
632
633 PropertyDescriptor propDescr = propDescrs[i];
634 propClass = propDescr.getPropertyType();
635 getMethodDescr = propDescr.getReadMethod();
636 setMethodDescr = propDescr.getWriteMethod();
637
638 if(
639 setMethodDescr != null &&
640 setMethodDescr.getName().equals("setFeatures")
641 )
642 setFeaturesMethod = setMethodDescr;
643
644 if(DEBUG) printProperty(propDescrs[i]);
645 }
646
647 LanguageResource res = (LanguageResource) resClass.newInstance();
650 FeatureMap feats = Factory.newFeatureMap();
651 feats.put("things are sunny in sunny countries", "aren't they?");
652 Object[] args = new Object[1];
653 args[0] = feats;
654 setFeaturesMethod.invoke(res, args);
655 assertTrue(
656 "features not added to resource properly",
657 res.getFeatures().get("things are sunny in sunny countries")
658 .equals("aren't they?")
659 );
660 }
662
663 public void testFactory() throws Exception {
664 FeatureMap params = Factory.newFeatureMap();
665 params.put("features", Factory.newFeatureMap());
666 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html")
667 );
668 Resource res =
669 Factory.createResource("gate.corpora.DocumentImpl", params);
670 }
672
675 public static void printProperty(PropertyDescriptor prop) {
676 Class propClass = prop.getPropertyType();
677 Method getMethodDescr = prop.getReadMethod();
678 Method setMethodDescr = prop.getWriteMethod();
679 Out.pr("prop dispname= " + prop.getDisplayName() + "; ");
680 Out.pr("prop type name= " + propClass.getName() + "; ");
681 if(getMethodDescr != null)
682 Out.pr("get meth name= " + getMethodDescr.getName() + "; ");
683 if(setMethodDescr != null)
684 Out.pr("set meth name= " + setMethodDescr.getName() + "; ");
685 Out.prln();
686 }
688
695 class DocumentImplBeanInfo extends SimpleBeanInfo {
696
697
701 public PropertyDescriptor[] getPropertyDescriptors() {
702 return new PropertyDescriptor[0];
703 }
705 }
707
708 public static Test suite() {
709 return new TestSuite(TestCreole.class);
710 }
712 }