diff options
Diffstat (limited to 'jdisc_core_test')
-rw-r--r-- | jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java index 58c6c1ba8d6..80de2a28322 100644 --- a/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java +++ b/jdisc_core_test/integration_test/src/test/java/com/yahoo/jdisc/core/BundleCollisionHookIntegrationTest.java @@ -6,16 +6,22 @@ import org.junit.Before; import org.junit.Test; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; +import org.osgi.framework.launch.Framework; import java.util.Collections; +import java.util.List; +import java.util.Set; import static com.yahoo.jdisc.core.FelixFrameworkIntegrationTest.startBundle; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** - * Note that the '-dup' bundles are necessary, because felix ignores duplicate bundles with the same location. + * Note that the '-dup' bundles are necessary, because installing a bundle with the same location as an already + * installed bundle is a NOP. From the OSGi spec: "Every bundle is uniquely identified by its location string." * * @author gjoranv */ @@ -71,7 +77,6 @@ public class BundleCollisionHookIntegrationTest { } catch (BundleException e) { assertTrue(e.getMessage().contains("Bundle symbolic name and version are not unique")); } - } @Test @@ -85,4 +90,83 @@ public class BundleCollisionHookIntegrationTest { startBundle(felix, "cert-ml-dup.jar"); } + @Test + public void allowed_duplicates_are_visible_to_the_framework_and_vice_versa() throws Exception { + Bundle bundleA = startBundle(felix, "cert-a.jar"); + Bundle bundleB = startBundle(felix, "cert-b.jar"); + + // Makes A and B visible to each other + felix.allowDuplicateBundles(Set.of(bundleA)); + felix.allowDuplicateBundles(Set.of(bundleB)); + + List<Bundle> visibleBundles = felix.getBundles(bundleA); + + // The framework bundle should always be visible (the FindHook does not remove it) + Bundle frameworkBundle = visibleBundles.get(0); + assertTrue(isFrameworkBundle(frameworkBundle)); + + // All bundles are always visible to the framework (this is according to the OSGi spec and handled by Felix) + List<Bundle> visibleToFramework = felix.getBundles(frameworkBundle); + assertEquals(3, visibleToFramework.size()); + } + + @Test + public void allowed_duplicates_are_visible_to_its_own_members() throws Exception { + Bundle bundleA = startBundle(felix, "cert-a.jar"); + Bundle bundleB = startBundle(felix, "cert-b.jar"); + + // Makes A and B visible to each other + felix.allowDuplicateBundles(Set.of(bundleA)); + felix.allowDuplicateBundles(Set.of(bundleB)); + + List<Bundle> visibleBundles = felix.getBundles(bundleA); + assertEquals(3, visibleBundles.size()); + assertSame(bundleA, visibleBundles.get(1)); + assertSame(bundleB, visibleBundles.get(2)); + } + + @Test + public void allowed_duplicates_are_invisible_to_unrelated_bundles() throws Exception { + Bundle bundleL = startBundle(felix, "cert-l1.jar"); + Bundle bundleA = startBundle(felix, "cert-a.jar"); + + // Makes L invisible to bundles outside the set of allowed duplicates + felix.allowDuplicateBundles(Set.of(bundleL)); + + List<Bundle> visibleBundles = felix.getBundles(bundleA); + assertEquals(2, visibleBundles.size()); + assertTrue(isFrameworkBundle(visibleBundles.get(0))); + assertSame(bundleA, visibleBundles.get(1)); + } + + @Test + public void set_of_allowed_duplicates_are_invisible_to_all_bundles_of_which_they_are_duplicates() throws Exception { + Bundle bundleL = startBundle(felix, "cert-l1.jar"); + + // Makes L invisible to bundles outside the set of allowed duplicates + felix.allowDuplicateBundles(Set.of(bundleL)); + Bundle bundleL2 = startBundle(felix, "cert-l1-dup.jar"); + + List<Bundle> visibleBundles = felix.getBundles(bundleL2); + assertEquals(2, visibleBundles.size()); + assertSame(bundleL2, visibleBundles.get(1)); + } + + @Test + public void allowed_duplicates_cannot_see_any_of_the_bundles_of_which_they_are_duplicates() throws Exception { + Bundle bundleL = startBundle(felix, "cert-l1.jar"); + + // Makes L invisible to bundles outside the set of allowed duplicates + felix.allowDuplicateBundles(Set.of(bundleL)); + Bundle invisibleToAllowedDuplicates = startBundle(felix, "cert-l1-dup.jar"); + + List<Bundle> visibleToAllowedDuplicates = felix.getBundles(bundleL); + assertEquals(2, visibleToAllowedDuplicates.size()); + assertSame(bundleL, visibleToAllowedDuplicates.get(1)); + } + + private boolean isFrameworkBundle(Bundle bundle) { + return (bundle == felix.bundleContext().getBundle() && (bundle instanceof Framework)); + } + } |