You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2017/09/15 09:41:49 UTC

[3/5] karaf git commit: [KARAF-5375] Add a unit test

[KARAF-5375] Add a unit test


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

Branch: refs/heads/master
Commit: b4875d651db6411c7787bd0c536c18c74f1d0bee
Parents: e430d25
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Fri Sep 15 10:49:08 2017 +0200
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Fri Sep 15 11:25:41 2017 +0200

----------------------------------------------------------------------
 .../internal/download/DownloadManager.java      |  5 +-
 .../download/impl/MavenDownloadManager.java     |  5 ++
 .../features/internal/service/Deployer.java     | 16 +----
 .../internal/service/FeaturesServiceConfig.java |  4 +-
 .../internal/service/FeaturesServiceImpl.java   | 18 ++---
 .../service/FeaturesServiceImplTest.java        | 76 +++++++++++++++++++-
 6 files changed, 99 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/b4875d65/features/core/src/main/java/org/apache/karaf/features/internal/download/DownloadManager.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/download/DownloadManager.java b/features/core/src/main/java/org/apache/karaf/features/internal/download/DownloadManager.java
index d9094a1..0893c90 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/download/DownloadManager.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/download/DownloadManager.java
@@ -18,10 +18,13 @@ package org.apache.karaf.features.internal.download;
 
 import java.util.Map;
 
