org.apache.commons.configuration2.io.FileHandler Java Examples

The following examples show how to use org.apache.commons.configuration2.io.FileHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestXPathExpressionEngineInConfig.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether configuration properties with a namespace can be handled.
 */
@Test
public void testPropertiesWithNamespace() throws ConfigurationException
{
    final String xml =
            "<Config>\n"
                    + "<dsig:Transforms xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\">\n"
                    + "  <dsig:Transform Algorithm=\"http://www.w3.org/TR/1999/REC-xpath-19991116\">\n"
                    + "    <dsig:XPath xmlns:ietf=\"http://www.ietf.org\" xmlns:pl=\"http://test.test\">self::pl:policy1</dsig:XPath>\n"
                    + "  </dsig:Transform>\n"
                    + "  <dsig:Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>\n"
                    + "</dsig:Transforms>" + "</Config>";
    final FileHandler handler = new FileHandler(config);
    handler.load(new StringReader(xml));

    for (final Iterator<String> it = config.getKeys(); it.hasNext();)
    {
        final String key = it.next();
        assertNotNull("No value for " + key, config.getString(key));
    }
}
 
Example #2
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a windows path can be saved correctly. This test is related
 * to CONFIGURATION-428.
 */
@Test
public void testSaveWindowsPath() throws ConfigurationException
{
    conf.clear();
    conf.setListDelimiterHandler(new DisabledListDelimiterHandler());
    conf.addProperty("path", "C:\\Temp");
    final StringWriter writer = new StringWriter();
    new FileHandler(conf).save(writer);
    final String content = writer.toString();
    assertThat("Path not found: ", content,
            containsString("<path>C:\\Temp</path>"));
    saveTestConfig();
    final XMLConfiguration conf2 = new XMLConfiguration();
    load(conf2, testSaveConf.getAbsolutePath());
    assertEquals("Wrong windows path", "C:\\Temp",
            conf2.getString("path"));
}
 
Example #3
Source File: FileConfigurationProvider.java    From james-project with Apache License 2.0 6 votes vote down vote up
public static XMLConfiguration getConfig(InputStream configStream) throws ConfigurationException {
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
        .configure(new Parameters()
            .xml()
            .setListDelimiterHandler(new DisabledListDelimiterHandler()));
    XMLConfiguration xmlConfiguration = builder.getConfiguration();
    FileHandler fileHandler = new FileHandler(xmlConfiguration);
    fileHandler.load(configStream);
    try {
        configStream.close();
    } catch (IOException ignored) {
        // Ignored
    }

    return xmlConfiguration;
}
 
Example #4
Source File: TestXMLPropertyListConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether the encoding is written when saving a configuration.
 */
@Test
public void testSaveWithEncoding() throws ConfigurationException
{
    final String encoding = "UTF-8";
    final FileHandler handler = new FileHandler(config);
    handler.setEncoding(encoding);
    final StringWriter writer = new StringWriter();
    handler.save(writer);
    assertTrue(
            "Encoding not found",
            writer.toString()
                    .indexOf(
                            "<?xml version=\"1.0\" encoding=\"" + encoding
                                    + "\"?>") >= 0);
}
 
Example #5
Source File: TestReloadingFileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a custom reloading detector factory can be installed.
 */
@Test
public void testCreateReloadingDetectoryCustomFactory()
        throws ConfigurationException
{
    final ReloadingDetector detector =
            EasyMock.createMock(ReloadingDetector.class);
    final ReloadingDetectorFactory factory =
            EasyMock.createMock(ReloadingDetectorFactory.class);
    final FileHandler handler = new FileHandler();
    final FileBasedBuilderParametersImpl params =
            new FileBasedBuilderParametersImpl();
    EasyMock.expect(factory.createReloadingDetector(handler, params))
            .andReturn(detector);
    EasyMock.replay(detector, factory);
    params.setReloadingDetectorFactory(factory);
    final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new ReloadingFileBasedConfigurationBuilder<>(
                    PropertiesConfiguration.class);
    assertSame("Wrong detector", detector,
            builder.createReloadingDetector(handler, params));
    EasyMock.verify(factory);
}
 
