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

The following examples show how to use org.apache.commons.configuration2.io.FileHandler#load() . 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 testIncludeLoadCyclicalMultiStepReferenceFail() throws Exception
{
    final PropertiesConfiguration pc = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(pc);
    handler.setBasePath(testBasePath);
    handler.setFileName("include-cyclical-root.properties");
    try {
        handler.load();
        Assert.fail("Expected " + Configuration.class.getCanonicalName());
    } catch (ConfigurationException e) {
        // expected
        // e.printStackTrace();
    }
    assertNull(pc.getString("keyA"));
}
 
Example 2
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 3
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 4
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether multiple include files can be resolved.
 */
@Test
public void testMultipleIncludeFiles() throws ConfigurationException
{
    conf = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(conf);
    handler.load(ConfigurationAssert.getTestFile("config/testMultiInclude.properties"));
    assertEquals("Wrong top-level property", "topValue",
            conf.getString("top"));
    assertEquals("Wrong included property (1)", 100,
            conf.getInt("property.c"));
    assertEquals("Wrong included property (2)", true,
            conf.getBoolean("include.loaded"));
}
 
Example 5
Source File: ConfigurationProviderImpl.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Load the xmlConfiguration from the given resource.
 * 
 * @param r
 * @return
 * @throws ConfigurationException
 * @throws IOException
 */
private XMLConfiguration getConfig(Resource r) throws ConfigurationException, IOException {
    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(r.getInputStream());

    return xmlConfiguration;
}
 
Example 6
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 7
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 8
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 9
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests removing the text of the root element.
 */
@Test
public void testClearTextRootElement() throws ConfigurationException
{
    final String xml = "<e a=\"v\">text</e>";
    conf.clear();
    final StringReader in = new StringReader(xml);
    final FileHandler handler = new FileHandler(conf);
    handler.load(in);
    assertEquals("Wrong text of root", "text", conf.getString(""));

    conf.clearProperty("");
    saveTestConfig();
    checkSavedConfig();
}
 
Example 10
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for loading the specified configuration file.
 *
 * @param config the configuration
 * @param fileName the name of the file to be loaded
 * @throws ConfigurationException if an error occurs
 */
private static void load(final XMLConfiguration config, final String fileName)
        throws ConfigurationException
{
    final FileHandler handler = new FileHandler(config);
    handler.setFileName(fileName);
    handler.load();
}
 
Example 11
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests loading a non well formed XML from a string.
 */
@Test(expected = ConfigurationException.class)
public void testLoadInvalidXML() throws Exception
{
    final String xml = "<?xml version=\"1.0\"?><config><test>1</rest></config>";
    conf = new XMLConfiguration();
    final FileHandler handler = new FileHandler(conf);
    handler.load(new StringReader(xml));
}
 
Example 12
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 13
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether include files can be resolved if a configuration file is
 * read from a reader.
 */
@Test
public void testLoadIncludeFromReader() throws ConfigurationException
{
    final StringReader in =
            new StringReader(PropertiesConfiguration.getInclude() + " = "
                    + ConfigurationAssert.getTestURL("include.properties"));
    conf = new PropertiesConfiguration();
    final FileHandler handler = new FileHandler(conf);
    handler.load(in);
    assertEquals("Include file not loaded", "true",
            conf.getString("include.loaded"));
}
 
Example 14
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 15
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 16
Source File: TestXMLConfiguration_605.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new configuration with the specified content and the given list
 * delimiter handler.
 *
 * @param content the XML content
 * @param delimiterHandler the list delimiter handler
 * @return the newly created configuration
 */
private static Configuration create(final String content, final ListDelimiterHandler delimiterHandler)
        throws ConfigurationException
{
    final XMLConfiguration config = new XMLConfiguration();
    config.setListDelimiterHandler(delimiterHandler);
    final FileHandler handler = new FileHandler(config);
    handler.load(new StringReader(content));
    return config;
}
 
Example 17
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 18
Source File: TestHierarchicalXMLConfiguration.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure various node types are correctly processed in config.
 */
@Test
public void testXmlNodeTypes() throws Exception
{
    // Number of keys expected from test configuration file
    final int KEY_COUNT = 5;

    // Load the configuration file
    final FileHandler handler = new FileHandler(config);
    handler.load(new File(TEST_FILE2).getAbsoluteFile().toURI().toURL());

    // Validate comment in element ignored
    assertEquals("Comment in element must not change element value.", "Case1Text", config
            .getString("case1"));

    // Validate sibling comment ignored
    assertEquals("Comment as sibling must not change element value.", "Case2Text", config
            .getString("case2.child"));

    // Validate comment ignored, CDATA processed
    assertEquals("Comment and use of CDATA must not change element value.", "Case3Text", config
            .getString("case3"));

    // Validate comment and processing instruction ignored
    assertEquals("Comment and use of PI must not change element value.", "Case4Text", config
            .getString("case4"));

    // Validate comment ignored in parent attribute
    assertEquals("Comment must not change attribute node value.", "Case5Text", config
            .getString("case5[@attr]"));

    // Validate non-text nodes haven't snuck in as keys
    final Iterator<String> iter = config.getKeys();
    int count = 0;
    while (iter.hasNext())
    {
        iter.next();
        count++;
    }
    assertEquals("Config must contain only " + KEY_COUNT + " keys.", KEY_COUNT, count);
}
 
Example 19
Source File: PropertiesConfiguration.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for loading an included properties file. This method is
 * called by {@code load()} when an {@code include} property
 * is encountered. It tries to resolve relative file names based on the
 * current base path. If this fails, a resolution based on the location of
 * this properties file is tried.
 *
 * @param fileName the name of the file to load
 * @param optional whether or not the {@code fileName} is optional
 * @param seenStack Stack of seen include URLs
 * @throws ConfigurationException if loading fails
 */
private void loadIncludeFile(final String fileName, final boolean optional, final Deque<URL> seenStack)
        throws ConfigurationException
{
    if (locator == null)
    {
        throw new ConfigurationException("Load operation not properly "
                + "initialized! Do not call read(InputStream) directly,"
                + " but use a FileHandler to load a configuration.");
    }

    URL url = locateIncludeFile(locator.getBasePath(), fileName);
    if (url == null)
    {
        final URL baseURL = locator.getSourceURL();
        if (baseURL != null)
        {
            url = locateIncludeFile(baseURL.toString(), fileName);
        }
    }

    if (optional && url == null)
    {
        return;
    }

    if (url == null)
    {
        getIncludeListener().accept(new ConfigurationException("Cannot resolve include file " + fileName,
                new FileNotFoundException(fileName)));
    }
    else
    {
        final FileHandler fh = new FileHandler(this);
        fh.setFileLocator(locator);
        final FileLocator orgLocator = locator;
        try
        {
            try
            {
                // Check for cycles
                if (seenStack.contains(url))
                {
                    throw new ConfigurationException(
                            String.format("Cycle detected loading %s, seen stack: %s", url, seenStack));
                }
                seenStack.add(url);
                try
                {
                    fh.load(url);
                }
                finally
                {
                    seenStack.pop();
                }
            }
            catch (ConfigurationException e)
            {
                getIncludeListener().accept(e);
            }
        }
        finally
        {
            locator = orgLocator; // reset locator which is changed by load
        }
    }
}
 
Example 20
Source File: TestCatalogResolver.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Loads the test configuration from the specified file.
 *
 * @param fileName the file name
 * @throws ConfigurationException if an error occurs
 */
private void load(final String fileName) throws ConfigurationException
{
    final FileHandler handler = new FileHandler(config);
    handler.load(fileName);
}