| TestCreole |

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

| TestCreole |