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