SLING-4284 - Support nested config path in BundleListContentProvider

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1649770 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java b/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java
index 64442fc..e2fdbbe 100644
--- a/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java
+++ b/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java
@@ -33,6 +33,7 @@
 import java.util.List;
 import java.util.Set;
 
+import org.apache.commons.io.FilenameUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.logging.Log;
@@ -71,18 +72,11 @@
     
     private Iterator<String> handleConfigPath() {
         if (getConfigDirectory().exists() && getConfigDirectory().isDirectory()) {
-            File[] configFiles = getConfigDirectory().listFiles(new FileFilter() {
-
-                public boolean accept(File file) {
-                    return file.isFile();
-                }
-            });
+            File[] configFiles = getConfigDirectory().listFiles();
 
             List<String> fileNames = new ArrayList<String>();
             for (File cfgFile : configFiles) {
-                if (cfgFile.isFile()) {
                     fileNames.add(CONFIG_PATH_PREFIX + "/" + cfgFile.getName());
-                }
             }
 
             return fileNames.iterator();
@@ -223,18 +217,27 @@
     }
     
     private Iterator<String> handleConfigSubpath(String path) {
-        // We don't handle config subpaths for now, but do not 
-        // warn if we're asked for the children of a file, just
-        // return empty in that case
         final File f = getConfigFile(path);
         if(!f.exists()) {
             getLog().warn("BundleListContentProvider cannot get children of config path: " + path);
+            return EMPTY_STRING_LIST.iterator();
         }
-        return EMPTY_STRING_LIST.iterator();
+
+        if (f.isFile()){
+            return EMPTY_STRING_LIST.iterator();
+        }
+
+        File[] configFiles = f.listFiles();
+        List<String> fileNames = new ArrayList<String>();
+        for (File cfgFile : configFiles) {
+            fileNames.add(path + "/" + cfgFile.getName());
+        }
+
+        return fileNames.iterator();
     }
     
     private File getConfigFile(String path) {
-        return new File(getConfigDirectory(), path.substring(CONFIG_PATH_PREFIX.length() + 1));
+        return new File(FilenameUtils.concat(getConfigDirectory().getAbsolutePath(), path.substring(CONFIG_PATH_PREFIX.length() + 1)));
     }
 
     public Iterator<String> getChildren(String path) {
diff --git a/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java b/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java
index 8041a56..6d5e66f 100644
--- a/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java
+++ b/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java
@@ -31,6 +31,7 @@
 import java.util.LinkedList;
 import java.util.List;
 
+import org.apache.commons.io.FilenameUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.logging.Log;
@@ -68,7 +69,9 @@
     private static final String [] CONFIG_FILES = {
         "file1.txt",
         "file2.cfg",
-        "someFile.properties"
+        "someFile.properties",
+        "dir1/file1.txt",
+        "dir1/dir2/file1.txt",
     };
     
     @BeforeClass
@@ -85,12 +88,14 @@
     
     @Before
     public void setupTemporaryFiles() throws IOException {
+        TemporaryFolder configRoot = new TemporaryFolder(tempFolder.getRoot());
+        configRoot.create();
         for(String filename: CONFIG_FILES) {
-            final File f = getConfigFile(filename);
+            final File f = getConfigFile(configRoot, filename);
             f.createNewFile();
             assertTrue("Expecting temporary config file to have been created: " + f.getAbsolutePath(), f.exists());
         }
-        configDirectory = tempFolder.getRoot();
+        configDirectory = configRoot.getRoot();
 
         resourceProviderRoot = new File(tempFolder.getRoot(), "RESOURCE_PROVIDER_ROOT");
         resourceProviderRoot.mkdirs();
@@ -98,9 +103,19 @@
         resourceProviderFile.createNewFile();
         fakeBundlePath = getFakeBundlePath();
     }
+
+    private File getConfigFile(File configRoot, String name) throws IOException {
+        return new File(FilenameUtils.concat(configRoot.getAbsolutePath(), name));
+    }
     
-    private File getConfigFile(String name) {
-        return new File(tempFolder.getRoot(), name);
+    private File getConfigFile(TemporaryFolder configRoot, String name) throws IOException {
+        File parentFolder = configRoot.getRoot();
+        if (name.contains("/")){
+            String parentPath = name.substring(0, name.lastIndexOf('/'));
+            name = name.substring(name.lastIndexOf('/') + 1);
+            parentFolder = configRoot.newFolder(parentPath.split("/"));
+        }
+        return new File(parentFolder, name);
     }
 
     private String getFakeBundlePath() {
@@ -252,14 +267,28 @@
         assertChildren("resources/config", 
                 "resources/config/file1.txt", 
                 "resources/config/file2.cfg", 
-                "resources/config/someFile.properties"); 
+                "resources/config/someFile.properties",
+                "resources/config/dir1");
     }
-    
+
+    @Test
+    public void testNestedConfig() {
+        assertChildren("resources/config/dir1",
+                "resources/config/dir1/file1.txt",
+                "resources/config/dir1/dir2");
+    }
+
     @Test
     public void testConfigFile() {
         assertChildren("resources/config/file1.txt");
         assertEquals("Expecting no warnings", 0, logWarningsCount);
     }
+
+    @Test
+    public void testNestedConfigFile() {
+        assertChildren("resources/config/dir1/file1.txt");
+        assertEquals("Expecting no warnings", 0, logWarningsCount);
+    }
     
     @Test
     public void testConfigSubpath() {
@@ -342,7 +371,14 @@
     public void testConfigResource() throws Exception {
         final URL url = provider.getResource("resources/config/file1.txt");
         assertNotNull("Expecting config resource to be found", url);
-        assertEquals(getConfigFile("file1.txt").toURI().toURL().toExternalForm(), url.toExternalForm());
+        assertEquals(getConfigFile(configDirectory, "file1.txt").toURI().toURL().toExternalForm(), url.toExternalForm());
+    }
+
+    @Test
+    public void testNestedConfigResource() throws Exception {
+        final URL url = provider.getResource("resources/config/dir1/file1.txt");
+        assertNotNull("Expecting config resource to be found", url);
+        assertEquals(getConfigFile(configDirectory, "dir1/file1.txt").toURI().toURL().toExternalForm(), url.toExternalForm());
     }
     
     @Test