Author: oheger
Date: Sun Sep 22 17:47:45 2013
New Revision: 1525396

URL: http://svn.apache.org/r1525396
Log:
Added FileSystemLocationStrategy class.

This is a specialized FileLocationStrategy which delegates to the FileSystem
in order to resolve the file in question.

Added:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileSystemLocationStrategy.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileSystemLocationStrategy.java

Added: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileSystemLocationStrategy.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileSystemLocationStrategy.java?rev=1525396&view=auto
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileSystemLocationStrategy.java
 (added)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileSystemLocationStrategy.java
 Sun Sep 22 17:47:45 2013
@@ -0,0 +1,47 @@
+/*
+ * 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.io;
+
+import java.net.URL;
+
+/**
+ * <p>
+ * A specialized implementation of {@code FileLocationStrategy} which uses the
+ * passed in {@link FileSystem} to locate a file.
+ * </p>
+ * <p>
+ * This strategy implementation ignores the URL of the passed in
+ * {@link FileLocator} and operates on its base path and file name. These
+ * properties are passed to the {@code locateFromURL()} method of
+ * {@code FileSystem}. So the burden of resolving the file is delegated to the
+ * {@code FileSystem}.
+ * </p>
+ *
+ * @version $Id: $
+ * @since 2.0
+ */
+public class FileSystemLocationStrategy implements FileLocationStrategy
+{
+    /**
+     * {@inheritDoc} This implementation delegates to the {@code FileSystem}.
+     */
+    public URL locate(FileSystem fileSystem, FileLocator locator)
+    {
+        return fileSystem.locateFromURL(locator.getBasePath(),
+                locator.getFileName());
+    }
+}

Added: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileSystemLocationStrategy.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileSystemLocationStrategy.java?rev=1525396&view=auto
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileSystemLocationStrategy.java
 (added)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileSystemLocationStrategy.java
 Sun Sep 22 17:47:45 2013
@@ -0,0 +1,70 @@
+/*
+ * 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.io;
+
+import static org.junit.Assert.assertSame;
+
+import java.net.URL;
+
+import org.apache.commons.configuration.ConfigurationAssert;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@code FileSystemLocationStrategy}.
+ *
+ * @version $Id: $
+ */
+public class TestFileSystemLocationStrategy
+{
+    /** The strategy to be tested. */
+    private FileSystemLocationStrategy strategy;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        strategy = new FileSystemLocationStrategy();
+    }
+
+    /**
+     * Tests a locate() operation.
+     */
+    @Test
+    public void testLocate()
+    {
+        FileSystem fs = EasyMock.createMock(FileSystem.class);
+        URL url = ConfigurationAssert.getTestURL("test.xml");
+        final String basePath = "testBasePath";
+        final String fileName = "testFileName.txt";
+        EasyMock.expect(fs.locateFromURL(basePath, fileName)).andReturn(url);
+        EasyMock.replay(fs);
+        FileLocator locator =
+                FileLocatorUtils
+                        .fileLocator()
+                        .basePath(basePath)
+                        .fileName(fileName)
+                        .fileSystem(FileLocatorUtils.DEFAULT_FILE_SYSTEM)
+                        .sourceURL(
+                                ConfigurationAssert
+                                        .getTestURL("test.properties"))
+                        .create();
+
+        assertSame("Wrong result", url, strategy.locate(fs, locator));
+        EasyMock.verify(fs);
+    }
+}


Reply via email to