1   /*
2    *  TestGate.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, 21/Jan/00
12   *
13   *  $Id: TestGate.java,v 1.228 2005/12/15 11:03:50 valyt Exp $
14   */
15  
16  package gate;
17  
18  import java.io.File;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  import java.util.StringTokenizer;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import com.ontotext.gate.gazetteer.TestHashGazetteer;
27  
28  import gate.annotation.TestAnnotation;
29  import gate.annotation.TestAnnotationDiff;
30  import gate.config.TestConfig;
31  import gate.corpora.*;
32  import gate.creole.*;
33  import gate.creole.ir.TestIndex;
34  import gate.creole.morph.TestMorph;
35  import gate.creole.gazetteer.TestFlexibleGazetteer;
36  import gate.email.TestEmail;
37  import gate.html.TestHtml;
38  import gate.jape.TestJape;
39  import gate.persist.TestPersist;
40  import gate.security.TestSecurity;
41  import gate.sgml.TestSgml;
42  import gate.util.*;
43  import gate.wordnet.TestWordNet;
44  import gate.xml.TestXml;
45  import gate.creole.ml.maxent.*;
46  import gate.creole.ontology.TestOntologyAPI;
47  import gate.xml.TestRepositioningInfo;
48  
49  import gnu.getopt.Getopt;
50  
51  /** Top-level entry point for GATE test suite;
52    * "main" will run the JUnit test runner interface.
53    * <P>
54    * Many tests require access to files; generally these files are located
55    * on Web servers. In cases where there is no net connection, or the
56    * Web servers are down, the test files are searched for in the file system
57    * or Jar code base that the system has been loaded from. The search
58    * order for test files is like this:
59    * <UL>
60    * <LI>
61    * <A HREF=http://derwent.dcs.shef.ac.uk:80/gate.ac.uk/>
62    * http://derwent.dcs.shef.ac.uk:80/gate.ac.uk/</A>
63    * <LI>
64    * <A HREF=http://gate.ac.uk:80/>http://gate.ac.uk:80/</A>
65    * <LI>
66    * <A HREF=http://localhost:80/gate.ac.uk/>http://localhost:80/gate.ac.uk/</A>
67    * <LI>
68    * the file system location that the classes came from, e.g.
69    * <TT>z:\gate\classes</TT>, or <TT>jar:....gate.jar</TT>.
70    * </UL>
71    * This search order can be modified by parameters to the main
72    * function (see below).
73    */
74  
75  public class TestGate {
76  
77    /** Debug flag */
78    private static final boolean DEBUG = false;
79  
80    /** Status flag for normal exit. */
81    private static final int STATUS_NORMAL = 0;
82  
83    /** Status flag for error exit. */
84    private static final int STATUS_ERROR = 1;
85  
86    private static final String
87                  defOracleDriver = "jdbc:oracle:thin:@derwent:1521:dbgate";
88    private static final String
89                  saiOracleDriver = "jdbc:oracle:thin:GATEUSER/gate@192.168.128.7:1521:GATE04";
90    private static final String
91                  defPSQLDriver = "jdbc:postgresql://redmires/gate";
92    private static final String
93                  saiPSQLDriver = "jdbc:postgresql://sirma/gate";
94  
95  
96    public static String oracleDriver = defOracleDriver;
97    public static String psqlDriver = defPSQLDriver;
98  
99    /** Main routine for the GATE test suite.
100     * Command-line arguments:
101     * <UL>
102     * <LI>
103     * <B>-a</B> means run the test runner in automatic class reload mode
104     * <LI>
105     * <B>-n</B> means assume there's no net connection
106     * <LI>
107     * <B>-t</B> means run the test runner in text mode
108     * (useful for
109     * debugging, as there's less confusion to do with threads and
110     * class loaders).
111     * <LI>
112     * <B>-i file</B> additional initialisation file (probably called
113     *   <TT>gate.xml</TT>). Used for site-wide initialisation by the
114     *   start-up scripts.
115     * </UL>
116     */
117   public static void main(String[] args) throws Exception {
118     boolean textMode = false;
119     boolean autoloadingMode = false;
120 
121     // process command-line options
122     Getopt g = new Getopt("GATE test suite", args, "tnNasi:");
123     int c;
124     while( (c = g.getopt()) != -1 )
125       switch(c) {
126         case 't':
127           textMode = true;
128           break;
129         case 'n':
130           Gate.setNetConnected(false);
131           break;
132         case 'N':
133           Gate.setNetConnected(false);
134           Gate.setLocalWebServer(false);
135           break;
136         case 'a':
137           autoloadingMode = true;
138           break;
139         case 's':
140           oracleDriver = saiOracleDriver;
141           psqlDriver = saiPSQLDriver;
142           break;
143         // -i gate.xml site-wide init file
144         case 'i':
145           String optionString = g.getOptarg();
146           URL u = null;
147           File f = new File(optionString);
148           try {
149             u = f.toURL();
150           } catch(MalformedURLException e) {
151             Err.prln("Bad initialisation file: " + optionString);
152             Err.prln(e);
153             System.exit(STATUS_ERROR);
154           }
155           Gate.setSiteConfigFile(f);
156           Out.prln(
157             "Initialisation file " + optionString +
158             " recorded for initialisation"
159           );
160           break;
161         case '?':
162           // leave the warning to getopt
163           return;
164         default:
165           Err.prln("getopt() returned " + c + "\n");
166       } // switch
167 
168     // set up arguments for the JUnit test runner
169     String junitArgs[] = new String[2];
170     junitArgs[0] = "-noloading";
171     junitArgs[1] = "gate.TestGate";
172 
173     // use the next line if you're running with output to console in text mode:
174     // junitArgs[1] = "-wait";
175 
176     // execute the JUnit test runner
177     if(textMode) { // text runner mode
178       junit.textui.TestRunner.main(junitArgs);
179     } else if(autoloadingMode) { // autoloading mode
180       junitArgs[0] = "gate.TestGate";
181       junitArgs[1] = "";
182 
183       // NOTE: the DB tests fail under this one (doesn't load oracle driver,
184       // even after the Class.forName call)
185       Class clazz = null;
186       clazz = Class.forName("oracle.jdbc.driver.OracleDriver");
187       clazz = null;
188       junit.swingui.TestRunner.main(junitArgs);
189 
190     } else { // by default us the single-run GUI version
191       junit.swingui.TestRunner.main(junitArgs);
192     }
193 
194   } // main
195 
196   /** GATE test suite. Every test case class has to be
197     * registered here.
198     */
199   public static Test suite() throws Exception {
200     // inialise the library. we re-throw any exceptions thrown by
201     // init, after printing them out, because the junit gui doesn't
202     // say anything more informative than "can't invoke suite" if there's
203     // an exception here...
204 
205     try {
206       //get the config if set through a property
207       String configFile = System.getProperty("gate.config");
208       if(configFile != null && configFile.length() > 0){
209         File f = new File(configFile);
210         try {
211           URL u = f.toURL();
212         } catch(MalformedURLException e) {
213           Err.prln("Bad initialisation file: " + configFile);
214           Err.prln(e);
215           System.exit(STATUS_ERROR);
216         }
217         Gate.setSiteConfigFile(f);
218       }
219       Gate.init();
220     } catch(GateException e) {
221       Out.prln("can't initialise GATE library! exception = " + e);
222       throw(e);
223     }
224 
225     TestSuite suite = new TestSuite();
226 
227     try {
228       ////////////////////////////////////////////////
229       // Test bench
230       ////////////////////////////////////////////////
231       // set this true to run all tests; false to run the just one below
232       boolean allTests = true;
233       if(! allTests){
234         suite.addTest(TestPR.suite());
235       } else {
236         
237         suite.addTest(TestWordNet.suite());
238         suite.addTest(TestIndex.suite());
239         suite.addTest(TestPersist.suite());
240         suite.addTest(TestBumpyStack.suite());
241         suite.addTest(TestControllers.suite());
242         suite.addTest(TestSecurity.suite());
243         suite.addTest(TestAnnotationDiff.suite());
244         suite.addTest(TestConfig.suite());
245         suite.addTest(TestAnnotation.suite());
246         suite.addTest(TestEmail.suite());
247         suite.addTest(TestOntologyAPI.suite());
248         
249         suite.addTest(TestXml.suite());
250         suite.addTest(TestHtml.suite());
251         suite.addTest(TestSgml.suite());
252         suite.addTest(TestXSchema.suite());
253         
254         suite.addTest(TestCreole.suite());
255         suite.addTest(CookBook.suite());
256         suite.addTest(TestFiles.suite());
257         suite.addTest(TestJavac.suite());
258         suite.addTest(TestReload.suite());
259         suite.addTest(TestJape.suite());
260         suite.addTest(TestTemplate.suite());
261         /* The TCL tests rely on the application being started from the
262          * gate directory. This is not possible from the nightly build script.
263          */
264 //        suite.addTest(TestJacl.suite());
265         suite.addTest(TestDocument.suite());
266         suite.addTest(TestRBTreeMap.suite());
267         suite.addTest(TestCorpus.suite());
268         suite.addTest(TestSerialCorpus.suite());
269         suite.addTest(TestDiffer.suite());
270 //no longer needed as replaced by testPR
271 //        suite.addTest(TestTokeniser.suite());
272 //        suite.addTest(TestGazetteer.suite());
273 //        suite.addTest(TestSplitterTagger.suite());
274         suite.addTest(TestFeatureMap.suite());
275         suite.addTest(TestPR.suite());
276         suite.addTest(TestMorph.suite());
277         suite.addTest(TestMaxentWrapper.suite());
278 
279         //test ontotext gazetteer
280         suite.addTest(TestHashGazetteer.suite());
281         suite.addTest(TestRepositioningInfo.suite());
282         suite.addTest(TestFlexibleGazetteer.suite());
283 
284       } // if(allTests)
285 
286     } catch(Exception e) {
287       Out.prln("can't add tests! exception = " + e);
288       throw(e);
289     }
290 
291     return suite;
292   } // suite
293 
294 } // class TestGate
295