1   /*
2    *  TestConfig.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, 9/Nov/00
12   *
13   *  $Id: TestConfig.java,v 1.14 2004/08/02 16:37:10 valyt 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      readConfig(Gate.getUrl("tests/gate.xml"));
78  
79      // check that we got the CREOLE dir entry; then remove it
80      // so it doesn't get accessed in other tests
81      CreoleRegister reg = Gate.getCreoleRegister();
82      Set dirs = reg.getDirectories();
83      assertTrue(
84        "CREOLE register doesn't contain URL from test gate.xml",
85        dirs != null && ! dirs.isEmpty() &&
86        dirs.contains(new URL("http://somewhere.on.the.net/creole/"))
87      );
88  
89      // we should have a GATECONFIG entry on Gate
90      String fullSizeKeyName = "FULLSIZE";
91      String fullSizeValueName = "yes";
92      Map gateConfig = Gate.getUserConfig();
93      assertNotNull("no gate config map", gateConfig);
94      String fullSizeValue = (String) gateConfig.get(fullSizeKeyName);
95      assertNotNull("no full size value", fullSizeValue);
96      assertEquals(
97        "incorrect config data from tests/gate.xml",
98        fullSizeValueName, fullSizeValue
99      );
100 
101     // clear the gate config for subsequent tests
102     gateConfig.clear();
103 
104 
105 // the code below is removed after serial controller stopped
106 // being a PR. the XML config scripting of runnable systems isn't
107 // working anyhow. when/if it does work, appropriate tests should be
108 // re-added here
109 //    // get a test system
110 //    ResourceData controllerResData =
111 //      (ResourceData) reg.get("gate.creole.SerialController");
112 //    assertNotNull("no resdata for serial controller", controllerResData);
113 //    ProcessingResource controller =
114 //      (ProcessingResource) controllerResData.getInstantiations().pop();
115 //    assertNotNull("no controller instance", controller);
116 //
117 //    // try running the system
118 //    controller.execute();
119   } // testConfigReading()
120 
121   /** Test config updating */
122   public void testConfigUpdating() throws Exception {
123     // clear the gate config so we don't write values from the
124     // system initialisation into the test file
125     Map configMap = Gate.getUserConfig();
126     configMap.clear();
127 
128     // if user config file exists, save it and remember the name
129     String configName = Gate.getUserConfigFileName();
130     File userConfigFile = new File(configName);
131     File savedConfigFile = null;
132     if(userConfigFile.exists()) {
133       if(DEBUG) {
134         Out.prln(userConfigFile);
135         Out.prln("can write: " + userConfigFile.canWrite());
136       }
137       String userConfigDirectory = userConfigFile.getParent();
138       if(userConfigDirectory == null)
139         userConfigDirectory = "";
140       savedConfigFile = new File(
141         userConfigDirectory + Strings.getFileSep() +
142         "__saved_gate.xml__for_TestConfig__" + System.currentTimeMillis()
143       );
144       if(DEBUG) Out.prln(savedConfigFile);
145       boolean renamed = userConfigFile.renameTo(savedConfigFile);
146       assertTrue("rename failed", renamed);
147     }
148     assertTrue("user config file still there", ! userConfigFile.exists());
149 
150     // call Gate.writeConfig - check it creates an empty config file
151     //this is no longer a valid test as the written user config will at least
152     //contain the values for the known and autload plugin paths.
153     Gate.writeUserConfig();
154     String writtenConfig = Files.getString(new File(configName));
155     String empty = Gate.getEmptyConfigFile();
156 //    assertEquals("written config doesn't match", writtenConfig, empty);
157 
158     // set some config attributes via Gate.getConfigData
159     configMap.put("A", "1");
160     configMap.put("B", "2");
161 
162     // call Gate.writeConfig, delete the config data from Gate's map,
163     // read the config file and check that the new data is present
164     Gate.writeUserConfig();
165     configMap.clear();
166     readConfig(userConfigFile.toURL());
167 
168     // reinstante saved user config file if not null
169     userConfigFile.delete();
170     if(savedConfigFile != null) {
171       savedConfigFile.renameTo(userConfigFile);
172     }
173 
174   } // testConfigUpdating
175 
176   /** Test session state file naming */
177   public void testSessionStateFileNaming() throws Exception {
178     String fileSep = Strings.getFileSep();
179     if(DEBUG) {
180       Out.prln("file sep is: " + fileSep);
181     }
182 
183     if(Gate.runningOnUnix()) {
184       assertTrue(fileSep.equals("/"));
185       assertTrue(
186         Gate.getUserSessionFileName().endsWith("."+GateConstants.GATE_DOT_SER)
187       );
188     } else {
189       assertTrue(! fileSep.equals("/"));
190       assertTrue(
191         ! Gate.getUserSessionFileName().endsWith("."+GateConstants.GATE_DOT_SER)
192       );
193     }
194 
195   } // testSessionStateFileNaming
196 
197   /** Test config file naming */
198   public void testConfigFileNaming() throws Exception {
199     String fileSep = Strings.getFileSep();
200     if(DEBUG) {
201       Out.prln("file sep is: " + fileSep);
202     }
203 
204     if(Gate.runningOnUnix()) {
205       assertTrue(fileSep.equals("/"));
206       assertTrue(
207         Gate.getUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
208       );
209     } else {
210       assertTrue(! fileSep.equals("/"));
211       assertTrue(
212         ! Gate.getUserConfigFileName().endsWith("."+GateConstants.GATE_DOT_XML)
213       );
214     }
215 
216   } // testConfigFileNaming
217 
218   /** Test suite routine for the test runner */
219   public static Test suite() {
220     return new TestSuite(TestConfig.class);
221   } // suite
222 
223 } // class TestConfig
224