incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1375092 - in /sling/trunk/installer/core/src: main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java
Date Mon, 20 Aug 2012 16:20:00 GMT
Author: bdelacretaz
Date: Mon Aug 20 16:19:59 2012
New Revision: 1375092

URL: http://svn.apache.org/viewvc?rev=1375092&view=rev
Log:
SLING-2567 - recursive test passes + logging added to RefreshDependenciesUtil

Modified:
    sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java
    sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java

Modified: sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java?rev=1375092&r1=1375091&r2=1375092&view=diff
==============================================================================
--- sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java
(original)
+++ sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java
Mon Aug 20 16:19:59 2012
@@ -18,7 +18,6 @@
  */
 package org.apache.sling.installer.core.impl.util;
 
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -34,6 +33,7 @@ import org.slf4j.LoggerFactory;
 /** Find out if a packages refresh will affect a given bundle */
 class RefreshDependenciesUtil {
 
+    private final Logger log = LoggerFactory.getLogger(getClass());
     private final PackageAdmin pckAdmin;
 
     public RefreshDependenciesUtil(final PackageAdmin pa) {
@@ -56,41 +56,51 @@ class RefreshDependenciesUtil {
      * Check whether a packages refresh of the supplied bundles would affect targetBundle
      */
     boolean isBundleAffected(Bundle targetBundle, final List<Bundle> bundles) {
-        // we put all bundle ids into a set
+        final Set<Long> checkedId = new HashSet<Long>();
+        final boolean result = isBundleAffected(checkedId, targetBundle, bundles);
+        log.debug("isBundleAffected({}, {}) -> {}", new Object[] { targetBundle, bundles,
result});
+        return result;
+    }
+    
+    private boolean isBundleAffected(Set<Long> checkedId, Bundle targetBundle, final
List<Bundle> bundles) {
+        
+        // Avoid cycles
+        if(checkedId.contains(targetBundle.getBundleId())) {
+            return false;
+        }
+        checkedId.add(targetBundle.getBundleId());
+        log.debug("Checking if {} is affected by {}", targetBundle, bundles);
+        
+        // ID of bundles that we check against
         final Set<Long> ids = new HashSet<Long>();
         for(final Bundle b : bundles) {
             ids.add(b.getBundleId());
         }
-
-        final Set<Long> processed = new HashSet<Long>();
-        final List<Bundle> toProcess = new ArrayList<Bundle>();
-        toProcess.add(targetBundle);
-        processed.add(targetBundle.getBundleId());
-
-        while ( !toProcess.isEmpty() ) {
-            final Bundle bundle = toProcess.remove(0);
-
-            if ( ids.contains(bundle.getBundleId()) ) {
-                return true;
-            }
-
-            for(final String name : this.getImportPackages(bundle) ) {
-
-                final ExportedPackage[] pcks = this.pckAdmin.getExportedPackages(name);
-                if ( pcks != null ) {
-                    for(final ExportedPackage pck : pcks) {
-                        final Bundle exportingBundle = pck.getExportingBundle();
-                        if ( exportingBundle.getBundleId() == 0 || exportingBundle.getBundleId()
== targetBundle.getBundleId() ) {
-                            continue;
-                        }
-                        if ( ids.contains(exportingBundle.getBundleId()) ) {
-                            return true;
-                        }
-                        if ( !processed.contains(exportingBundle.getBundleId())) {
-                            processed.add(exportingBundle.getBundleId());
-                            toProcess.add(exportingBundle);
-                        }
+        
+        // Find all bundles from which we import packages, return true if one of them is
in our bundles list,
+        // and call this method recursively on them as well.
+        // We don't care about possible duplicates in there, will be removed by the above
cycle avoidance code.
+        for(final String name : getImportPackages(targetBundle)) {
+            final ExportedPackage[] pcks = this.pckAdmin.getExportedPackages(name);
+            if ( pcks == null ) {
+                log.debug("No bundles present that export {}", name);
+            } else {
+                log.debug("{} imports {}", targetBundle, name);
+                for(final ExportedPackage pck : pcks) {
+                    final Bundle exportingBundle = pck.getExportingBundle();
+                    log.debug("Checking {} which exports {}", exportingBundle, pck.getName());
+                    if ( exportingBundle.getBundleId() == 0 || exportingBundle.getBundleId()
== targetBundle.getBundleId() ) {
+                        log.debug("Exporting bundle {} is framework bundle or self, ignored",
exportingBundle);
+                        continue;
+                    } else if ( ids.contains(exportingBundle.getBundleId()) ) {
+                        log.debug("Bundle {} affects {} due to package {}, returning true",

+                                new Object[] { exportingBundle, targetBundle, pck.getName()
});
+                        return true;
+                    } else if(isBundleAffected(checkedId, exportingBundle, bundles)) {
+                        log.debug("{} recursively affects {}, returning true", exportingBundle,
targetBundle); 
+                        return true;
                     }
+                    log.debug("{} does not affect {}", exportingBundle, targetBundle); 
                 }
             }
         }

Modified: sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java?rev=1375092&r1=1375091&r2=1375092&view=diff
==============================================================================
--- sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java
(original)
+++ sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java
Mon Aug 20 16:19:59 2012
@@ -29,7 +29,6 @@ import java.util.List;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
@@ -44,6 +43,7 @@ public class RefreshDependenciesUtilTest
     private Bundle B;
     private Bundle C;
     private Bundle D;
+    private Bundle E;
     private PackageAdmin pa;
     private RefreshDependenciesUtil rdu;
     private long counter = 1;
@@ -72,6 +72,8 @@ public class RefreshDependenciesUtilTest
             jmock.checking(new Expectations() {{
                 allowing(ep).getExportingBundle();
                 will(returnValue(result));
+                allowing(ep).getName();
+                will(returnValue(exportsPackages));
             }});
         }
             
@@ -90,13 +92,14 @@ public class RefreshDependenciesUtilTest
         rdu = new RefreshDependenciesUtil(pa);
         
         // Target depends on A directly and does not depend on B
-        target = setupBundle("target", "com.targetImportsOne;com.targetImportsTwo", null);
+        target = setupBundle("testTarget", "com.targetImportsOne;com.targetImportsTwo", null);
         A = setupBundle("A", null, "com.targetImportsOne");
         B = setupBundle("B", "some.import", "some.export");
         
         // Target depends on C which in turns depends on D
-        C = setupBundle("C", null, "com.targetImportsTwo");
-        D = setupBundle("D", null, null);
+        C = setupBundle("C", "com.CimportsOne", "com.targetImportsTwo");
+        D = setupBundle("D", null, "com.CimportsOne");
+        E = setupBundle("E", null, null);
     }
     
     @Test
@@ -114,6 +117,14 @@ public class RefreshDependenciesUtilTest
     }
     
     @Test
+    public void testTargetDoesNotDependOnBundleBorE() {
+        final List<Bundle> bundles = new ArrayList<Bundle>();
+        bundles.add(B);
+        bundles.add(E);
+        assertFalse(rdu.isBundleAffected(target, bundles));
+    }
+    
+    @Test
     public void testTargetDependsOnBundleC() {
         final List<Bundle> bundles = new ArrayList<Bundle>();
         bundles.add(C);
@@ -121,11 +132,21 @@ public class RefreshDependenciesUtilTest
     }
     
     @Test
-    @Ignore("SLING-2567 - transitive dependencies are not taken into account")
     public void testTargetDependsOnBundleD() {
         final List<Bundle> bundles = new ArrayList<Bundle>();
         bundles.add(D);
         assertTrue(rdu.isBundleAffected(target, bundles));
     }
-        
+    
+    @Test
+    public void testAllBundlesInList() {
+        final List<Bundle> bundles = new ArrayList<Bundle>();
+        bundles.add(A);
+        bundles.add(B);
+        bundles.add(C);
+        bundles.add(D);
+        bundles.add(E);
+        bundles.add(target);
+        assertTrue(rdu.isBundleAffected(target, bundles));
+    }
 }
\ No newline at end of file



Mime
View raw message