Unit tests for the apiregions runtime component.
Removed old variant of the unit tests now that they are all converted.
diff --git a/src/test/java/org/apache/sling/feature/apiregions/impl/ResolverHookImplTest.java b/src/test/java/org/apache/sling/feature/apiregions/impl/ResolverHookImplTest.java
index a80404e..2127804 100644
--- a/src/test/java/org/apache/sling/feature/apiregions/impl/ResolverHookImplTest.java
+++ b/src/test/java/org/apache/sling/feature/apiregions/impl/ResolverHookImplTest.java
@@ -18,7 +18,6 @@
*/
package org.apache.sling.feature.apiregions.impl;
-import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.osgi.framework.Bundle;
@@ -37,154 +36,17 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.Map.Entry;
+import java.util.Set;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
public class ResolverHookImplTest {
- @Test @Ignore
- public void xxtestFilterMatches() throws Exception {
- /*
- String f = "gid:aid:0.0.9";
- String f2 = "gid2:aid2:1.0.0-SNAPSHOT";
- String f3 = "gid3:aid3:1.2.3";
- String f4 = "gid4:aid4:1.2.3";
- String f5 = "gid5:aid5:1.2.3";
-
- Features fs = Mockito.mock(Features.class);
- Mockito.when(fs.getFeaturesForBundle("a.b.c", new Version(0,0,0)))
- .thenReturn(Collections.singleton(f)); // b7
- Mockito.when(fs.getFeaturesForBundle("some.other.bundle", new Version(9,9,9,"suffix")))
- .thenReturn(Collections.singleton(f2)); // b9
- Mockito.when(fs.getFeaturesForBundle("a-bundle", new Version(1,0,0,"SNAPSHOT")))
- .thenReturn(Collections.singleton(f2)); // b10
- Mockito.when(fs.getFeaturesForBundle("a.b.c", new Version(1,2,3)))
- .thenReturn(Collections.singleton(f3)); // b17
- Mockito.when(fs.getFeaturesForBundle("z.z.z", new Version(3,2,1)))
- .thenReturn(new HashSet<>(Arrays.asList(f, f3))); // b18
- Mockito.when(fs.getFeaturesForBundle("x.y.z", new Version(9,9,9)))
- .thenReturn(Collections.singleton(f3)); // b19
- Mockito.when(fs.getFeaturesForBundle("zzz", new Version(1,0,0)))
- .thenReturn(Collections.singleton(f4)); // b20
- Mockito.when(fs.getFeaturesForBundle("www", new Version(1,0,0)))
- .thenReturn(Collections.singleton(f5)); // b20
-
- ServiceTracker st = Mockito.mock(ServiceTracker.class);
- Mockito.when(st.waitForService(Mockito.anyLong())).thenReturn(fs);
-
- Map<String, Set<String>> rpm = new HashMap<>();
- rpm.put("r0", Collections.singleton("org.bar"));
- rpm.put("r1", new HashSet<>(Arrays.asList("org.blah", "org.foo")));
- rpm.put(WhitelistService.GLOBAL_REGION, Collections.singleton("org.bar.tar"));
- rpm.put("r3", Collections.singleton("xyz"));
-
- Map<String, Set<String>> frm = new HashMap<>();
- frm.put("gid:aid:0.0.9",
- new HashSet<>(Arrays.asList("r1", "r2", WhitelistService.GLOBAL_REGION)));
- frm.put("gid2:aid2:1.0.0-SNAPSHOT", Collections.singleton("r2"));
- frm.put("gid3:aid3:1.2.3", Collections.singleton("r3"));
- frm.put("gid4:aid4:1.2.3", Collections.singleton("r3"));
- frm.put("gid5:aid5:1.2.3", Collections.emptySet());
-
- WhitelistService wls = new WhitelistServiceImpl(rpm, frm);
- ResolverHookImpl rh = new ResolverHookImpl(st, wls);
-
- // Check that we can get the capability from another bundle in the same region
- // Bundle 7 is in feature f with regions r1, r2. Bundle 9 is in feature f2 with regions r2
- BundleRequirement req = mockRequirement(7, "a.b.c", new Version(0,0,0));
- BundleCapability bc1 = mockCapability("org.foo", 9, "some.other.bundle", new Version(9,9,9,"suffix"));
- List<BundleCapability> candidates = new ArrayList<>(Arrays.asList(bc1));
- rh.filterMatches(req, candidates);
- assertEquals(Collections.singletonList(bc1), candidates);
-
- // Check that we cannot get the capability from another bundle in a different region
- // Bundle 9 is in feature f2 with region r2
- BundleRequirement req2 = mockRequirement(9, "some.other.bundle", new Version(9,9,9,"suffix"));
- BundleCapability bc2 = mockCapability("org.bar", 17, "a.b.c", new Version(1,2,3));
- Collection<BundleCapability> c2 = new ArrayList<>(Arrays.asList(bc2));
- rh.filterMatches(req2, c2);
- assertEquals(0, c2.size());
-
- // Check that we can get the capability from the same bundle
- BundleRequirement req3 = mockRequirement(9, "some.other.bundle", new Version(9,9,9,"suffix"));
- BundleCapability bc3 = mockCapability("org.bar", 9, "some.other.bundle", new Version(9,9,9,"suffix"));
- Collection<BundleCapability> c3 = new ArrayList<>(Arrays.asList(bc3));
- rh.filterMatches(req3, c3);
- assertEquals(Collections.singletonList(bc3), c3);
-
- // Check that we can get the capability from the another bundle in the same feature
- BundleRequirement req4 = mockRequirement(9, "some.other.bundle", new Version(9,9,9,"suffix"));
- BundleCapability bc4 = mockCapability("org.bar", 10, "a-bundle", new Version(1,0,0,"SNAPSHOT"));
- Collection<BundleCapability> c4 = new ArrayList<>(Arrays.asList(bc4));
- rh.filterMatches(req4, c4);
- assertEquals(Collections.singletonList(bc4), c4);
-
- // Check that we can get the capability from another bundle where the capability
- // is globally visible (from bundle 9, f2)
- BundleRequirement req5 = mockRequirement(17, "a.b.c", new Version(1,2,3));
- BundleCapability bc5 = mockCapability("org.bar.tar", 9, "some.other.bundle", new Version(9,9,9,"suffix"));
- Collection<BundleCapability> c5 = new ArrayList<>(Arrays.asList(bc5));
- rh.filterMatches(req5, c5);
- assertEquals(Collections.singletonList(bc5), c5);
-
- // Check that we can get the capability from another bundle where the capability
- // is globally visible (from bundle 7, f)
- BundleRequirement req6 = mockRequirement(7, "a.b.c", new Version(0,0,0));
- BundleCapability bc6 = mockCapability("org.bar.tar", 17, "a.b.c", new Version(1,2,3));
- Collection<BundleCapability> c6 = new ArrayList<>(Arrays.asList(bc6));
- rh.filterMatches(req6, c6);
- assertEquals(Collections.singletonList(bc6), c6);
-
- // Check that capabilities in non-package namespaces are ignored
- BundleRequirement req7 = Mockito.mock(BundleRequirement.class);
- Mockito.when(req7.getNamespace()).thenReturn("some.other.namespace");
- BundleCapability bc7 = mockCapability("org.bar", 17, "a.b.c", new Version(1,2,3));
- Collection<BundleCapability> c7 = new ArrayList<>(Arrays.asList(bc7));
- rh.filterMatches(req7, c7);
- assertEquals(Collections.singletonList(bc7), c7);
-
- // Check that we can get the capability from another provider in the same region
- BundleRequirement req8 = mockRequirement(20, "zzz", new Version(1,0,0));
- BundleCapability bc8 = mockCapability("xyz", 19, "x.y.z", new Version(9,9,9));
- Collection<BundleCapability> c8 = new ArrayList<>(Arrays.asList(bc8));
- rh.filterMatches(req8, c8);
- assertEquals(Collections.singletonList(bc8), c8);
-
- // A requirement from a bundle that has no feature cannot access one in a region
- BundleRequirement req9 = mockRequirement(11, "qqq", new Version(6,6,6));
- BundleCapability bc9 = mockCapability("org.bar", 17, "a.b.c", new Version(1,2,3));
- ArrayList c9 = new ArrayList<>(Arrays.asList(bc9));
- rh.filterMatches(req9, c9);
- assertEquals(0, c9.size());
-
- // A requirement from a bundle that has no feature can still access on in the global region
- BundleRequirement req10 = mockRequirement(11, "qqq", new Version(6,6,6));
- BundleCapability bc10 = mockCapability("org.bar.tar", 18, "z.z.z", new Version(3,2,1));
- ArrayList c10 = new ArrayList<>(Arrays.asList(bc10));
- rh.filterMatches(req10, c10);
- assertEquals(Collections.singletonList(bc10), c10);
-
- // A requirement from a bundle that has no feature can be satisfied by a capability
- // from a bundle that has no feature
- BundleRequirement req11 = mockRequirement(11, "qqq", new Version(6,6,6));
- BundleCapability bc11 = mockCapability("org.bar.tar", 20, "www", new Version(1,0,0));
- ArrayList c11 = new ArrayList<>(Arrays.asList(bc11));
- rh.filterMatches(req11, c11);
- assertEquals(Collections.singletonList(bc11), c11);
-
- // A capability from the system bundle is always accessible
- BundleRequirement req12 = mockRequirement(11, "qqq", new Version(6,6,6));
- BundleCapability bc12 = mockCapability("ping.pong", 0, "system.bundle", new Version(3,2,1));
- ArrayList c12 = new ArrayList<>(Arrays.asList(bc12));
- rh.filterMatches(req12, c12);
- assertEquals(Collections.singletonList(bc12), c12);
- */
- }
-
@Test
public void testFilterMatches() {
Map<Entry<String, Version>, List<String>> bsnvermap = new HashMap<>();
+ bsnvermap.put(new AbstractMap.SimpleEntry<String,Version>("system.bundle", new Version(3,2,1)),
+ Collections.singletonList("b0"));
bsnvermap.put(new AbstractMap.SimpleEntry<String,Version>("a.b.c", new Version(0,0,0)),
Collections.singletonList("b7"));
bsnvermap.put(new AbstractMap.SimpleEntry<String,Version>("a.bundle", new Version(1,0,0)),
@@ -195,6 +57,8 @@
Collections.singletonList("b10"));
bsnvermap.put(new AbstractMap.SimpleEntry<String,Version>("not.in.a.feature", new Version(0,0,1)),
Collections.singletonList("b11"));
+ bsnvermap.put(new AbstractMap.SimpleEntry<String,Version>("also.not.in.a.feature", new Version(0,0,1)),
+ Collections.singletonList("b12"));
bsnvermap.put(new AbstractMap.SimpleEntry<String,Version>("a.b.c", new Version(1,2,3)),
Collections.singletonList("b17"));
bsnvermap.put(new AbstractMap.SimpleEntry<String,Version>("x.y.z", new Version(9,9,9)),
@@ -307,6 +171,21 @@
Collection<BundleCapability> c10 = new ArrayList<>(Arrays.asList(bc10));
rh.filterMatches(req10, c10);
assertEquals(Collections.singletonList(bc10), c10);
+
+ // A requirement from a bundle that has no feature can be satisfied by a capability
+ // from a bundle that has no feature
+ BundleRequirement req11 = mockRequirement("b11", bsnvermap);
+ BundleCapability bc11 = mockCapability("ding.dong", "b12", bsnvermap);
+ Collection<BundleCapability> c11 = new ArrayList<>(Arrays.asList(bc11));
+ rh.filterMatches(req11, c11);
+ assertEquals(Collections.singletonList(bc11), c11);
+
+ // A capability from the system bundle is always accessible
+ BundleRequirement req12 = mockRequirement("b11", bsnvermap);
+ BundleCapability bc12 = mockCapability("ping.pong", "b0", bsnvermap);
+ Collection<BundleCapability> c12 = new ArrayList<>(Arrays.asList(bc12));
+ rh.filterMatches(req12, c12);
+ assertEquals(Collections.singletonList(bc12), c12);
}
private BundleCapability mockCapability(String pkgName, String bid, Map<Entry<String, Version>, List<String>> bsnvermap) {