1   /*
2    *  TestControllers.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: TestControllers.java,v 1.17 2001/10/30 12:45:35 valyt Exp $
14   */
15  
16  package gate.creole;
17  
18  import java.util.*;
19  import java.io.*;
20  import java.net.*;
21  import junit.framework.*;
22  
23  import gate.*;
24  import gate.creole.*;
25  import gate.util.*;
26  
27  /** Tests for controller classes
28    */
29  public class TestControllers extends TestCase
30  {
31    /** Debug flag */
32    private static final boolean DEBUG = false;
33  
34    /** The CREOLE register */
35    CreoleRegister reg;
36  
37    /** Construction */
38    public TestControllers(String name) { super(name); }
39  
40    /** Fixture set up */
41    public void setUp() throws GateException {
42      // Initialise the GATE library and get the creole register
43      Gate.init();
44      reg = Gate.getCreoleRegister();
45  
46    } // setUp
47  
48    /** Put things back as they should be after running tests
49      * (reinitialise the CREOLE register).
50      */
51    public void tearDown() throws Exception {
52      reg.clear();
53      Gate.init();
54    } // tearDown
55  
56    /** Serial controller test 1 */
57    public void testSerial1() throws Exception {
58      // a controller
59      SerialController c1 = new SerialController();
60      assertNotNull("c1 controller is null", c1);
61  
62      //get a document
63      FeatureMap params = Factory.newFeatureMap();
64      params.put("sourceUrl", Gate.getUrl("tests/doc0.html"));
65      params.put("markupAware", "false");
66      Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
67                                                      params);
68  
69      if(DEBUG) {
70        ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
71        assertNotNull("Couldn't find document res data", docRd);
72        Out.prln(docRd.getParameterList().getInitimeParameters());
73      }
74  
75      //create a default tokeniser
76      params = Factory.newFeatureMap();
77      params.put("tokeniserRulesURL",
78                  "gate:/creole/tokeniser/DefaultTokeniser.rules");
79      params.put("transducerGrammarURL",
80                  "gate:/creole/tokeniser/postprocess.jape");
81      params.put("encoding", "UTF-8");
82      params.put("document", doc);
83      ProcessingResource tokeniser = (ProcessingResource) Factory.createResource(
84        "gate.creole.tokeniser.DefaultTokeniser", params
85      );
86  
87      //create a default gazetteer
88      params = Factory.newFeatureMap();
89      params.put("document", doc);
90      params.put("listsURL", "gate:/creole/gazeteer/default/lists.def");
91      ProcessingResource gaz = (ProcessingResource) Factory.createResource(
92        "gate.creole.gazetteer.DefaultGazetteer", params
93      );
94  
95      // get the controller to encapsulate the tok and gaz
96      c1.add(tokeniser);
97      c1.add(gaz);
98      c1.execute();
99  
100     // check the resulting annotations
101     if(DEBUG) {
102       Out.prln(doc.getAnnotations());
103       Out.prln(doc.getContent());
104     }
105     AnnotationSet annots = doc.getAnnotations();
106     assertTrue("no annotations from doc!", !annots.isEmpty());
107     Annotation a = annots.get(new Integer(580));
108     assertNotNull("couldn't get annot with id 580", a);
109 //sorry, this is no way to write a test!
110 //    assert( // check offset - two values depending on whether saved with \r\n
111 //      "wrong value: " + a.getStartNode().getOffset(),
112 //      (a.getStartNode().getOffset().equals(new Long(1360)) ||
113 //      a.getStartNode().getOffset().equals(new Long(1367)))
114 //    );
115 //    assert( // check offset - two values depending on whether saved with \r\n
116 //      "wrong value: " + a.getEndNode().getOffset(),
117 //      a.getEndNode().getOffset().equals(new Long(1361)) ||
118 //      a.getEndNode().getOffset().equals(new Long(1442))
119 //    );
120   } // testSerial1()
121 
122   /** Serial controller test 2 */
123   public void testSerial2() throws Exception {
124     // a controller
125     Controller c1 = new SerialController();
126     assertNotNull("c1 controller is null", c1);
127 /*
128     // a couple of PRs
129     ResourceData pr1rd = (ResourceData) reg.get("testpkg.TestPR1");
130     ResourceData pr2rd = (ResourceData) reg.get("testpkg.TestPR2");
131     assert("couldn't find PR1/PR2 res data", pr1rd != null && pr2rd != null);
132     assert("wrong name on PR1", pr1rd.getName().equals("Sheffield Test PR 1"));
133     ProcessingResource pr1 = (ProcessingResource)
134       Factory.createResource("testpkg.TestPR1", Factory.newFeatureMap());
135     ProcessingResource pr2 = (ProcessingResource)
136       Factory.createResource("testpkg.TestPR2", Factory.newFeatureMap());
137 
138     // add the PRs to the controller and run it
139     c1.add(pr1);
140     c1.add(pr2);
141     c1.run();
142 */
143   } // testSerial2()
144 
145   /** Test suite routine for the test runner */
146   public static Test suite() {
147     return new TestSuite(TestControllers.class);
148   } // suite
149 
150 } // class TestControllers
151