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