[jboss-cvs] JBossAS SVN: r81783 - in projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test: java/org/jboss/test/microcontainer/test and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Nov 28 12:13:25 EST 2008
Author: kabir.khan at jboss.com
Date: 2008-11-28 12:13:25 -0500 (Fri, 28 Nov 2008)
New Revision: 81783
Added:
projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java
projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java
projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml
Log:
Add test to make sure that GenericBeanAspectFactory uses the correct classloader to create the aspect instances
Added: projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java
===================================================================
--- projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java (rev 0)
+++ projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/support/ParentLastURLClassLoader.java 2008-11-28 17:13:25 UTC (rev 81783)
@@ -0,0 +1,121 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.microcontainer.support;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.URLConnection;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ParentLastURLClassLoader extends URLClassLoader
+{
+ ClassLoader parent;
+ URLClassLoader delegate;
+
+ public ParentLastURLClassLoader(URL[] urls, ClassLoader parent)
+ {
+ super(urls, parent);
+ delegate = new URLClassLoader(urls);
+ this.parent = parent;
+ }
+
+ @Override
+ public URL findResource(String name)
+ {
+ URL url = delegate.findResource(name);
+ if (url == null && parent instanceof URLClassLoader)
+ {
+ url = ((URLClassLoader)parent).findResource(name);
+ }
+ return url;
+ }
+
+ @Override
+ public URL getResource(String name)
+ {
+ URL url = delegate.getResource(name);
+ if (url == null)
+ {
+ url = parent.getResource(name);
+ }
+ return url;
+ }
+
+ @Override
+ public Class<?> loadClass(String name) throws ClassNotFoundException
+ {
+ Class<?> clazz = findLoadedClass(name);
+ if (clazz != null)
+ {
+ return clazz;
+ }
+ String resourceName = name.replace('.', '/') + ".class";
+ URL mine = delegate.findResource(resourceName);
+ URL parents = parent.getResource(resourceName);
+ if (mine == null || mine.equals(parent))
+ {
+ return delegate.loadClass(name);
+ }
+ else
+ {
+ byte[] bytes = loadBytes(name, resourceName);
+ return defineClass(name, bytes, 0, bytes.length);
+ }
+ }
+
+ private byte[] loadBytes(String name, String resourceName)
+ {
+ InputStream in = delegate.getResourceAsStream(resourceName);
+ byte[] bytes = null;
+ try
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] tmp = new byte[1024];
+ int read = 0;
+ while ( (read = in.read(tmp)) >= 0 )
+ baos.write(tmp, 0, read);
+ return baos.toByteArray();
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Unable to load class byte code " + name, e);
+ }
+ finally
+ {
+ try
+ {
+ in.close();
+ }
+ catch (IOException e)
+ {
+ }
+ }
+ }
+
+}
\ No newline at end of file
Added: projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java
===================================================================
--- projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java (rev 0)
+++ projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/java/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.java 2008-11-28 17:13:25 UTC (rev 81783)
@@ -0,0 +1,154 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.microcontainer.test;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.net.URL;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import junit.framework.Test;
+
+import org.jboss.aop.advice.AspectDefinition;
+import org.jboss.aop.advice.AspectFactory;
+import org.jboss.aop.advice.AspectFactoryWithClassLoader;
+import org.jboss.aop.microcontainer.beans.Aspect;
+import org.jboss.test.aop.junit.AOPMicrocontainerTest;
+import org.jboss.test.microcontainer.support.ParentLastURLClassLoader;
+import org.jboss.test.microcontainer.support.TestAspect;
+
+/**
+ * Checks that an aspect can have its classloader overridden
+ *
+ * @author <a href="mailto:kabir.khan at jboss.com">Kabir Khan</a>
+ */
+public class OverriddenClassLoaderTestCase extends AOPMicrocontainerTest
+{
+ public OverriddenClassLoaderTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static Test suite()
+ {
+ return suite(OverriddenClassLoaderTestCase.class);
+ }
+
+// public static AbstractTestDelegate getDelegate(Class<?> clazz) throws Exception
+// {
+// //Don't use security for this test
+// AbstractTypeTestDelegate delegate = new AbstractTypeTestDelegate(clazz);
+// return delegate;
+// }
+
+
+ public void testOverriddenLoader() throws Exception
+ {
+ ClassLoader loader = createParentLastURLClassLoader();
+
+ Class<?> clazz = loader.loadClass(TestAspect.class.getName());
+ assertNotSame(TestAspect.class, clazz);
+ assertSame(loader, clazz.getClassLoader());
+
+ Aspect aspect = (Aspect)getBean("Aspect");
+ assertNotNull(aspect);
+
+ AspectDefinition def = aspect.getDefinition();
+ AspectFactory factory = def.getFactory();
+
+ Object global = factory.createPerVM();
+ assertSame(this.getClass().getClassLoader(), global.getClass().getClassLoader());
+
+ assertInstanceOf(factory, AspectFactoryWithClassLoader.class);
+ AspectFactoryWithClassLoader factoryCl = (AspectFactoryWithClassLoader)factory;
+
+ factoryCl.pushScopedClassLoader(loader);
+ try
+ {
+ Object scoped = factory.createPerVM();
+ assertSame(loader, scoped.getClass().getClassLoader());
+ }
+ finally
+ {
+ factoryCl.popScopedClassLoader();
+ }
+ }
+
+ private ClassLoader createParentLastURLClassLoader() throws Exception
+ {
+ File jarFile = createJar();
+
+ URL jarUrl = jarFile.toURL();
+ return new ParentLastURLClassLoader(new URL[] {jarUrl}, this.getClass().getClassLoader());
+ }
+
+ private File createJar() throws Exception
+ {
+ String resource = TestAspect.class.getName().replace('.', '/') + ".class";
+ URL url = this.getClass().getClassLoader().getResource(resource);
+ File classFile = new File(url.toURI());
+
+ // File jarFile = new File("test-overridden-classloader.jar");
+ File jarFile = File.createTempFile("test-overridden-classloader", "jar");
+ jarFile.deleteOnExit();
+
+ if (jarFile.exists())
+ {
+ jarFile.delete();
+ }
+ JarOutputStream out = null;
+ try
+ {
+ out = new JarOutputStream(new FileOutputStream(jarFile), new Manifest());
+ JarEntry entry = new JarEntry(resource);
+ out.putNextEntry(entry);
+ FileInputStream in = null;
+ try
+ {
+ in = new FileInputStream(classFile);
+ byte[] buf = new byte[200000];
+ while(true)
+ {
+ int i = in.read(buf, 0, buf.length);
+ if (i <=0)
+ {
+ break;
+ }
+ out.write(buf, 0, i);
+ }
+ }
+ finally
+ {
+ in.close();
+ }
+ }
+ finally
+ {
+ out.close();
+ }
+ getLog().debug("===> Created jar " + jarFile.getAbsolutePath());
+ return jarFile;
+ }
+}
Added: projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml
===================================================================
--- projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml (rev 0)
+++ projects/microcontainer/branches/Branch_2_0/aop-mc-int/src/test/resources/org/jboss/test/microcontainer/test/OverriddenClassLoaderTestCase.xml 2008-11-28 17:13:25 UTC (rev 81783)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+ <bean name="AspectManager" class="org.jboss.aop.AspectManager">
+ <constructor factoryClass="org.jboss.aop.AspectManager" factoryMethod="instance"/>
+ </bean>
+
+ <aspect xmlns="urn:jboss:aop-beans:1.0" name="Aspect" class="org.jboss.test.microcontainer.support.TestAspect"/>
+</deployment>
More information about the jboss-cvs-commits
mailing list