SLING-8815 - [API Regions] Prevent from resolving to customer-provided
bundles
added unit test to prove the filtering correctness
diff --git a/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHook.java b/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHook.java
index 0c7ef48..ee1b635 100644
--- a/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHook.java
+++ b/src/main/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHook.java
@@ -68,7 +68,7 @@
}
}
- private boolean filter(BundleRequirement requirement, BundleCapability candidate) {
+ boolean filter(BundleRequirement requirement, BundleCapability candidate) {
String requirementNamespace = requirement.getNamespace();
String candidateNamespace = candidate.getNamespace();
if (!OSGI_WIRING_PACKAGE_NAMESPACE.equals(requirementNamespace)
@@ -82,7 +82,7 @@
Version expectedVersion = bsnVerMap.get(candidateSymbolicName);
- return expectedVersion != null && expectedVersion.equals(candidateVersion);
+ return expectedVersion != null && !expectedVersion.equals(candidateVersion);
}
@Override
diff --git a/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcerTest.java b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcerTest.java
new file mode 100644
index 0000000..13d1339
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationEnforcerTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.feature.apiregions.impl;
+
+import static org.junit.Assert.*;
+import static org.apache.sling.feature.apiregions.impl.AbstractResolverHookFactory.IDBSNVER_FILENAME;
+import static org.apache.sling.feature.apiregions.impl.AbstractResolverHookFactory.PROPERTIES_RESOURCE_PREFIX;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Version;
+
+public class PlatformIsolationEnforcerTest {
+
+ @Test
+ public void testLoadBSNVerMap() throws Exception {
+ String propertiesFile = getClass().getResource("/idbsnver1.properties").getFile();
+
+ BundleContext context = mock(BundleContext.class);
+ when(context.getProperty(PROPERTIES_RESOURCE_PREFIX + IDBSNVER_FILENAME)).thenReturn(propertiesFile);
+
+ PlatformIsolationEnforcer enforcer = new PlatformIsolationEnforcer(context);
+
+ assertFalse(enforcer.bsnVerMap.isEmpty());
+ assertEquals(2, enforcer.bsnVerMap.size());
+
+ Map<String, Version> expected = new HashMap<>();
+ expected.put("b2", new Version(1, 2, 3));
+ expected.put("b1", new Version(1, 0, 0));
+
+ assertEquals(expected, enforcer.bsnVerMap);
+ }
+
+}
diff --git a/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHookTest.java b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHookTest.java
new file mode 100644
index 0000000..f495ede
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/apiregions/impl/PlatformIsolationHookTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.feature.apiregions.impl;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.osgi.framework.Version;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.BundleRequirement;
+import org.osgi.framework.wiring.BundleRevision;
+
+public class PlatformIsolationHookTest {
+
+ @Test
+ public void doNotFilterRequirementWithDifferentNamespace() {
+ BundleRequirement requirement = mock(BundleRequirement.class);
+ when(requirement.getNamespace()).thenReturn("something.irrilevant");
+
+ // won't be touched
+ BundleCapability candidate = newBundleCapability();
+
+ PlatformIsolationHook hook = new PlatformIsolationHook(new HashMap<String, Version>());
+ assertFalse(hook.filter(requirement, candidate));
+ }
+
+ @Test
+ public void doNotFilterCandidateWithDifferentNamespace() {
+ BundleRequirement requirement = newRequirement();
+
+ BundleCapability candidate = mock(BundleCapability.class);
+ when(candidate.getNamespace()).thenReturn("something.irrilevant");
+
+ PlatformIsolationHook hook = new PlatformIsolationHook(new HashMap<String, Version>());
+ assertFalse(hook.filter(requirement, candidate));
+ }
+
+ @Test
+ public void letUnownBundleBeResolved() {
+ Map<String, Version> bsnVerMap = new HashMap<>();
+ bsnVerMap.put("b2", new Version(1, 2, 3));
+ bsnVerMap.put("b1", new Version(1, 0, 0));
+ PlatformIsolationHook hook = new PlatformIsolationHook(bsnVerMap);
+
+ BundleRequirement requirement = newRequirement();
+
+ BundleCapability candidate = newBundleCapability();
+ BundleRevision revision = newBundleRevision("asd", "1.0.0.CUSTOM");
+ when(candidate.getRevision()).thenReturn(revision);
+
+ assertFalse(hook.filter(requirement, candidate));
+ }
+
+ @Test
+ public void letKnownBundleBeResolved() {
+ Map<String, Version> bsnVerMap = new HashMap<>();
+ bsnVerMap.put("b2", new Version(1, 2, 3));
+ bsnVerMap.put("b1", new Version(1, 0, 0));
+ PlatformIsolationHook hook = new PlatformIsolationHook(bsnVerMap);
+
+ BundleRequirement requirement = newRequirement();
+
+ BundleCapability candidate = newBundleCapability();
+ BundleRevision revision = newBundleRevision("b1", "1.0.0");
+ when(candidate.getRevision()).thenReturn(revision);
+
+ assertFalse(hook.filter(requirement, candidate));
+ }
+
+ @Test
+ public void upatedBundleFilteredOut() {
+ Map<String, Version> bsnVerMap = new HashMap<>();
+ bsnVerMap.put("b2", new Version(1, 2, 3));
+ bsnVerMap.put("b1", new Version(1, 0, 0));
+ PlatformIsolationHook hook = new PlatformIsolationHook(bsnVerMap);
+
+ BundleRequirement requirement = newRequirement();
+
+ BundleCapability candidate = newBundleCapability();
+ BundleRevision revision = newBundleRevision("b1", "1.0.0.CUSTOM");
+ when(candidate.getRevision()).thenReturn(revision);
+
+ assertTrue(hook.filter(requirement, candidate));
+ }
+
+ private static BundleRequirement newRequirement() {
+ BundleRequirement requirement = mock(BundleRequirement.class);
+ when(requirement.getNamespace()).thenReturn(PlatformIsolationHook.OSGI_WIRING_PACKAGE_NAMESPACE);
+ return requirement;
+ }
+
+ private static BundleCapability newBundleCapability() {
+ BundleCapability candidate = mock(BundleCapability.class);
+ when(candidate.getNamespace()).thenReturn(PlatformIsolationHook.OSGI_WIRING_PACKAGE_NAMESPACE);
+ return candidate;
+ }
+
+ private static BundleRevision newBundleRevision(String symbolicName, String version) {
+ BundleRevision revision = mock(BundleRevision.class);
+ when(revision.getSymbolicName()).thenReturn(symbolicName);
+ when(revision.getVersion()).thenReturn(Version.valueOf(version));
+ return revision;
+ }
+
+}