sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1535392 - in /sling/trunk/tooling/maven/maven-launchpad-plugin/src: main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java
Date Thu, 24 Oct 2013 14:34:35 GMT
Author: bdelacretaz
Date: Thu Oct 24 14:34:35 2013
New Revision: 1535392

URL: http://svn.apache.org/r1535392
Log:
SLING-3205 - refactor if statement into more readable methods

Modified:
    sling/trunk/tooling/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java
    sling/trunk/tooling/maven/maven-launchpad-plugin/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java

Modified: sling/trunk/tooling/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java?rev=1535392&r1=1535391&r2=1535392&view=diff
==============================================================================
--- sling/trunk/tooling/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java
(original)
+++ sling/trunk/tooling/maven/maven-launchpad-plugin/src/main/java/org/apache/sling/maven/projectsupport/BundleListContentProvider.java
Thu Oct 24 14:34:35 2013
@@ -46,82 +46,100 @@ import org.apache.sling.maven.projectsup
 abstract class BundleListContentProvider implements LaunchpadContentProvider {
     
     private final File resourceProviderRoot;
+    private final static List<String> EMPTY_STRING_LIST = Collections.emptyList();
     
     BundleListContentProvider(File resourceProviderRoot) {
         this.resourceProviderRoot = resourceProviderRoot;
     }
-
-    public Iterator<String> getChildren(String path) {
-        if (path.equals(BUNDLE_PATH_PREFIX)) {
-            final Set<String> levels = new HashSet<String>();
-            for (final StartLevel level : getInitializedBundleList().getStartLevels()) {
-                // we treat the boot level as level 1
-                if ( level.getStartLevel() == -1 ) {
-                    levels.add(BUNDLE_PATH_PREFIX + "/1/");
-                } else {
-                    levels.add(BUNDLE_PATH_PREFIX + "/" + level.getLevel() + "/");
-                }
+    
+    private Iterator<String> handleBundlePathRoot(String path) {
+        final Set<String> levels = new HashSet<String>();
+        for (final StartLevel level : getInitializedBundleList().getStartLevels()) {
+            // we treat the boot level as level 1
+            if ( level.getStartLevel() == -1 ) {
+                levels.add(BUNDLE_PATH_PREFIX + "/1/");
+            } else {
+                levels.add(BUNDLE_PATH_PREFIX + "/" + level.getLevel() + "/");
             }
-            return levels.iterator();
-        } else if (path.equals("resources/corebundles")) {
-            List<String> empty = Collections.emptyList();
-            return empty.iterator();
-        } else if (path.equals(CONFIG_PATH_PREFIX)) {
-            if (getConfigDirectory().exists() && getConfigDirectory().isDirectory())
{
-                File[] configFiles = getConfigDirectory().listFiles(new FileFilter() {
+        }
+        return levels.iterator();
+    }
+    
+    private Iterator<String> handleConfigPath() {
+        if (getConfigDirectory().exists() && getConfigDirectory().isDirectory())
{
+            File[] configFiles = getConfigDirectory().listFiles(new FileFilter() {
 
-                    public boolean accept(File file) {
-                        return file.isFile();
-                    }
-                });
+                public boolean accept(File file) {
+                    return file.isFile();
+                }
+            });
 
-                List<String> fileNames = new ArrayList<String>();
-                for (File cfgFile : configFiles) {
-                    if (cfgFile.isFile()) {
-                        fileNames.add(CONFIG_PATH_PREFIX + "/" + cfgFile.getName());
-                    }
+            List<String> fileNames = new ArrayList<String>();
+            for (File cfgFile : configFiles) {
+                if (cfgFile.isFile()) {
+                    fileNames.add(CONFIG_PATH_PREFIX + "/" + cfgFile.getName());
                 }
+            }
 
-                return fileNames.iterator();
+            return fileNames.iterator();
 
-            } else {
-                List<String> empty = Collections.emptyList();
-                return empty.iterator();
-            }
-        } else if (path.startsWith(BUNDLE_PATH_PREFIX)) {
-            final String startLevelInfo = path.substring(BUNDLE_PATH_PREFIX.length() + 1);
-            try {
-                final int startLevel = Integer.parseInt(startLevelInfo);
+        } else {
+            return EMPTY_STRING_LIST.iterator();
+        }
+    }
+    
+    private Iterator<String> handleBundlePathFolder(String path) {
+        final String startLevelInfo = path.substring(BUNDLE_PATH_PREFIX.length() + 1);
+        try {
+            final int startLevel = Integer.parseInt(startLevelInfo);
 
-                final List<String> bundles = new ArrayList<String>();
-                for (final StartLevel level : getInitializedBundleList().getStartLevels())
{
-                    if (level.getStartLevel() == startLevel || (startLevel == 1 &&
level.getStartLevel() == -1)) {
-                        for (final Bundle bundle : level.getBundles()) {
-                            final ArtifactDefinition d = new ArtifactDefinition(bundle, startLevel);
-                            try {
-                                final Artifact artifact = getArtifact(d);
-                                bundles.add(artifact.getFile().toURI().toURL().toExternalForm());
-                            } catch (Exception e) {
-                                getLog().error("Unable to resolve artifact ", e);
-                            }
+            final List<String> bundles = new ArrayList<String>();
+            for (final StartLevel level : getInitializedBundleList().getStartLevels()) {
+                if (level.getStartLevel() == startLevel || (startLevel == 1 && level.getStartLevel()
== -1)) {
+                    for (final Bundle bundle : level.getBundles()) {
+                        final ArtifactDefinition d = new ArtifactDefinition(bundle, startLevel);
+                        try {
+                            final Artifact artifact = getArtifact(d);
+                            bundles.add(artifact.getFile().toURI().toURL().toExternalForm());
+                        } catch (Exception e) {
+                            getLog().error("Unable to resolve artifact ", e);
                         }
                     }
                 }
-                return bundles.iterator();
-
-            } catch (NumberFormatException e) {
-                // we ignore this
             }
+            return bundles.iterator();
+
+        } catch (NumberFormatException e) {
+            // we ignore this
+        }
+        return null;
+    }
+    
+    private Iterator<String> handleResourcesRoot() {
+        final Set<String> subDirs = new HashSet<String>();
+        subDirs.add(BUNDLE_PATH_PREFIX);
+        subDirs.add(CONFIG_PATH_PREFIX);
+        subDirs.add("resources/corebundles");
+        return subDirs.iterator();
+    }
+
+    public Iterator<String> getChildren(String path) {
+        Iterator<String> result = null;
+        if (path.equals(BUNDLE_PATH_PREFIX)) {
+            result = handleBundlePathRoot(path);
+        } else if (path.equals("resources/corebundles")) {
+            result = EMPTY_STRING_LIST.iterator();
+        } else if (path.equals(CONFIG_PATH_PREFIX)) {
+            result = handleConfigPath();
+        } else if (path.startsWith(BUNDLE_PATH_PREFIX)) {
+            result = handleBundlePathFolder(path);
         } else if (path.equals("resources") ) {
-            final Set<String> subDirs = new HashSet<String>();
-            subDirs.add(BUNDLE_PATH_PREFIX);
-            subDirs.add(CONFIG_PATH_PREFIX);
-            subDirs.add("resources/corebundles");
-            return subDirs.iterator();
+            result = handleResourcesRoot();
+        } else {
+            getLog().warn("un-handlable " + getClass().getSimpleName() + " path: " + path);
         }
 
-        getLog().warn("un-handlable path " + path);
-        return null;
+        return result;
     }
 
     public URL getResource(String path) {

Modified: sling/trunk/tooling/maven/maven-launchpad-plugin/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-launchpad-plugin/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java?rev=1535392&r1=1535391&r2=1535392&view=diff
==============================================================================
--- sling/trunk/tooling/maven/maven-launchpad-plugin/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java
(original)
+++ sling/trunk/tooling/maven/maven-launchpad-plugin/src/test/java/org/apache/sling/maven/projectsupport/BundleListContentProviderTest.java
Thu Oct 24 14:34:35 2013
@@ -53,6 +53,7 @@ public class BundleListContentProviderTe
     private LaunchpadContentProvider provider;
     private File resourceProviderRoot;
     private File resourceProviderFile;
+    private File configDirectory;
     
     @Rule
     public TemporaryFolder tempFolder = new TemporaryFolder();
@@ -82,6 +83,7 @@ public class BundleListContentProviderTe
             f.createNewFile();
             assertTrue("Expecting temporary config file to have been created: " + f.getAbsolutePath(),
f.exists());
         }
+        configDirectory = tempFolder.getRoot();
         
         resourceProviderRoot = new File(tempFolder.getRoot(), "RESOURCE_PROVIDER_ROOT");
         resourceProviderRoot.mkdirs();
@@ -105,7 +107,7 @@ public class BundleListContentProviderTe
 
             @Override
             File getConfigDirectory() {
-                return tempFolder.getRoot();
+                return configDirectory;
             }
 
             @Override
@@ -186,6 +188,12 @@ public class BundleListContentProviderTe
     }
     
     @Test
+    public void testNonExistentConfigDirectory() {
+        configDirectory = new File("/NON_EXISTENT_" + System.currentTimeMillis());
+        assertChildren("resources/config");
+    }
+
+    @Test
     public void testBundles0() {
         assertChildren("resources/bundles/0", 
                 "file:/commons-io/0/null", 
@@ -262,4 +270,5 @@ public class BundleListContentProviderTe
     public void testNullResult() {
         assertNull(provider.getChildren("/FOO/bar"));
     }
+    
 }



Mime
View raw message