Java Code Examples for org.apache.commons.configuration2.io.FileHandler#save()

The following examples show how to use org.apache.commons.configuration2.io.FileHandler#save() . 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: 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 2
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 3
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests adding properties through a DataConfiguration. This is related to
 * CONFIGURATION-332.
 */
@Test
public void testSaveWithDataConfig() throws ConfigurationException
{
    conf = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(conf);
    handler.setFile(testSavePropertiesFile);
    final DataConfiguration dataConfig = new DataConfiguration(conf);
    dataConfig.setProperty("foo", "bar");
    assertEquals("Property not set", "bar", conf.getString("foo"));

    handler.save();
    final PropertiesConfiguration config2 = new PropertiesConfiguration();
    load(config2, testSavePropertiesFile.getAbsolutePath());
    assertEquals("Property not saved", "bar", config2.getString("foo"));
}
 
Example 4
Source File: TestSystemConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether system properties can be set from a configuration file.
 */
@Test
public void testSetSystemPropertiesFromPropertiesFile()
        throws ConfigurationException, IOException
{
    final File file = folder.newFile("sys.properties");
    final PropertiesConfiguration pconfig = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pconfig);
    pconfig.addProperty("fromFile", Boolean.TRUE);
    handler.setFile(file);
    handler.save();
    SystemConfiguration.setSystemProperties(handler.getBasePath(),
            handler.getFileName());
    final SystemConfiguration sconf = new SystemConfiguration();
    assertTrue("Property from file not found", sconf.getBoolean("fromFile"));
}
 
Example 5
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 6
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests initializing a properties configuration from a non existing file.
 * There was a bug, which caused properties getting lost when later save()
 * is called.
 */
@Test
public void testInitFromNonExistingFile() throws ConfigurationException
{
    final String testProperty = "test.successfull";
    conf = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(conf);
    handler.setFile(testSavePropertiesFile);
    conf.addProperty(testProperty, "true");
    handler.save();
    checkSavedConfig();
}
 
Example 7
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testSaveToCustomURL() throws Exception
{
    // save the configuration to a custom URL
    final URL url = new URL("foo", "", 0, folder.newFile("testsave-custom-url.properties").getAbsolutePath(), new FileURLStreamHandler());
    final FileHandler handlerSave = new FileHandler(conf);
    handlerSave.save(url);

    // reload the configuration
    final PropertiesConfiguration config2 = new PropertiesConfiguration();
    final FileHandler handlerLoad = new FileHandler(config2);
    handlerLoad.load(url);
    assertEquals("true", config2.getString("configuration.loaded"));
}
 
Example 8
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if the base path is taken into account by the save() method.
 */
@Test
public void testSaveWithBasePath() throws Exception
{
    conf.setProperty("test", "true");
    final FileHandler handler = new FileHandler(conf);
    handler.setBasePath(testSavePropertiesFile.getParentFile().toURI().toURL()
            .toString());
    handler.setFileName(testSavePropertiesFile.getName());
    handler.save();
    assertTrue(testSavePropertiesFile.exists());
}
 
Example 9
Source File: TestXMLPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testSave() throws Exception
{
    // load the configuration
    final XMLPropertiesConfiguration conf = load(TEST_PROPERTIES_FILE);

    // update the configuration
    conf.addProperty("key4", "value4");
    conf.clearProperty("key2");
    conf.setHeader("Description of the new property list");

    // save the configuration
    final File saveFile = folder.newFile("test2.properties.xml");
    final FileHandler saveHandler = new FileHandler(conf);
    saveHandler.save(saveFile);

    // reload the configuration
    final XMLPropertiesConfiguration conf2 = load(saveFile.getAbsolutePath());

    // test the configuration
    assertEquals("header", "Description of the new property list", conf2.getHeader());

    assertFalse("The configuration is empty", conf2.isEmpty());
    assertEquals("'key1' property", "value1", conf2.getProperty("key1"));
    assertEquals("'key3' property", "value3", conf2.getProperty("key3"));
    assertEquals("'key4' property", "value4", conf2.getProperty("key4"));
}
 
Example 10
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests saving to a URL.
 */
@Test
public void testSaveToURL() throws Exception
{
    final FileHandler handler = new FileHandler(conf);
    handler.save(testSaveConf.toURI().toURL());
    checkSavedConfig(testSaveConf);
}
 
Example 11
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the encoding is written to the generated XML file.
 */
@Test
public void testSaveWithEncoding() throws ConfigurationException
{
    conf = new XMLConfiguration();
    conf.setProperty("test", "a value");
    final FileHandler handler = new FileHandler(conf);
    handler.setEncoding(ENCODING);

    final StringWriter out = new StringWriter();
    handler.save(out);
    assertThat("Encoding was not written to file", out.toString(),
            containsString("encoding=\"" + ENCODING + "\""));
}
 
Example 12
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * 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.
 */
