Repository: karaf
Updated Branches:
  refs/heads/karaf-2.3.x 0bf3f5424 -> a7111fae1


[KARAF-3023] Fix association features/repositories in features:list -o command


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/a7111fae
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/a7111fae
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/a7111fae

Branch: refs/heads/karaf-2.3.x
Commit: a7111fae1b44737caeadf2d67accaa55c9d3fd40
Parents: 0bf3f54
Author: Jean-Baptiste Onofré <jbono...@apache.org>
Authored: Mon Sep 15 14:51:24 2014 +0200
Committer: Jean-Baptiste Onofré <jbono...@apache.org>
Committed: Mon Sep 15 14:51:24 2014 +0200

----------------------------------------------------------------------
 .../features/command/ListFeaturesCommand.java   | 57 +++++++------
 .../command/ListFeaturesCommandTest.java        | 90 ++++++++++++++++++++
 2 files changed, 121 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/a7111fae/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
----------------------------------------------------------------------
diff --git 
a/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
 
b/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
index f627f96..7b875af 100644
--- 
a/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
+++ 
b/features/command/src/main/java/org/apache/karaf/features/command/ListFeaturesCommand.java
@@ -48,18 +48,16 @@ public class ListFeaturesCommand extends 
FeaturesCommandSupport {
     protected void doExecute(FeaturesService admin) throws Exception {
 
         // Get the feature data to print.
-        List<Feature> features = new ArrayList<Feature>();
-        List<Repository> repositories = new ArrayList<Repository>();
+        List<FeatureAndRepository> featuresAndRepositories = new 
ArrayList<FeatureAndRepository>();
         for (Repository r : Arrays.asList(admin.listRepositories())) {
             for (Feature f : r.getFeatures()) {
                 if (installed && !admin.isInstalled(f)) {
                     continue;
                 }
-                features.add(f);
-                repositories.add(r);
+                featuresAndRepositories.add(new FeatureAndRepository(f, r));
             }
         }
-        if (features.size() == 0) {
+        if (featuresAndRepositories.size() == 0) {
             if (installed) {
                 System.out.println("No features installed.");
             } else {
@@ -70,16 +68,16 @@ public class ListFeaturesCommand extends 
FeaturesCommandSupport {
 
         // Print column headers.
         int maxVersionSize = VERSION.length();
-        for (Feature f : features) {
-            maxVersionSize = Math.max(maxVersionSize, f.getVersion().length());
+        for (FeatureAndRepository far : featuresAndRepositories) {
+            maxVersionSize = Math.max(maxVersionSize, 
far.feature.getVersion().length());
         }
         int maxNameSize = NAME.length();
-        for (Feature f : features) {
-            maxNameSize = Math.max(maxNameSize, f.getName().length());
+        for (FeatureAndRepository far : featuresAndRepositories) {
+            maxNameSize = Math.max(maxNameSize, 
far.feature.getName().length());
         }
         int maxRepositorySize = REPOSITORY.length();
-        for (Repository repository:repositories) {
-                maxRepositorySize = Math.max(maxRepositorySize,  
repository.getName().length());
+        for (FeatureAndRepository far : featuresAndRepositories) {
+                maxRepositorySize = Math.max(maxRepositorySize,  
far.repository.getName().length());
         }
 
         StringBuilder sb = new StringBuilder();
@@ -102,36 +100,35 @@ public class ListFeaturesCommand extends 
FeaturesCommandSupport {
         // Print the feature data.
         boolean needsLegend = false;
         if (ordered) {
-            Collections.sort(features, new FeatureComparator());
+            Collections.sort(featuresAndRepositories, new 
FeatureAndRepositoryComparator());
         }
-        for (Feature f : features) {
+        for (FeatureAndRepository far : featuresAndRepositories) {
 
             sb.setLength(0);
             sb.append("[");
-            if (admin.isInstalled(f)) {
+            if (admin.isInstalled(far.feature)) {
                 sb.append(INSTALLED);
             } else {
                 sb.append(UNINSTALLED);
             }
 
             sb.append("] [");
-            String str = f.getVersion();
+            String str = far.feature.getVersion();
             sb.append(str);
             for (int i = str.length(); i < maxVersionSize; i++) {
                 sb.append(" ");
             }
             sb.append("] ");
 
-            str = f.getName();
+            str = far.feature.getName();
             sb.append(str);
             for (int i = str.length(); i < maxNameSize; i++) {
                 sb.append(" ");
             }
 
             sb.append(" ");
-            String name = repositories.get(0).getName();
+            String name = far.repository.getName();
             sb.append(name);
-            repositories.remove(0);
             
             if (name.charAt(name.length() - 1) == '*') {
                 needsLegend = true;
@@ -143,8 +140,8 @@ public class ListFeaturesCommand extends 
FeaturesCommandSupport {
 
             sb.append(" ");
             String description = "";
-            if(f.getDescription() != null) {
-            description = f.getDescription();
+            if (far.feature.getDescription() != null) {
+            description = far.feature.getDescription();
             }
             sb.append(description);
 
@@ -156,12 +153,20 @@ public class ListFeaturesCommand extends 
FeaturesCommandSupport {
         }
 
     }
-    
-    class FeatureComparator implements Comparator<Feature>
-    {
-        public int compare(Feature o1, Feature o2)
-        {
-            return o1.getName().toLowerCase().compareTo( 
o2.getName().toLowerCase() );
+
+    class FeatureAndRepository {
+        public Feature feature;
+        public Repository repository;
+
+        FeatureAndRepository(Feature feature, Repository repository) {
+            this.feature = feature;
+            this.repository = repository;
+        }
+    }
+
+    class FeatureAndRepositoryComparator implements 
Comparator<FeatureAndRepository> {
+        public int compare(FeatureAndRepository o1, FeatureAndRepository o2) {
+            return o1.feature.getName().toLowerCase().compareTo( 
o2.feature.getName().toLowerCase() );
         }
     }
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/a7111fae/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java
----------------------------------------------------------------------
diff --git 
a/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java
 
b/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java
new file mode 100644
index 0000000..8c5d495
--- /dev/null
+++ 
b/features/command/src/test/java/org/apache/karaf/features/command/ListFeaturesCommandTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.karaf.features.command;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.FeaturesService;
+import org.apache.karaf.features.Repository;
+import org.apache.karaf.features.internal.FeatureImpl;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class ListFeaturesCommandTest {
+
+    private FeaturesService featuresService;
+
+    @Before
+    public void init() throws Exception {
+        this.featuresService = EasyMock.createMock(FeaturesService.class);
+        
EasyMock.expect(this.featuresService.isInstalled(EasyMock.anyObject(Feature.class))).andReturn(true).anyTimes();
+
+        Repository r1 = EasyMock.createMock(Repository.class);
+        Repository r2 = EasyMock.createMock(Repository.class);
+        EasyMock.expect(r1.getName()).andReturn("r1").anyTimes();
+        EasyMock.expect(r2.getName()).andReturn("r2").anyTimes();
+        EasyMock.expect(this.featuresService.listRepositories()).andReturn(new 
Repository[] { r1, r2 });
+
+        EasyMock.expect(r1.getFeatures()).andReturn(new Feature[] {
+                new FeatureImpl("f2", "v2")
+        });
+        EasyMock.expect(r2.getFeatures()).andReturn(new Feature[] {
+                new FeatureImpl("f1", "v1")
+        });
+
+        EasyMock.replay(this.featuresService, r1, r2);
+    }
+
+    @Test
+    public void testListOfFeatures() throws Exception {
+        ListFeaturesCommand command = new ListFeaturesCommand();
+        command.ordered = false;
+        command.installed = false;
+
+        PrintStream oldOut = System.out;
+        ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
+        System.setOut(new PrintStream(resultStream));
+        command.doExecute(this.featuresService);
+        System.setOut(oldOut);
+
+        String result = new String(resultStream.toByteArray());
+        assertThat(result.contains("[v1     ] f1   r2"), is(true));
+    }
+
+    @Test
+    public void testSortingListOfFeatures() throws Exception {
+        ListFeaturesCommand command = new ListFeaturesCommand();
+        command.ordered = true;
+        command.installed = false;
+
+        PrintStream oldOut = System.out;
+        ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
+        System.setOut(new PrintStream(resultStream));
+        command.doExecute(this.featuresService);
+        System.setOut(oldOut);
+
+        String result = new String(resultStream.toByteArray());
+        assertThat(result.contains("[v1     ] f1   r2"), is(true));
+    }
+
+}

Reply via email to