Author: cschneider
Date: Sun Sep 30 10:14:57 2012
New Revision: 1391978

URL: http://svn.apache.org/viewvc?rev=1391978&view=rev
Log:
KARAF-608 BundleManager independent of InstallationState, simplify tests

Added:
    
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/TestBase.java
   (with props)
Modified:
    
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
    
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
    
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
    
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/BundleManagerTest.java
    
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java

Modified: 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java?rev=1391978&r1=1391977&r2=1391978&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
 (original)
+++ 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/BundleManager.java
 Sun Sep 30 10:14:57 2012
@@ -192,19 +192,19 @@ public class BundleManager {
         }
     }
     
-    protected Set<Bundle> findBundlesToRefresh(InstallationState state) {
+    protected Set<Bundle> findBundlesToRefresh(Set<Bundle> existing, 
Set<Bundle> installed) {
         Set<Bundle> bundles = new HashSet<Bundle>();
-        bundles.addAll(findBundlesWithOptionalPackagesToRefresh(state));
-        bundles.addAll(findBundlesWithFragmentsToRefresh(state));
+        bundles.addAll(findBundlesWithOptionalPackagesToRefresh(existing, 
installed));
+        bundles.addAll(findBundlesWithFragmentsToRefresh(existing, installed));
         return bundles;
     }
 
-    protected Set<Bundle> findBundlesWithFragmentsToRefresh(InstallationState 
state) {
+    protected Set<Bundle> findBundlesWithFragmentsToRefresh(Set<Bundle> 
existing, Set<Bundle> installed) {
         Set<Bundle> bundles = new HashSet<Bundle>();
-        Set<Bundle> oldBundles = new HashSet<Bundle>(state.bundles);
-        oldBundles.removeAll(state.installed);
+        Set<Bundle> oldBundles = new HashSet<Bundle>(existing);
+        oldBundles.removeAll(installed);
         if (!oldBundles.isEmpty()) {
-            for (Bundle b : state.installed) {
+            for (Bundle b : installed) {
                 String hostHeader = (String) 
b.getHeaders().get(Constants.FRAGMENT_HOST);
                 if (hostHeader != null) {
                     Clause[] clauses = Parser.parseHeader(hostHeader);
@@ -230,10 +230,10 @@ public class BundleManager {
         return bundles;
     }
 
-    protected Set<Bundle> 
findBundlesWithOptionalPackagesToRefresh(InstallationState state) {
+    protected Set<Bundle> findBundlesWithOptionalPackagesToRefresh(Set<Bundle> 
existing, Set<Bundle> installed) {
         // First pass: include all bundles contained in these features
-        Set<Bundle> bundles = new HashSet<Bundle>(state.bundles);
-        bundles.removeAll(state.installed);
+        Set<Bundle> bundles = new HashSet<Bundle>(existing);
+        bundles.removeAll(installed);
         if (bundles.isEmpty()) {
             return bundles;
         }
@@ -255,7 +255,7 @@ public class BundleManager {
         // Third pass: compute a list of packages that are exported by our 
bundles and see if
         //             some exported packages can be wired to the optional 
imports
         List<Clause> exports = new ArrayList<Clause>();
-        for (Bundle b : state.installed) {
+        for (Bundle b : installed) {
             String exportsStr = (String) 
b.getHeaders().get(Constants.EXPORT_PACKAGE);
             if (exportsStr != null) {
                 Clause[] exportsList = Parser.parseHeader(exportsStr);
@@ -335,7 +335,7 @@ public class BundleManager {
         }
     }
     
-    void uninstallBundles(Set<Long> bundles) throws BundleException, 
InterruptedException {
+    public void uninstallBundles(Set<Long> bundles) throws BundleException, 
InterruptedException {
         for (long bundleId : bundles) {
             Bundle b = bundleContext.getBundle(bundleId);
             if (b != null) {
@@ -367,11 +367,11 @@ public class BundleManager {
         return new ServiceTracker(bundleContext, 
FrameworkUtil.createFilter(filter), null);
     }
 
-    void refreshBundles(InstallationState state, EnumSet<Option> options) {
+    public void refreshBundles(Set<Bundle> existing, Set<Bundle> installed, 
EnumSet<Option> options) {
         boolean print = options.contains(Option.PrintBundlesToRefresh);
         boolean refresh = !options.contains(Option.NoAutoRefreshBundles);
         if (print || refresh) {
-            Set<Bundle> bundlesToRefresh = findBundlesToRefresh(state);
+            Set<Bundle> bundlesToRefresh = findBundlesToRefresh(existing, 
installed);
             StringBuilder sb = new StringBuilder();
             for (Bundle b : bundlesToRefresh) {
                 if (sb.length() > 0) {

Modified: 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java?rev=1391978&r1=1391977&r2=1391978&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
 (original)
+++ 
karaf/trunk/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
 Sun Sep 30 10:14:57 2012
@@ -395,7 +395,7 @@ public class FeaturesServiceImpl impleme
                     }
                }
             }
-            bundleManager.refreshBundles(state, options);
+            bundleManager.refreshBundles(state.bundles, state.installed, 
options);
             // Start all bundles
             for (Bundle b : state.bundles) {
                 LOGGER.info("Starting bundle: {}", b.getSymbolicName());

Modified: 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java?rev=1391978&r1=1391977&r2=1391978&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
 (original)
+++ 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/FeaturesServiceTest.java
 Sun Sep 30 10:14:57 2012
@@ -22,6 +22,11 @@ import static org.easymock.EasyMock.isA;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.reset;
 import static org.easymock.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.FileWriter;
@@ -35,21 +40,25 @@ import java.util.Collections;
 import java.util.EnumSet;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
 import java.util.concurrent.CopyOnWriteArraySet;
 
-import junit.framework.TestCase;
-
 import org.apache.karaf.features.internal.BundleManager;
+import org.apache.karaf.features.internal.BundleManager.BundleInstallerResult;
 import org.apache.karaf.features.internal.FeaturesServiceImpl;
+import org.apache.karaf.features.internal.TestBase;
 import org.easymock.EasyMock;
 import org.junit.Assert;
+import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.wiring.FrameworkWiring;
 import org.osgi.service.blueprint.container.BlueprintContainer;
 import org.slf4j.LoggerFactory;
 
-public class FeaturesServiceTest extends TestCase {
+public class FeaturesServiceTest extends TestBase {
 
     File dataFile;
 
@@ -57,6 +66,7 @@ public class FeaturesServiceTest extends
         dataFile = File.createTempFile("features", null, null);
     }
 
+    @Test
     public void testInstallFeature() throws Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -72,19 +82,38 @@ public class FeaturesServiceTest extends
 
         URI uri = tmp.toURI();
 
-        BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
-        Bundle installedBundle = EasyMock.createMock(Bundle.class);
-        // required since the sorted set uses it
-        
expect(installedBundle.compareTo(EasyMock.<Bundle>anyObject())).andReturn(0).anyTimes();
-
-        
expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
+        BundleManager bundleManager = EasyMock.createMock(BundleManager.class);
+        Bundle installedBundle = createDummyBundle(12345L, name, headers());
+        FeaturesServiceImpl svc = testAddRepository(name, tmp, uri, 
bundleManager, installedBundle);
+        
+        reset(bundleManager);
+        
+        expect(bundleManager.installBundleIfNeeded(eq(name), eq(0), 
eq((String)null))).andReturn(new BundleInstallerResult(installedBundle, true));
+        TreeSet<Bundle> existing = new 
TreeSet<Bundle>(Arrays.asList(installedBundle));
+        bundleManager.refreshBundles(eq(existing), eq(existing), 
eq(EnumSet.of(FeaturesService.Option.NoAutoRefreshBundles)));
+        EasyMock.expectLastCall();
+        
expect(bundleManager.getDataFile(EasyMock.anyObject(String.class))).andReturn(tmp);
+        
+        replay(bundleManager);
+        svc.installFeature("f1", 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION, 
EnumSet.of(FeaturesService.Option.NoAutoRefreshBundles));
+        verify(bundleManager);
+        
+        Feature[] installed = svc.listInstalledFeatures();
+        assertEquals(1, installed.length);
+        assertEquals("f1", installed[0].getName());
+    }
 
-        replay(bundleContext, installedBundle);
+    private FeaturesServiceImpl testAddRepository(String name, File tmp, URI 
uri, BundleManager bundleManager,
+            Bundle installedBundle) throws IOException, BundleException, 
Exception {
+        
expect(bundleManager.getDataFile(EasyMock.anyObject(String.class))).andReturn(tmp);
+        expect(bundleManager.installBundleIfNeeded(eq(name), eq(0), 
eq((String)null))).andReturn(new BundleInstallerResult(installedBundle, 
true)).anyTimes();
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl(new 
BundleManager(bundleContext));
+        replay(bundleManager);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager);
         svc.addRepository(uri);
-        
         Repository[] repositories = svc.listRepositories();
+        verify(bundleManager);
+
         assertNotNull(repositories);
         assertEquals(1, repositories.length);
         assertNotNull(repositories[0]);
@@ -99,37 +128,11 @@ public class FeaturesServiceTest extends
         assertEquals(1, features[0].getBundles().size());
         assertEquals(name, features[0].getBundles().get(0).getLocation());
         assertTrue(features[0].getBundles().get(0).isStart());
-
-        verify(bundleContext, installedBundle);
-
-        reset(bundleContext, installedBundle);
-
-        // required since the sorted set uses it
-        
expect(installedBundle.compareTo(EasyMock.<Bundle>anyObject())).andReturn(0).anyTimes();
-
-        
expect(bundleContext.createFilter(EasyMock.<String>anyObject())).andReturn(null).anyTimes();
-        expect(installedBundle.getSymbolicName()).andReturn(name).anyTimes();
-        expect(bundleContext.getBundles()).andReturn(new Bundle[0]);
-        expect(bundleContext.installBundle(isA(String.class),
-                                           
isA(InputStream.class))).andReturn(installedBundle);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(bundleContext.getBundle(12345L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
-        installedBundle.start();
-
-        
expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
-
-        replay(bundleContext, installedBundle);
-
-        svc.installFeature("f1", 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION, 
EnumSet.of(FeaturesService.Option.NoAutoRefreshBundles));
-        
-        Feature[] installed = svc.listInstalledFeatures();
-        assertEquals(1, installed.length);
-        assertEquals("f1", installed[0].getName());
+        return svc;
     }
 
+    @SuppressWarnings("unchecked")
+    @Test
     public void testUninstallFeature() throws Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -148,65 +151,21 @@ public class FeaturesServiceTest extends
 
         URI uri = tmp.toURI();
         
-        BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
-        Bundle installedBundle = EasyMock.createMock(Bundle.class);
-        Bundle framework = EasyMock.createMock(Bundle.class);
-        
expect(installedBundle.compareTo(EasyMock.<Bundle>anyObject())).andReturn(0).anyTimes();
-
-        // required since the sorted set uses it
-        
expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
-
-        replay(bundleContext, installedBundle, framework);
+        Bundle installedBundle = createDummyBundle(12345L, name, headers());
 
-        FeaturesServiceImpl svc = new FeaturesServiceImpl(new 
BundleManager(bundleContext));
+        BundleManager bundleManager = EasyMock.createMock(BundleManager.class);
+        
expect(bundleManager.getDataFile(EasyMock.anyObject(String.class))).andReturn(tmp).anyTimes();
+        expect(bundleManager.installBundleIfNeeded(name, 0, 
null)).andReturn(new BundleInstallerResult(installedBundle, true));
+        expect(bundleManager.installBundleIfNeeded(name, 0, 
null)).andReturn(new BundleInstallerResult(installedBundle, false));
+        bundleManager.refreshBundles(EasyMock.anyObject(Set.class), 
EasyMock.anyObject(Set.class), EasyMock.anyObject(EnumSet.class));
+        EasyMock.expectLastCall().anyTimes();
+        bundleManager.uninstallBundles(EasyMock.anyObject(Set.class));
+        EasyMock.expectLastCall().times(2);
+        
+        replay(bundleManager);
+        FeaturesServiceImpl svc = new FeaturesServiceImpl(bundleManager);
         svc.addRepository(uri);
 
-        verify(bundleContext, installedBundle, framework);
-
-        reset(bundleContext, installedBundle, framework);
-
-        // required since the sorted set uses it
-        
expect(installedBundle.compareTo(EasyMock.<Bundle>anyObject())).andReturn(0).anyTimes();
-
-        // Installs f1 and 0.1
-        
expect(bundleContext.createFilter(EasyMock.<String>anyObject())).andReturn(null).anyTimes();
-        expect(installedBundle.getSymbolicName()).andReturn(name).anyTimes();
-        expect(bundleContext.getBundles()).andReturn(new Bundle[0]);
-        expect(bundleContext.installBundle(isA(String.class),
-                                           
isA(InputStream.class))).andReturn(installedBundle);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(installedBundle.getBundleId()).andReturn(12345L);
-        expect(bundleContext.getBundle(12345L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
-        installedBundle.start();
-
-        // Installs f1 and 0.2
-        expect(bundleContext.getBundles()).andReturn(new Bundle[0]);
-        expect(bundleContext.installBundle(isA(String.class),
-                                           
isA(InputStream.class))).andReturn(installedBundle);
-        expect(installedBundle.getBundleId()).andReturn(123456L);
-        expect(installedBundle.getBundleId()).andReturn(123456L);
-        expect(installedBundle.getBundleId()).andReturn(123456L);
-        expect(bundleContext.getBundle(123456L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
-        installedBundle.start();
-
-        // UnInstalls f1 and 0.1
-        expect(bundleContext.getBundle(12345)).andReturn(installedBundle);
-        installedBundle.uninstall();
-
-        // UnInstalls f1 and 0.2
-        expect(bundleContext.getBundle(123456)).andReturn(installedBundle);
-        installedBundle.uninstall();
-
-        
expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
-
-        expect(bundleContext.getBundle()).andReturn(framework).anyTimes();
-        
expect(framework.adapt(FrameworkWiring.class)).andReturn(null).anyTimes();
-
-        replay(bundleContext, installedBundle, framework);
-
         try {
             svc.uninstallFeature("f1");
             fail("Uninstall should have failed as feature is not installed");
@@ -229,6 +188,7 @@ public class FeaturesServiceTest extends
     }    
     
     // Tests Add and Remove Repository
+    @Test
     public void testAddAndRemoveRepository() throws Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -266,6 +226,7 @@ public class FeaturesServiceTest extends
     // with a feature dependency
     // The dependant feature is in the same repository
     // Tests uninstall of features
+    @Test
     public void testInstallFeatureWithDependantFeatures() throws Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -301,7 +262,7 @@ public class FeaturesServiceTest extends
         expect(installedBundle.getBundleId()).andReturn(12345L);
         expect(installedBundle.getBundleId()).andReturn(12345L);
         expect(bundleContext.getBundle(12345L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
+        expect(installedBundle.getHeaders()).andReturn(new Hashtable<String, 
String>());
         installedBundle.start();
 
         // Then installs f1
@@ -314,7 +275,7 @@ public class FeaturesServiceTest extends
         expect(installedBundle.getBundleId()).andReturn(1234L);
         expect(installedBundle.getBundleId()).andReturn(1234L);
         expect(bundleContext.getBundle(1234L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new 
Hashtable()).anyTimes();
+        expect(installedBundle.getHeaders()).andReturn(new Hashtable<String, 
String>()).anyTimes();
         installedBundle.start();
 
         // uninstalls first feature name = f1, version = 0.1
@@ -344,6 +305,7 @@ public class FeaturesServiceTest extends
     }
 
     // Tests install of a Repository that includes a feature with a feature 
dependency
+    @Test
     public void 
testInstallFeatureWithDependantFeaturesAndVersionWithoutPreinstall() throws 
Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -378,6 +340,7 @@ public class FeaturesServiceTest extends
     }
 
     // Tests install of a Repository that includes a feature with a feature 
dependency
+    @Test
     public void 
testInstallFeatureWithDependantFeaturesAndNoVersionWithoutPreinstall() throws 
Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -411,6 +374,7 @@ public class FeaturesServiceTest extends
         svc.uninstallFeature("f2", "0.2");
     }
 
+    @Test
     public void 
testInstallFeatureWithDependantFeaturesAndRangeWithoutPreinstall() throws 
Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -444,6 +408,7 @@ public class FeaturesServiceTest extends
         svc.uninstallFeature("f2", "0.2");
     }
 
+    @Test
     public void 
testInstallFeatureWithDependantFeaturesAndRangeWithPreinstall() throws 
Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -479,7 +444,7 @@ public class FeaturesServiceTest extends
                                            
isA(InputStream.class))).andReturn(installedBundle);
         expect(installedBundle.getBundleId()).andReturn(12345L).anyTimes();
         
expect(bundleContext.getBundle(12345L)).andReturn(installedBundle).anyTimes();
-        expect(installedBundle.getHeaders()).andReturn(new 
Hashtable()).anyTimes();
+        expect(installedBundle.getHeaders()).andReturn(new Hashtable<String, 
String>()).anyTimes();
         installedBundle.start();
 
         expect(bundleContext.getBundles()).andReturn(new Bundle[] { 
installedBundle });
@@ -509,6 +474,7 @@ public class FeaturesServiceTest extends
         svc.uninstallFeature("f2", "0.1");
     }
 
+    @Test
     public void testGetFeaturesShouldHandleDifferentVersionPatterns() throws 
Exception {
 
         String name = getJarUrl(BlueprintContainer.class);
@@ -571,7 +537,7 @@ public class FeaturesServiceTest extends
         expect(installedBundle.getBundleId()).andReturn(12345L);
         expect(installedBundle.getBundleId()).andReturn(12345L);
         expect(bundleContext.getBundle(12345L)).andReturn(installedBundle);
-        expect(installedBundle.getHeaders()).andReturn(new Hashtable());
+        expect(installedBundle.getHeaders()).andReturn(new Hashtable<String, 
String>());
         installedBundle.start();
 
         // uninstalls first feature name = f2, version = 0.1
@@ -589,6 +555,7 @@ public class FeaturesServiceTest extends
         return bundleContext;
     }
 
+    @Test
     public void testInstallBatchFeatureWithContinueOnFailureNoClean() throws 
Exception {
         String bundle1 = getJarUrl(BlueprintContainer.class);
         String bundle2 = getJarUrl(LoggerFactory.class);
@@ -629,7 +596,7 @@ public class FeaturesServiceTest extends
         expect(installedBundle2.getBundleId()).andReturn(54321L);
         expect(installedBundle2.getBundleId()).andReturn(54321L);
         expect(installedBundle2.getBundleId()).andReturn(54321L);
-        expect(installedBundle2.getHeaders()).andReturn(new 
Hashtable()).anyTimes();
+        expect(installedBundle2.getHeaders()).andReturn(new Hashtable<String, 
String>()).anyTimes();
         installedBundle2.start();
 
         
expect(bundleContext.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
@@ -647,6 +614,7 @@ public class FeaturesServiceTest extends
 //        verify(preferencesService, prefs, repositoriesNode, featuresNode, 
bundleContext, installedBundle1, installedBundle2);
     }
 
+    @Test
     public void testInstallBatchFeatureWithContinueOnFailureClean() throws 
Exception {
         String bundle1 = getJarUrl(BlueprintContainer.class);
         String bundle2 = getJarUrl(LoggerFactory.class);
@@ -689,7 +657,7 @@ public class FeaturesServiceTest extends
         expect(installedBundle2.getBundleId()).andReturn(54321L);
         expect(installedBundle2.getBundleId()).andReturn(54321L);
         expect(installedBundle2.getBundleId()).andReturn(54321L);
-        expect(installedBundle2.getHeaders()).andReturn(new 
Hashtable()).anyTimes();
+        expect(installedBundle2.getHeaders()).andReturn(new Hashtable<String, 
String>()).anyTimes();
         
expect(installedBundle2.getSymbolicName()).andReturn("bundle2").anyTimes();
         installedBundle2.start();
 
@@ -706,6 +674,7 @@ public class FeaturesServiceTest extends
 //        verify(preferencesService, prefs, repositoriesNode, featuresNode, 
bundleContext, installedBundle1, installedBundle2);
     }
 
+    @Test
     public void testInstallBatchFeatureWithoutContinueOnFailureNoClean() 
throws Exception {
         String bundle1 = getJarUrl(BlueprintContainer.class);
         String bundle2 = getJarUrl(LoggerFactory.class);
@@ -767,6 +736,7 @@ public class FeaturesServiceTest extends
 //        verify(preferencesService, prefs, repositoriesNode, featuresNode, 
bundleContext, installedBundle1, installedBundle2);
     }
 
+    @Test
     public void testInstallBatchFeatureWithoutContinueOnFailureClean() throws 
Exception {
         String bundle1 = getJarUrl(BlueprintContainer.class);
         String bundle2 = getJarUrl(LoggerFactory.class);
@@ -832,6 +802,7 @@ public class FeaturesServiceTest extends
     /**
      * This test checks schema validation of submited uri.
      */
+    @Test
     public void testSchemaValidation() throws Exception {
         File tmp = File.createTempFile("karaf", ".feature");
         PrintWriter pw = new PrintWriter(new FileWriter(tmp));
@@ -864,6 +835,7 @@ public class FeaturesServiceTest extends
     /**
      * This test checks feature service behavior with old, non namespaced 
descriptor.
      */
+    @Test
     public void testLoadOldFeatureFile() throws Exception {
         String bundle1 = getJarUrl(BlueprintContainer.class);
         String bundle2 = getJarUrl(LoggerFactory.class);
@@ -892,7 +864,7 @@ public class FeaturesServiceTest extends
         Assert.assertEquals(2, bundles.size());
     }
 
-    private String getJarUrl(Class cl) {
+    private String getJarUrl(Class<?> cl) {
         String name = cl.getName();
         name = name.replace(".", "/")  + ".class";
         name = getClass().getClassLoader().getResource(name).toString();

Modified: 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/BundleManagerTest.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/BundleManagerTest.java?rev=1391978&r1=1391977&r2=1391978&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/BundleManagerTest.java
 (original)
+++ 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/BundleManagerTest.java
 Sun Sep 30 10:14:57 2012
@@ -16,11 +16,10 @@
  */
 package org.apache.karaf.features.internal;
 
-import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
 
-import java.util.Dictionary;
-import java.util.Hashtable;
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Set;
 
 import junit.framework.Assert;
@@ -31,39 +30,45 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 
-public class BundleManagerTest {
-
-    public Bundle createDummyBundle(long id, String symbolicName, 
Dictionary<String,String> headers) {
-        Bundle bundle = EasyMock.createNiceMock(Bundle.class);
-        expect(bundle.getBundleId()).andReturn(id).anyTimes();
-        expect(bundle.getSymbolicName()).andReturn(symbolicName);
-        expect(bundle.getHeaders()).andReturn(headers).anyTimes();
-        replay(bundle);
-        return bundle;
-    }
+public class BundleManagerTest extends TestBase {
     
     @Test
     public void testfindBundlestoRefreshWithHostToRefresh() throws Exception {
-        Bundle hostBundle = createDummyBundle(12345l, "Host", new 
Hashtable<String, String>());
-        
-        Hashtable<String, String> d = new Hashtable<String, String>();
-        d.put(Constants.FRAGMENT_HOST, "Host");
-        Bundle fragmentBundle = createDummyBundle(54321l, "fragment", d);
+        Bundle hostBundle = createDummyBundle(12345l, "Host", headers());
+        Bundle fragmentBundle = createDummyBundle(54321l, "fragment", 
headers(Constants.FRAGMENT_HOST, "Host"));
 
         BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
         BundleManager bundleManager = new BundleManager(bundleContext);
 
         // Host was already installed, fragment is new
-        InstallationState state = new InstallationState();
-        state.bundles.add(hostBundle);
-        state.bundles.add(fragmentBundle);
-        state.installed.add(fragmentBundle);
+        Set<Bundle> existing = new HashSet<Bundle>(Arrays.asList(hostBundle, 
fragmentBundle));
+        Set<Bundle> installed = new 
HashSet<Bundle>(Arrays.asList(fragmentBundle));
         
         replay(bundleContext);
-        Set<Bundle> bundles = bundleManager.findBundlesToRefresh(state);
+        Set<Bundle> bundles = 
bundleManager.findBundlesWithFragmentsToRefresh(existing, installed);
         EasyMock.verify(bundleContext);
 
         Assert.assertEquals(1, bundles.size());
         Assert.assertEquals(hostBundle, bundles.iterator().next());
-    } 
+    }
+    
+    @Test
+    public void testfindBundlestoRefreshWithOptionalPackages() throws 
Exception {
+        Bundle exporterBundle = createDummyBundle(12345l, "exporter", 
headers(Constants.EXPORT_PACKAGE, "org.my.package"));
+        Bundle importerBundle = createDummyBundle(54321l, "importer", 
headers(Constants.IMPORT_PACKAGE, "org.my.package;resolution:=optional"));
+
+        BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
+        BundleManager bundleManager = new BundleManager(bundleContext);
+
+        // Importer was already installed, exporter is new
+        Set<Bundle> existing = new 
HashSet<Bundle>(Arrays.asList(importerBundle, exporterBundle));
+        Set<Bundle> installed = new 
HashSet<Bundle>(Arrays.asList(exporterBundle));
+        
+        replay(bundleContext);
+        Set<Bundle> bundles = 
bundleManager.findBundlesWithOptionalPackagesToRefresh(existing, installed);
+        EasyMock.verify(bundleContext);
+
+        Assert.assertEquals(1, bundles.size());
+        Assert.assertEquals(importerBundle, bundles.iterator().next());
+    }
 }

Modified: 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java?rev=1391978&r1=1391977&r2=1391978&view=diff
==============================================================================
--- 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java
 (original)
+++ 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/FeaturesServiceImplTest.java
 Sun Sep 30 10:14:57 2012
@@ -18,88 +18,85 @@ package org.apache.karaf.features.intern
 
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.EnumSet;
-import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Hashtable;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import junit.framework.TestCase;
-
 import org.apache.felix.utils.manifest.Clause;
 import org.apache.karaf.features.Feature;
 import org.apache.karaf.features.internal.BundleManager.BundleInstallerResult;
 import org.easymock.EasyMock;
-import org.osgi.framework.Bundle;
+import org.junit.Before;
+import org.junit.Test;
 
 /**
  * Test cases for {@link FeaturesServiceImpl}
  */
-public class FeaturesServiceImplTest extends TestCase {
+public class FeaturesServiceImplTest extends TestBase {
     
     File dataFile;
 
-    protected void setUp() throws IOException {
+    @Before
+    public void setUp() throws IOException {
         dataFile = File.createTempFile("features", null, null);
     }
 
+    @Test
     public void testGetFeature() throws Exception {
-        final Map<String, Map<String, Feature>> features = new HashMap<String, 
Map<String,Feature>>();
-        Map<String, Feature> versions = new HashMap<String, Feature>();
-        org.apache.karaf.features.internal.model.Feature feature = new 
org.apache.karaf.features.internal.model.Feature("transaction");
-        versions.put("1.0.0", feature);
-        features.put("transaction", versions);
+        Feature transactionFeature = feature("transaction", "1.0.0");
+        final Map<String, Map<String, Feature>> features = 
features(transactionFeature);
         final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws 
Exception {
                 return features;
             };
         };
         assertNotNull(impl.getFeature("transaction", 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION));
-        assertSame(feature, impl.getFeature("transaction", 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION));
+        assertSame(transactionFeature, impl.getFeature("transaction", 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION));
     }
     
+    @Test
     public void testGetFeatureStripVersion() throws Exception {
-        final Map<String, Map<String, Feature>> features = new HashMap<String, 
Map<String,Feature>>();
-        Map<String, Feature> versions = new HashMap<String, Feature>();
-        org.apache.karaf.features.internal.model.Feature feature = new 
org.apache.karaf.features.internal.model.Feature("transaction");
-        versions.put("1.0.0", feature);
-        features.put("transaction", versions);
         final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws 
Exception {
-                return features;
+                return features(feature("transaction", "1.0.0"));
             };
         };
-        assertNotNull(impl.getFeature("transaction", "  1.0.0  "));
-        assertSame(feature, impl.getFeature("transaction", "  1.0.0   "));
+        Feature feature = impl.getFeature("transaction", "  1.0.0  ");
+        assertNotNull(feature);
+        assertSame("transaction", feature.getName());
     }
     
+    @Test
     public void testGetFeatureNotAvailable() throws Exception {
-        final Map<String, Map<String, Feature>> features = new HashMap<String, 
Map<String,Feature>>();
-        Map<String, Feature> versions = new HashMap<String, Feature>();
-        versions.put("1.0.0", new 
org.apache.karaf.features.internal.model.Feature("transaction"));
-        features.put("transaction", versions);
         final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws 
Exception {
-                return features;
+                return features(feature("transaction", "1.0.0"));
             };
         };
         assertNull(impl.getFeature("activemq", 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION));
     }
     
+    @Test
     public void testGetFeatureHighestAvailable() throws Exception {
-        final Map<String, Map<String, Feature>> features = new HashMap<String, 
Map<String,Feature>>();
-        Map<String, Feature> versions = new HashMap<String, Feature>();
-        versions.put("1.0.0", new 
org.apache.karaf.features.internal.model.Feature("transaction", "1.0.0"));
-        versions.put("2.0.0", new 
org.apache.karaf.features.internal.model.Feature("transaction", "2.0.0"));
-        features.put("transaction", versions);
+        final Map<String, Map<String, Feature>> features = features(
+                feature("transaction", "1.0.0"),
+                feature("transaction", "2.0.0")
+        );
         final FeaturesServiceImpl impl = new FeaturesServiceImpl(null, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws 
Exception {
                 return features;
@@ -109,6 +106,7 @@ public class FeaturesServiceImplTest ext
         assertSame("2.0.0", impl.getFeature("transaction", 
org.apache.karaf.features.internal.model.Feature.DEFAULT_VERSION).getVersion());
     }
 
+    @Test
     public void testStartDoesNotFailWithOneInvalidUri()  {
         BundleManager bundleManager = EasyMock.createMock(BundleManager.class);
         
expect(bundleManager.getDataFile(EasyMock.<String>anyObject())).andReturn(dataFile).anyTimes();
@@ -126,22 +124,19 @@ public class FeaturesServiceImplTest ext
     /**
      * This test checks KARAF-388 which allows you to specify version of boot 
feature.
      */
+    @Test
     @SuppressWarnings("unchecked")
     public void testStartDoesNotFailWithNonExistentVersion()  {
         BundleManager bundleManager = EasyMock.createMock(BundleManager.class);
         
expect(bundleManager.createAndRegisterEventAdminListener()).andReturn(null);
-        
bundleManager.refreshBundles(EasyMock.anyObject(InstallationState.class), 
EasyMock.anyObject(EnumSet.class));
+        bundleManager.refreshBundles(EasyMock.anyObject(Set.class), 
EasyMock.anyObject(Set.class), EasyMock.anyObject(EnumSet.class));
         EasyMock.expectLastCall().anyTimes();
 
-        final Map<String, Map<String, Feature>> features = new HashMap<String, 
Map<String,Feature>>();
-        Map<String, Feature> versions = new HashMap<String, Feature>();
-        versions.put("1.0.0", new 
org.apache.karaf.features.internal.model.Feature("transaction", "1.0.0"));
-        versions.put("2.0.0", new 
org.apache.karaf.features.internal.model.Feature("transaction", "2.0.0"));
-        features.put("transaction", versions);
-
-        Map<String, Feature> versions2 = new HashMap<String, Feature>();
-        versions2.put("1.0.0", new 
org.apache.karaf.features.internal.model.Feature("ssh", "1.0.0"));
-        features.put("ssh", versions2);
+        final Map<String, Map<String, Feature>> features = features(
+                feature("transaction", "1.0.0"),
+                feature("transaction", "2.0.0"),
+                feature("ssh", "1.0.0")
+        );
 
         final FeaturesServiceImpl impl = new 
FeaturesServiceImpl(bundleManager, null) {
             protected Map<String,Map<String,Feature>> getFeatures() throws 
Exception {
@@ -173,26 +168,18 @@ public class FeaturesServiceImplTest ext
         
     }
     
-    public Bundle createDummyBundle(long id, String symbolicName) {
-        Bundle bundle = EasyMock.createNiceMock(Bundle.class);
-        expect(bundle.getBundleId()).andReturn(id).anyTimes();
-        expect(bundle.getSymbolicName()).andReturn(symbolicName);
-        expect(bundle.getHeaders()).andReturn(new Hashtable<String, String>());
-        replay(bundle);
-        return bundle;
-    }
-
     /**
      * This test ensures that every feature get installed only once, even if 
it appears multiple times in the list
      * of transitive feature dependencies (KARAF-1600)
      */
+    @Test
     @SuppressWarnings("unchecked")
     public void testNoDuplicateFeaturesInstallation() throws Exception {
         final List<Feature> installed = new LinkedList<Feature>();
         BundleManager bundleManager = EasyMock.createMock(BundleManager.class);
         
expect(bundleManager.installBundleIfNeeded(EasyMock.anyObject(String.class), 
EasyMock.anyInt(), EasyMock.anyObject(String.class)))
-            .andReturn(new BundleInstallerResult(createDummyBundle(1l, ""), 
true)).anyTimes();
-        
bundleManager.refreshBundles(EasyMock.anyObject(InstallationState.class), 
EasyMock.anyObject(EnumSet.class));
+            .andReturn(new BundleInstallerResult(createDummyBundle(1l, "", 
headers()), true)).anyTimes();
+        bundleManager.refreshBundles(EasyMock.anyObject(Set.class), 
EasyMock.anyObject(Set.class), EasyMock.anyObject(EnumSet.class));
         EasyMock.expectLastCall();
         final FeaturesServiceImpl impl = new 
FeaturesServiceImpl(bundleManager, null) {
             // override methods which refers to bundle context to avoid 
mocking everything
@@ -225,6 +212,7 @@ public class FeaturesServiceImplTest ext
         assertEquals("Every feature should only have been installed once", 
installed.size(), noduplicates.size());
     }
 
+    @Test
     public void testGetOptionalImportsOnly() {
         BundleManager bundleManager = new BundleManager(null, null, 0l);
 

Added: 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/TestBase.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/TestBase.java?rev=1391978&view=auto
==============================================================================
--- 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/TestBase.java
 (added)
+++ 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/TestBase.java
 Sun Sep 30 10:14:57 2012
@@ -0,0 +1,62 @@
+package org.apache.karaf.features.internal;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.karaf.features.Feature;
+import org.easymock.EasyMock;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.startlevel.BundleStartLevel;
+
+public class TestBase {
+    public Bundle createDummyBundle(long id, final String symbolicName, 
Dictionary<String,String> headers) {
+        Bundle bundle = EasyMock.createNiceMock(Bundle.class);
+        expect(bundle.getBundleId()).andReturn(id).anyTimes();
+        expect(bundle.getSymbolicName()).andReturn(symbolicName);
+        expect(bundle.getHeaders()).andReturn(headers).anyTimes();
+        BundleStartLevel sl = EasyMock.createMock(BundleStartLevel.class);
+        expect(sl.isPersistentlyStarted()).andReturn(true);
+        expect(bundle.adapt(BundleStartLevel.class)).andReturn(sl );
+        replay(bundle);
+        return bundle;
+    }
+    
+    public Dictionary<String, String> headers(String ... keyAndHeader) {
+        Hashtable<String, String> headersTable = new Hashtable<String, 
String>();
+        int c=0;
+        while (c < keyAndHeader.length) {
+            String key = keyAndHeader[c++];
+            String value = keyAndHeader[c++];
+            headersTable.put(key, value);
+        }
+        return headersTable;
+        
+    }
+    
+    public Map<String, Map<String, Feature>> features(Feature ... features) {
+        final Map<String, Map<String, Feature>> featuresMap = new 
HashMap<String, Map<String,Feature>>();
+        for (Feature feature : features) {
+            Map<String, Feature> featureVersion = getOrCreate(featuresMap, 
feature);
+            featureVersion.put(feature.getVersion(), feature);
+        }
+        return featuresMap;
+    }
+    
+    private Map<String, Feature> getOrCreate(final Map<String, Map<String, 
Feature>> featuresMap, Feature feature) {
+        Map<String, Feature> featureVersion = 
featuresMap.get(feature.getName());
+        if (featureVersion == null) {
+            featureVersion = new HashMap<String, Feature>();
+            featuresMap.put(feature.getName(), featureVersion);
+        }
+        return featureVersion;
+    }
+
+    public Feature feature(String name, String version) {
+        return new org.apache.karaf.features.internal.model.Feature(name, 
version);
+    }
+}

Propchange: 
karaf/trunk/features/core/src/test/java/org/apache/karaf/features/internal/TestBase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to