@Test
public void testSaveWithNullEncoding() throws ConfigurationException
{
    conf = new XMLConfiguration();
    conf.setProperty("testNoEncoding", "yes");
    final FileHandler handler = new FileHandler(conf);

    final StringWriter out = new StringWriter();
    handler.save(out);
    assertThat("Encoding was written to file", out.toString(),
            containsString("encoding=\"UTF-"));
}
 
Example 13
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testSaveWithRootAttributes() throws ConfigurationException
{
    conf.setProperty("[@xmlns:ex]", "http://example.com/");
    assertEquals("Root attribute not set", "http://example.com/", conf
            .getString("[@xmlns:ex]"));
    final FileHandler handler = new FileHandler(conf);

    final StringWriter out = new StringWriter();
    handler.save(out);
    assertThat("Encoding was not written to file", out.toString(),
            containsString("testconfig xmlns:ex=\"http://example.com/\""));
}
 
Example 14
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testSaveWithRootAttributes_ByHand() throws ConfigurationException
{
    conf = new XMLConfiguration();
    conf.addProperty(  "[@xmlns:foo]",  "http://example.com/" );
    assertEquals("Root attribute not set", "http://example.com/", conf
            .getString("[@xmlns:foo]"));
    final FileHandler handler = new FileHandler(conf);

    final StringWriter out = new StringWriter();
    handler.save(out);
    assertThat("Encoding was not written to file", out.toString(),
            containsString("configuration xmlns:foo=\"http://example.com/\""));
}
 
Example 15
Source File: TestXMLListHandling.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Saves the test configuration into a string.
 *
 * @return the resulting string
 */
private String saveToString() throws ConfigurationException
{
    final StringWriter writer = new StringWriter(4096);
    final FileHandler handler = new FileHandler(config);
    handler.save(writer);
    return writer.toString();
}
 
Example 16
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
@Test(expected = ConfigurationException.class)
public void testSaveMissingFilename() throws ConfigurationException
{
    final FileHandler handler = new FileHandler(conf);
    handler.save();
}
 
Example 17
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Saves the test configuration to a default output file.
 *
 * @throws ConfigurationException if an error occurs
 */
private void saveTestConfig() throws ConfigurationException
{
    final FileHandler handler = new FileHandler(conf);
    handler.save(testSavePropertiesFile);
}
 
Example 18
Source File: TestCombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Tests whether reloading support works for MultiFileConfigurationBuilder.
 */
@Test
public void testMultiTenentConfigurationReloading()
        throws ConfigurationException, InterruptedException
{
    final CombinedConfiguration config =
            createMultiFileConfig("testCCMultiTenentReloading.xml");
    final File outFile =
            ConfigurationAssert.getOutFile("MultiFileReloadingTest.xml");
    switchToMultiFile(outFile.getAbsolutePath());
    final XMLConfiguration reloadConfig = new XMLConfiguration();
    final FileHandler handler = new FileHandler(reloadConfig);
    handler.setFile(outFile);
    final String key = "test.reload";
    reloadConfig.setProperty(key, "no");
    handler.save();
    try
    {
        assertEquals("Wrong property", "no", config.getString(key));
        final ConfigurationBuilder<? extends Configuration> childBuilder =
                builder.getNamedBuilder("clientConfig");
        assertTrue("Not a reloading builder",
                childBuilder instanceof ReloadingControllerSupport);
        final ReloadingController ctrl =
                ((ReloadingControllerSupport) childBuilder)
                        .getReloadingController();
        ctrl.checkForReloading(null); // initialize reloading
        final BuilderEventListenerImpl l = new BuilderEventListenerImpl();
        childBuilder.addEventListener(ConfigurationBuilderEvent.RESET, l);
        reloadConfig.setProperty(key, "yes");
        handler.save();

        int attempts = 10;
        boolean changeDetected;
        do
        {
            changeDetected = ctrl.checkForReloading(null);
            if (!changeDetected)
            {
                Thread.sleep(1000);
                handler.save(outFile);
            }
        } while (!changeDetected && --attempts > 0);
        assertTrue("No change detected", changeDetected);
        assertEquals("Wrong updated property", "yes", builder
                .getConfiguration().getString(key));
        final ConfigurationBuilderEvent event = l.nextEvent(ConfigurationBuilderEvent.RESET);
        l.assertNoMoreEvents();
        final BasicConfigurationBuilder<? extends Configuration> multiBuilder =
                (BasicConfigurationBuilder<? extends Configuration>) event.getSource();
        childBuilder.removeEventListener(ConfigurationBuilderEvent.RESET, l);
        multiBuilder.resetResult();
        l.assertNoMoreEvents();
    }
    finally
    {
        assertTrue("Output file could not be deleted", outFile.delete());
    }
}
 
Example 19
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Helper method for saving the test configuration to the default output
 * file.
 *
 * @throws ConfigurationException if an error occurs
 */
private void saveTestConfig() throws ConfigurationException
{
    final FileHandler handler = new FileHandler(conf);
    handler.save(testSaveConf);
}