-public interface DownloadManager {
+public interface DownloadManager extends AutoCloseable {
 
     Downloader createDownloader();
 
     Map<String, StreamProvider> getProviders();
 
+    default void close() {
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/b4875d65/features/core/src/main/java/org/apache/karaf/features/internal/download/impl/MavenDownloadManager.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/download/impl/MavenDownloadManager.java b/features/core/src/main/java/org/apache/karaf/features/internal/download/impl/MavenDownloadManager.java
index 65f798b..ede3337 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/download/impl/MavenDownloadManager.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/download/impl/MavenDownloadManager.java
@@ -80,6 +80,11 @@ public class MavenDownloadManager implements DownloadManager {
         return (Map) Collections.synchronizedMap(downloaded);
     }
 
+    @Override
+    public void close() {
+        executorService.shutdown();
+    }
+
     protected class MavenDownloader implements Downloader {
 
         private volatile int pending = 0;

http://git-wip-us.apache.org/repos/asf/karaf/blob/b4875d65/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java
index 0e29e2c..e2c3522 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/Deployer.java
@@ -1101,22 +1101,12 @@ public class Deployer {
     }
 
     private static void removeFragmentsAndBundlesInState(Collection<Bundle> bundles, int state) {
-        for (Iterator<Bundle> iterator = bundles.iterator(); iterator.hasNext();) {
-            Bundle bundle = iterator.next();
-            if ((bundle.getState() & state) != 0
-                    || bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null) {
-                iterator.remove();
-            }
-        }
+        bundles.removeIf(bundle -> (bundle.getState() & state) != 0
+                || bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null);
     }
 
     private static void removeBundlesInState(Collection<Bundle> bundles, int state) {
-        for (Iterator<Bundle> iterator = bundles.iterator(); iterator.hasNext();) {
-            Bundle bundle = iterator.next();
-            if ((bundle.getState() & state) != 0) {
-                iterator.remove();
-            }
-        }
+        bundles.removeIf(bundle -> (bundle.getState() & state) != 0);
     }
 
     protected void logWiring(Map<Resource, List<Wire>> wiring, boolean onlyFeatures) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/b4875d65/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceConfig.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceConfig.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceConfig.java
index 68f957c..f6fe033 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceConfig.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceConfig.java
@@ -16,6 +16,8 @@
  */
 package org.apache.karaf.features.internal.service;
 
+import org.apache.karaf.features.FeaturesService;
+
 public class FeaturesServiceConfig {
 
     public final String overrides;
@@ -56,7 +58,7 @@ public class FeaturesServiceConfig {
     public final String blacklisted;
 
     public FeaturesServiceConfig() {
-        this(null, null, null, null, 1, 0, 0, null, null);
+        this(null, FeaturesService.DEFAULT_FEATURE_RESOLUTION_RANGE, FeaturesService.DEFAULT_BUNDLE_UPDATE_RANGE, null, 1, 0, 0, null, null);
     }
 
     public FeaturesServiceConfig(String overrides, String featureResolutionRange, String bundleUpdateRange, String updateSnapshots, int downloadThreads, long scheduleDelay, int scheduleMaxRun, String blacklisted, String serviceRequirements) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/b4875d65/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
index c1e7b0c..e07d9db 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
@@ -978,13 +978,7 @@ public class FeaturesServiceImpl implements FeaturesService, Deployer.DeployCall
                              EnumSet<Option> options,                              // installation options
                              String outputFile                                     // file to store the resolution or null
     ) throws Exception {
-
-        Dictionary<String, String> props = getMavenConfig();
-        MavenResolver resolver = MavenResolvers.createMavenResolver(props, "org.ops4j.pax.url.mvn");
-        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(cfg.downloadThreads);
-        executor.setMaximumPoolSize(cfg.downloadThreads);
-        DownloadManager manager = DownloadManagers.createDownloadManager(resolver, executor, cfg.scheduleDelay, cfg.scheduleMaxRun);
-        try {
+        try (DownloadManager manager = createDownloadManager()) {
             Set<String> prereqs = new HashSet<>();
             while (true) {
                 try {
@@ -1001,11 +995,17 @@ public class FeaturesServiceImpl implements FeaturesService, Deployer.DeployCall
                     }
                 }
             }
-        } finally {
-            executor.shutdown();
         }
     }
 
+    protected DownloadManager createDownloadManager() throws IOException {
+        Dictionary<String, String> props = getMavenConfig();
+        MavenResolver resolver = MavenResolvers.createMavenResolver(props, "org.ops4j.pax.url.mvn");
+        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(cfg.downloadThreads);
+        executor.setMaximumPoolSize(cfg.downloadThreads);
+        return DownloadManagers.createDownloadManager(resolver, executor, cfg.scheduleDelay, cfg.scheduleMaxRun);
+    }
+
     private Dictionary<String, String> getMavenConfig() throws IOException {
         Hashtable<String, String> props = new Hashtable<>();
         if (configurationAdmin != null) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/b4875d65/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
----------------------------------------------------------------------
diff --git a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
index f5363d4..c351ad7 100644
--- a/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
+++ b/features/core/src/test/java/org/apache/karaf/features/internal/service/FeaturesServiceImplTest.java
@@ -22,25 +22,38 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.net.*;
+import java.util.Collections;
 import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.Map;
 
 import org.apache.felix.resolver.ResolverImpl;
 import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.FeatureState;
 import org.apache.karaf.features.FeaturesService;
 import org.apache.karaf.features.TestBase;
 import org.apache.karaf.features.FeaturesService.Option;
+import org.apache.karaf.features.internal.download.DownloadManager;
 import org.apache.karaf.features.internal.resolver.Slf4jResolverLog;
 import org.apache.karaf.features.internal.service.BundleInstallSupport.FrameworkInfo;
+import org.apache.karaf.features.internal.support.TestDownloadManager;
+import org.easymock.Capture;
 import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Version;
+import org.osgi.framework.startlevel.BundleStartLevel;
+import org.osgi.framework.wiring.BundleRevision;
 import org.osgi.service.resolver.Resolver;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -162,7 +175,68 @@ public class FeaturesServiceImplTest extends TestBase {
         waitInstalled(featureService, test110);
         assertNotInstalled(featureService, test100);
     }
-    
+
+    @Test
+    public void testInstallAndStop() throws Exception {
+        Capture<Bundle> stoppedBundle = Capture.newInstance();
+
+        Bundle bundle = EasyMock.niceMock(Bundle.class);
+        BundleStartLevel bundleStartLevel = EasyMock.niceMock(BundleStartLevel.class);
+        BundleRevision bundleRevision = EasyMock.niceMock(BundleRevision.class);
+
+        FeaturesServiceConfig cfg = new FeaturesServiceConfig();
+        BundleInstallSupport installSupport = EasyMock.niceMock(BundleInstallSupport.class);
+        FrameworkInfo dummyInfo = new FrameworkInfo();
+        expect(installSupport.getInfo()).andReturn(dummyInfo).atLeastOnce();
+        expect(installSupport.installBundle(EasyMock.eq("root"), EasyMock.eq("a100"), anyObject())).andReturn(bundle);
+        installSupport.startBundle(bundle);
+        expectLastCall();
+        expect(bundle.getBundleId()).andReturn(1L).anyTimes();
+        expect(bundle.getSymbolicName()).andReturn("a").anyTimes();
+        expect(bundle.getVersion()).andReturn(new Version("1.0.0")).anyTimes();
+        expect(bundle.getHeaders()).andReturn(new Hashtable<>()).anyTimes();
+        expect(bundle.adapt(BundleStartLevel.class)).andReturn(bundleStartLevel).anyTimes();
+        expect(bundle.adapt(BundleRevision.class)).andReturn(bundleRevision).anyTimes();
+        expect(bundleRevision.getBundle()).andReturn(bundle).anyTimes();
+        expect(bundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()).anyTimes();
+        expect(bundleRevision.getRequirements(null)).andReturn(Collections.emptyList()).anyTimes();
+        EasyMock.replay(installSupport, bundle, bundleStartLevel, bundleRevision);
+        FeaturesService featureService =  new FeaturesServiceImpl(new Storage(), null, null, this.resolver,
+                installSupport, null, cfg) {
+            @Override
+            protected DownloadManager createDownloadManager() throws IOException {
+                return new TestDownloadManager(FeaturesServiceImplTest.class, "data1");
+            }
+        };
+
+        URI repoA = URI.create("custom:data1/features.xml");
+        featureService.addRepository(repoA);
+        Feature test100 = featureService.getFeature("f", "1.0.0");
+        installFeature(featureService, test100);
+        assertInstalled(featureService, test100);
+
+        dummyInfo.bundles.put(1L, bundle);
+        Map<String, Map<String, FeatureState>> states = new HashMap<>();
+        states.computeIfAbsent("root", k -> new HashMap<>()).put("f/1.0.0", FeatureState.Resolved);
+        EasyMock.reset(installSupport, bundle, bundleRevision, bundleStartLevel);
+        expect(installSupport.getInfo()).andReturn(dummyInfo).anyTimes();
+        installSupport.stopBundle(EasyMock.capture(stoppedBundle), EasyMock.anyInt());
+        expectLastCall();
+        expect(bundle.getBundleId()).andReturn(1L).anyTimes();
+        expect(bundle.getSymbolicName()).andReturn("a").anyTimes();
+        expect(bundle.getVersion()).andReturn(new Version("1.0.0")).anyTimes();
+        expect(bundle.getHeaders()).andReturn(new Hashtable<>()).anyTimes();
+        expect(bundle.adapt(BundleStartLevel.class)).andReturn(bundleStartLevel).anyTimes();
+        expect(bundle.adapt(BundleRevision.class)).andReturn(bundleRevision).anyTimes();
+        expect(bundleRevision.getBundle()).andReturn(bundle).anyTimes();
+        expect(bundleRevision.getCapabilities(null)).andReturn(Collections.emptyList()).anyTimes();
+        expect(bundleRevision.getRequirements(null)).andReturn(Collections.emptyList()).anyTimes();
+        EasyMock.replay(installSupport, bundle, bundleRevision, bundleStartLevel);
+
+        featureService.updateFeaturesState(states, EnumSet.noneOf(Option.class));
+        assertSame(bundle, stoppedBundle.getValue());
+    }
+
     @Test
     public void testRemoveRepo2() throws Exception {
         final FeaturesService featureService = createTestFeatureService();