1
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
30 public class TestConfig extends TestCase
31 {
32
33 private static final boolean DEBUG = false;
34
35
36 public TestConfig(String name) throws GateException { super(name); }
37
38
39 public void setUp() throws Exception {
40 CreoleRegister register = Gate.getCreoleRegister();
41 register.registerDirectories(Gate.getUrl("tests"));
42 }
44
47 public void tearDown() throws Exception {
48 CreoleRegister register = Gate.getCreoleRegister();
49 register.clear();
50 Gate.init();
51 }
53
56 private void readConfig(URL configUrl) throws Exception {
57 ConfigDataProcessor configProcessor = new ConfigDataProcessor();
58
59 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 }
75
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 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 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 gateConfig.clear();
104
105
106 }
122
123 public void testConfigUpdating() throws Exception {
124 Map configMap = Gate.getUserConfig();
127 configMap.clear();
128
129 String configName = Gate.getUserConfigFileName();
131 File userConfigFile = new File(configName);
132 File savedConfigFile = null;
133 if(userConfigFile.exists()) {
134 if(DEBUG) {
135 Out.prln(userConfigFile);
136 Out.prln("can write: " + userConfigFile.canWrite());
137 }
138 String userConfigDirectory = userConfigFile.getParent();
139 if(userConfigDirectory == null)
140 userConfigDirectory = "";
141 savedConfigFile = new File(
142 userConfigDirectory + Strings.getFileSep() +
143 "__saved_gate.xml__for_TestConfig__" + System.currentTimeMillis()
144 );
145 if(DEBUG) Out.prln(savedConfigFile);
146 boolean renamed = userConfigFile.renameTo(savedConfigFile);
147 assertTrue("rename failed", renamed);
148 }
149 assertTrue("user config file still there", ! userConfigFile.exists());
150
151 Gate.writeUserConfig();
155 String writtenConfig = Files.getString(new File(configName));
156 String empty = Gate.getEmptyConfigFile();
157
159 configMap.put("A", "1");
161 configMap.put("B", "2");
162
163 Gate.writeUserConfig();
166 configMap.clear();
167 readConfig(userConfigFile.toURL());
168
169 userConfigFile.delete();
171 if(savedConfigFile != null) {
172 savedConfigFile.renameTo(userConfigFile);
173 }
174
175 }
177
178 public void testSessionStateFileNaming() throws Exception {
179 String fileSep = Strings.getFileSep();
180 if(DEBUG) {
181 Out.prln("file sep is: " + fileSep);
182 }
183
184 if(Gate.runningOnUnix()) {
185 assertTrue(fileSep.equals("/"));
186 assertTrue(
187 Gate.getUserSessionFileName().endsWith("."+GateConstants.GATE_DOT_SER)
188 );
189 } else {
190 assertTrue(! fileSep.equals("/"));
191 assertTrue(
192 ! Gate.getUserSessionFileName().endsWith("."+GateConstants.GATE_DOT_SER)
193 );
194 }
195
196 }
198
199 public void testConfigFileNaming() throws Exception {
200 String fileSep = Strings.getFileSep();
201 if(DEBUG) {
202 Out.prln("file sep is: " + fileSep);
203 }
204
205 if(Gate.runningOnUnix()) {
206 assertTrue(fileSep.equals("/"));
207 assertTrue(
208 Gate.getUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
209 );
210 } else {
211 assertTrue(! fileSep.equals("/"));
212 assertTrue(
213 ! Gate.getUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
214 );
215 }
216
217 }
219
220 public static Test suite() {
221 return new TestSuite(TestConfig.class);
222 }
224 }