001 /*
002 * TestConfig.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, 9/Nov/00
013 *
014 * $Id: TestConfig.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.config;
018
019 import java.io.*;
020 import java.net.URL;
021 import java.util.Map;
022 import java.util.Set;
023
024 import junit.framework.*;
025
026 import gate.*;
027 import gate.util.*;
028
029 /** CREOLE test class
030 */
031 public class TestConfig extends TestCase
032 {
033 /** Debug flag */
034 private static final boolean DEBUG = false;
035
036 /** Construction */
037 public TestConfig(String name) throws GateException { super(name); }
038
039 /** Fixture set up */
040 public void setUp() throws Exception {
041 CreoleRegister register = Gate.getCreoleRegister();
042 register.registerDirectories(Gate.getUrl("tests"));
043 } // setUp
044
045 /** Put things back as they should be after running tests
046 * (reinitialise the CREOLE register).
047 */
048 public void tearDown() throws Exception {
049 CreoleRegister register = Gate.getCreoleRegister();
050 register.clear();
051 Gate.init();
052 } // tearDown
053
054 /**
055 * Helper method that processes a config file.
056 */
057 private void readConfig(URL configUrl) throws Exception {
058 ConfigDataProcessor configProcessor = new ConfigDataProcessor();
059
060 // open a stream to the builtin config data file (tests version)
061 InputStream configStream = null;
062 try {
063 configStream = configUrl.openStream();
064 } catch(IOException e) {
065 throw new GateException(
066 "Couldn't open config data test file: " + configUrl + " " + e
067 );
068 }
069 if (DEBUG)
070 Out.prln(
071 "Parsing config file ... " + configStream + "from URL" + configUrl
072 );
073 configProcessor.parseConfigFile(configStream, configUrl);
074 } // readConfig
075
076 /** Test config loading */
077 public void testConfigReading() throws Exception {
078 System.out.println("Reading GATE config from : " + Gate.getUrl("tests/gate.xml"));
079 readConfig(Gate.getUrl("tests/gate.xml"));
080
081 // check that we got the CREOLE dir entry; then remove it
082 // so it doesn't get accessed in other tests
083 CreoleRegister reg = Gate.getCreoleRegister();
084 Set dirs = reg.getDirectories();
085 assertTrue(
086 "CREOLE register doesn't contain URL from test gate.xml",
087 dirs != null && ! dirs.isEmpty() &&
088 dirs.contains(new URL("http://gate.ac.uk/tests/"))
089 );
090
091 // we should have a GATECONFIG entry on Gate
092 String fullSizeKeyName = "FULLSIZE";
093 String fullSizeValueName = "yes";
094 Map gateConfig = Gate.getUserConfig();
095 assertNotNull("no gate config map", gateConfig);
096 String fullSizeValue = (String) gateConfig.get(fullSizeKeyName);
097 assertNotNull("no full size value", fullSizeValue);
098 assertEquals(
099 "incorrect config data from tests/gate.xml",
100 fullSizeValueName, fullSizeValue
101 );
102
103 // clear the gate config for subsequent tests
104 gateConfig.clear();
105
106
107 // the code below is removed after serial controller stopped
108 // being a PR. the XML config scripting of runnable systems isn't
109 // working anyhow. when/if it does work, appropriate tests should be
110 // re-added here
111 // // get a test system
112 // ResourceData controllerResData =
113 // (ResourceData) reg.get("gate.creole.SerialController");
114 // assertNotNull("no resdata for serial controller", controllerResData);
115 // ProcessingResource controller =
116 // (ProcessingResource) controllerResData.getInstantiations().pop();
117 // assertNotNull("no controller instance", controller);
118 //
119 // // try running the system
120 // controller.execute();
121 } // testConfigReading()
122
123 /** Test config updating */
124 public void testConfigUpdating() throws Exception {
125 // clear the gate config so we don't write values from the
126 // system initialisation into the test file
127 Map configMap = Gate.getUserConfig();
128 configMap.clear();
129
130 // if user config file exists, save it and remember the name
131 //String configName = Gate.getUserConfigFileName();
132 //File userConfigFile = new File(configName);
133 File userConfigFile = Gate.getUserConfigFile();
134 String configName = userConfigFile.getAbsolutePath();
135 File savedConfigFile = null;
136 if(userConfigFile.exists()) {
137 if(DEBUG) {
138 Out.prln(userConfigFile);
139 Out.prln("can write: " + userConfigFile.canWrite());
140 }
141 String userConfigDirectory = userConfigFile.getParent();
142 if(userConfigDirectory == null)
143 userConfigDirectory = "";
144 savedConfigFile = new File(
145 userConfigDirectory + Strings.getFileSep() +
146 "__saved_gate.xml__for_TestConfig__" + System.currentTimeMillis()
147 );
148 if(DEBUG) Out.prln(savedConfigFile);
149 boolean renamed = userConfigFile.renameTo(savedConfigFile);
150 assertTrue("rename failed", renamed);
151 }
152 assertTrue("user config file still there", ! userConfigFile.exists());
153
154 // call Gate.writeConfig - check it creates an empty config file
155 //this is no longer a valid test as the written user config will at least
156 //contain the values for the known and autload plugin paths.
157 Gate.writeUserConfig();
158 String writtenConfig = Files.getString(new File(configName));
159 String empty = Gate.getEmptyConfigFile();
160 // assertEquals("written config doesn't match", writtenConfig, empty);
161
162 // set some config attributes via Gate.getConfigData
163 configMap.put("A", "1");
164 configMap.put("B", "2");
165
166 // call Gate.writeConfig, delete the config data from Gate's map,
167 // read the config file and check that the new data is present
168 Gate.writeUserConfig();
169 configMap.clear();
170 readConfig(userConfigFile.toURI().toURL());
171
172 // reinstante saved user config file if not null
173 userConfigFile.delete();
174 if(savedConfigFile != null) {
175 savedConfigFile.renameTo(userConfigFile);
176 }
177
178 } // testConfigUpdating
179
180 /** Test session state file naming */
181 public void testSessionStateFileNaming() throws Exception {
182 String fileSep = Strings.getFileSep();
183 if(DEBUG) {
184 Out.prln("file sep is: " + fileSep);
185 }
186
187 if(Gate.runningOnUnix()) {
188 assertTrue(fileSep.equals("/"));
189 assertTrue(
190 Gate.getUserSessionFile().toString().endsWith("."+GateConstants.GATE_DOT_SER)
191 );
192 } else {
193 assertTrue(! fileSep.equals("/"));
194 assertTrue(
195 ! Gate.getUserSessionFile().toString().endsWith("."+GateConstants.GATE_DOT_SER)
196 );
197 }
198
199 } // testSessionStateFileNaming
200
201 /** Test config file naming */
202 public void testConfigFileNaming() throws Exception {
203 String fileSep = Strings.getFileSep();
204 if(DEBUG) {
205 Out.prln("file sep is: " + fileSep);
206 }
207
208 if(Gate.runningOnUnix()) {
209 assertTrue(fileSep.equals("/"));
210 assertTrue(
211 Gate.getDefaultUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
212 );
213 } else {
214 assertTrue(! fileSep.equals("/"));
215 assertTrue(
216 ! Gate.getDefaultUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
217 );
218 }
219
220 } // testConfigFileNaming
221
222 /** Test suite routine for the test runner */
223 public static Test suite() {
224 return new TestSuite(TestConfig.class);
225 } // suite
226
227 } // class TestConfig
|