You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2010/10/21 00:52:12 UTC

svn commit: r1025787 - in /felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation: BootDelegationTest.java CustomClassLoader.java external/ExternalClassLoader.java

Author: rickhall
Date: Wed Oct 20 22:52:12 2010
New Revision: 1025787

URL: http://svn.apache.org/viewvc?rev=1025787&view=rev
Log:
Try to test more implicit boot delegation scenarios.

Added:
    felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/CustomClassLoader.java
    felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/external/ExternalClassLoader.java
Modified:
    felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/BootDelegationTest.java

Modified: felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/BootDelegationTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/BootDelegationTest.java?rev=1025787&r1=1025786&r2=1025787&view=diff
==============================================================================
--- felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/BootDelegationTest.java (original)
+++ felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/BootDelegationTest.java Wed Oct 20 22:52:12 2010
@@ -20,6 +20,7 @@ package org.apache.felix.test.framework.
 
 import junit.framework.TestCase;
 import org.apache.felix.test.framework.bootdelegation.external.BundleLoader;
+import org.apache.felix.test.framework.bootdelegation.external.ExternalClassLoader;
 import org.osgi.framework.BundleContext;
 
 public class BootDelegationTest extends TestCase
@@ -108,23 +109,40 @@ public class BootDelegationTest extends 
 
     public void testImplicitBootDelegation()
     {
-        // Try to load a non-boot-delegated class from a bundle via
-        // external code using Bundle.loadClass(). Implicit boot delegation
-        // should not occur.
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is loaded by bundle class loader.
+        //    - Class load requested via Bundle.loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            Class clazz =
+                m_context.getBundle().loadClass("javax.management.Attribute");
+            assertTrue("Class load should have failed: " + clazz, false);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            // We are expecting a failure.
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is loaded by external class loader.
+        //    - Class load requested via Bundle.loadClass().
+        // Implicit boot delegation should NOT occur.
         try
         {
             Class clazz =
                 BundleLoader.loadClass(m_context.getBundle(), "javax.management.Attribute");
-            assertTrue("Class should have failed: " + clazz, false);
+            assertTrue("Class load should have failed: " + clazz, false);
         }
         catch (ClassNotFoundException ex)
         {
             // We are expecting a failure.
         }
 
-        // Try to load a non-boot-delegated class from a bundle via
-        // external code using the bundle's class loader. Implicit boot
-        // delegation should occur.
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is loaded by external class loader.
+        //    - Class load requested via bundle class loader loadClass().
+        // Implicit boot delegation should occur.
         try
         {
             Class clazz =
@@ -133,7 +151,129 @@ public class BootDelegationTest extends 
         }
         catch (ClassNotFoundException ex)
         {
-            assertTrue("Class should have succeeded: " + ex, false);
+            assertTrue("Class load should have succeeded: " + ex, false);
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is a custom class loader loaded by a bundle.
+        //    - Custom class loader delegates to Bundle.loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            ClassLoader loader = new CustomClassLoader(m_context.getBundle());
+            Class clazz = loader.loadClass("javax.management.Attribute");
+            assertTrue("Class load should have failed: " + clazz, false);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            // We are expecting a failure.
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is a custom class loader loaded by a bundle.
+        //    - Custom class loader delegates to bundle class loader loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            ClassLoader loader = new CustomClassLoader(this.getClass().getClassLoader());
+            Class clazz = loader.loadClass("javax.management.Attribute");
+            assertTrue("Class load should have failed: " + clazz, false);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            // We are expecting a failure.
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is a custom class loader loaded by external class loader.
+        //    - Custom class loader delegates to Bundle.loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            ClassLoader loader = new ExternalClassLoader(m_context.getBundle());
+            Class clazz = loader.loadClass("javax.management.Attribute");
+            assertTrue("Class load should have failed: " + clazz, false);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            // We are expecting a failure.
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is a custom class loader loaded by external class loader.
+        //    - Custom class loader delegates to bundle class loader loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            ClassLoader loader = new ExternalClassLoader(this.getClass().getClassLoader());
+            Class clazz = loader.loadClass("javax.management.Attribute");
+        }
+        catch (ClassNotFoundException ex)
+        {
+            assertTrue("Class load should have succeeded: " + ex, false);
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is an inner cass of a custom class loader
+        //      loaded by a bundle.
+        //    - Custom class loader delegates to Bundle.loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            CustomClassLoader loader = new CustomClassLoader(m_context.getBundle());
+            Class clazz = loader.loadClassViaInnerClass("javax.management.Attribute");
+            assertTrue("Class load should have failed: " + clazz, false);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            // We are expecting a failure.
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is an inner cass of a custom class loader
+        //      loaded by a bundle.
+        //    - Custom class loader delegates to bundle class loader loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            CustomClassLoader loader = new CustomClassLoader(this.getClass().getClassLoader());
+            Class clazz = loader.loadClassViaInnerClass("javax.management.Attribute");
+            assertTrue("Class load should have failed: " + clazz, false);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            // We are expecting a failure.
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is an inner cass of a custom class loader
+        //      loaded by external class loader.
+        //    - Custom class loader delegates to Bundle.loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            ExternalClassLoader loader = new ExternalClassLoader(m_context.getBundle());
+            Class clazz = loader.loadClassViaInnerClass("javax.management.Attribute");
+            assertTrue("Class load should have failed: " + clazz, false);
+        }
+        catch (ClassNotFoundException ex)
+        {
+            // We are expecting a failure.
+        }
+
+        // Try to trigger implicit boot delegation under these conditions:
+        //    - Instigating class is an inner cass of a custom class loader
+        //      loaded by external class loader.
+        //    - Custom class loader delegates to bundle class loader loadClass().
+        // Implicit boot delegation should NOT occur.
+        try
+        {
+            ExternalClassLoader loader = new ExternalClassLoader(this.getClass().getClassLoader());
+            Class clazz = loader.loadClassViaInnerClass("javax.management.Attribute");
+        }
+        catch (ClassNotFoundException ex)
+        {
+            assertTrue("Class load should have succeeded: " + ex, false);
         }
     }
 }
