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