Author: bdelacretaz
Date: Mon Aug 20 15:42:33 2012
New Revision: 1375070
URL: http://svn.apache.org/viewvc?rev=1375070&view=rev
Log:
SLING-2567 - factor out RefreshDependenciesUtil and add tests - one of them
fails for now
Added:
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/
sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java
Modified:
sling/trunk/installer/core/pom.xml
sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/PABundleRefresher.java
Modified: sling/trunk/installer/core/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/installer/core/pom.xml?rev=1375070&r1=1375069&r2=1375070&view=diff
==============================================================================
--- sling/trunk/installer/core/pom.xml (original)
+++ sling/trunk/installer/core/pom.xml Mon Aug 20 15:42:33 2012
@@ -162,5 +162,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock</artifactId>
+ <version>2.5.1</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Modified:
sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/PABundleRefresher.java
URL:
http://svn.apache.org/viewvc/sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/PABundleRefresher.java?rev=1375070&r1=1375069&r2=1375070&view=diff
==============================================================================
---
sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/PABundleRefresher.java
(original)
+++
sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/PABundleRefresher.java
Mon Aug 20 15:42:33 2012
@@ -18,19 +18,13 @@
*/
package org.apache.sling.installer.core.impl.util;
-import java.util.ArrayList;
-import java.util.HashSet;
import java.util.List;
-import java.util.Set;
-import org.apache.sling.commons.osgi.ManifestHeader;
import org.apache.sling.installer.api.tasks.InstallationContext;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
-import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,7 +43,7 @@ public class PABundleRefresher implement
private volatile long refreshEventCount;
private final PackageAdmin pckAdmin;
-
+ private final RefreshDependenciesUtil rdu;
private final BundleContext bundleContext;
/** Lock object for syncing. */
@@ -58,15 +52,7 @@ public class PABundleRefresher implement
public PABundleRefresher(final PackageAdmin pa, final BundleContext
bundleContext) {
this.pckAdmin = pa;
this.bundleContext = bundleContext;
- }
-
- private Set<String> getImportPackages(final Bundle bundle) {
- final ManifestHeader header =
ManifestHeader.parse(bundle.getHeaders().get(Constants.IMPORT_PACKAGE).toString());
- final Set<String> packages = new HashSet<String>();
- for(final ManifestHeader.Entry entry : header.getEntries()) {
- packages.add(entry.getValue());
- }
- return packages;
+ this.rdu = new RefreshDependenciesUtil(pa);
}
/**
@@ -118,46 +104,7 @@ public class PABundleRefresher implement
* @see
org.apache.sling.installer.core.impl.util.BundleRefresher#isInstallerBundleAffected(java.util.List)
*/
public boolean isInstallerBundleAffected(final List<Bundle> bundles) {
- // we put all bundle ids into a set
- 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(this.bundleContext.getBundle());
- processed.add(this.bundleContext.getBundle().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() == this.bundleContext.getBundle().getBundleId() )
{
- continue;
- }
- if ( ids.contains(exportingBundle.getBundleId()) ) {
- return true;
- }
- if (
!processed.contains(exportingBundle.getBundleId())) {
- processed.add(exportingBundle.getBundleId());
- toProcess.add(exportingBundle);
- }
- }
- }
- }
- }
-
- return false;
+ return rdu.isBundleAffected(bundleContext.getBundle(), bundles);
}
/**
@@ -172,4 +119,4 @@ public class PABundleRefresher implement
}
}
}
-}
+}
\ No newline at end of file
Added:
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=1375070&view=auto
==============================================================================
---
sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java
(added)
+++
sling/trunk/installer/core/src/main/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtil.java
Mon Aug 20 15:42:33 2012
@@ -0,0 +1,100 @@
+/*
+ * 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.sling.installer.core.impl.util;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.sling.commons.osgi.ManifestHeader;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Find out if a packages refresh will affect a given bundle */
+class RefreshDependenciesUtil {
+
+ private final PackageAdmin pckAdmin;
+
+ public RefreshDependenciesUtil(final PackageAdmin pa) {
+ this.pckAdmin = pa;
+ }
+
+ private Set<String> getImportPackages(final Bundle bundle) {
+ final Set<String> packages = new HashSet<String>();
+ final String imports =
(String)bundle.getHeaders().get(Constants.IMPORT_PACKAGE);
+ if(imports != null) {
+ final ManifestHeader header = ManifestHeader.parse(imports);
+ for(final ManifestHeader.Entry entry : header.getEntries()) {
+ packages.add(entry.getValue());
+ }
+ }
+ return packages;
+ }
+
+ /**
+ * 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> 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);
+ }
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+}
\ No newline at end of file
Added:
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=1375070&view=auto
==============================================================================
---
sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java
(added)
+++
sling/trunk/installer/core/src/test/java/org/apache/sling/installer/core/impl/util/RefreshDependenciesUtilTest.java
Mon Aug 20 15:42:33 2012
@@ -0,0 +1,131 @@
+/*
+ * 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.sling.installer.core.impl.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+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;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+public class RefreshDependenciesUtilTest {
+
+ private Mockery jmock;
+ private Bundle target;
+ private Bundle A;
+ private Bundle B;
+ private Bundle C;
+ private Bundle D;
+ private PackageAdmin pa;
+ private RefreshDependenciesUtil rdu;
+ private long counter = 1;
+
+ private Bundle setupBundle(String mockName, String importPackages, final
String exportsPackages) {
+ final Bundle result = jmock.mock(Bundle.class, mockName);
+
+ final Dictionary<Object, Object> headers = new Hashtable<Object,
Object>();
+ if(importPackages != null) {
+ headers.put(Constants.IMPORT_PACKAGE, importPackages);
+ }
+
+ jmock.checking(new Expectations() {{
+ allowing(result).getBundleId();
+ will(returnValue(counter++));
+
+ allowing(result).getHeaders();
+ will(returnValue(headers));
+ }});
+
+ final List<ExportedPackage> eps = new ArrayList<ExportedPackage>();
+
+ if(exportsPackages != null) {
+ final ExportedPackage ep = jmock.mock(ExportedPackage.class,
"ExportedPackage" + mockName);
+ eps.add(ep);
+ jmock.checking(new Expectations() {{
+ allowing(ep).getExportingBundle();
+ will(returnValue(result));
+ }});
+ }
+
+ jmock.checking(new Expectations() {{
+ allowing(pa).getExportedPackages(exportsPackages);
+ will(returnValue(eps.toArray(new ExportedPackage[]{})));
+ }});
+
+ return result;
+ }
+
+ @Before
+ public void setup() {
+ jmock = new Mockery();
+ pa = jmock.mock(PackageAdmin.class);
+ rdu = new RefreshDependenciesUtil(pa);
+
+ // Target depends on A directly and does not depend on B
+ target = setupBundle("target",
"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);
+ }
+
+ @Test
+ public void testTargetDependsOnBundleA() {
+ final List<Bundle> bundles = new ArrayList<Bundle>();
+ bundles.add(A);
+ assertTrue(rdu.isBundleAffected(target, bundles));
+ }
+
+ @Test
+ public void testTargetDoesNotDependOnBundleB() {
+ final List<Bundle> bundles = new ArrayList<Bundle>();
+ bundles.add(B);
+ assertFalse(rdu.isBundleAffected(target, bundles));
+ }
+
+ @Test
+ public void testTargetDependsOnBundleC() {
+ final List<Bundle> bundles = new ArrayList<Bundle>();
+ bundles.add(C);
+ assertTrue(rdu.isBundleAffected(target, bundles));
+ }
+
+ @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));
+ }
+
+}
\ No newline at end of file