Author: oheger
Date: Mon Sep 16 20:24:36 2013
New Revision: 1523796
URL: http://svn.apache.org/r1523796
Log:
Added support for a default file system.
FileLocatorUtils now defines a constant pointing to the default file system
instance. There is a method which checks whether a file system is set. If not,
the default file system is returned. This mechanism is going to replace the
static default file system field used by the FileSystem class.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java?rev=1523796&r1=1523795&r2=1523796&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
Mon Sep 16 20:24:36 2013
@@ -21,6 +21,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.configuration.ConfigurationUtils;
+import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
@@ -45,6 +46,14 @@ import org.apache.commons.logging.LogFac
*/
public final class FileLocatorUtils
{
+ /**
+ * Constant for the default {@code FileSystem}. This file system is used by
+ * operations of this class if no specific file system is provided. An
+ * instance of {@link DefaultFileSystem} is used.
+ */
+ public static final FileSystem DEFAULT_FILE_SYSTEM =
+ new DefaultFileSystem();
+
/** Constant for the file URL protocol */
private static final String FILE_SCHEME = "file:";
@@ -109,6 +118,22 @@ public final class FileLocatorUtils
}
/**
+ * Obtains a non-<b>null</b> {@code FileSystem} object from the passed in
+ * {@code FileLocator}. If the passed in {@code FileLocator} has a
+ * {@code FileSystem} object, it is returned. Otherwise, result is the
+ * default {@code FileSystem}.
+ *
+ * @param locator the {@code FileLocator} (may be <b>null</b>)
+ * @return the {@code FileSystem} to be used for this {@code FileLocator}
+ */
+ public static FileSystem obtainFileSystem(FileLocator locator)
+ {
+ return (locator != null) ? ObjectUtils.defaultIfNull(
+ locator.getFileSystem(), DEFAULT_FILE_SYSTEM)
+ : DEFAULT_FILE_SYSTEM;
+ }
+
+ /**
* Return the location of the specified resource by searching the user home
* directory, the current classpath and the system classpath.
*
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java?rev=1523796&r1=1523795&r2=1523796&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
Mon Sep 16 20:24:36 2013
@@ -320,4 +320,40 @@ public class TestFileLocatorUtils
assertThat(s, containsString("encoding=" + ENCODING));
assertThat(s, containsString("fileSystem=" + fileSystem));
}
+
+ /**
+ * Tests whether obtainFileSystem() can handle a null locator.
+ */
+ @Test
+ public void testObtainFileSystemNullLocator()
+ {
+ assertSame("Wrong file system", FileLocatorUtils.DEFAULT_FILE_SYSTEM,
+ FileLocatorUtils.obtainFileSystem(null));
+ }
+
+ /**
+ * Tests whether the default file system is returned if it is not set in a
+ * locator.
+ */
+ @Test
+ public void testObtainFileSystemNotSetInLocator()
+ {
+ assertSame("Wrong file system", FileLocatorUtils.DEFAULT_FILE_SYSTEM,
+ FileLocatorUtils.obtainFileSystem(FileLocatorUtils
+ .fileLocator().create()));
+ }
+
+ /**
+ * Tests whether obtainFileSystem() returns the file system stored in the
+ * locator.
+ */
+ @Test
+ public void testObtainFileSystemSetInLocator()
+ {
+ FileSystem fs = EasyMock.createMock(FileSystem.class);
+ FileLocator locator =
+ FileLocatorUtils.fileLocator().fileSystem(fs).create();
+ assertSame("Wrong file system", fs,
+ FileLocatorUtils.obtainFileSystem(locator));
+ }
}