This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.classloader-0.9.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-classloader.git
commit 58366312eb8851b0659df8985905d09e00ef0137 Author: Carsten Ziegeler <[email protected]> AuthorDate: Tue Jul 14 11:35:44 2009 +0000 Add simple junit test for dynamic class loading through package admin. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/commons/classloader@793858 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 9 +++ .../commons/classloader/impl/ClassLoadingTest.java | 80 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/pom.xml b/pom.xml index 87f1013..1071ee6 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java b/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java new file mode 100644 index 0000000..62f8862 --- /dev/null +++ b/src/test/java/org/apache/sling/commons/classloader/impl/ClassLoadingTest.java @@ -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()); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
