001 /*
002 * TestControllers.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Hamish Cunningham, 16/Mar/00
013 *
014 * $Id: TestControllers.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.creole;
018
019 import junit.framework.*;
020
021 import gate.*;
022 import gate.creole.gazetteer.DefaultGazetteer;
023 import gate.creole.tokeniser.DefaultTokeniser;
024 import gate.util.GateException;
025 import gate.util.Out;
026
027 /** Tests for controller classes
028 */
029 public class TestControllers extends TestCase
030 {
031 /** Debug flag */
032 private static final boolean DEBUG = false;
033
034 /** The CREOLE register */
035 CreoleRegister reg;
036
037 /** Construction */
038 public TestControllers(String name) { super(name); }
039
040 /** Fixture set up */
041 public void setUp() throws GateException {
042 // Initialise the GATE library and get the creole register
043 Gate.init();
044 reg = Gate.getCreoleRegister();
045
046 } // setUp
047
048 /** Put things back as they should be after running tests
049 * (reinitialise the CREOLE register).
050 */
051 public void tearDown() throws Exception {
052 reg.clear();
053 Gate.init();
054 } // tearDown
055
056 /** Serial controller test 1 */
057 public void testSerial1() throws Exception {
058 // a controller
059 SerialController c1 = new SerialController();
060 assertNotNull("c1 controller is null", c1);
061
062 // set a name for this controller
063 c1.setName("SerialController_"+Gate.genSym());
064
065 //get a document
066 FeatureMap params = Factory.newFeatureMap();
067 params.put(Document.DOCUMENT_URL_PARAMETER_NAME, Gate.getUrl("tests/doc0.html"));
068 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME, "false");
069 Document doc = (Document)Factory.createResource("gate.corpora.DocumentImpl",
070 params);
071
072 if(DEBUG) {
073 ResourceData docRd = (ResourceData) reg.get("gate.corpora.DocumentImpl");
074 assertNotNull("Couldn't find document res data", docRd);
075 Out.prln(docRd.getParameterList().getInitimeParameters());
076 }
077
078 //create a default tokeniser
079 params = Factory.newFeatureMap();
080 params.put(DefaultTokeniser.DEF_TOK_DOCUMENT_PARAMETER_NAME, doc);
081 ProcessingResource tokeniser = (ProcessingResource) Factory.createResource(
082 "gate.creole.tokeniser.DefaultTokeniser", params
083 );
084
085 //create a default gazetteer
086 params = Factory.newFeatureMap();
087 params.put(DefaultGazetteer.DEF_GAZ_DOCUMENT_PARAMETER_NAME, doc);
088 ProcessingResource gaz = (ProcessingResource) Factory.createResource(
089 "gate.creole.gazetteer.DefaultGazetteer", params
090 );
091
092 // get the controller to encapsulate the tok and gaz
093 c1.add(tokeniser);
094 c1.add(gaz);
095 c1.execute();
096
097 // check the resulting annotations
098 if(DEBUG) {
099 Out.prln(doc.getAnnotations());
100 Out.prln(doc.getContent());
101 }
102 AnnotationSet annots = doc.getAnnotations();
103 assertTrue("no annotations from doc!", !annots.isEmpty());
104 Annotation a = annots.get(new Integer(580));
105 assertNotNull("couldn't get annot with id 580", a);
106 //sorry, this is no way to write a test!
107 // assert( // check offset - two values depending on whether saved with \r\n
108 // "wrong value: " + a.getStartNode().getOffset(),
109 // (a.getStartNode().getOffset().equals(new Long(1360)) ||
110 // a.getStartNode().getOffset().equals(new Long(1367)))
111 // );
112 // assert( // check offset - two values depending on whether saved with \r\n
113 // "wrong value: " + a.getEndNode().getOffset(),
114 // a.getEndNode().getOffset().equals(new Long(1361)) ||
115 // a.getEndNode().getOffset().equals(new Long(1442))
116 // );
117 } // testSerial1()
118
119 /** Serial controller test 2 */
120 public void testSerial2() throws Exception {
121 // a controller
122 Controller c1 = new SerialController();
123 assertNotNull("c1 controller is null", c1);
124 /*
125 // a couple of PRs
126 ResourceData pr1rd = (ResourceData) reg.get("testpkg.TestPR1");
127 ResourceData pr2rd = (ResourceData) reg.get("testpkg.TestPR2");
128 assert("couldn't find PR1/PR2 res data", pr1rd != null && pr2rd != null);
129 assert("wrong name on PR1", pr1rd.getName().equals("Sheffield Test PR 1"));
130 ProcessingResource pr1 = (ProcessingResource)
131 Factory.createResource("testpkg.TestPR1", Factory.newFeatureMap());
132 ProcessingResource pr2 = (ProcessingResource)
133 Factory.createResource("testpkg.TestPR2", Factory.newFeatureMap());
134
135 // add the PRs to the controller and run it
136 c1.add(pr1);
137 c1.add(pr2);
138 c1.run();
139 */
140 } // testSerial2()
141
142 /** Test suite routine for the test runner */
143 public static Test suite() {
144 return new TestSuite(TestControllers.class);
145 } // suite
146
147 } // class TestControllers
|