SLING-6392 support entity id changes for the same url (by uninstalling stale resources)

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1783196 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/test/java/org/apache/sling/installer/it/BundleInstallUpgradeDowngradeTest.java b/src/test/java/org/apache/sling/installer/it/BundleInstallUpgradeDowngradeTest.java
index ef3b894..70d93c9 100644
--- a/src/test/java/org/apache/sling/installer/it/BundleInstallUpgradeDowngradeTest.java
+++ b/src/test/java/org/apache/sling/installer/it/BundleInstallUpgradeDowngradeTest.java
@@ -19,6 +19,8 @@
 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 @@
         }
 
     }
+
+    /**
+     * 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
diff --git a/src/test/java/org/apache/sling/installer/it/MockInstallableResource.java b/src/test/java/org/apache/sling/installer/it/MockInstallableResource.java
index 434382d..1593d89 100644
--- a/src/test/java/org/apache/sling/installer/it/MockInstallableResource.java
+++ b/src/test/java/org/apache/sling/installer/it/MockInstallableResource.java
@@ -45,8 +45,8 @@
                 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);
     }
diff --git a/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java b/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java
index 5768a09..a4fcf34 100644
--- a/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java
+++ b/src/test/java/org/apache/sling/installer/it/OsgiInstallerTestBase.java
@@ -408,14 +408,17 @@
     }
 
     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);
     }