\ No newline at end of file

Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/CustomClassLoader.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/CustomClassLoader.java?rev=1025787&view=auto
==============================================================================
--- felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/CustomClassLoader.java (added)
+++ felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/CustomClassLoader.java Wed Oct 20 22:52:12 2010
@@ -0,0 +1,74 @@
+/*
+ * 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.felix.test.framework.bootdelegation;
+
+import java.security.SecureClassLoader;
+import org.osgi.framework.Bundle;
+
+public class CustomClassLoader extends SecureClassLoader
+{
+    private final Bundle m_bundle;
+    private final ClassLoader m_loader;
+
+    public CustomClassLoader(Bundle bundle)
+    {
+        m_bundle = bundle;
+        m_loader = null;
+    }
+
+    public CustomClassLoader(ClassLoader loader)
+    {
+        m_bundle = null;
+        m_loader = loader;
+    }
+
+    public Class loadClass(String name) throws ClassNotFoundException
+    {
+        if (m_bundle != null)
+        {
+            return m_bundle.loadClass(name);
+        }
+        else
+        {
+            return m_loader.loadClass(name);
+        }
+    }
+
+    public Class loadClassViaInnerClass(String name) throws ClassNotFoundException
+    {
+        return new Loader() {
+            public Class loadClass(String name) throws ClassNotFoundException
+            {
+                if (m_bundle != null)
+                {
+                    return m_bundle.loadClass(name);
+                }
+                else
+                {
+                    return m_loader.loadClass(name);
+                }
+            }
+        }.loadClass(name);
+    }
+
+    interface Loader
+    {
+        Class loadClass(String name) throws ClassNotFoundException;
+    }
+}
\ No newline at end of file

Added: felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/external/ExternalClassLoader.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/external/ExternalClassLoader.java?rev=1025787&view=auto
==============================================================================
--- felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/external/ExternalClassLoader.java (added)
+++ felix/sandbox/rickhall/bnd-test/org.apache.felix.test.framework.bootdelegation/src/org/apache/felix/test/framework/bootdelegation/external/ExternalClassLoader.java Wed Oct 20 22:52:12 2010
@@ -0,0 +1,74 @@
+/*
+ * 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.felix.test.framework.bootdelegation.external;
+
+import java.security.SecureClassLoader;
+import org.osgi.framework.Bundle;
+
+public class ExternalClassLoader extends SecureClassLoader
+{
+    private final Bundle m_bundle;
+    private final ClassLoader m_loader;
+
+    public ExternalClassLoader(Bundle bundle)
+    {
+        m_bundle = bundle;
+        m_loader = null;
+    }
+
+    public ExternalClassLoader(ClassLoader loader)
+    {
+        m_bundle = null;
+        m_loader = loader;
+    }
+
+    public Class loadClass(String name) throws ClassNotFoundException
+    {
+        if (m_bundle != null)
+        {
+            return m_bundle.loadClass(name);
+        }
+        else
+        {
+            return m_loader.loadClass(name);
+        }
+    }
+
+    public Class loadClassViaInnerClass(String name) throws ClassNotFoundException
+    {
+        return new Loader() {
+            public Class loadClass(String name) throws ClassNotFoundException
+            {
+                if (m_bundle != null)
+                {
+                    return m_bundle.loadClass(name);
+                }
+                else
+                {
+                    return m_loader.loadClass(name);
+                }
+            }
+        }.loadClass(name);
+    }
+
+    interface Loader
+    {
+        Class loadClass(String name) throws ClassNotFoundException;
+    }
+}
\ No newline at end of file