commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1617826 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/fluent/Configurations.java test/java/org/apache/commons/configuration/builder/fluent/TestConfigurations.java
Date Wed, 13 Aug 2014 20:15:04 GMT
Author: oheger
Date: Wed Aug 13 20:15:04 2014
New Revision: 1617826

URL: http://svn.apache.org/r1617826
Log:
Created new Configurations class.

This class contains a bunch of convenience methods that simplify the creation
of concrete configuration classes. It can be used when no complex
initialization is required and default settings are appropriate.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Configurations.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestConfigurations.java

Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Configurations.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Configurations.java?rev=1617826&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Configurations.java
(added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Configurations.java
Wed Aug 13 20:15:04 2014
@@ -0,0 +1,640 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder.fluent;
+
+import java.io.File;
+import java.net.URL;
+
+import org.apache.commons.configuration.CombinedConfiguration;
+import org.apache.commons.configuration.FileBasedConfiguration;
+import org.apache.commons.configuration.INIConfiguration;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration.builder.FileBasedConfigurationBuilder;
+import org.apache.commons.configuration.builder.combined.CombinedConfigurationBuilder;
+import org.apache.commons.configuration.ex.ConfigurationException;
+
+/**
+ * <p>
+ * A convenience class which simplifies the creation of standard configurations
+ * and their builders.
+ * </p>
+ * <p>
+ * Complex initializations of configuration builders can be done in a pretty
+ * straight-forward way by making use of the provided fluent API. However, if
+ * only default settings are used (and maybe a configuration file to be loaded
+ * has to be specified), this approach tends to become a bit verbose. This class
+ * was introduced to simplify the creation of configuration objects in such
+ * cases. It offers a bunch of methods which allow the creation of some standard
+ * configuration classes with default settings passing in only a minimum
+ * required parameters.
+ * </p>
+ * <p>
+ * An an example consider the creation of a {@code PropertiesConfiguration}
+ * object from a file. Using a builder, code like the following one would have
+ * to be written:
+ *
+ * <pre>
+ * Parameters params = new Parameters();
+ * FileBasedConfigurationBuilder&lt;PropertiesConfiguration&gt; builder =
+ *         new FileBasedConfigurationBuilder&lt;PropertiesConfiguration&gt;(
+ *                 PropertiesConfiguration.class).configure(params.fileBased()
+ *                 .setFile(new File(&quot;config.properties&quot;)));
+ * PropertiesConfiguration config = builder.getConfiguration();
+ * </pre>
+ *
+ * With a convenience method of {@code Configurations} the same can be achieved
+ * with the following:
+ *
+ * <pre>
+ * Configurations configurations = new Configurations();
+ * PropertiesConfiguration config = configurations.properties(new File(
+ *         &quot;config.properties&quot;));
+ * </pre>
+ *
+ * There are similar methods for constructing builder objects from which
+ * configurations can then be obtained.
+ * </p>
+ * <p>
+ * This class is thread-safe. A single instance can be created by an application
+ * and used in a central way to create configuration objects. When an instance
+ * is created a {@link Parameters} instance can be passed in. Otherwise, a
+ * default instance is created. In any case, the {@code Parameters} instance
+ * associated with a {@code Configurations} object can be used to define default
+ * settings for the configurations to be created.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ * @see org.apache.commons.configuration.builder.DefaultParametersManager
+ */
+public class Configurations
+{
+    /** The parameters object associated with this instance. */
+    private final Parameters parameters;
+
+    /**
+     * Creates a new {@code Configurations} instance with default settings.
+     */
+    public Configurations()
+    {
+        this(null);
+    }
+
+    /**
+     * Creates a new instance of {@code Configurations} and initializes it with
+     * the specified {@code Parameters} object.
+     *
+     * @param params the {@code Parameters} (may be <b>null</b>, then a default
+     *        instance is created)
+     */
+    public Configurations(Parameters params)
+    {
+        parameters = (params != null) ? params : new Parameters();
+    }
+
+    /**
+     * Returns the {@code Parameters} instance associated with this object.
+     *
+     * @return the associated {@code Parameters} object
+     */
+    public Parameters getParameters()
+    {
+        return parameters;
+    }
+
+    /**
+     * Creates a {@code FileBasedConfigurationBuilder} for the specified
+     * configuration class and initializes it with the file to be loaded.
+     *
+     * @param configClass the configuration class
+     * @param file the file to be loaded
+     * @param <T> the type of the configuration to be constructed
+     * @return the new {@code FileBasedConfigurationBuilder}
+     */
+    public <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T>
fileBasedBuilder(
+            Class<T> configClass, File file)
+    {
+        return createFileBasedBuilder(configClass, fileParams(file));
+    }
+
+    /**
+     * Creates a {@code FileBasedConfigurationBuilder} for the specified
+     * configuration class and initializes it with the URL to the file to be
+     * loaded.
+     *
+     * @param configClass the configuration class
+     * @param url the URL to be loaded
+     * @param <T> the type of the configuration to be constructed
+     * @return the new {@code FileBasedConfigurationBuilder}
+     */
+    public <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T>
fileBasedBuilder(
+            Class<T> configClass, URL url)
+    {
+        return createFileBasedBuilder(configClass, fileParams(url));
+    }
+
+    /**
+     * Creates a {@code FileBasedConfigurationBuilder} for the specified
+     * configuration class and initializes it with the path to the file to be
+     * loaded.
+     *
+     * @param configClass the configuration class
+     * @param path the path to the file to be loaded
+     * @param <T> the type of the configuration to be constructed
+     * @return the new {@code FileBasedConfigurationBuilder}
+     */
+    public <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T>
fileBasedBuilder(
+            Class<T> configClass, String path)
+    {
+        return createFileBasedBuilder(configClass, fileParams(path));
+    }
+
+    /**
+     * Creates an instance of the specified file-based configuration class from
+     * the content of the given file. This is a convenience method which can be
+     * used if no builder is needed for managing the configuration object.
+     * (Although, behind the scenes a builder is created).
+     *
+     * @param configClass the configuration class
+     * @param file the file to be loaded
+     * @param <T> the type of the configuration to be constructed
+     * @return a {@code FileBasedConfiguration} object initialized from this
+     *         file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public <T extends FileBasedConfiguration> T fileBased(Class<T> configClass,
+            File file) throws ConfigurationException
+    {
+        return fileBasedBuilder(configClass, file).getConfiguration();
+    }
+
+    /**
+     * Creates an instance of the specified file-based configuration class from
+     * the content of the given URL. This is a convenience method which can be
+     * used if no builder is needed for managing the configuration object.
+     * (Although, behind the scenes a builder is created).
+     *
+     * @param configClass the configuration class
+     * @param url the URL to be loaded
+     * @param <T> the type of the configuration to be constructed
+     * @return a {@code FileBasedConfiguration} object initialized from this
+     *         file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public <T extends FileBasedConfiguration> T fileBased(Class<T> configClass,
+            URL url) throws ConfigurationException
+    {
+        return fileBasedBuilder(configClass, url).getConfiguration();
+    }
+
+    /**
+     * Creates an instance of the specified file-based configuration class from
+     * the content of the file identified by the given path. This is a
+     * convenience method which can be used if no builder is needed for managing
+     * the configuration object. (Although, behind the scenes a builder is
+     * created).
+     *
+     * @param configClass the configuration class
+     * @param path the path to the file to be loaded
+     * @param <T> the type of the configuration to be constructed
+     * @return a {@code FileBasedConfiguration} object initialized from this
+     *         file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public <T extends FileBasedConfiguration> T fileBased(Class<T> configClass,
+            String path) throws ConfigurationException
+    {
+        return fileBasedBuilder(configClass, path).getConfiguration();
+    }
+
+    /**
+     * Creates a builder for a {@code PropertiesConfiguration} and initializes
+     * it with the given file to be loaded.
+     *
+     * @param file the file to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder(
+            File file)
+    {
+        return fileBasedBuilder(PropertiesConfiguration.class, file);
+    }
+
+    /**
+     * Creates a builder for a {@code PropertiesConfiguration} and initializes
+     * it with the given URL to be loaded.
+     *
+     * @param url the URL to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder(
+            URL url)
+    {
+        return fileBasedBuilder(PropertiesConfiguration.class, url);
+    }
+
+    /**
+     * Creates a builder for a {@code PropertiesConfiguration} and initializes
+     * it with the given path to the file to be loaded.
+     *
+     * @param path the path to the file to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder(
+            String path)
+    {
+        return fileBasedBuilder(PropertiesConfiguration.class, path);
+    }
+
+    /**
+     * Creates a {@code PropertiesConfiguration} instance from the content of
+     * the given file. This is a convenience method which can be used if no
+     * builder is needed for managing the configuration object. (Although,
+     * behind the scenes a builder is created).
+     *
+     * @param file the file to be loaded
+     * @return a {@code PropertiesConfiguration} object initialized from this
+     *         file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public PropertiesConfiguration properties(File file)
+            throws ConfigurationException
+    {
+        return propertiesBuilder(file).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code PropertiesConfiguration} instance from the content of
+     * the given URL. This is a convenience method which can be used if no
+     * builder is needed for managing the configuration object. (Although,
+     * behind the scenes a builder is created).
+     *
+     * @param url the URL to be loaded
+     * @return a {@code PropertiesConfiguration} object initialized from this
+     *         URL
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public PropertiesConfiguration properties(URL url)
+            throws ConfigurationException
+    {
+        return propertiesBuilder(url).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code PropertiesConfiguration} instance from the content of
+     * the file identified by the given path. This is a convenience method which
+     * can be used if no builder is needed for managing the configuration
+     * object. (Although, behind the scenes a builder is created).
+     *
+     * @param path the path to the file to be loaded
+     * @return a {@code PropertiesConfiguration} object initialized from this
+     *         path
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public PropertiesConfiguration properties(String path)
+            throws ConfigurationException
+    {
+        return propertiesBuilder(path).getConfiguration();
+    }
+
+    /**
+     * Creates a builder for a {@code XMLConfiguration} and initializes it with
+     * the given file to be loaded.
+     *
+     * @param file the file to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<XMLConfiguration> xmlBuilder(File file)
+    {
+        return fileBasedBuilder(XMLConfiguration.class, file);
+    }
+
+    /**
+     * Creates a builder for a {@code XMLConfiguration} and initializes it with
+     * the given URL to be loaded.
+     *
+     * @param url the URL to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<XMLConfiguration> xmlBuilder(URL url)
+    {
+        return fileBasedBuilder(XMLConfiguration.class, url);
+    }
+
+    /**
+     * Creates a builder for a {@code XMLConfiguration} and initializes it with
+     * the given path to the file to be loaded.
+     *
+     * @param path the path to the file to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<XMLConfiguration> xmlBuilder(
+            String path)
+    {
+        return fileBasedBuilder(XMLConfiguration.class, path);
+    }
+
+    /**
+     * Creates a {@code XMLConfiguration} instance from the content of the given
+     * file. This is a convenience method which can be used if no builder is
+     * needed for managing the configuration object. (Although, behind the
+     * scenes a builder is created).
+     *
+     * @param file the file to be loaded
+     * @return a {@code XMLConfiguration} object initialized from this file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public XMLConfiguration xml(File file) throws ConfigurationException
+    {
+        return xmlBuilder(file).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code XMLConfiguration} instance from the content of the given
+     * URL. This is a convenience method which can be used if no builder is
+     * needed for managing the configuration object. (Although, behind the
+     * scenes a builder is created).
+     *
+     * @param url the URL to be loaded
+     * @return a {@code XMLConfiguration} object initialized from this file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public XMLConfiguration xml(URL url) throws ConfigurationException
+    {
+        return xmlBuilder(url).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code XMLConfiguration} instance from the content of the file
+     * identified by the given path. This is a convenience method which can be
+     * used if no builder is needed for managing the configuration object.
+     * (Although, behind the scenes a builder is created).
+     *
+     * @param path the path to the file to be loaded
+     * @return a {@code XMLConfiguration} object initialized from this file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public XMLConfiguration xml(String path) throws ConfigurationException
+    {
+        return xmlBuilder(path).getConfiguration();
+    }
+
+    /**
+     * Creates a builder for a {@code INIConfiguration} and initializes it with
+     * the given file to be loaded.
+     *
+     * @param file the file to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(File file)
+    {
+        return fileBasedBuilder(INIConfiguration.class, file);
+    }
+
+    /**
+     * Creates a builder for a {@code INIConfiguration} and initializes it with
+     * the given URL to be loaded.
+     *
+     * @param url the URL to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(URL url)
+    {
+        return fileBasedBuilder(INIConfiguration.class, url);
+    }
+
+    /**
+     * Creates a builder for a {@code INIConfiguration} and initializes it with
+     * the file file identified by the given path.
+     *
+     * @param path the path to the file to be loaded
+     * @return the newly created {@code FileBasedConfigurationBuilder}
+     */
+    public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(
+            String path)
+    {
+        return fileBasedBuilder(INIConfiguration.class, path);
+    }
+
+    /**
+     * Creates a {@code INIConfiguration} instance from the content of the given
+     * file. This is a convenience method which can be used if no builder is
+     * needed for managing the configuration object. (Although, behind the
+     * scenes a builder is created).
+     *
+     * @param file the file to be loaded
+     * @return a {@code INIConfiguration} object initialized from this file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public INIConfiguration ini(File file) throws ConfigurationException
+    {
+        return iniBuilder(file).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code INIConfiguration} instance from the content of the given
+     * URL. This is a convenience method which can be used if no builder is
+     * needed for managing the configuration object. (Although, behind the
+     * scenes a builder is created).
+     *
+     * @param url the URL to be loaded
+     * @return a {@code INIConfiguration} object initialized from this file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public INIConfiguration ini(URL url) throws ConfigurationException
+    {
+        return iniBuilder(url).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code INIConfiguration} instance from the content of the file
+     * identified by the given path. This is a convenience method which can be
+     * used if no builder is needed for managing the configuration object.
+     * (Although, behind the scenes a builder is created).
+     *
+     * @param path the path to the file to be loaded
+     * @return a {@code INIConfiguration} object initialized from this file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public INIConfiguration ini(String path) throws ConfigurationException
+    {
+        return iniBuilder(path).getConfiguration();
+    }
+
+    /**
+     * Creates a builder for a {@code CombinedConfiguration} and initializes it
+     * with the given file to be loaded.
+     *
+     * @param file the file to be loaded
+     * @return the newly created {@code CombinedConfigurationBuilder}
+     */
+    public CombinedConfigurationBuilder combinedBuilder(File file)
+    {
+        return new CombinedConfigurationBuilder().configure(fileParams(file));
+    }
+
+    /**
+     * Creates a builder for a {@code CombinedConfiguration} and initializes it
+     * with the given URL to be loaded.
+     *
+     * @param url the URL to be loaded
+     * @return the newly created {@code CombinedConfigurationBuilder}
+     */
+    public CombinedConfigurationBuilder combinedBuilder(URL url)
+    {
+        return new CombinedConfigurationBuilder().configure(fileParams(url));
+    }
+
+    /**
+     * Creates a builder for a {@code CombinedConfiguration} and initializes it
+     * with the given path to the file to be loaded.
+     *
+     * @param path the path to the file to be loaded
+     * @return the newly created {@code CombinedConfigurationBuilder}
+     */
+    public CombinedConfigurationBuilder combinedBuilder(String path)
+    {
+        return new CombinedConfigurationBuilder().configure(fileParams(path));
+    }
+
+    /**
+     * Creates a {@code CombinedConfiguration} instance from the content of the
+     * given file. This is a convenience method which can be used if no builder
+     * is needed for managing the configuration object. (Although, behind the
+     * scenes a builder is created).
+     *
+     * @param file the file to be loaded
+     * @return a {@code CombinedConfiguration} object initialized from this file
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public CombinedConfiguration combined(File file)
+            throws ConfigurationException
+    {
+        return combinedBuilder(file).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code CombinedConfiguration} instance from the content of the
+     * given URL. This is a convenience method which can be used if no builder
+     * is needed for managing the configuration object. (Although, behind the
+     * scenes a builder is created).
+     *
+     * @param url the URL to be loaded
+     * @return a {@code CombinedConfiguration} object initialized from this URL
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public CombinedConfiguration combined(URL url)
+            throws ConfigurationException
+    {
+        return combinedBuilder(url).getConfiguration();
+    }
+
+    /**
+     * Creates a {@code CombinedConfiguration} instance from the content of the
+     * file identified by the given path. This is a convenience method which can
+     * be used if no builder is needed for managing the configuration object.
+     * (Although, behind the scenes a builder is created).
+     *
+     * @param path the path to the file to be loaded
+     * @return a {@code CombinedConfiguration} object initialized from this URL
+     * @throws ConfigurationException if an error occurred when loading the
+     *         configuration
+     */
+    public CombinedConfiguration combined(String path)
+            throws ConfigurationException
+    {
+        return combinedBuilder(path).getConfiguration();
+    }
+
+    /**
+     * Creates a configured builder for a file-based configuration of the
+     * specified type.
+     *
+     * @param configClass the configuration class
+     * @param params the parameters object for configuring the builder
+     * @param <T> the type of the configuration to be constructed
+     * @return the newly created builder
+     */
+    private <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T>
createFileBasedBuilder(
+            Class<T> configClass, FileBasedBuilderParameters params)
+    {
+        return new FileBasedConfigurationBuilder<T>(configClass)
+                .configure(params);
+    }
+
+    /**
+     * Convenience method for creating a parameters object for a file-based
+     * configuration.
+     *
+     * @return the newly created parameters object
+     */
+    private FileBasedBuilderParameters fileParams()
+    {
+        return getParameters().fileBased();
+    }
+
+    /**
+     * Convenience method for creating a file-based parameters object
+     * initialized with the given file.
+     *
+     * @param file the file to be loaded
+     * @return the initialized parameters object
+     */
+    private FileBasedBuilderParameters fileParams(File file)
+    {
+        return fileParams().setFile(file);
+    }
+
+    /**
+     * Convenience method for creating a file-based parameters object
+     * initialized with the given file.
+     *
+     * @param url the URL to be loaded
+     * @return the initialized parameters object
+     */
+    private FileBasedBuilderParameters fileParams(URL url)
+    {
+        return fileParams().setURL(url);
+    }
+
+    /**
+     * Convenience method for creating a file-based parameters object
+     * initialized with the given file path.
+     *
+     * @param path the path to the file to be loaded
+     * @return the initialized parameters object
+     */
+    private FileBasedBuilderParameters fileParams(String path)
+    {
+        return fileParams().setFileName(path);
+    }
+}

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestConfigurations.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestConfigurations.java?rev=1617826&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestConfigurations.java
(added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestConfigurations.java
Wed Aug 13 20:15:04 2014
@@ -0,0 +1,531 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder.fluent;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+
+import java.io.File;
+import java.net.URL;
+
+import org.apache.commons.configuration.CombinedConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationAssert;
+import org.apache.commons.configuration.INIConfiguration;
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.commons.configuration.builder.FileBasedConfigurationBuilder;
+import org.apache.commons.configuration.builder.combined.CombinedConfigurationBuilder;
+import org.apache.commons.configuration.ex.ConfigurationException;
+import org.apache.commons.configuration.plist.PropertyListConfiguration;
+import org.junit.Test;
+
+/**
+ * Test class for {@code Configurations}.
+ *
+ * @version $Id$
+ */
+public class TestConfigurations
+{
+    /** Constant for the name of the test properties file. */
+    private static final String TEST_PROPERTIES = "test.properties";
+
+    /** Constant for the name of the test XML file. */
+    private static final String TEST_XML = "test.xml";
+
+    /** Constant for the name of the test ini file. */
+    private static final String TEST_INI = "test.ini";
+
+    /** Constant for the name of the configuration definition file. */
+    private static final String TEST_COMBINED = "testDigesterConfiguration.xml";
+
+    /** Constant for the name of the test PList file. */
+    private static final String TEST_PLIST = "test.plist";
+
+    /**
+     * Generates a full path for the test file with the given name.
+     *
+     * @param name the name of the test file
+     * @return the full path to this file
+     */
+    private static String filePath(String name)
+    {
+        return ConfigurationAssert.getTestFile(name).getAbsolutePath();
+    }
+
+    /**
+     * Tests whether a default {@code Parameters} instance is created if
+     * necessary.
+     */
+    @Test
+    public void testDefaultParameters()
+    {
+        Configurations configs = new Configurations();
+        assertNotNull("No parameters", configs.getParameters());
+    }
+
+    /**
+     * Tests whether parameters can be passed in at construction time.
+     */
+    @Test
+    public void testInitWithParameters()
+    {
+        Parameters params = new Parameters();
+        Configurations configs = new Configurations(params);
+        assertSame("Wrong parameters", params, configs.getParameters());
+    }
+
+    /**
+     * Tests whether a builder for a file-based configuration can be created if
+     * an input File is specified.
+     */
+    @Test
+    public void testFileBasedBuilderWithFile()
+    {
+        Configurations configs = new Configurations();
+        File file = ConfigurationAssert.getTestFile(TEST_PROPERTIES);
+        FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+                configs.fileBasedBuilder(PropertiesConfiguration.class, file);
+        assertEquals("Wrong file", file.toURI(), builder.getFileHandler()
+                .getFile().toURI());
+    }
+
+    /**
+     * Tests whether a builder for a file-based configuration can be created if
+     * a URL is specified.
+     */
+    @Test
+    public void testFileBasedBuilderWithURL()
+    {
+        Configurations configs = new Configurations();
+        URL url = ConfigurationAssert.getTestURL("test.properties");
+        FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+                configs.fileBasedBuilder(PropertiesConfiguration.class, url);
+        assertEquals("Wrong URL", url, builder.getFileHandler().getURL());
+    }
+
+    /**
+     * Tests whether a builder for a file-based configuration can be created if
+     * a file name is specified.
+     */
+    @Test
+    public void testFileBasedBuilderWithPath()
+    {
+        Configurations configs = new Configurations();
+        String filePath = filePath(TEST_PROPERTIES);
+        FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+                configs.fileBasedBuilder(PropertiesConfiguration.class,
+                        filePath);
+        assertEquals("Wrong path", filePath, builder.getFileHandler()
+                .getFileName());
+    }
+
+    /**
+     * Checks whether a property list configuration was correctly loaded.
+     *
+     * @param config the configuration instance to be checked.
+     */
+    private static void checkPList(Configuration config)
+    {
+        assertEquals("string1", config.getProperty("simple-string"));
+    }
+
+    /**
+     * Tests whether a file-based configuration can be loaded from a file.
+     */
+    @Test
+    public void testFileBasedFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        PropertyListConfiguration config =
+                configs.fileBased(PropertyListConfiguration.class,
+                        ConfigurationAssert.getTestFile(TEST_PLIST));
+        checkPList(config);
+    }
+
+    /**
+     * Tests whether a file-based configuration can be loaded from a URL.
+     */
+    @Test
+    public void testFileBasedURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        PropertyListConfiguration config =
+                configs.fileBased(PropertyListConfiguration.class,
+                        ConfigurationAssert.getTestURL(TEST_PLIST));
+        checkPList(config);
+    }
+
+    /**
+     * Tests whether a file-based configuration can be loaded from a file path.
+     */
+    @Test
+    public void testFileBasedPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        PropertyListConfiguration config =
+                configs.fileBased(PropertyListConfiguration.class,
+                        filePath(TEST_PLIST));
+        checkPList(config);
+    }
+
+    /**
+     * Checks whether a test properties configuration was correctly loaded.
+     *
+     * @param config the configuration instance to be checked.
+     */
+    private static void checkProperties(Configuration config)
+    {
+        assertEquals("true", config.getString("configuration.loaded"));
+    }
+
+    /**
+     * Tests whether a builder for a properties configuration can be created for
+     * a given file.
+     */
+    @Test
+    public void testPropertiesBuilderFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+                configs.propertiesBuilder(ConfigurationAssert
+                        .getTestFile(TEST_PROPERTIES));
+        checkProperties(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a properties configuration can be loaded from a file.
+     */
+    @Test
+    public void testPropertiesFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        PropertiesConfiguration config =
+                configs.properties(ConfigurationAssert
+                        .getTestFile(TEST_PROPERTIES));
+        checkProperties(config);
+    }
+
+    /**
+     * Tests whether a builder for a properties configuration can be created for
+     * a given URL.
+     */
+    @Test
+    public void testPropertiesBuilderFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+                configs.propertiesBuilder(ConfigurationAssert
+                        .getTestURL(TEST_PROPERTIES));
+        checkProperties(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a properties configuration can be loaded from a URL.
+     */
+    @Test
+    public void testPropertiesFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        PropertiesConfiguration config =
+                configs.properties(ConfigurationAssert
+                        .getTestURL(TEST_PROPERTIES));
+        checkProperties(config);
+    }
+
+    /**
+     * Tests whether a builder for a properties configuration can be created for
+     * a given file path.
+     */
+    @Test
+    public void testPropertiesBuilderFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
+                configs.propertiesBuilder(filePath(TEST_PROPERTIES));
+        checkProperties(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a properties configuration can be loaded from a file path.
+     */
+    @Test
+    public void testPropertiesFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        PropertiesConfiguration config =
+                configs.properties(filePath(TEST_PROPERTIES));
+        checkProperties(config);
+    }
+
+    /**
+     * Checks whether a test XML configuration was correctly loaded.
+     *
+     * @param config the configuration instance to be checked.
+     */
+    private static void checkXML(Configuration config)
+    {
+        assertEquals("value", config.getProperty("element"));
+    }
+
+    /**
+     * Tests whether a builder for a XML configuration can be created for a
+     * given file.
+     */
+    @Test
+    public void testXMLBuilderFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<XMLConfiguration> builder =
+                configs.xmlBuilder(ConfigurationAssert.getTestFile(TEST_XML));
+        checkXML(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a XML configuration can be loaded from a file.
+     */
+    @Test
+    public void testXMLFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        XMLConfiguration config =
+                configs.xml(ConfigurationAssert.getTestFile(TEST_XML));
+        checkXML(config);
+    }
+
+    /**
+     * Tests whether a builder for a XML configuration can be created for a
+     * given URL.
+     */
+    @Test
+    public void testXMLBuilderFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<XMLConfiguration> builder =
+                configs.xmlBuilder(ConfigurationAssert.getTestURL(TEST_XML));
+        checkXML(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a XML configuration can be loaded from a URL.
+     */
+    @Test
+    public void testXMLFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        XMLConfiguration config =
+                configs.xml(ConfigurationAssert.getTestURL(TEST_XML));
+        checkXML(config);
+    }
+
+    /**
+     * Tests whether a builder for a XML configuration can be created for a
+     * given file path.
+     */
+    @Test
+    public void testXMLBuilderFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<XMLConfiguration> builder =
+                configs.xmlBuilder(filePath(TEST_XML));
+        checkXML(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a XML configuration can be loaded from a URL.
+     */
+    @Test
+    public void testXMLFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        XMLConfiguration config = configs.xml(filePath(TEST_XML));
+        checkXML(config);
+    }
+
+    /**
+     * Checks whether a test INI configuration was correctly loaded.
+     *
+     * @param config the configuration instance to be checked.
+     */
+    private static void checkINI(INIConfiguration config)
+    {
+        assertEquals("yes", config.getProperty("testini.loaded"));
+    }
+
+    /**
+     * Tests whether a builder for a INI configuration can be created for a
+     * given file.
+     */
+    @Test
+    public void testINIBuilderFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<INIConfiguration> builder =
+                configs.iniBuilder(ConfigurationAssert.getTestFile(TEST_INI));
+        checkINI(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a INI configuration can be loaded from a file.
+     */
+    @Test
+    public void testINIFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        INIConfiguration config =
+                configs.ini(ConfigurationAssert.getTestFile(TEST_INI));
+        checkINI(config);
+    }
+
+    /**
+     * Tests whether a builder for a INI configuration can be created for a
+     * given URL.
+     */
+    @Test
+    public void testINIBuilderFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<INIConfiguration> builder =
+                configs.iniBuilder(ConfigurationAssert.getTestURL(TEST_INI));
+        checkINI(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a INI configuration can be loaded from a URL.
+     */
+    @Test
+    public void testINIFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        INIConfiguration config =
+                configs.ini(ConfigurationAssert.getTestURL(TEST_INI));
+        checkINI(config);
+    }
+
+    /**
+     * Tests whether a builder for a INI configuration can be created for a
+     * given file path.
+     */
+    @Test
+    public void testINIBuilderFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        FileBasedConfigurationBuilder<INIConfiguration> builder =
+                configs.iniBuilder(filePath(TEST_INI));
+        checkINI(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a INI configuration can be loaded from a file path.
+     */
+    @Test
+    public void testINIFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        INIConfiguration config = configs.ini(filePath(TEST_INI));
+        checkINI(config);
+    }
+
+    /**
+     * Checks whether a combined configuration was successfully loaded.
+     *
+     * @param config the configuration instance to be checked.
+     */
+    private static void checkCombined(Configuration config)
+    {
+        checkProperties(config);
+        checkXML(config);
+    }
+
+    /**
+     * Tests whether a combined configuration builder can be constructed for a
+     * file.
+     */
+    @Test
+    public void testCombinedBuilderFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        CombinedConfigurationBuilder builder =
+                configs.combinedBuilder(ConfigurationAssert
+                        .getTestFile(TEST_COMBINED));
+        checkCombined(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a combined configuration can be loaded from a file.
+     */
+    @Test
+    public void testCombinedFromFile() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        CombinedConfiguration config =
+                configs.combined(ConfigurationAssert.getTestFile(TEST_COMBINED));
+        checkCombined(config);
+    }
+
+    /**
+     * Tests whether a combined configuration builder can be constructed for a
+     * URL.
+     */
+    @Test
+    public void testCombinedBuilderFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        CombinedConfigurationBuilder builder =
+                configs.combinedBuilder(ConfigurationAssert
+                        .getTestURL(TEST_COMBINED));
+        checkCombined(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a combined configuration can be loaded from a URL.
+     */
+    @Test
+    public void testCombinedFromURL() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        CombinedConfiguration config =
+                configs.combined(ConfigurationAssert.getTestURL(TEST_COMBINED));
+        checkCombined(config);
+    }
+
+    /**
+     * Tests whether a combined configuration builder can be constructed for a
+     * file path.
+     */
+    @Test
+    public void testCombinedBuilderFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        CombinedConfigurationBuilder builder =
+                configs.combinedBuilder(filePath(TEST_COMBINED));
+        checkCombined(builder.getConfiguration());
+    }
+
+    /**
+     * Tests whether a combined configuration can be loaded from a file path.
+     */
+    @Test
+    public void testCombinedFromPath() throws ConfigurationException
+    {
+        Configurations configs = new Configurations();
+        CombinedConfiguration config =
+                configs.combined(filePath(TEST_COMBINED));
+        checkCombined(config);
+    }
+}



Mime
View raw message