Example #6
Source File: DefaultReloadingDetectorFactory.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Override
public ReloadingDetector createReloadingDetector(final FileHandler handler,
        final FileBasedBuilderParametersImpl params)
        throws ConfigurationException
{
    final Long refreshDelay = params.getReloadingRefreshDelay();

    final FileHandlerReloadingDetector fileHandlerReloadingDetector =
            refreshDelay != null ? new FileHandlerReloadingDetector(
            handler, refreshDelay) : new FileHandlerReloadingDetector(
            handler);

    fileHandlerReloadingDetector.refresh();

    return fileHandlerReloadingDetector;
}
 
Example #7
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Test
public void testIncludeIncludeLoadCyclicalReferenceFail() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath);
    handler.setFileName("include-include-cyclical-reference.properties");
    try {
        handler.load();
        Assert.fail("Expected " + Configuration.class.getCanonicalName());
    } catch (ConfigurationException e) {
        // expected
        // e.printStackTrace();
    }
    assertNull(pc.getString("keyA"));
}
 
Example #8
Source File: TestDefaultReloadingDetectorFactory.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a reloading detector is created correctly.
 */
@Test
public void testCreateReloadingDetector() throws ConfigurationException
{
    final FileHandler handler = new FileHandler();
    final FileBasedBuilderParametersImpl params =
            new FileBasedBuilderParametersImpl();
    final Long refreshDelay = 10000L;
    params.setReloadingRefreshDelay(refreshDelay);
    final FileHandlerReloadingDetector detector =
            (FileHandlerReloadingDetector) factory.createReloadingDetector(
                    handler, params);
    assertSame("Wrong file handler", handler, detector.getFileHandler());
    assertEquals("Wrong refresh delay", refreshDelay.longValue(),
            detector.getRefreshDelay());
}
 
Example #9
Source File: TestCompositeConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception
{
    cc = new CompositeConfiguration();
    final ListDelimiterHandler listHandler = new LegacyListDelimiterHandler(',');
    conf1 = new PropertiesConfiguration();
    conf1.setListDelimiterHandler(listHandler);
    final FileHandler handler1 = new FileHandler(conf1);
    handler1.setFileName(testProperties);
    handler1.load();
    conf2 = new PropertiesConfiguration();
    conf2.setListDelimiterHandler(listHandler);
    final FileHandler handler2 = new FileHandler(conf2);
    handler2.setFileName(testProperties2);
    handler2.load();
    xmlConf = new XMLConfiguration();
    final FileHandler handler3 = new FileHandler(xmlConf);
    handler3.load(new File(testPropertiesXML));

    cc.setThrowExceptionOnMissing(true);
}
 
Example #10
Source File: TestReloadingFileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a correct reloading detector is created if no custom factory
 * was set.
 */
@Test
public void testCreateReloadingDetectorDefaultFactory() throws ConfigurationException
{
    final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new ReloadingFileBasedConfigurationBuilder<>(
                    PropertiesConfiguration.class);
    final FileHandler handler = new FileHandler();
    final FileBasedBuilderParametersImpl params = new FileBasedBuilderParametersImpl();
    final long refreshDelay = 60000L;
    params.setReloadingRefreshDelay(refreshDelay);
    final FileHandlerReloadingDetector detector =
            (FileHandlerReloadingDetector) builder.createReloadingDetector(
                    handler, params);
    assertSame("Wrong file handler", handler, detector.getFileHandler());
    assertEquals("Wrong refresh delay", refreshDelay,
            detector.getRefreshDelay());
}
 
Example #11
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the escaping of quotation marks in a properties value. This test is
 * related to CONFIGURATION-516.
 */
@Test
public void testEscapeQuote() throws ConfigurationException
{
    conf.clear();
    final String text = "\"Hello World!\"";
    conf.setProperty(PROP_NAME, text);
    final StringWriter out = new StringWriter();
    new FileHandler(conf).save(out);
    assertTrue("Value was escaped: " + out,
            out.toString().contains(text));
    saveTestConfig();
    final PropertiesConfiguration c2 = new PropertiesConfiguration();
    load(c2, testSavePropertiesFile.getAbsolutePath());
    assertEquals("Wrong value", text, c2.getString(PROP_NAME));
}
 
Example #12
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a properties configuration can be successfully cloned. It
 * is especially checked whether the layout object is taken into account.
 */
