Example usage for org.apache.commons.configuration XMLConfiguration XMLConfiguration

List of usage examples for org.apache.commons.configuration XMLConfiguration XMLConfiguration

Introduction

In this page you can find the example usage for org.apache.commons.configuration XMLConfiguration XMLConfiguration.

Prototype

public XMLConfiguration() 

Source Link

Document

Creates a new instance of XMLConfiguration.

Usage

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests the isEmpty() method for an empty configuration that was reloaded.
 *//*from  ww w  . ja  v a  2s. c o  m*/
@Test
public void testEmptyReload() throws ConfigurationException {
    XMLConfiguration config = new XMLConfiguration();
    assertTrue("Newly created configuration not empty", config.isEmpty());
    config.save(testSaveConf);
    config.load(testSaveConf);
    assertTrue("Reloaded configuration not empty", config.isEmpty());
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests whether the encoding is correctly detected by the XML parser. This
 * is done by loading an XML file with the encoding "UTF-16". If this
 * encoding is not detected correctly, an exception will be thrown that
 * "Content is not allowed in prolog". This test case is related to issue
 * 34204.//from w w  w . j a v a 2s.  co m
 */
@Test
@Ignore
public void testLoadWithEncoding() throws ConfigurationException {
    File file = ConfigurationAssert.getTestFile("testEncoding.xml");
    conf = new XMLConfiguration();
    conf.load(file);
    assertEquals("test3_yoge", conf.getString("yoge"));
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests whether the encoding is written to the generated XML file.
 *//*from w w  w  . j  a  v a  2  s.  c  o  m*/
@Test
public void testSaveWithEncoding() throws ConfigurationException {
    conf = new XMLConfiguration();
    conf.setProperty("test", "a value");
    conf.setEncoding(ENCODING);

    StringWriter out = new StringWriter();
    conf.save(out);
    assertTrue("Encoding was not written to file",
            out.toString().indexOf("encoding=\"" + ENCODING + "\"") >= 0);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests whether a default encoding is used if no specific encoding is set.
 * According to the XSLT specification (http://www.w3.org/TR/xslt#output)
 * this should be either UTF-8 or UTF-16.
 */// w  w w .j a va 2  s . c  o  m
@Test
public void testSaveWithNullEncoding() throws ConfigurationException {
    conf = new XMLConfiguration();
    conf.setProperty("testNoEncoding", "yes");
    conf.setEncoding(null);

    StringWriter out = new StringWriter();
    conf.save(out);
    assertTrue("Encoding was written to file", out.toString().indexOf("encoding=\"UTF-") >= 0);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests whether the DOCTYPE survives a save operation.
 *//*  ww  w  .  j a  v  a2  s  .  c o m*/
@Test
public void testSaveWithDoctype() throws ConfigurationException {
    String content = "<?xml  version=\"1.0\"?>" + DOCTYPE + "properties" + DOCTYPE_DECL
            + "<properties version=\"1.0\"><entry key=\"test\">value</entry></properties>";
    StringReader in = new StringReader(content);
    conf = new XMLConfiguration();
    conf.setFileName("testDtd.xml");
    conf.load();
    conf.clear();
    conf.load(in);

    assertEquals("Wrong public ID", PUBLIC_ID, conf.getPublicID());
    assertEquals("Wrong system ID", SYSTEM_ID, conf.getSystemID());
    StringWriter out = new StringWriter();
    conf.save(out);
    assertTrue("Did not find DOCTYPE", out.toString().indexOf(DOCTYPE) >= 0);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests setting text of the root element.
 *//*  w  w  w. j a  v  a 2 s . c o m*/
@Test
public void testSetTextRootElement() throws ConfigurationException {
    conf.setProperty("", "Root text");
    conf.save(testSaveConf);
    XMLConfiguration copy = new XMLConfiguration();
    copy.setFile(testSaveConf);
    checkSavedConfig(copy);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests removing the text of the root element.
 *///  w w  w . j  av  a 2s .  c  o  m
@Test
public void testClearTextRootElement() throws ConfigurationException {
    final String xml = "<e a=\"v\">text</e>";
    conf.clear();
    StringReader in = new StringReader(xml);
    conf.load(in);
    assertEquals("Wrong text of root", "text", conf.getString(""));

    conf.clearProperty("");
    conf.save(testSaveConf);
    XMLConfiguration copy = new XMLConfiguration();
    copy.setFile(testSaveConf);
    checkSavedConfig(copy);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Helper method for testing saving and loading a configuration when
 * delimiter parsing is disabled./*from   ww  w. j  a  v a2  s  .c o  m*/
 * 
 * @param key
 *            the key to be checked
 * @throws ConfigurationException
 *             if an error occurs
 */
private void checkSaveDelimiterParsingDisabled(String key) throws ConfigurationException {
    conf.clear();
    conf.setDelimiterParsingDisabled(true);
    conf.load();
    conf.setProperty(key, "C:\\Temp\\,C:\\Data\\");
    conf.addProperty(key, "a,b,c");
    conf.save(testSaveConf);
    XMLConfiguration checkConf = new XMLConfiguration();
    checkConf.setDelimiterParsingDisabled(true);
    checkConf.setFile(testSaveConf);
    checkSavedConfig(checkConf);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests using multiple attribute values, which are partly escaped when
 * delimiter parsing is not disabled.// w w  w.j a va2s.c o m
 */
@Test
public void testMultipleAttrValuesEscaped() throws ConfigurationException {
    conf.addProperty("test.dir[@name]", "C:\\Temp\\");
    conf.addProperty("test.dir[@name]", "C:\\Data\\");
    conf.save(testSaveConf);
    XMLConfiguration checkConf = new XMLConfiguration();
    checkConf.setFile(testSaveConf);
    checkSavedConfig(checkConf);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests whether the name of the root element is copied for a configuration
 * for which not yet a document exists./*from w  w  w .j  av  a  2  s  .c o m*/
 */
@Test
public void testCopyRootNameNoDocument() throws ConfigurationException {
    final String rootName = "rootElement";
    conf = new XMLConfiguration();
    conf.setRootElementName(rootName);
    conf.setProperty("test", Boolean.TRUE);
    XMLConfiguration copy = new XMLConfiguration(conf);
    assertEquals("Wrong name of root element", rootName, copy.getRootElementName());
    copy.save(testSaveConf);
    copy = new XMLConfiguration(testSaveConf);
    assertEquals("Wrong name of root element after save", rootName, copy.getRootElementName());
}