karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject karaf git commit: [KARAF-4973] Adding test and fixing issues in BundleWires
Date Tue, 09 May 2017 12:00:45 GMT
Repository: karaf
Updated Branches:
  refs/heads/karaf-4.1.x c2d70bd23 -> da47185bc


[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/da47185b
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/da47185b
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/da47185b

Branch: refs/heads/karaf-4.1.x
Commit: da47185bc641d122ac264866f270629395c21cd1
Parents: c2d70bd
Author: Christian Schneider <chris@die-schneider.net>
Authored: Tue May 9 13:15:42 2017 +0200
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Tue May 9 14:00:37 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/da47185b/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<String, String> wiring;
+	Map<String, String> wiring = new HashMap<>();
 
 	BundleWires(Bundle bundle) {
 		this.bundleId = bundle.getBundleId();
-		this.wiring = new HashMap<>();
-        Map<String, String> 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<String, String> 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/da47185b/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<Long, BundleWires> 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<BundleRevision> candidates) {
     }
 
     @Override
-    public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability>
collisionCandidates) {
+    public void filterSingletonCollisions(BundleCapability singleton,
+                                          Collection<BundleCapability> 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/da47185b/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<String> 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<String, String> 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<BundleCapability> 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<String, String> 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<String, Object> attrs = new HashMap<>();
+        attrs.put(Constants.VERSION_ATTRIBUTE, version);
+        expect(cap.getAttributes()).andReturn(attrs);
+        return cap;
+    }
+
+    private Bundle wiredBundle(List<BundleWire> 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/da47185b/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


Mime
View raw message