Author: oheger
Date: Sat Sep 28 19:29:32 2013
New Revision: 1527225
URL: http://svn.apache.org/r1527225
Log:
Added a property for the FileLocationStrategy to FileLocator.
The locator now supports defining the strategy how to resolve the referenced
file.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java?rev=1527225&r1=1527224&r2=1527225&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocator.java
Sat Sep 28 19:29:32 2013
@@ -68,6 +68,9 @@ public final class FileLocator
/** The file system. */
private final FileSystem fileSystem;
+ /** The file location strategy. */
+ private final FileLocationStrategy locationStrategy;
+
/**
* Creates a new instance of {@code FileLocatorImpl} and initializes it
from
* the given builder instance
@@ -81,6 +84,7 @@ public final class FileLocator
sourceURL = builder.sourceURL;
encoding = builder.encoding;
fileSystem = builder.fileSystem;
+ locationStrategy = builder.locationStrategy;
}
/**
@@ -139,6 +143,19 @@ public final class FileLocator
}
/**
+ * Returns the {@code FileLocationStrategy} to be used for locating the
+ * referenced file. If no specific {@code FileLocationStrategy} has been
+ * set, result is <b>null</b>. This means that the default strategy should
+ * be used.
+ *
+ * @return the {@code FileLocationStrategy} to be used
+ */
+ public FileLocationStrategy getLocationStrategy()
+ {
+ return locationStrategy;
+ }
+
+ /**
* Returns a hash code for this object.
*
* @return a hash code for this object
@@ -148,7 +165,8 @@ public final class FileLocator
{
return new HashCodeBuilder().append(getFileName())
.append(getBasePath()).append(sourceURLAsString())
- .append(getEncoding()).append(getFileSystem()).toHashCode();
+ .append(getEncoding()).append(getFileSystem())
+ .append(getLocationStrategy()).toHashCode();
}
/**
@@ -176,7 +194,9 @@ public final class FileLocator
.append(getBasePath(), c.getBasePath())
.append(sourceURLAsString(), c.sourceURLAsString())
.append(getEncoding(), c.getEncoding())
- .append(getFileSystem(), c.getFileSystem()).isEquals();
+ .append(getFileSystem(), c.getFileSystem())
+ .append(getLocationStrategy(), c.getLocationStrategy())
+ .isEquals();
}
/**
@@ -192,7 +212,8 @@ public final class FileLocator
.append("basePath", getBasePath())
.append("sourceURL", sourceURLAsString())
.append("encoding", getEncoding())
- .append("fileSystem", getFileSystem()).toString();
+ .append("fileSystem", getFileSystem())
+ .append("locationStrategy", getLocationStrategy()).toString();
}
/**
@@ -232,6 +253,9 @@ public final class FileLocator
/** The file system. */
private FileSystem fileSystem;
+ /** The location strategy. */
+ private FileLocationStrategy locationStrategy;
+
/**
* Creates a new instance of {@code FileLocatorBuilder} and initializes
* the builder's properties from the passed in {@code FileLocator}
@@ -308,6 +332,19 @@ public final class FileLocator
}
/**
+ * Specifies the {@code FileLocationStrategy} to be used when the
+ * referenced file is to be located.
+ *
+ * @param strategy the {@code FileLocationStrategy}
+ * @return a reference to this builder for method chaining
+ */
+ public FileLocatorBuilder locationStrategy(FileLocationStrategy
strategy)
+ {
+ locationStrategy = strategy;
+ return this;
+ }
+
+ /**
* Creates a new immutable {@code FileLocatorImpl} object based on the
* properties set so far for this builder.
*
@@ -331,6 +368,7 @@ public final class FileLocator
sourceURL = src.getSourceURL();
encoding = src.getEncoding();
fileSystem = src.getFileSystem();
+ locationStrategy = src.getLocationStrategy();
}
}
}
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java?rev=1527225&r1=1527224&r2=1527225&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocator.java
Sat Sep 28 19:29:32 2013
@@ -52,12 +52,16 @@ public class TestFileLocator
/** A test file system. */
private static FileSystem fileSystem;
+ /** A test location strategy. */
+ private static FileLocationStrategy locationStrategy;
+
@BeforeClass
public static void setUpOnce() throws Exception
{
sourceURL = ConfigurationAssert.getTestURL(FILE_NAME);
fileSystem = EasyMock.createMock(FileSystem.class);
- EasyMock.replay(fileSystem);
+ locationStrategy = EasyMock.createMock(FileLocationStrategy.class);
+ EasyMock.replay(fileSystem, locationStrategy);
}
/**
@@ -72,6 +76,7 @@ public class TestFileLocator
assertNull("Got a URL", locator.getSourceURL());
assertNull("Got an encoding", locator.getEncoding());
assertNull("Got a file system", locator.getFileSystem());
+ assertNull("Got a location strategy", locator.getLocationStrategy());
}
/**
@@ -87,6 +92,8 @@ public class TestFileLocator
assertEquals("Wrong URL", sourceURL.toExternalForm(), locator
.getSourceURL().toExternalForm());
assertSame("Wrong file system", fileSystem, locator.getFileSystem());
+ assertSame("Wrong location strategy", locationStrategy,
+ locator.getLocationStrategy());
}
/**
@@ -98,7 +105,8 @@ public class TestFileLocator
FileLocator locator =
FileLocatorUtils.fileLocator().basePath(BASE_PATH)
.fileName(FILE_NAME).encoding(ENCODING)
- .fileSystem(fileSystem).sourceURL(sourceURL).create();
+ .fileSystem(fileSystem).sourceURL(sourceURL)
+ .locationStrategy(locationStrategy).create();
checkLocator(locator);
}
@@ -111,7 +119,8 @@ public class TestFileLocator
FileLocator locatorSrc =
FileLocatorUtils.fileLocator().basePath(BASE_PATH)
.fileName("someFile").encoding(ENCODING)
- .fileSystem(fileSystem).sourceURL(sourceURL).create();
+ .fileSystem(fileSystem).sourceURL(sourceURL)
+ .locationStrategy(locationStrategy).create();
FileLocator locator =
FileLocatorUtils.fileLocator(locatorSrc).fileName(FILE_NAME)
.create();
@@ -132,11 +141,13 @@ public class TestFileLocator
loc1 =
FileLocatorUtils.fileLocator().basePath(BASE_PATH)
.fileName(FILE_NAME).encoding(ENCODING)
- .fileSystem(fileSystem).sourceURL(sourceURL).create();
+ .fileSystem(fileSystem).sourceURL(sourceURL)
+ .locationStrategy(locationStrategy).create();
loc2 =
FileLocatorUtils.fileLocator().basePath(BASE_PATH)
.fileName(FILE_NAME).encoding(ENCODING)
- .fileSystem(fileSystem).sourceURL(sourceURL).create();
+ .fileSystem(fileSystem).sourceURL(sourceURL)
+ .locationStrategy(locationStrategy).create();
ConfigurationAssert.checkEquals(loc1, loc2, true);
}
@@ -150,7 +161,8 @@ public class TestFileLocator
FileLocator loc1 =
FileLocatorUtils.fileLocator().basePath(BASE_PATH)
.fileName(FILE_NAME).encoding(ENCODING)
- .fileSystem(fileSystem).sourceURL(sourceURL).create();
+ .fileSystem(fileSystem).sourceURL(sourceURL)
+ .locationStrategy(locationStrategy).create();
FileLocator loc2 =
FileLocatorUtils.fileLocator(loc1)
.basePath(BASE_PATH + "_other").create();
@@ -176,6 +188,13 @@ public class TestFileLocator
.getTestURL("test.properties"))
.create();
ConfigurationAssert.checkEquals(loc1, loc2, false);
+ loc2 =
+ FileLocatorUtils
+ .fileLocator(loc1)
+ .locationStrategy(
+
EasyMock.createMock(FileLocationStrategy.class))
+ .create();
+ ConfigurationAssert.checkEquals(loc1, loc2, false);
}
/**
@@ -209,12 +228,14 @@ public class TestFileLocator
FileLocator loc =
FileLocatorUtils.fileLocator().basePath(BASE_PATH)
.fileName(FILE_NAME).encoding(ENCODING)
- .fileSystem(fileSystem).sourceURL(sourceURL).create();
+ .fileSystem(fileSystem).sourceURL(sourceURL)
+ .locationStrategy(locationStrategy).create();
String s = loc.toString();
assertThat(s, containsString("fileName=" + FILE_NAME));
assertThat(s, containsString("basePath=" + BASE_PATH));
assertThat(s, containsString("sourceURL=" + sourceURL));
assertThat(s, containsString("encoding=" + ENCODING));
assertThat(s, containsString("fileSystem=" + fileSystem));
+ assertThat(s, containsString("locationStrategy=" + locationStrategy));
}
}