Author: niallp
Date: Sun Mar 13 00:45:10 2011
New Revision: 1081025

URL: http://svn.apache.org/viewvc?rev=1081025&view=rev
Log:
IO-261 Add getFile() methods with varargs parameter

Modified:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
    
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Modified: 
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1081025&r1=1081024&r2=1081025&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java 
(original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java 
Sun Mar 13 00:45:10 2011
@@ -122,6 +122,50 @@ public class FileUtils {
 
     //-----------------------------------------------------------------------
     /**
+     * Construct a file from the set of name elements.
+     * 
+     * @param directory the parent directory
+     * @param names the name elements
+     * @return the file
+     * @since Commons IO 2.1
+     */
+    public static File getFile(File directory, String... names) {
+        if (directory == null) {
+            throw new NullPointerException("directorydirectory must not be 
null");
+        }
+        if (names == null) {
+            throw new NullPointerException("names must not be null");
+        }
+        File file = directory;
+        for (String name : names) {
+            file = new File(file, name);
+        }
+        return file;
+    }
+
+    /**
+     * Construct a file from the set of name elements.
+     * 
+     * @param names the name elements
+     * @return the file
+     * @since Commons IO 2.1
+     */
+    public static File getFile(String... names) {
+        if (names == null) {
+            throw new NullPointerException("names must not be null");
+        }
+        File file = null;
+        for (String name : names) {
+            if (file == null) {
+                file = new File(name);
+            } else {
+                file = new File(file, name);
+            }
+        }
+        return file;
+    }
+
+    /**
      * Returns the path to the system temporary directory.
      * 
      * @return the path to the system temporary directory.

Modified: 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1081025&r1=1081024&r2=1081025&view=diff
==============================================================================
--- 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
 (original)
+++ 
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
 Sun Mar 13 00:45:10 2011
@@ -102,6 +102,49 @@ public class FileUtilsTestCase extends F
 
     //-----------------------------------------------------------------------
     /**
+     * Tests the {@link FileUtils#getFile(String...)} method.
+     */
+    public void testGetFile() {
+        File expected_A = new File("src");
+        File expected_B = new File(expected_A, "main");
+        File expected_C = new File(expected_B, "java");
+        assertEquals("A", expected_A, FileUtils.getFile("src"));
+        assertEquals("B", expected_B, FileUtils.getFile("src", "main"));
+        assertEquals("C", expected_C, FileUtils.getFile("src", "main", 
"java"));
+        try {
+            FileUtils.getFile((String[])null);
+            fail("Expected NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
+     * Tests the {@link FileUtils#getFile(File, String...)} method.
+     */
+    public void testGetFile_Parent() {
+        File parent = new File("parent");
+        File expected_A = new File(parent, "src");
+        File expected_B = new File(expected_A, "main");
+        File expected_C = new File(expected_B, "java");
+        assertEquals("A", expected_A, FileUtils.getFile(parent, "src"));
+        assertEquals("B", expected_B, FileUtils.getFile(parent, "src", 
"main"));
+        assertEquals("C", expected_C, FileUtils.getFile(parent, "src", "main", 
"java"));
+        try {
+            FileUtils.getFile(parent, (String[])null);
+            fail("Expected NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+        try {
+            FileUtils.getFile((File)null, "src");
+            fail("Expected NullPointerException");
+        } catch (NullPointerException e) {
+            // expected
+        }
+    }
+
+    /**
      * Tests the {@link FileUtils#getTempDirectoryPath()} method.
      */
     public void testGetTempDirectoryPath() {


Reply via email to