| TestConfig |

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

| TestConfig |