aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1718163 - in /aries/trunk/spi-fly: spi-fly-core/src/main/java/org/apache/aries/spifly/BaseActivator.java spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
Date Sun, 06 Dec 2015 11:35:33 GMT
Author: davidb
Date: Sun Dec  6 11:35:33 2015
New Revision: 1718163

URL: http://svn.apache.org/viewvc?rev=1718163&view=rev
Log:
ARIES-1461 Tests for the Fragment Support

Modified:
    aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/BaseActivator.java
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java

Modified: aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/BaseActivator.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/BaseActivator.java?rev=1718163&r1=1718162&r2=1718163&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/BaseActivator.java
(original)
+++ aries/trunk/spi-fly/spi-fly-core/src/main/java/org/apache/aries/spifly/BaseActivator.java
Sun Dec  6 11:35:33 2015
@@ -130,7 +130,10 @@ public abstract class BaseActivator impl
 
         List<String> l = new ArrayList<String>();
         for (Bundle bf : bundlesFragments) {
-            l.add(bf.getHeaders().get(headerName));
+            String header = bf.getHeaders().get(headerName);
+            if (header != null) {
+                l.add(header);
+            }
         }
 
         return l;

Modified: aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java?rev=1718163&r1=1718162&r2=1718163&view=diff
==============================================================================
--- aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
(original)
+++ aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/ClientWeavingHookGenericCapabilityTest.java
Sun Dec  6 11:35:33 2015
@@ -57,7 +57,9 @@ import org.osgi.framework.BundleReferenc
 import org.osgi.framework.Version;
 import org.osgi.framework.hooks.weaving.WeavingHook;
 import org.osgi.framework.hooks.weaving.WovenClass;
+import org.osgi.framework.wiring.BundleRequirement;
 import org.osgi.framework.wiring.BundleRevision;
+import org.osgi.framework.wiring.BundleWire;
 import org.osgi.framework.wiring.BundleWiring;
 
 public class ClientWeavingHookGenericCapabilityTest {
@@ -112,6 +114,64 @@ public class ClientWeavingHookGenericCap
         Assert.assertEquals(Collections.singleton("olleh"), result);
     }
 
+    @Test
+    public void testHeadersFromFragment() throws Exception {
+        // Register the bundle that provides the SPI implementation.
+        Bundle providerBundle = mockProviderBundle("impl1", 1);
+        activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle,
new HashMap<String, Object>());
+
+        Dictionary<String, String> fragmentConsumerHeaders = new Hashtable<String,
String>();
+        fragmentConsumerHeaders.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT);
+
+        Bundle fragment = EasyMock.createMock(Bundle.class);
+        EasyMock.expect(fragment.getHeaders()).andReturn(fragmentConsumerHeaders).anyTimes();
+        EasyMock.replay(fragment);
+        BundleRevision frev = EasyMock.createMock(BundleRevision.class);
+        EasyMock.expect(frev.getBundle()).andReturn(fragment).anyTimes();
+        EasyMock.replay(frev);
+        BundleRequirement req = EasyMock.createMock(BundleRequirement.class);
+        EasyMock.expect(req.getRevision()).andReturn(frev).anyTimes();
+        EasyMock.replay(req);
+        BundleWire wire = EasyMock.createMock(BundleWire.class);
+        EasyMock.expect(wire.getRequirement()).andReturn(req).anyTimes();
+        EasyMock.replay(wire);
+        List<BundleWire> wires = Collections.singletonList(wire);
+        BundleWiring wiring = EasyMock.createMock(BundleWiring.class);
+        EasyMock.expect(wiring.getProvidedWires("osgi.wiring.host")).andReturn(wires).anyTimes();
+        EasyMock.replay(wiring);
+        BundleRevision rev = EasyMock.createMock(BundleRevision.class);
+        EasyMock.expect(rev.getWiring()).andReturn(wiring).anyTimes();
+        EasyMock.replay(rev);
+
+        Bundle consumerBundle = mockConsumerBundle(new Hashtable<String, String>(),
rev, providerBundle);
+        activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
+
+        Bundle spiFlyBundle = mockSpiFlyBundle("spifly", Version.parseVersion("1.9.4"), consumerBundle,
providerBundle);
+        WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
+
+        // Weave the TestClient class.
+        URL clsUrl = getClass().getResource("TestClient.class");
+        Assert.assertNotNull("Precondition", clsUrl);
+
+        String clientClassName = "org.apache.aries.spifly.dynamic.TestClient";
+        WovenClass wc = new MyWovenClass(clsUrl, clientClassName, consumerBundle);
+        Assert.assertEquals("Precondition", 0, wc.getDynamicImports().size());
+        wh.weave(wc);
+        Assert.assertEquals(1, wc.getDynamicImports().size());
+        String di1 = "org.apache.aries.spifly;bundle-symbolic-name=spifly;bundle-version=1.9.4";
+        String di2 = "org.apache.aries.spifly;bundle-version=1.9.4;bundle-symbolic-name=spifly";
+        String di = wc.getDynamicImports().get(0);
+        Assert.assertTrue("Weaving should have added a dynamic import", di1.equals(di) ||
di2.equals(di));
+
+        // Invoke the woven class and check that it properly sets the TCCL so that the
+        // META-INF/services/org.apache.aries.mytest.MySPI file from impl1 is visible.
+        Class<?> cls = wc.getDefinedClass();
+        Method method = cls.getMethod("test", new Class [] {String.class});
+        Object result = method.invoke(cls.newInstance(), "hello");
+        Assert.assertEquals(Collections.singleton("olleh"), result);
+
+    }
+
 
     @Test
     public void testTCCLResetting() throws Exception {
@@ -655,6 +715,11 @@ public class ClientWeavingHookGenericCap
     }
 
     private Bundle mockConsumerBundle(Dictionary<String, String> headers, Bundle ...
otherBundles) {
+        return mockConsumerBundle(headers, null, otherBundles);
+    }
+
+    private Bundle mockConsumerBundle(Dictionary<String, String> headers, BundleRevision
rev,
+            Bundle ... otherBundles) {
         // Create a mock object for the client bundle which holds the code that uses ServiceLoader.load()
         // or another SPI invocation.
         BundleContext bc = EasyMock.createMock(BundleContext.class);
@@ -665,7 +730,8 @@ public class ClientWeavingHookGenericCap
         EasyMock.expect(consumerBundle.getHeaders()).andReturn(headers).anyTimes();
         EasyMock.expect(consumerBundle.getBundleContext()).andReturn(bc).anyTimes();
         EasyMock.expect(consumerBundle.getBundleId()).andReturn(Long.MAX_VALUE).anyTimes();
-        EasyMock.expect(consumerBundle.adapt(BundleRevision.class)).andReturn(null).anyTimes();
+        EasyMock.expect(consumerBundle.adapt(BundleRevision.class)).andReturn(rev).anyTimes();
+
         EasyMock.replay(consumerBundle);
 
         List<Bundle> allBundles = new ArrayList<Bundle>(Arrays.asList(otherBundles));



Mime
View raw message