Author: oheger Date: Sun Sep 22 17:49:36 2013 New Revision: 1525399 URL: http://svn.apache.org/r1525399 Log: Added BasePathLocationStrategy.
This FileLocationStrategy implementation tries to construct a path name from the specified base path and file name. Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/BasePathLocationStrategy.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestBasePathLocationStrategy.java Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/BasePathLocationStrategy.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/BasePathLocationStrategy.java?rev=1525399&view=auto ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/BasePathLocationStrategy.java (added) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/BasePathLocationStrategy.java Sun Sep 22 17:49:36 2013 @@ -0,0 +1,62 @@ +/* + * 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.io.File; +import java.net.URL; + +import org.apache.commons.lang3.StringUtils; + +/** + * <p> + * A specialized implementation of {@code FileLocationStrategy} which tries to + * construct a file path from the locator's base path and file name. + * </p> + * <p> + * This strategies ignores the URL stored in the passed in {@link FileLocator}. + * It generates a path by concatenating the base path (if present) and the file + * name. If the resulting path points to a valid file, the corresponding URL is + * returned. + * </p> + * + * @version $Id: $ + * @since 2.0 + */ +public class BasePathLocationStrategy implements FileLocationStrategy +{ + /** + * {@inheritDoc} This implementation uses utility methods from + * {@code FileLocatorUtils} to generate a {@code File} from the locator's + * base path and file name. If this {@code File} exists, its URL is + * returned. + */ + public URL locate(FileSystem fileSystem, FileLocator locator) + { + if (StringUtils.isNotEmpty(locator.getFileName())) + { + File file = + FileLocatorUtils.constructFile(locator.getBasePath(), + locator.getFileName()); + if (file.isFile()) + { + return FileLocatorUtils.convertFileToURL(file); + } + } + + return null; + } +} Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestBasePathLocationStrategy.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestBasePathLocationStrategy.java?rev=1525399&view=auto ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestBasePathLocationStrategy.java (added) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestBasePathLocationStrategy.java Sun Sep 22 17:49:36 2013 @@ -0,0 +1,118 @@ +/* + * 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.io.File; +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 BasePathLocationStrategy}. + * + * @version $Id: $ + */ +public class TestBasePathLocationStrategy +{ + /** Constant for the name of the test file. */ + private static final String TEST_FILE = "test.xml"; + + /** A mock for the file system. */ + private FileSystem fileSystem; + + /** The strategy to be tested. */ + private BasePathLocationStrategy strategy; + + @Before + public void setUp() throws Exception + { + fileSystem = EasyMock.createMock(FileSystem.class); + EasyMock.replay(fileSystem); + strategy = new BasePathLocationStrategy(); + } + + /** + * Checks whether the passed in URL points to the expected test file. + * + * @param url the URL to be checked + */ + private static void checkURL(URL url) + { + assertEquals("Wrong URL", FileLocatorUtils.fileFromURL(url) + .getAbsoluteFile(), ConfigurationAssert.getTestFile(TEST_FILE) + .getAbsoluteFile()); + } + + /** + * Tests a successful locate() operation with a valid base path and file + * name. + */ + @Test + public void testLocateSuccess() + { + File path = ConfigurationAssert.TEST_DIR; + FileLocator locator = + FileLocatorUtils.fileLocator().basePath(path.getAbsolutePath()) + .fileName(TEST_FILE).create(); + checkURL(strategy.locate(fileSystem, locator)); + } + + /** + * Tests whether a prefix for relative file names is handled correctly. + */ + @Test + public void testLocateSuccessRelativePrefix() + { + File path = ConfigurationAssert.TEST_DIR; + FileLocator locator = + FileLocatorUtils.fileLocator().basePath(path.getAbsolutePath()) + .fileName("." + File.separator + TEST_FILE).create(); + checkURL(strategy.locate(fileSystem, locator)); + } + + /** + * Tests a locate() operation if no file name is provided. + */ + @Test + public void testNullFileName() + { + FileLocator locator = + FileLocatorUtils + .fileLocator() + .basePath( + ConfigurationAssert.getTestFile(TEST_FILE) + .getAbsolutePath()).create(); + assertNull("Got a URL", strategy.locate(fileSystem, locator)); + } + + /** + * Tests whether a null base path is handled correctly. + */ + @Test + public void testNullBasePath() + { + FileLocator locator = + FileLocatorUtils.fileLocator().fileName(TEST_FILE).create(); + assertNull("Got a URL", strategy.locate(fileSystem, locator)); + } +}