You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2009/07/14 13:35:45 UTC

svn commit: r793858 - in /sling/trunk/contrib/commons/classloader: ./ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/sling/ src/test/java/org/apache/sling/commons/ src/test/java/org/apache/sling/commons/c...

Author: cziegeler
Date: Tue Jul 14 11:35:44 2009
New Revision: 793858

URL: http://svn.apache.org/viewvc?rev=793858&view=rev
Log:
Add simple junit test for dynamic class loading through package admin.

Added:
    sling/trunk/contrib/commons/classloader/src/test/
    sling/trunk/contrib/commons/classloader/src/test/java/
    sling/trunk/contrib/commons/classloader/src/test/java/org/
    sling/trunk/contrib/commons/classloader/src/test/java/org/apache/
    sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/
    sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/
    sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/
    sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/
    sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java   (with props)
Modified:
    sling/trunk/contrib/commons/classloader/pom.xml

Modified: sling/trunk/contrib/commons/classloader/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/classloader/pom.xml?rev=793858&r1=793857&r2=793858&view=diff
==============================================================================
--- sling/trunk/contrib/commons/classloader/pom.xml (original)
+++ sling/trunk/contrib/commons/classloader/pom.xml Tue Jul 14 11:35:44 2009
@@ -94,5 +94,14 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.jmock</groupId>
+            <artifactId>jmock-junit4</artifactId>
+        </dependency>
     </dependencies>
 </project>

Added: sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java?rev=793858&view=auto
==============================================================================
--- sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java (added)
+++ sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java Tue Jul 14 11:35:44 2009
@@ -0,0 +1,80 @@
+/*
+ * 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.sling.commons.classloader.impl;
+
+import junit.framework.Assert;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.Sequence;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceListener;
+import org.osgi.service.packageadmin.ExportedPackage;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+/**
+ * Simple test for the dynamic class loader.
+ */
+public class ClassLoadingTest {
+
+    protected Mockery context;
+
+    public ClassLoadingTest() {
+        this.context = new JUnit4Mockery();
+    }
+
+    /**
+     * This method tests the dynamic class loading through the package admin.
+     * The returned class changes from map to array list.
+     */
+    @Test public void testLoading() throws Exception {
+        final Sequence sequence = this.context.sequence("load-sequence");
+        final BundleContext bundleContext = this.context.mock(BundleContext.class);
+        final PackageAdmin packageAdmin = this.context.mock(PackageAdmin.class);
+        final ExportedPackage ep = this.context.mock(ExportedPackage.class);
+        final Bundle bundle = this.context.mock(Bundle.class);
+        this.context.checking(new Expectations() {{
+            allowing(bundleContext).createFilter(with(any(String.class)));
+            will(returnValue(null));
+            allowing(bundleContext).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
+            allowing(bundleContext).removeServiceListener(with(any(ServiceListener.class)));
+            allowing(bundleContext).getServiceReferences(with(any(String.class)), with(any(String.class)));
+            will(returnValue(null));
+            allowing(packageAdmin).getExportedPackage("org.apache.sling.test");
+            will(returnValue(ep));
+            allowing(ep).getExportingBundle();
+            will(returnValue(bundle));
+            one(bundle).loadClass("org.apache.sling.test.A"); inSequence(sequence);
+            will(returnValue(java.util.Map.class));
+            one(bundle).loadClass("org.apache.sling.test.A"); inSequence(sequence);
+            will(returnValue(java.util.Map.class));
+            one(bundle).loadClass("org.apache.sling.test.A"); inSequence(sequence);
+            will(returnValue(java.util.ArrayList.class));
+        }});
+        DynamicClassLoaderManagerImpl manager = new DynamicClassLoaderManagerImpl(bundleContext, packageAdmin);
+        final ClassLoader cl = manager.getDynamicClassLoader();
+        final Class c1 = cl.loadClass("org.apache.sling.test.A");
+        Assert.assertEquals("java.util.Map", c1.getName());
+        final Class c2 = cl.loadClass("org.apache.sling.test.A");
+        Assert.assertEquals("java.util.Map", c2.getName());
+        final Class c3 = cl.loadClass("org.apache.sling.test.A");
+        Assert.assertEquals("java.util.ArrayList", c3.getName());
+    }
+}

Propchange: sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/contrib/commons/classloader/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain