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 2013/09/22 19:50:52 UTC
svn commit: r1525401 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/io/ClasspathLocationStrategy.java
test/java/org/apache/commons/configuration/io/TestClasspathLocationStrategy.java
Author: oheger
Date: Sun Sep 22 17:50:51 2013
New Revision: 1525401
URL: http://svn.apache.org/r1525401
Log:
Added ClasspathLocationStrategy.
This FileLocationStrategy implementation interprets a locator's file name as
resource name and tries to locate it on the classpath.
Added:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ClasspathLocationStrategy.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestClasspathLocationStrategy.java
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ClasspathLocationStrategy.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ClasspathLocationStrategy.java?rev=1525401&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ClasspathLocationStrategy.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/ClasspathLocationStrategy.java Sun Sep 22 17:50:51 2013
@@ -0,0 +1,48 @@
+/*
+ * 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;
+
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * <p>
+ * A specialized {@code FileLocationStrategy} implementation which searches for
+ * files on the class path.
+ * </p>
+ * <p>
+ * This strategy implementation ignores the URL and the base path components of
+ * the passed in {@link FileLocator}. It tries to look up the file name on both
+ * the class path and the system class path.
+ * </p>
+ *
+ * @version $Id: $
+ * @since 2.0
+ */
+public class ClasspathLocationStrategy implements FileLocationStrategy
+{
+ /**
+ * {@inheritDoc} This implementation looks up the locator's file name as a
+ * resource on the class path.
+ */
+ public URL locate(FileSystem fileSystem, FileLocator locator)
+ {
+ return StringUtils.isEmpty(locator.getFileName()) ? null
+ : FileLocatorUtils.locateFromClasspath(locator.getFileName());
+ }
+}
Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestClasspathLocationStrategy.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestClasspathLocationStrategy.java?rev=1525401&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestClasspathLocationStrategy.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestClasspathLocationStrategy.java Sun Sep 22 17:50:51 2013
@@ -0,0 +1,89 @@
+/*
+ * 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.assertEquals;
+import static org.junit.Assert.assertNull;
+
+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 ClasspathLocationStrategy}.
+ *
+ * @version $Id: $
+ */
+public class TestClasspathLocationStrategy
+{
+ /** Constant for a test file name. */
+ private static final String FILE_NAME = "test.xml";
+
+ /** A mock for the file system. */
+ private FileSystem fileSystem;
+
+ /** The strategy to be tested. */
+ private ClasspathLocationStrategy strategy;
+
+ @Before
+ public void setUp() throws Exception
+ {
+ fileSystem = EasyMock.createMock(FileSystem.class);
+ EasyMock.replay(fileSystem);
+ strategy = new ClasspathLocationStrategy();
+ }
+
+ /**
+ * Tests a successful location of a provided resource name.
+ */
+ @Test
+ public void testLocateSuccess()
+ {
+ FileLocator locator =
+ FileLocatorUtils.fileLocator().fileName(FILE_NAME)
+ .basePath("somePath").create();
+ URL url = strategy.locate(fileSystem, locator);
+ assertEquals("Wrong URL", ConfigurationAssert.getTestURL(FILE_NAME)
+ .toExternalForm(), url.toExternalForm());
+ }
+
+ /**
+ * Tests a failed locate() operation.
+ */
+ @Test
+ public void testLocateFailed()
+ {
+ FileLocator locator =
+ FileLocatorUtils.fileLocator()
+ .fileName("non existing resource name!").create();
+ assertNull("Got a URL", strategy.locate(fileSystem, locator));
+ }
+
+ /**
+ * Tests a locate() operation if no file name is provided.
+ */
+ @Test
+ public void testLocateNoFileName()
+ {
+ FileLocator locator =
+ FileLocatorUtils.fileLocator().fileName("").create();
+ assertNull("Got a URL", strategy.locate(fileSystem, locator));
+ }
+}