@Test
public void testClone() throws ConfigurationException
{
    final PropertiesConfiguration copy = (PropertiesConfiguration) conf.clone();
    assertNotSame("Copy has same layout object", conf.getLayout(),
            copy.getLayout());
    assertEquals("Wrong number of event listeners for original", 1, conf
            .getEventListeners(ConfigurationEvent.ANY).size());
    assertEquals("Wrong number of event listeners for clone", 1, copy
            .getEventListeners(ConfigurationEvent.ANY).size());
    assertSame("Wrong event listener for original", conf.getLayout(), conf
            .getEventListeners(ConfigurationEvent.ANY).iterator().next());
    assertSame("Wrong event listener for clone", copy.getLayout(), copy
            .getEventListeners(ConfigurationEvent.ANY).iterator().next());
    final StringWriter outConf = new StringWriter();
    new FileHandler(conf).save(outConf);
    final StringWriter outCopy = new StringWriter();
    new FileHandler(copy).save(outCopy);
    assertEquals("Output from copy is different", outConf.toString(), outCopy.toString());
}
 
Example #13
Source File: TestHierarchicalXMLConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Test
public void testSave() throws Exception
{
    final FileHandler handler = new FileHandler(config);
    handler.setFileName(TEST_FILE3);
    handler.load();
    final File saveFile = folder.newFile(TEST_SAVENAME);
    handler.save(saveFile);

    config = new XMLConfiguration();
    final FileHandler handler2 = new FileHandler(config);
    handler2.load(saveFile.toURI().toURL());
    assertEquals("value", config.getProperty("element"));
    assertEquals("I'm complex!", config.getProperty("element2.subelement.subsubelement"));
    assertEquals(8, config.getInt("test.short"));
    assertEquals("one", config.getString("list(0).item(0)[@name]"));
    assertEquals("two", config.getString("list(0).item(1)"));
    assertEquals("six", config.getString("list(1).sublist.item(1)"));
}
 
Example #14
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether the name of the root element is copied when a configuration
 * is created using the copy constructor.
 */
@Test
public void testCopyRootName() throws ConfigurationException
{
    final String rootName = "rootElement";
    final String xml = "<" + rootName + "><test>true</test></" + rootName
            + ">";
    conf.clear();
    new FileHandler(conf).load(new StringReader(xml));
    XMLConfiguration copy = new XMLConfiguration(conf);
    assertEquals("Wrong name of root element", rootName, copy
            .getRootElementName());
    new FileHandler(copy).save(testSaveConf);
    copy = new XMLConfiguration();
    load(copy, testSaveConf.getAbsolutePath());
    assertEquals("Wrong name of root element after save", rootName, copy
            .getRootElementName());
}
 
Example #15
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether saving works correctly with the default list delimiter
 * handler implementation.
 */
@Test
public void testSaveWithDefaultListDelimiterHandler() throws ConfigurationException
{
    conf.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
    saveTestConfig();

    final PropertiesConfiguration checkConfig = new PropertiesConfiguration();
    checkConfig.setListDelimiterHandler(conf.getListDelimiterHandler());
    new FileHandler(checkConfig).load(testSavePropertiesFile);
    ConfigurationAssert.assertConfigurationEquals(conf, checkConfig);
}
 
Example #16
Source File: TestFileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the default encoding is set for the file handler if none is
 * specified.
 */
@Test
public void testInitFileHandlerSetDefaultEncoding()
        throws ConfigurationException
{
    final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new FileBasedConfigurationBuilder<>(
                    PropertiesConfiguration.class);
    final FileHandler handler = new FileHandler();
    builder.initFileHandler(handler);
    assertEquals("Wrong encoding",
            PropertiesConfiguration.DEFAULT_ENCODING, handler.getEncoding());
}
 
Example #17
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeIncludeLoadAllOnNotFound() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    pc.setIncludeListener(PropertiesConfiguration.NOOP_INCLUDE_LISTENER);
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath);
    handler.setFileName("include-include-not-found.properties");
    handler.load();
    assertEquals("valueA", pc.getString("keyA"));
    assertEquals("valueB", pc.getString("keyB"));
}
 
Example #18
Source File: TestFileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether a test configuration was saved successfully.
 *
 * @param file the file to which the configuration was saved
 * @param expValue the expected value of the test property
 * @throws ConfigurationException if an error occurs
 */
private static void checkSavedConfig(final File file, final int expValue)
        throws ConfigurationException
{
    final PropertiesConfiguration config = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(config);
    handler.load(file);
    assertEquals("Configuration was not saved", expValue,
            config.getInt(PROP));
}
 
