You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2012/10/07 22:03:37 UTC
svn commit: r1395373 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java
test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
Author: oheger
Date: Sun Oct 7 20:03:37 2012
New Revision: 1395373
URL: http://svn.apache.org/viewvc?rev=1395373&view=rev
Log:
Added parameters class for file-based builders.
Added:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java (with props)
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java (with props)
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java?rev=1395373&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java Sun Oct 7 20:03:37 2012
@@ -0,0 +1,204 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.commons.configuration.io.FileHandler;
+
+/**
+ * <p>
+ * An implementation of {@code BuilderParameters} which contains parameters
+ * related to {@code Configuration} implementations that are loaded from files.
+ * </p>
+ * <p>
+ * The parameters defined here are interpreted by builder implementations that
+ * can deal with file-based configurations. Note that these parameters are
+ * typically no initialization properties of configuration objects (i.e. they
+ * are not passed to set methods after the creation of the result
+ * configuration). Rather, the parameters object is stored as a whole in the
+ * builder's map with initialization parameters and can be accessed from there.
+ * </p>
+ * <p>
+ * This class is not thread-safe. It is intended that an instance is constructed
+ * and initialized by a single thread during configuration of a
+ * {@code ConfigurationBuilder}.
+ * </p>
+ *
+ * @version $Id$
+ * @since 2.0
+ */
+public class FileBasedBuilderParameters implements BuilderParameters
+{
+ /** Constant for the key in the parameters map used by this class. */
+ private static final String PARAM_KEY =
+ BasicConfigurationBuilder.RESERVED_PARAMETER
+ + FileBasedBuilderParameters.class.getName();
+
+ /**
+ * Stores the associated file handler for the location of the configuration.
+ */
+ private final FileHandler fileHandler;
+
+ /** The refresh delay for reloading support. */
+ private long reloadingRefreshDelay;
+
+ /**
+ * Creates a new instance of {@code FileBasedBuilderParameters} with an
+ * uninitialized {@code FileHandler} object.
+ */
+ public FileBasedBuilderParameters()
+ {
+ this(null);
+ }
+
+ /**
+ * Creates a new instance of {@code FileBasedBuilderParameters} and
+ * associates it with the given {@code FileHandler} object. If the handler
+ * is <b>null</b>, a new handler instance is created.
+ *
+ * @param handler the associated {@code FileHandler} (can be <b>null</b>)
+ */
+ public FileBasedBuilderParameters(FileHandler handler)
+ {
+ fileHandler = (handler != null) ? handler : new FileHandler();
+ }
+
+ /**
+ * Looks up an instance of this class in the specified parameters map. This
+ * is equivalent to {@code fromParameters(params, false};}
+ *
+ * @param params the map with parameters (must not be <b>null</b>
+ * @return the instance obtained from the map or <b>null</b>
+ * @throws NullPointerException if the map is <b>null</b>
+ */
+ public static FileBasedBuilderParameters fromParameters(
+ Map<String, Object> params)
+ {
+ return fromParameters(params, false);
+ }
+
+ /**
+ * Looks up an instance of this class in the specified parameters map and
+ * optionally creates a new one if none is found. This method can be used to
+ * obtain an instance of this class which has been stored in a parameters
+ * map. It is compatible with the {@code getParameters()} method.
+ *
+ * @param params the map with parameters (must not be <b>null</b>
+ * @param createIfMissing determines the behavior if no instance is found in
+ * the map; if <b>true</b>, a new instance with default settings is
+ * created; if <b>false</b>, <b>null</b> is returned
+ * @return the instance obtained from the map or <b>null</b>
+ * @throws NullPointerException if the map is <b>null</b>
+ */
+ public static FileBasedBuilderParameters fromParameters(
+ Map<String, Object> params, boolean createIfMissing)
+ {
+ FileBasedBuilderParameters instance =
+ (FileBasedBuilderParameters) params.get(PARAM_KEY);
+ if (instance == null && createIfMissing)
+ {
+ instance = new FileBasedBuilderParameters();
+ }
+ return instance;
+ }
+
+ /**
+ * Returns the {@code FileHandler} associated with this parameters object.
+ *
+ * @return the {@code FileHandler}
+ */
+ public FileHandler getFileHandler()
+ {
+ return fileHandler;
+ }
+
+ /**
+ * Returns the refresh delay for reloading support.
+ *
+ * @return the refresh delay (in milliseconds)
+ */
+ public long getReloadingRefreshDelay()
+ {
+ return reloadingRefreshDelay;
+ }
+
+ /**
+ * Sets the refresh delay for reloading support
+ *
+ * @param reloadingRefreshDelay the refresh delay (in milliseconds)
+ * @return a reference to this object for method chaining
+ */
+ public FileBasedBuilderParameters setReloadingRefreshDelay(
+ long reloadingRefreshDelay)
+ {
+ this.reloadingRefreshDelay = reloadingRefreshDelay;
+ return this;
+ }
+
+ /**
+ * Sets the location of the associated {@code FileHandler} as a {@code File}
+ * object.
+ *
+ * @param file the {@code File} location
+ * @return a reference to this object for method chaining
+ */
+ public FileBasedBuilderParameters setFile(File file)
+ {
+ getFileHandler().setFile(file);
+ return this;
+ }
+
+ /**
+ * Sets the location of the associated {@code FileHandler} as a {@code URL}
+ * object.
+ *
+ * @param url the {@code URL} location
+ * @return a reference to this object for method chaining
+ */
+ public FileBasedBuilderParameters setURL(URL url)
+ {
+ getFileHandler().setURL(url);
+ return this;
+ }
+
+ /**
+ * Sets the location of the associated {@code FileHandler} as an absolute
+ * file path.
+ *
+ * @param path the path location
+ * @return a reference to this object for method chaining
+ */
+ public FileBasedBuilderParameters setPath(String path)
+ {
+ getFileHandler().setPath(path);
+ return this;
+ }
+
+ /**
+ * {@inheritDoc} This implementation returns a map which contains this
+ * object itself under a specific key. The static {@code fromParameters()}
+ * method can be used to extract an instance from a parameters map.
+ */
+ public Map<String, Object> getParameters()
+ {
+ return Collections.singletonMap(PARAM_KEY, (Object) this);
+ }
+}
Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/FileBasedBuilderParameters.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java?rev=1395373&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java Sun Oct 7 20:03:37 2012
@@ -0,0 +1,169 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.configuration.ConfigurationAssert;
+import org.apache.commons.configuration.io.FileHandler;
+import org.junit.Test;
+
+/**
+ * Test class for {@code FileBasedBuilderParameters}.
+ *
+ * @version $Id$
+ */
+public class TestFileBasedBuilderParameters
+{
+ /**
+ * Tests the standard constructor.
+ */
+ @Test
+ public void testInitDefaults()
+ {
+ FileBasedBuilderParameters params = new FileBasedBuilderParameters();
+ assertFalse("Got a location", params.getFileHandler()
+ .isLocationDefined());
+ assertEquals("Got a refresh delay", 0,
+ params.getReloadingRefreshDelay());
+ }
+
+ /**
+ * Tests whether a file handler is accepted by the constructor.
+ */
+ @Test
+ public void testInitFileHandler()
+ {
+ FileHandler handler = new FileHandler();
+ FileBasedBuilderParameters params =
+ new FileBasedBuilderParameters(handler);
+ assertSame("Wrong handler", handler, params.getFileHandler());
+ }
+
+ /**
+ * Tests whether the refresh delay can be set.
+ */
+ @Test
+ public void testSetReloadingRefreshDelay()
+ {
+ FileBasedBuilderParameters params = new FileBasedBuilderParameters();
+ final int delay = 10000;
+ assertSame("Wrong result", params,
+ params.setReloadingRefreshDelay(delay));
+ assertEquals("Wrong delay", delay, params.getReloadingRefreshDelay());
+ }
+
+ /**
+ * Tests whether a file can be set.
+ */
+ @Test
+ public void testSetFile()
+ {
+ File file =
+ ConfigurationAssert.getTestFile("test.properties")
+ .getAbsoluteFile();
+ FileBasedBuilderParameters params = new FileBasedBuilderParameters();
+ assertSame("Wrong result", params, params.setFile(file));
+ assertEquals("Wrong file", file, params.getFileHandler().getFile());
+ }
+
+ /**
+ * Tests whether a URL can be set.
+ */
+ @Test
+ public void testSetURL()
+ {
+ URL url = ConfigurationAssert.getTestURL("test.properties");
+ FileBasedBuilderParameters params = new FileBasedBuilderParameters();
+ assertSame("Wrong result", params, params.setURL(url));
+ assertEquals("Wrong URL", url.toExternalForm(), params.getFileHandler()
+ .getURL().toExternalForm());
+ }
+
+ /**
+ * Tests whether a path can be set.
+ */
+ @Test
+ public void testSetPath()
+ {
+ String path =
+ ConfigurationAssert.getTestFile("test.properties")
+ .getAbsolutePath();
+ FileBasedBuilderParameters params = new FileBasedBuilderParameters();
+ assertSame("Wrong result", params, params.setPath(path));
+ assertEquals("Wrong path", path, params.getFileHandler().getPath());
+ }
+
+ /**
+ * Tests whether a map with parameters can be queried.
+ */
+ @Test
+ public void testGetParameters()
+ {
+ FileBasedBuilderParameters params =
+ new FileBasedBuilderParameters().setReloadingRefreshDelay(1000);
+ Map<String, Object> map = params.getParameters();
+ assertEquals("Wrong number of items", 1, map.size());
+ assertTrue("Object not stored", map.values().contains(params));
+ }
+
+ /**
+ * Tests fromParameters() if the map does not contain an instance.
+ */
+ @Test
+ public void testFromParametersNotFound()
+ {
+ assertNull("Got an instance",
+ FileBasedBuilderParameters
+ .fromParameters(new HashMap<String, Object>()));
+ }
+
+ /**
+ * Tests whether fromParameters() can return a default instance if the map
+ * does not contain an instance.
+ */
+ @Test
+ public void testFromParametersDefaultInstance()
+ {
+ FileBasedBuilderParameters params =
+ FileBasedBuilderParameters.fromParameters(
+ new HashMap<String, Object>(), true);
+ assertFalse("Got a location", params.getFileHandler()
+ .isLocationDefined());
+ }
+
+ /**
+ * Tests whether an instance can be extracted from a parameters map.
+ */
+ @Test
+ public void testFromParametersExtract()
+ {
+ FileBasedBuilderParameters params = new FileBasedBuilderParameters();
+ Map<String, Object> map = params.getParameters();
+ assertSame("Wrong parameters", params,
+ FileBasedBuilderParameters.fromParameters(map));
+ }
+}
Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestFileBasedBuilderParameters.java
------------------------------------------------------------------------------
svn:mime-type = text/plain