use of org.apache.felix.resolver.test.util.ResolveContextImpl in project felix by apache.
the class FELIX_4914_Test method testResolution.
@Test
public void testResolution() throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/felix-4914.json")));
Map<String, Object> resolution = (Map<String, Object>) JsonReader.read(reader);
List<Resource> repository = readRepository(resolution.get("repository"));
Map<String, CapabilitySet> capSets = new HashMap<String, CapabilitySet>();
for (Resource resource : repository) {
for (Capability cap : resource.getCapabilities(null)) {
String ns = cap.getNamespace();
CapabilitySet set = capSets.get(ns);
if (set == null) {
set = new CapabilitySet(Collections.singletonList(ns));
capSets.put(ns, set);
}
set.addCapability(cap);
}
}
Resource root = null;
for (Resource resource : repository) {
if ("root".equals(Util.getSymbolicName(resource))) {
root = resource;
break;
}
}
List<Resource> mandatory = new ArrayList<Resource>();
mandatory.add(root);
Map<Requirement, List<Capability>> candidates = new HashMap<Requirement, List<Capability>>();
for (Resource resource : repository) {
for (Requirement requirement : resource.getRequirements(null)) {
CapabilitySet set = capSets.get(requirement.getNamespace());
if (set != null) {
String filter = requirement.getDirectives().get(Constants.FILTER_DIRECTIVE);
SimpleFilter sf = (filter != null) ? SimpleFilter.parse(filter) : SimpleFilter.convert(requirement.getAttributes());
candidates.put(requirement, new ArrayList<Capability>(set.match(sf, true)));
} else {
candidates.put(requirement, Collections.<Capability>emptyList());
}
}
}
ResolveContextImpl rci = new ResolveContextImpl(Collections.<Resource, Wiring>emptyMap(), candidates, mandatory, Collections.EMPTY_LIST);
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
}
use of org.apache.felix.resolver.test.util.ResolveContextImpl in project felix by apache.
the class ResolverTest method testScenario6.
@Test
public void testScenario6() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario6(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
int aResources = 0;
for (Resource r : wireMap.keySet()) {
if ("A".equals(getResourceName(r))) {
aResources++;
List<Wire> wires = wireMap.get(r);
assertEquals(4, wires.size());
List<String> providers = new ArrayList<String>();
for (Wire w : wires) {
providers.add(getResourceName(w.getProvider()));
}
Collections.sort(providers);
assertEquals(Arrays.asList("B", "C", "D", "D"), providers);
}
}
assertEquals("Should have found two resolved resources named 'A'", 2, aResources);
}
use of org.apache.felix.resolver.test.util.ResolveContextImpl in project felix by apache.
the class ResolverTest method populateScenario17.
private ResolveContext populateScenario17(boolean realSubstitute, boolean felixResolveContext, boolean existingWirings) {
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
ResourceImpl core = new ResourceImpl("core");
Capability core_pkgCap = addCap(core, PackageNamespace.PACKAGE_NAMESPACE, "pkg1");
Capability core_bundleCap = addCap(core, BundleNamespace.BUNDLE_NAMESPACE, "core");
Requirement core_pkgReq = addReq(core, PackageNamespace.PACKAGE_NAMESPACE, "pkg1");
ResourceImpl misc = new ResourceImpl("misc");
Capability misc_pkgCap = addCap(misc, PackageNamespace.PACKAGE_NAMESPACE, "pkg1");
Capability misc_bundleCap = addCap(misc, BundleNamespace.BUNDLE_NAMESPACE, "misc");
Requirement misc_bundleReq = addReq(misc, BundleNamespace.BUNDLE_NAMESPACE, "core");
ResourceImpl importsCore = new ResourceImpl("importsCore");
Capability importsCore_pkgCap = addCap(importsCore, PackageNamespace.PACKAGE_NAMESPACE, "pkg2", "pkg1");
Requirement importsCore_pkgReq = addReq(importsCore, PackageNamespace.PACKAGE_NAMESPACE, "pkg1");
ResourceImpl requiresMisc = new ResourceImpl("requiresMisc");
Requirement requiresMisc_pkgReq = addReq(requiresMisc, PackageNamespace.PACKAGE_NAMESPACE, "pkg2");
Requirement requiresMisc_bundleReq = addReq(requiresMisc, BundleNamespace.BUNDLE_NAMESPACE, "misc");
ResourceImpl substitutesCore = new ResourceImpl("substitutesCore");
Capability substitutesCore_pkgCap = addCap(substitutesCore, PackageNamespace.PACKAGE_NAMESPACE, "pkg1");
candMap.put(core_pkgReq, Collections.singletonList(realSubstitute ? substitutesCore_pkgCap : core_pkgCap));
candMap.put(misc_bundleReq, Collections.singletonList(core_bundleCap));
candMap.put(importsCore_pkgReq, Collections.singletonList(realSubstitute ? substitutesCore_pkgCap : core_pkgCap));
candMap.put(requiresMisc_pkgReq, Collections.singletonList(importsCore_pkgCap));
candMap.put(requiresMisc_bundleReq, Collections.singletonList(misc_bundleCap));
Map<Resource, List<Wire>> wires = new HashMap<Resource, List<Wire>>();
wires.put(substitutesCore, new ArrayList<Wire>());
wires.put(core, new ArrayList<Wire>());
if (realSubstitute) {
wires.get(core).add(new SimpleWire(core_pkgReq, substitutesCore_pkgCap));
}
wires.put(misc, new ArrayList<Wire>());
wires.get(misc).add(new SimpleWire(misc_bundleReq, core_bundleCap));
Wiring coreWiring = null;
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
if (existingWirings) {
Map<Resource, List<Wire>> invertedWires = new HashMap<Resource, List<Wire>>();
invertedWires.put(substitutesCore, new ArrayList<Wire>());
if (realSubstitute) {
invertedWires.get(substitutesCore).add(new SimpleWire(core_pkgReq, substitutesCore_pkgCap));
}
invertedWires.put(core, new ArrayList<Wire>());
invertedWires.get(core).add(new SimpleWire(misc_bundleReq, core_bundleCap));
invertedWires.put(misc, new ArrayList<Wire>());
wirings.put(substitutesCore, new SimpleWiring(substitutesCore, Arrays.asList(substitutesCore_pkgCap), wires, invertedWires));
coreWiring = new SimpleWiring(core, Arrays.asList(core_bundleCap, core_pkgCap), wires, invertedWires);
wirings.put(core, coreWiring);
wirings.put(misc, new SimpleWiring(misc, Arrays.asList(misc_bundleCap, misc_pkgCap), wires, invertedWires));
}
Collection<Resource> mandatory = Collections.<Resource>singletonList(requiresMisc);
if (felixResolveContext) {
Map<Wiring, Collection<Wire>> substitutions = new HashMap<Wiring, Collection<Wire>>();
if (realSubstitute && coreWiring != null) {
substitutions.put(coreWiring, Arrays.<Wire>asList(new SimpleWire(core_pkgReq, substitutesCore_pkgCap)));
}
return new ResolveContextImpl.FelixResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList(), substitutions);
} else {
return new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
}
}
use of org.apache.felix.resolver.test.util.ResolveContextImpl in project felix by apache.
the class ResolverTest method testScenario9.
@Test
public void testScenario9() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario9(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
Resource resB = findResource("B", wireMap.keySet());
Resource resA = findResource("A", wirings.keySet());
Resource resC1 = findResource("C1", wirings.keySet());
assertNotNull(resB);
assertNotNull(resC1);
assertEquals(1, wireMap.size());
List<Wire> wiresB = wireMap.get(resB);
assertEquals(2, wiresB.size());
// should be wired to A and C1
assertEquals(resA, wiresB.get(0).getProvider());
assertEquals(resC1, wiresB.get(1).getProvider());
}
use of org.apache.felix.resolver.test.util.ResolveContextImpl in project felix by apache.
the class ResolverTest method testScenario7.
@Test
public void testScenario7() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario7(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
assertEquals(4, wireMap.size());
Resource aRes = findResource("A", wireMap.keySet());
List<Wire> aWires = wireMap.get(aRes);
assertEquals(0, aWires.size());
Resource f1Res = findResource("F1", wireMap.keySet());
List<Wire> f1Wires = wireMap.get(f1Res);
assertEquals(1, f1Wires.size());
Wire f1Wire = f1Wires.get(0);
assertEquals(f1Res, f1Wire.getRequirer());
assertEquals(aRes, f1Wire.getProvider());
Requirement req = f1Wire.getRequirement();
assertEquals(HostNamespace.HOST_NAMESPACE, req.getNamespace());
assertEquals(0, req.getAttributes().size());
assertEquals(f1Res, req.getResource());
assertEquals(1, req.getDirectives().size());
assertEquals("(osgi.wiring.host=A)", req.getDirectives().get("filter"));
Capability cap = f1Wire.getCapability();
assertEquals(aRes, cap.getResource());
assertEquals(HostNamespace.HOST_NAMESPACE, cap.getNamespace());
assertEquals(0, cap.getDirectives().size());
assertEquals(1, cap.getAttributes().size());
assertEquals("A", cap.getAttributes().get(HostNamespace.HOST_NAMESPACE));
Resource f2Res = findResource("F2", wireMap.keySet());
List<Wire> f2Wires = wireMap.get(f2Res);
assertEquals(1, f2Wires.size());
Wire f2Wire = f2Wires.get(0);
assertEquals(f2Res, f2Wire.getRequirer());
assertEquals(aRes, f2Wire.getProvider());
Requirement req2 = f2Wire.getRequirement();
assertEquals(HostNamespace.HOST_NAMESPACE, req2.getNamespace());
assertEquals(0, req2.getAttributes().size());
assertEquals(f2Res, req2.getResource());
assertEquals(1, req2.getDirectives().size());
assertEquals("(osgi.wiring.host=A)", req2.getDirectives().get("filter"));
Capability cap2 = f1Wire.getCapability();
assertEquals(aRes, cap2.getResource());
assertEquals(HostNamespace.HOST_NAMESPACE, cap2.getNamespace());
assertEquals(0, cap2.getDirectives().size());
assertEquals(1, cap2.getAttributes().size());
assertEquals("A", cap2.getAttributes().get(HostNamespace.HOST_NAMESPACE));
Resource bRes = findResource("B", wireMap.keySet());
List<Wire> bWires = wireMap.get(bRes);
assertEquals(1, bWires.size());
Wire bWire = bWires.get(0);
assertEquals(bRes, bWire.getRequirer());
assertEquals(f2Res, bWire.getProvider());
Requirement bReq = bWire.getRequirement();
assertEquals(IdentityNamespace.IDENTITY_NAMESPACE, bReq.getNamespace());
assertEquals(0, bReq.getAttributes().size());
assertEquals(bRes, bReq.getResource());
assertEquals(1, bReq.getDirectives().size());
assertEquals("(osgi.identity=F2)", bReq.getDirectives().get("filter"));
}
Aggregations