1   /*
2    *  TestConfig.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, 9/Nov/00
12   *
13   *  $Id: TestConfig.java,v 1.17 2005/10/03 15:05:57 ian_roberts Exp $
14   */
15  
16  package gate.config;
17  
18  import java.io.*;
19  import java.net.URL;
20  import java.util.Map;
21  import java.util.Set;
22  
23  import junit.framework.*;
24  
25  import gate.*;
26  import gate.util.*;
27  
28  /** CREOLE test class
29    */
30  public class TestConfig extends TestCase
31  {
32    /** Debug flag */
33    private static final boolean DEBUG = false;
34  
35    /** Construction */
36    public TestConfig(String name) throws GateException { super(name); }
37  
38    /** Fixture set up */
39    public void setUp() throws Exception {
40      CreoleRegister register = Gate.getCreoleRegister();
41      register.registerDirectories(Gate.getUrl("tests"));
42    } // setUp
43  
44    /** Put things back as they should be after running tests
45      * (reinitialise the CREOLE register).
46      */
47    public void tearDown() throws Exception {
48      CreoleRegister register = Gate.getCreoleRegister();
49      register.clear();
50      Gate.init();
51    } // tearDown
52  
53    /**
54     * Helper method that processes a config file.
55     */
56    private void readConfig(URL configUrl) throws Exception {
57      ConfigDataProcessor configProcessor = new ConfigDataProcessor();
58  
59      // open a stream to the builtin config data file (tests version)
60      InputStream configStream = null;
61      try {
62        configStream = configUrl.openStream();
63      } catch(IOException e) {
64        throw new GateException(
65          "Couldn't open config data test file: " + configUrl + " " + e
66        );
67      }
68      if (DEBUG)
69        Out.prln(
70          "Parsing config file ... " + configStream + "from URL" + configUrl
71        );
72      configProcessor.parseConfigFile(configStream, configUrl);
73    } // readConfig
74  
75    /** Test config loading */
76    public void testConfigReading() throws Exception {
77      System.out.println("Reading GATE config from : " + Gate.getUrl("tests/gate.xml"));
78      readConfig(Gate.getUrl("tests/gate.xml"));
79  
80      // check that we got the CREOLE dir entry; then remove it
81      // so it doesn't get accessed in other tests
82      CreoleRegister reg = Gate.getCreoleRegister();
83      Set dirs = reg.getDirectories();
84      assertTrue(
85        "CREOLE register doesn't contain URL from test gate.xml",
86        dirs != null && ! dirs.isEmpty() &&
87        dirs.contains(new URL("http://gate.ac.uk/tests/"))
88      );
89  
90      // we should have a GATECONFIG entry on Gate
91      String fullSizeKeyName = "FULLSIZE";
92      String fullSizeValueName = "yes";
93      Map gateConfig = Gate.getUserConfig();
94      assertNotNull("no gate config map", gateConfig);
95      String fullSizeValue = (String) gateConfig.get(fullSizeKeyName);
96      assertNotNull("no full size value", fullSizeValue);
97      assertEquals(
98        "incorrect config data from tests/gate.xml",
99        fullSizeValueName, fullSizeValue
100     );
101 
102     // clear the gate config for subsequent tests
103     gateConfig.clear();
104 
105 
106 // the code below is removed after serial controller stopped
107 // being a PR. the XML config scripting of runnable systems isn't
108 // working anyhow. when/if it does work, appropriate tests should be
109 // re-added here
110 //    // get a test system
111 //    ResourceData controllerResData =
112 //      (ResourceData) reg.get("gate.creole.SerialController");
113 //    assertNotNull("no resdata for serial controller", controllerResData);
114 //    ProcessingResource controller =
115 //      (ProcessingResource) controllerResData.getInstantiations().pop();
116 //    assertNotNull("no controller instance", controller);
117 //
118 //    // try running the system
119 //    controller.execute();
120   } // testConfigReading()
121 
122   /** Test config updating */
123   public void testConfigUpdating() throws Exception {
124     // clear the gate config so we don't write values from the
125     // system initialisation into the test file
126     Map configMap = Gate.getUserConfig();
127     configMap.clear();
128 
129     // if user config file exists, save it and remember the name
130     //String configName = Gate.getUserConfigFileName();
131     //File userConfigFile = new File(configName);
132     File userConfigFile = Gate.getUserConfigFile();
133     String configName = userConfigFile.getAbsolutePath();
134     File savedConfigFile = null;
135     if(userConfigFile.exists()) {
136       if(DEBUG) {
137         Out.prln(userConfigFile);
138         Out.prln("can write: " + userConfigFile.canWrite());
139       }
140       String userConfigDirectory = userConfigFile.getParent();
141       if(userConfigDirectory == null)
142         userConfigDirectory = "";
143       savedConfigFile = new File(
144         userConfigDirectory + Strings.getFileSep() +
145         "__saved_gate.xml__for_TestConfig__" + System.currentTimeMillis()
146       );
147       if(DEBUG) Out.prln(savedConfigFile);
148       boolean renamed = userConfigFile.renameTo(savedConfigFile);
149       assertTrue("rename failed", renamed);
150     }
151     assertTrue("user config file still there", ! userConfigFile.exists());
152 
153     // call Gate.writeConfig - check it creates an empty config file
154     //this is no longer a valid test as the written user config will at least
155     //contain the values for the known and autload plugin paths.
156     Gate.writeUserConfig();
157     String writtenConfig = Files.getString(new File(configName));
158     String empty = Gate.getEmptyConfigFile();
159 //    assertEquals("written config doesn't match", writtenConfig, empty);
160 
161     // set some config attributes via Gate.getConfigData
162     configMap.put("A", "1");
163     configMap.put("B", "2");
164 
165     // call Gate.writeConfig, delete the config data from Gate's map,
166     // read the config file and check that the new data is present
167     Gate.writeUserConfig();
168     configMap.clear();
169     readConfig(userConfigFile.toURL());
170 
171     // reinstante saved user config file if not null
172     userConfigFile.delete();
173     if(savedConfigFile != null) {
174       savedConfigFile.renameTo(userConfigFile);
175     }
176 
177   } // testConfigUpdating
178 
179   /** Test session state file naming */
180   public void testSessionStateFileNaming() throws Exception {
181     String fileSep = Strings.getFileSep();
182     if(DEBUG) {
183       Out.prln("file sep is: " + fileSep);
184     }
185 
186     if(Gate.runningOnUnix()) {
187       assertTrue(fileSep.equals("/"));
188       assertTrue(
189         Gate.getUserSessionFileName().endsWith("."+GateConstants.GATE_DOT_SER)
190       );
191     } else {
192       assertTrue(! fileSep.equals("/"));
193       assertTrue(
194         ! Gate.getUserSessionFileName().endsWith("."+GateConstants.GATE_DOT_SER)
195       );
196     }
197 
198   } // testSessionStateFileNaming
199 
200   /** Test config file naming */
201   public void testConfigFileNaming() throws Exception {
202     String fileSep = Strings.getFileSep();
203     if(DEBUG) {
204       Out.prln("file sep is: " + fileSep);
205     }
206 
207     if(Gate.runningOnUnix()) {
208       assertTrue(fileSep.equals("/"));
209       assertTrue(
210         Gate.getDefaultUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
211       );
212     } else {
213       assertTrue(! fileSep.equals("/"));
214       assertTrue(
215         ! Gate.getDefaultUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
216       );
217     }
218 
219   } // testConfigFileNaming
220 
221   /** Test suite routine for the test runner */
222   public static Test suite() {
223     return new TestSuite(TestConfig.class);
224   } // suite
225 
226 } // class TestConfig
227