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