Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 7E7DA200C6F for ; Tue, 9 May 2017 13:16:42 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 7D0A5160BB6; Tue, 9 May 2017 11:16:42 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 76B66160BB3 for ; Tue, 9 May 2017 13:16:41 +0200 (CEST) Received: (qmail 2970 invoked by uid 500); 9 May 2017 11:16:40 -0000 Mailing-List: contact commits-help@karaf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@karaf.apache.org Delivered-To: mailing list commits@karaf.apache.org Received: (qmail 2961 invoked by uid 99); 9 May 2017 11:16:40 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 May 2017 11:16:40 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 73D7FDFE61; Tue, 9 May 2017 11:16:40 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cschneider@apache.org To: commits@karaf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: karaf git commit: [KARAF-4973] Adding test and fixing issues in BundleWires Date: Tue, 9 May 2017 11:16:40 +0000 (UTC) archived-at: Tue, 09 May 2017 11:16:42 -0000 Repository: karaf Updated Branches: refs/heads/master d5d774f06 -> e32435252 [KARAF-4973] Adding test and fixing issues in BundleWires Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/e3243525 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/e3243525 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/e3243525 Branch: refs/heads/master Commit: e324352523af0c2f9fd0eb3ad2fe6df6cf2909a4 Parents: d5d774f Author: Christian Schneider Authored: Tue May 9 13:15:42 2017 +0200 Committer: Christian Schneider Committed: Tue May 9 13:16:32 2017 +0200 ---------------------------------------------------------------------- .../karaf/features/extension/BundleWires.java | 12 +- .../extension/StoredWiringResolver.java | 55 +++---- .../features/extension/BundleWiresTest.java | 150 +++++++++++++++++++ features/extension/src/test/resources/wirings/1 | 2 + 4 files changed, 185 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/e3243525/features/extension/src/main/java/org/apache/karaf/features/extension/BundleWires.java ---------------------------------------------------------------------- diff --git a/features/extension/src/main/java/org/apache/karaf/features/extension/BundleWires.java b/features/extension/src/main/java/org/apache/karaf/features/extension/BundleWires.java index 48b289a..a355b08 100644 --- a/features/extension/src/main/java/org/apache/karaf/features/extension/BundleWires.java +++ b/features/extension/src/main/java/org/apache/karaf/features/extension/BundleWires.java @@ -43,24 +43,22 @@ import org.osgi.resource.Requirement; class BundleWires { long bundleId; - Map wiring; + Map wiring = new HashMap<>(); BundleWires(Bundle bundle) { this.bundleId = bundle.getBundleId(); - this.wiring = new HashMap<>(); - Map bw = new HashMap<>(); for (BundleWire wire : bundle.adapt(BundleWiring.class).getRequiredWires(null)) { - bw.put(getRequirementId(wire.getRequirement()), getCapabilityId(wire.getCapability())); + this.wiring.put(getRequirementId(wire.getRequirement()), getCapabilityId(wire.getCapability())); } } - BundleWires(BufferedReader reader) throws IOException { - Map map = new HashMap<>(); + BundleWires(long bundleId, BufferedReader reader) throws IOException { + this.bundleId = bundleId; while (true) { String key = reader.readLine(); String val = reader.readLine(); if (key != null && val != null) { - map.put(key, val); + this.wiring.put(key, val); } else { break; } http://git-wip-us.apache.org/repos/asf/karaf/blob/e3243525/features/extension/src/main/java/org/apache/karaf/features/extension/StoredWiringResolver.java ---------------------------------------------------------------------- diff --git a/features/extension/src/main/java/org/apache/karaf/features/extension/StoredWiringResolver.java b/features/extension/src/main/java/org/apache/karaf/features/extension/StoredWiringResolver.java index 3ebd8b4..ebbfe9c 100644 --- a/features/extension/src/main/java/org/apache/karaf/features/extension/StoredWiringResolver.java +++ b/features/extension/src/main/java/org/apache/karaf/features/extension/StoredWiringResolver.java @@ -37,22 +37,22 @@ import org.osgi.resource.Resource; class StoredWiringResolver implements ResolverHook { private final Map wiring = new HashMap<>(); - private Path path; - + private Path path; + StoredWiringResolver(Path path) { - this.path = path; - load(); - } + this.path = path; + load(); + } - void load() { + void load() { try { Files.createDirectories(path); Files.list(path).forEach(p -> { String name = p.getFileName().toString(); if (name.matches("[0-9]+")) { + long id = Long.parseLong(name); try (BufferedReader reader = Files.newBufferedReader(p)) { - long id = Long.parseLong(name); - wiring.put(id, new BundleWires(reader)); + wiring.put(id, new BundleWires(id, reader)); } catch (IOException e) { throw new UncheckedIOException(e); } @@ -62,13 +62,14 @@ class StoredWiringResolver implements ResolverHook { throw new UncheckedIOException(e); } } - + @Override public void filterResolvable(Collection candidates) { } @Override - public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) { + public void filterSingletonCollisions(BundleCapability singleton, + Collection collisionCandidates) { } @Override @@ -76,37 +77,37 @@ class StoredWiringResolver implements ResolverHook { long sourceId = getBundleId(requirement); wiring.get(sourceId).filterMatches(requirement, candidates); } - + @Override public void end() { } - private long getBundleId(BundleRequirement requirement) { - long sourceId = requirement.getRevision().getBundle().getBundleId(); + private long getBundleId(BundleRequirement requirement) { + long sourceId = requirement.getRevision().getBundle().getBundleId(); if (isFragment(requirement.getRevision()) - && !requirement.getNamespace().equals(HostNamespace.HOST_NAMESPACE)) { + && !requirement.getNamespace().equals(HostNamespace.HOST_NAMESPACE)) { sourceId = wiring.get(sourceId).getFragmentHost(); } - return sourceId; - } + return sourceId; + } private static boolean isFragment(Resource resource) { for (Capability cap : resource.getCapabilities(null)) { if (IdentityNamespace.IDENTITY_NAMESPACE.equals(cap.getNamespace())) { - return IdentityNamespace.TYPE_FRAGMENT.equals( - cap.getAttributes().get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE)); + return IdentityNamespace.TYPE_FRAGMENT + .equals(cap.getAttributes().get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE)); } } return false; } - synchronized void update(Bundle bundle) { - BundleWires bw = new BundleWires(bundle); - bw.save(path); - wiring.put(bundle.getBundleId(), bw); - } - - synchronized void delete(Bundle bundle) { - wiring.get(bundle.getBundleId()).delete(path); - } + synchronized void update(Bundle bundle) { + BundleWires bw = new BundleWires(bundle); + bw.save(path); + wiring.put(bundle.getBundleId(), bw); + } + + synchronized void delete(Bundle bundle) { + wiring.get(bundle.getBundleId()).delete(path); + } } http://git-wip-us.apache.org/repos/asf/karaf/blob/e3243525/features/extension/src/test/java/org/apache/karaf/features/extension/BundleWiresTest.java ---------------------------------------------------------------------- diff --git a/features/extension/src/test/java/org/apache/karaf/features/extension/BundleWiresTest.java b/features/extension/src/test/java/org/apache/karaf/features/extension/BundleWiresTest.java new file mode 100644 index 0000000..90c15e2 --- /dev/null +++ b/features/extension/src/test/java/org/apache/karaf/features/extension/BundleWiresTest.java @@ -0,0 +1,150 @@ +/* + * 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.karaf.features.extension; + +import static org.easymock.EasyMock.expect; +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.osgi.framework.Bundle; +import org.osgi.framework.Constants; +import org.osgi.framework.namespace.PackageNamespace; +import org.osgi.framework.wiring.BundleCapability; +import org.osgi.framework.wiring.BundleRequirement; +import org.osgi.framework.wiring.BundleRevision; +import org.osgi.framework.wiring.BundleWire; +import org.osgi.framework.wiring.BundleWiring; +import org.osgi.resource.Namespace; + +public class BundleWiresTest { + + private static final Path BASE_PATH = new File("target/bundles").toPath(); + private static final String targetBundleVersion = "1.0.1"; + private static final int targetBundleId = 2; + private static final String packageFilter = "(&(osgi.wiring.package=org.osgi.framework)(version>=1.6.0)(!(version>=2.0.0)))"; + private IMocksControl c; + + @Before + public void init() { + c = EasyMock.createControl(); + } + + @Test + public void testFromBundle() throws IOException { + BundleWire wire = packageWire(packageFilter, bundleCap(targetBundleId, targetBundleVersion)); + Bundle bundle = wiredBundle(Arrays.asList(wire)); + c.replay(); + BundleWires bwires = new BundleWires(bundle); + bwires.save(BASE_PATH); + c.verify(); + Iterator lines = Files.lines(new File("target/bundles/1").toPath()).iterator(); + Assert.assertEquals(PackageNamespace.PACKAGE_NAMESPACE + "; " + packageFilter, lines.next()); + Assert.assertEquals(targetBundleId + "; version=" + targetBundleVersion, lines.next()); + bwires.delete(BASE_PATH); + } + + @Test + public void testFromFile() throws IOException { + BundleWires wires = readFromFile(); + assertEquals(1, wires.wiring.size()); + Entry wire = wires.wiring.entrySet().iterator().next(); + assertEquals(PackageNamespace.PACKAGE_NAMESPACE + "; " + packageFilter, wire.getKey()); + assertEquals(targetBundleId + "; version=" + targetBundleVersion, wire.getValue()); + } + + @Test + public void testFilterMatches() throws IOException { + BundleWires wires = readFromFile(); + BundleRequirement req = packageRequirement(packageFilter); + BundleCapability candidate1 = bundleCap(targetBundleId, targetBundleVersion); + List candidates = new ArrayList<>(); + candidates.add(candidate1); + BundleCapability matchingCandidate = bundleCap(targetBundleId, "1.1.0"); + candidates.add(matchingCandidate); + c.replay(); + + wires.filterMatches(req, candidates); + assertEquals(1, candidates.size()); + assertEquals(candidate1, candidates.iterator().next()); + c.verify(); + } + + private BundleWires readFromFile() throws IOException { + File wiringsFile = new File("src/test/resources/wirings/1"); + BufferedReader reader = new BufferedReader(new FileReader(wiringsFile)); + BundleWires wires = new BundleWires(1, reader); + return wires; + } + + private BundleWire packageWire(String packageFilter, BundleCapability bundleRef) { + BundleWire wire = c.createMock(BundleWire.class); + BundleRequirement req = packageRequirement(packageFilter); + expect(wire.getRequirement()).andReturn(req); + expect(wire.getCapability()).andReturn(bundleRef); + return wire; + } + + private BundleRequirement packageRequirement(String packageFilter) { + BundleRequirement req = c.createMock(BundleRequirement.class); + Map directives = new HashMap<>(); + directives.put(Namespace.REQUIREMENT_FILTER_DIRECTIVE, packageFilter); + expect(req.getDirectives()).andReturn(directives); + expect(req.getNamespace()).andReturn(PackageNamespace.PACKAGE_NAMESPACE); + return req; + } + + private BundleCapability bundleCap(long bundleId, String version) { + BundleRevision rev = c.createMock(BundleRevision.class); + Bundle bundle = c.createMock(Bundle.class); + expect(bundle.getBundleId()).andReturn(bundleId); + expect(rev.getBundle()).andReturn(bundle); + BundleCapability cap = c.createMock(BundleCapability.class); + expect(cap.getRevision()).andReturn(rev); + Map attrs = new HashMap<>(); + attrs.put(Constants.VERSION_ATTRIBUTE, version); + expect(cap.getAttributes()).andReturn(attrs); + return cap; + } + + private Bundle wiredBundle(List wires) { + Bundle bundle = c.createMock(Bundle.class); + EasyMock.expect(bundle.getBundleId()).andReturn(1l); + BundleWiring wiring = c.createMock(BundleWiring.class); + expect(wiring.getRequiredWires(null)).andReturn(wires); + expect(bundle.adapt(BundleWiring.class)).andReturn(wiring); + return bundle; + } + +} http://git-wip-us.apache.org/repos/asf/karaf/blob/e3243525/features/extension/src/test/resources/wirings/1 ---------------------------------------------------------------------- diff --git a/features/extension/src/test/resources/wirings/1 b/features/extension/src/test/resources/wirings/1 new file mode 100644 index 0000000..4e721ab --- /dev/null +++ b/features/extension/src/test/resources/wirings/1 @@ -0,0 +1,2 @@ +osgi.wiring.package; (&(osgi.wiring.package=org.osgi.framework)(version>=1.6.0)(!(version>=2.0.0))) +2; version=1.0.1