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

The following examples show how to use org.apache.commons.configuration2.io.FileHandler#setFile() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: TestVFSFileHandlerReloadingDetector.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a non existing file is handled correctly.
 */
@Test
public void testLastModificationDateNonExisting()
{
    final File file = ConfigurationAssert.getOutFile("NonExistingFile.xml");
    final FileHandler handler = new FileHandler();
    handler.setFileSystem(new VFSFileSystem());
    handler.setFile(file);
    final VFSFileHandlerReloadingDetector strategy =
            new VFSFileHandlerReloadingDetector(handler);
    assertEquals("Got a modification date", 0,
            strategy.getLastModificationDate());
}
 
Example 8
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());
    }
}