Example #19
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadIncludeOptional() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath);
    handler.setFileName("includeoptional.properties");
    handler.load();

    assertTrue("Make sure we have multiple keys", pc.getBoolean("includeoptional.loaded"));
}
 
Example #20
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadViaPropertyWithBasePath() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath);
    handler.setFileName("test.properties");
    handler.load();

    assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
}
 
Example #21
Source File: TestCompositeConfigurationNonStringProperties.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception
{
    final CompositeConfiguration cc = new CompositeConfiguration();
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pc);
    handler.setFileName(testProperties);
    handler.load();
    cc.addConfiguration(pc);
    conf = cc;
    nonStringTestHolder.setConfiguration(conf);
}
 
Example #22
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadFromFile() throws Exception
{
    final File file = ConfigurationAssert.getTestFile("test.properties");
    conf.clear();
    final FileHandler handler = new FileHandler(conf);
    handler.setFile(file);
    handler.load();

    assertEquals("true", conf.getString("configuration.loaded"));
}
 
Example #23
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Test the creation of a file containing a '#' in its name.
 */
@Test
public void testFileWithSharpSymbol() throws Exception
{
    final File file = folder.newFile("sharp#1.properties");

    final PropertiesConfiguration conf = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(conf);
    handler.setFile(file);
    handler.load();
    handler.save();

    assertTrue("Missing file " + file, file.exists());
}
 
Example #24
Source File: TestFileHandlerReloadingDetector.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
   * Tests whether an instance can be created with a file handler.
   */
  @Test
  public void testInitWithFileHandler()
  {
final FileHandler handler = new FileHandler();
final FileHandlerReloadingDetector detector = new FileHandlerReloadingDetector(
		handler);
assertSame("Different file handler", handler, detector.getFileHandler());
  }
 
Example #25
Source File: TestFileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the default encoding can be overridden when initializing
 * the file handler.
 */
@Test
public void testInitFileHandlerOverrideDefaultEncoding()
        throws ConfigurationException
{
    final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new FileBasedConfigurationBuilder<>(
                    PropertiesConfiguration.class);
    final FileHandler handler = new FileHandler();
    final String encoding = "testEncoding";
    handler.setEncoding(encoding);
    builder.initFileHandler(handler);
    assertEquals("Encoding was changed", encoding, handler.getEncoding());
}
 
Example #26
Source File: TestXMLPropertyListConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the header of a saved file if no encoding is specified.
 */
@Test
public void testSaveNoEncoding() throws ConfigurationException
{
    final StringWriter writer = new StringWriter();
    new FileHandler(config).save(writer);
    assertTrue("Wrong document header",
            writer.toString().indexOf("<?xml version=\"1.0\"?>") >= 0);
}
 
Example #27
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if properties can be appended by simply calling load() another
 * time.
 */
@Test
public void testAppend() throws Exception
{
    final File file2 = ConfigurationAssert.getTestFile("threesome.properties");
    final FileHandler handler = new FileHandler(conf);
    handler.load(file2);
    assertEquals("aaa", conf.getString("test.threesome.one"));
    assertEquals("true", conf.getString("configuration.loaded"));
}
 
Example #28
Source File: TestPropertyListConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadWithError()
{
    config = new PropertyListConfiguration();
    try {
        new FileHandler(config).load(new StringReader(""));
        fail("No exception thrown on loading an empty file");
    } catch (final ConfigurationException e) {
        // expected
        assertNotNull(e.getMessage());
    }
}
 
Example #29
Source File: TestHierarchicalXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that it is not allowed to change the root element name when the
 * configuration was loaded from a file.
 */
@Test(expected = UnsupportedOperationException.class)
public void testSetRootElementNameWhenLoadedFromFile() throws Exception
{
    final FileHandler handler = new FileHandler(config);
    handler.setFile(new File(TEST_FILE3));
    handler.load();
    assertEquals("testconfig", config.getRootElementName());
    config.setRootElementName("anotherRootElement");
}
 
Example #30
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeLoadAllOnLoadException() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    pc.setIncludeListener(PropertiesConfiguration.NOOP_INCLUDE_LISTENER);
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath);
    handler.setFileName("include-load-exception.properties");
    handler.load();
    assertEquals("valueA", pc.getString("keyA"));
}