Author: oheger
Date: Sun Sep 22 17:47:08 2013
New Revision: 1525395

URL: http://svn.apache.org/r1525395
Log:
Added ProvidedURLLocationStrategy.

This is a simple FileLocationStrategy implementation which checks whether the
passed in FileLocator already has a URL set. In this case, the file can be
accessed directly.

Added:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ProvidedURLLocationStrategy.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestProvidedURLLocationStrategy.java

Added: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ProvidedURLLocationStrategy.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ProvidedURLLocationStrategy.java?rev=1525395&view=auto
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ProvidedURLLocationStrategy.java
 (added)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ProvidedURLLocationStrategy.java
 Sun Sep 22 17:47:08 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 checks
+ * whether a passed in {@link FileLocator} already has a defined URL.
+ * </p>
+ * <p>
+ * {@code FileLocator} objects that have a URL already reference a file in an
+ * unambiguous way. Therefore, this strategy just returns the URL of the passed
+ * in {@code FileLocator}. It can be used as a first step of the file resolving
+ * process. If it fails, more sophisticated attempts for resolving the file can
+ * be made.
+ * </p>
+ *
+ * @version $Id: $
+ * @since 2.0
+ */
+public class ProvidedURLLocationStrategy implements FileLocationStrategy
+{
+    /**
+     * {@inheritDoc} This implementation just returns the URL stored in the
+     * given {@code FileLocator}.
+     */
+    public URL locate(FileSystem fileSystem, FileLocator locator)
+    {
+        return locator.getSourceURL();
+    }
+}

Added: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestProvidedURLLocationStrategy.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestProvidedURLLocationStrategy.java?rev=1525395&view=auto
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestProvidedURLLocationStrategy.java
 (added)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestProvidedURLLocationStrategy.java
 Sun Sep 22 17:47:08 2013
@@ -0,0 +1,72 @@
+/*
+ * 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.assertNull;
+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 ProvidedURLLocationStrategy}.
+ *
+ * @version $Id: $
+ */
+public class TestProvidedURLLocationStrategy
+{
+    /** The strategy to be tested. */
+    private ProvidedURLLocationStrategy strategy;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        strategy = new ProvidedURLLocationStrategy();
+    }
+
+    /**
+     * Tests a successful locate() operation.
+     */
+    @Test
+    public void testLocateSuccess()
+    {
+        FileSystem fs = EasyMock.createMock(FileSystem.class);
+        EasyMock.replay(fs);
+        URL url = ConfigurationAssert.getTestURL("test.xml");
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().sourceURL(url).create();
+        assertSame("Wrong URL", url, strategy.locate(fs, locator));
+    }
+
+    /**
+     * Tests a failed locate() operation.
+     */
+    @Test
+    public void testLocateFail()
+    {
+        FileSystem fs = EasyMock.createMock(FileSystem.class);
+        EasyMock.replay(fs);
+        FileLocator locator =
+                FileLocatorUtils.fileLocator().basePath("somePath")
+                        .fileName("someFile.xml").create();
+        assertNull("Got a URL", strategy.locate(fs, locator));
+    }
+}


Reply via email to