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

The following examples show how to use org.apache.commons.configuration2.io.FileHandler#setFileName() . 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: 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 2
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
@Test
public void testIncludeLoadCyclicalReferenceFail() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath);
    handler.setFileName("include-cyclical-reference.properties");
    try {
        handler.load();
        Assert.fail("Expected " + Configuration.class.getCanonicalName());
    } catch (ConfigurationException e) {
        // expected
        // e.printStackTrace();
    }
    assertNull(pc.getString("keyA"));
}
 
Example 3
Source File: TestNullCompositeConfiguration.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(false);
}
 
Example 4
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 5
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 6
Source File: TestNonStringProperties.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception
{
    final PropertiesConfiguration c = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(c);
    handler.setFileName(testProperties);
    handler.load();
    conf = c;
    nonStringTestHolder.setConfiguration(conf);
}
 
Example 7
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeIncludeLoadCyclicalReferenceIgnore() 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-cyclical-reference.properties");
    handler.load();
    assertEquals("valueA", pc.getString("keyA"));
}
 
Example 8
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeLoadCyclicalMultiStepReferenceIgnore() 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-cyclical-root.properties");
    handler.load();
    assertEquals("valueA", pc.getString("keyA"));
}
 
Example 9
Source File: TestEqualsProperty.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testEquals() throws Exception
{
    final PropertiesConfiguration conf = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(conf);
    handler.setFileName(testProperties);
    handler.load();

    final String equals = conf.getString("test.equals");
    assertEquals("value=one", equals);
}
 
Example 10
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"));
}
 
Example 11
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 12
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 13
Source File: SystemConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Sets system properties from a file specified using its base path and
 * file name. The file can either be a properties file or an XML properties
 * file. It is loaded, and all properties it contains are added to system
 * properties.
 *
 * @param basePath The base path to look for the property file.
 * @param fileName The name of the property file.
 * @throws ConfigurationException if an error occurs.
 * @since 1.6
 */
public static void setSystemProperties(final String basePath, final String fileName)
        throws ConfigurationException
{
    final FileBasedConfiguration config =
            fileName.endsWith(".xml") ? new XMLPropertiesConfiguration()
                    : new PropertiesConfiguration();

    final FileHandler handler = new FileHandler(config);
    handler.setBasePath(basePath);
    handler.setFileName(fileName);
    handler.load();
    setSystemProperties(config);
}
 
Example 14
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 15
Source File: TestHierarchicalXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadBasePath1() throws Exception
{
    final FileHandler handler = new FileHandler(config);
    handler.setBasePath(TEST_DIR);
    handler.setFileName(TEST_FILENAME);
    handler.load();
    configTest(config);
}
 
Example 16
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadViaPropertyWithBasePath2() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath2);
    handler.setFileName("test.properties");
    handler.load();

    assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
}
 
Example 17
Source File: TestEqualBehavior.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
private Configuration setupSimpleConfiguration()
        throws Exception
{
    final String simpleConfigurationFile = ConfigurationAssert.getTestFile("testEqual.properties").getAbsolutePath();
    final PropertiesConfiguration c = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(c);
    handler.setFileName(simpleConfigurationFile);
    handler.load();
    return c;
}
 
Example 18
Source File: TestPropertiesSequence.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationValuesInSameOrderWithManualAdd() throws Exception
{
    final String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
    final String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();

    final PropertiesConfiguration simpleConfiguration = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(simpleConfiguration);
    handler.setFileName(simpleConfigurationFile);
    handler.load();

    final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
    builder.configure(new FileBasedBuilderParametersImpl().setFileName(compositeConfigurationFile));
    final Configuration compositeConfiguration = builder.getConfiguration();

    simpleConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
    simpleConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);

    compositeConfiguration.setProperty("prefix.Co.test", Boolean.TRUE);
    compositeConfiguration.setProperty("prefix.Av.test", Boolean.TRUE);

    final Configuration a = simpleConfiguration.subset("prefix");
    final Configuration b = compositeConfiguration.subset("prefix");

    final List<String> keysSimpleConfiguration = ConfigurationAssert.keysToList(a);
    final List<String> keysCompositeConfiguration = ConfigurationAssert.keysToList(b);

    assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
    assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());

    for (int i = 0; i < keysSimpleConfiguration.size(); i++)
    {
        assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
    }
}
 
Example 19
Source File: TestPropertiesSequence.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationValuesInSameOrderFromFile() throws Exception
{
    final String simpleConfigurationFile = ConfigurationAssert.getTestFile("testSequence.properties").getAbsolutePath();
    final String compositeConfigurationFile = ConfigurationAssert.getTestFile("testSequenceDigester.xml").getAbsolutePath();

    final PropertiesConfiguration simpleConfiguration = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(simpleConfiguration);
    handler.setFileName(simpleConfigurationFile);
    handler.load();

    final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
    builder.configure(new FileBasedBuilderParametersImpl().setFileName(compositeConfigurationFile));
    final Configuration compositeConfiguration = builder.getConfiguration();

    final Configuration a = simpleConfiguration.subset("prefix");
    final Configuration b = compositeConfiguration.subset("prefix");

    final List<String> keysSimpleConfiguration = ConfigurationAssert.keysToList(a);
    final List<String> keysCompositeConfiguration = ConfigurationAssert.keysToList(b);

    assertTrue("Size:" + keysSimpleConfiguration.size(), keysSimpleConfiguration.size() > 0);
    assertEquals(keysSimpleConfiguration.size(), keysCompositeConfiguration.size());

    for (int i = 0; i < keysSimpleConfiguration.size(); i++)
    {
        assertEquals(keysSimpleConfiguration.get(i), keysCompositeConfiguration.get(i));
    }
}
 
Example 20
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeLoadCyclicalReferenceIgnore() 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-cyclical-reference.properties");
    handler.load();
    assertEquals("valueA", pc.getString("keyA"));
}