Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 1C375200D41 for ; Tue, 7 Nov 2017 10:19:30 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 1A9BF160C00; Tue, 7 Nov 2017 09:19:30 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 42594160C01 for ; Tue, 7 Nov 2017 10:19:29 +0100 (CET) Received: (qmail 1221 invoked by uid 500); 7 Nov 2017 09:19:28 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 1069 invoked by uid 99); 7 Nov 2017 09:19:28 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 07 Nov 2017 09:19:28 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id BC20784F71; Tue, 7 Nov 2017 09:19:27 +0000 (UTC) Date: Tue, 07 Nov 2017 09:19:29 +0000 To: "commits@sling.apache.org" Subject: [sling-org-apache-sling-commons-classloader] 02/21: Add simple junit test for dynamic class loading through package admin. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: rombert@apache.org In-Reply-To: <151004636737.25049.2132311309169485277@gitbox.apache.org> References: <151004636737.25049.2132311309169485277@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: sling-org-apache-sling-commons-classloader X-Git-Refname: refs/tags/org.apache.sling.commons.classloader-0.9.0 X-Git-Reftype: annotated tag X-Git-Rev: 58366312eb8851b0659df8985905d09e00ef0137 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20171107091927.BC20784F71@gitbox.apache.org> archived-at: Tue, 07 Nov 2017 09:19:30 -0000 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 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 @@ org.slf4j slf4j-api + + + junit + junit + + + org.jmock + jmock-junit4 + 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 "commits@sling.apache.org" .