You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by da...@apache.org on 2012/02/21 17:50:53 UTC

svn commit: r1291908 - in /aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test: java/org/apache/aries/spifly/dynamic/ java/org/apache/aries/spifly/dynamic/impl5/ resources/org/apache/aries/spifly/dynamic/impl5/ resources/org/apache/aries/spifly/dynamic...

Author: davidb
Date: Tue Feb 21 16:50:53 2012
New Revision: 1291908

URL: http://svn.apache.org/viewvc?rev=1291908&view=rev
Log:
Added test to check that TCCL is properly set back when the SPI implemetation throws an exception.

Added:
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/impl5/
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/impl5/MySPIImpl5.java   (with props)
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/META-INF/
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/META-INF/services/
    aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/META-INF/services/org.apache.aries.mytest.MySPI
Modified:
    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-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=1291908&r1=1291907&r2=1291908&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 Tue Feb 21 16:50:53 2012
@@ -23,6 +23,7 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
@@ -153,8 +154,45 @@ public class ClientWeavingHookGenericCap
 
 
     @Test
-    public void testTCCLResettingOnException() {
-        // TODO
+    public void testTCCLResettingOnException() throws Exception {
+        ClassLoader cl = new URLClassLoader(new URL [] {});
+        Thread.currentThread().setContextClassLoader(cl);
+        Assert.assertSame("Precondition", cl, Thread.currentThread().getContextClassLoader());
+
+        Dictionary<String, String> headers = new Hashtable<String, String>();
+        headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.CLIENT_REQUIREMENT +
+                "; " + SpiFlyConstants.PROVIDER_FILTER_DIRECTIVE + ":=\"(bundle-symbolic-name=impl5)\"");
+
+        Bundle providerBundle5 = mockProviderBundle("impl5", 1);
+        activator.registerProviderBundle("org.apache.aries.mytest.MySPI", providerBundle5, new HashMap<String, Object>());
+
+        Bundle consumerBundle = mockConsumerBundle(headers, providerBundle5);
+        activator.addConsumerWeavingData(consumerBundle, SpiFlyConstants.REQUIRE_CAPABILITY);
+
+        Bundle spiFlyBundle = mockSpiFlyBundle(consumerBundle,providerBundle5);
+        WeavingHook wh = new ClientWeavingHook(spiFlyBundle.getBundleContext(), activator);
+
+        // Weave the TestClient class.
+        URL clsUrl = getClass().getResource("TestClient.class");
+        WovenClass wc = new MyWovenClass(clsUrl, "org.apache.aries.spifly.dynamic.TestClient", consumerBundle);
+        wh.weave(wc);
+
+        Class<?> cls = wc.getDefinedClass();
+        Method method = cls.getMethod("test", new Class [] {String.class});
+
+        // Invoke the woven class, check that it properly set the TCCL so that the implementation of impl5 is called.
+        // That implementation throws an exception, after which we are making sure that the TCCL is set back appropriately.
+        try {
+            method.invoke(cls.newInstance(), "hello");
+            Assert.fail("Invocation should have thrown an exception");
+        } catch (InvocationTargetException ite) {
+            RuntimeException re = (RuntimeException) ite.getCause();
+            String msg = re.getMessage();
+            Assert.assertEquals("Uh-oh: hello", msg);
+
+            // The TCCL should have been reset correctly
+            Assert.assertSame(cl, Thread.currentThread().getContextClassLoader());
+        }
     }
 
     @Test
@@ -623,17 +661,6 @@ public class ClientWeavingHookGenericCap
         return consumerBundle;
     }
 
-    /*
-    private Bundle mockSystemBundle() {
-        Bundle systemBundle = EasyMock.createMock(Bundle.class);
-        EasyMock.expect(systemBundle.getBundleId()).andReturn(0L).anyTimes();
-        EasyMock.expect(systemBundle.getSymbolicName()).andReturn("system.bundle").anyTimes();
-        EasyMock.replay(systemBundle);
-
-        return systemBundle;
-    }
-    */
-
     // A classloader that loads anything starting with org.apache.aries.spifly.dynamic.impl1 from it
     // and the rest from the parent. This is to mimic a bundle that holds a specific SPI implementation.
     public static class TestProviderBundleClassLoader extends URLClassLoader {

Added: aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/impl5/MySPIImpl5.java
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/impl5/MySPIImpl5.java?rev=1291908&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/impl5/MySPIImpl5.java (added)
+++ aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/impl5/MySPIImpl5.java Tue Feb 21 16:50:53 2012
@@ -0,0 +1,28 @@
+/**
+ * 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.aries.spifly.dynamic.impl5;
+
+import org.apache.aries.mytest.MySPI;
+
+public class MySPIImpl5 implements MySPI{
+    @Override
+    public String someMethod(String s) {
+        throw new RuntimeException("Uh-oh: " + s);
+    }
+}

Propchange: aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/java/org/apache/aries/spifly/dynamic/impl5/MySPIImpl5.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/META-INF/services/org.apache.aries.mytest.MySPI
URL: http://svn.apache.org/viewvc/aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/META-INF/services/org.apache.aries.mytest.MySPI?rev=1291908&view=auto
==============================================================================
--- aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/META-INF/services/org.apache.aries.mytest.MySPI (added)
+++ aries/trunk/spi-fly/spi-fly-dynamic-bundle/src/test/resources/org/apache/aries/spifly/dynamic/impl5/META-INF/services/org.apache.aries.mytest.MySPI Tue Feb 21 16:50:53 2012
@@ -0,0 +1 @@
+ org.apache.aries.spifly.dynamic.impl5.MySPIImpl5