SLING-6392 support entity id changes for the same url (by uninstalling stale resources)
authorKonrad Windszus <kwin@apache.org>
Thu, 16 Feb 2017 10:43:39 +0000 (10:43 +0000)
committerKonrad Windszus <kwin@apache.org>
Thu, 16 Feb 2017 10:43:39 +0000 (10:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1783196 13f79535-47bb-0310-9956-ffa450edef68

src/test/java/org/apache/sling/installer/it/BundleInstallUpgradeDowngradeTest.java
src/test/java/org/apache/sling/installer/it/MockInstallableResource.java
src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java

index ef3b894..70d93c9 100644 (file)
@@ -19,6 +19,8 @@ package org.apache.sling.installer.it;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 
+import org.apache.sling.installer.api.InstallableResource;
+import org.apache.sling.installer.it.OsgiInstallerTestBase.BundleEvent;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -207,4 +209,44 @@ public class BundleInstallUpgradeDowngradeTest extends OsgiInstallerTestBase {
         }
 
     }
+
+    /**
+     * This test class assures that whenever a new bundle is 
+     * provided with the same url as an already installed bundle,
+     * the already installed bundle is uninstalled and the new one installed.
+     * @see <a href="https://issues.apache.org/jira/browse/SLING-6392">SLING-6392</a>
+     */
+    @Test
+    public void testReplaceBundleWithSameUrlButDifferentSymbolicName() throws Exception {
+        final String symbolicName = "osgi-installer-testbundle";
+        final String symbolicName2 = "osgi-installer-testA";
+        final String installableResourceId = "stable-id";
+        
+        assertNull("Test bundle must not be present before test", findBundle(symbolicName));
+        assertNull("Test A bundle must not be present before test", findBundle(symbolicName2));
+        {
+            //assertNull("Test bundle must be absent before installing", findBundle(symbolicName));
+            final Object listener = this.startObservingBundleEvents();
+            installer.updateResources(URL_SCHEME, getInstallableResource(
+                    getTestBundle(BUNDLE_BASE_NAME + "-testbundle-1.0.jar"), installableResourceId, "1", InstallableResource.DEFAULT_PRIORITY), null);
+            this.waitForBundleEvents(symbolicName + " must be installed", listener,
+                    new BundleEvent(symbolicName, "1.0", org.osgi.framework.BundleEvent.INSTALLED),
+                    new BundleEvent(symbolicName, "1.0", org.osgi.framework.BundleEvent.STARTED));
+            assertBundle("After installing", symbolicName, "1.0", Bundle.ACTIVE);
+        }
+
+        // now modify the bundle (having the same url but a different symbolic name and different digest)
+        {
+            final Object listener = this.startObservingBundleEvents();
+            installer.updateResources(URL_SCHEME, getInstallableResource(
+                    getTestBundle(BUNDLE_BASE_NAME + "-testA-1.0.jar"), installableResourceId, "2", InstallableResource.DEFAULT_PRIORITY), null);
+            this.waitForBundleEvents(symbolicName2 + " must be installed and " + symbolicName + " uninstalled", listener,
+                    new BundleEvent(symbolicName, "1.0", org.osgi.framework.BundleEvent.STOPPED),
+                    new BundleEvent(symbolicName, "1.0", org.osgi.framework.BundleEvent.UNINSTALLED),
+                    new BundleEvent(symbolicName2, "1.0", org.osgi.framework.BundleEvent.INSTALLED),
+                    new BundleEvent(symbolicName2, "1.0", org.osgi.framework.BundleEvent.STARTED));
+            assertBundle("After installing a different bundle with same id " + installableResourceId, symbolicName2, "1.0", Bundle.ACTIVE);
+            assertNull("Test bundle must not be present after removing it", findBundle(symbolicName));
+        }
+    }
 }
\ No newline at end of file
index 434382d..1593d89 100644 (file)
@@ -45,8 +45,8 @@ public class MockInstallableResource extends InstallableResource {
                 InstallableResource.TYPE_BUNDLE, null);
     }
 
-    public MockInstallableResource(String uri, InputStream is, String digest, String type, Integer priority) {
-        super(uri, is,
+    public MockInstallableResource(String id, InputStream is, String digest, String type, Integer priority) {
+        super(id, is,
                 null, digest,
                 type != null ? type : InstallableResource.TYPE_BUNDLE, priority);
     }
index 5768a09..a4fcf34 100644 (file)
@@ -408,14 +408,17 @@ public class OsgiInstallerTestBase implements FrameworkListener {
     }
 
     protected InstallableResource[] getInstallableResource(File testBundle, String digest, int priority) throws IOException {
-        final String url = testBundle.getAbsolutePath();
+        return getInstallableResource(testBundle, testBundle.getAbsolutePath(), digest, priority);
+    }
+
+    protected InstallableResource[] getInstallableResource(File testBundle, String id, String digest, int priority) throws IOException {
         if (digest == null) {
             digest = String.valueOf(testBundle.lastModified());
         }
-        final InstallableResource result = new MockInstallableResource(url, new FileInputStream(testBundle), digest, null, priority);
+        final InstallableResource result = new MockInstallableResource(id, new FileInputStream(testBundle), digest, null, priority);
         return new InstallableResource[] {result};
     }
-
+    
     protected InstallableResource[] getInstallableResource(String configPid, Dictionary<String, Object> data) {
         return getInstallableResource(configPid, copy(data), InstallableResource.DEFAULT_PRIORITY);
     }