[jboss-cvs] JBossAS SVN: r97797 - in projects/jboss-classpool/trunk: classpool/src/main/java/org/jboss/test/classpool/support and 3 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Dec 14 13:04:55 EST 2009
Author: flavia.rainone at jboss.com
Date: 2009-12-14 13:04:55 -0500 (Mon, 14 Dec 2009)
New Revision: 97797
Added:
projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassFactory.java
projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderFactory.java
projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderInfo.java
projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java
projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/SanityTestScenario.java
projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/TestScenario.java
Removed:
projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassFactory.java
projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderFactory.java
projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderInfo.java
projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java
projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/SanityTestScenario.java
projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/TestScenario.java
Modified:
projects/jboss-classpool/trunk/jbosscl/pom.xml
projects/jboss-classpool/trunk/pom.xml
projects/jboss-classpool/trunk/ucl/pom.xml
Log:
[JBREFLECT-75] Common test classes were misplaced in main/java. Moved those to test/java.
Deleted: projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassFactory.java
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassFactory.java 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassFactory.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -1,116 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, 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.classpool.support;
-
-import javassist.CannotCompileException;
-import javassist.ClassPool;
-import javassist.CtClass;
-import junit.framework.Assert;
-
-import org.jboss.classpool.spi.AbstractClassPool;
-import org.jboss.classpool.spi.ClassPoolRepository;
-
-/**
- * Creates classes using ClassPools.
- *
- * @author <a href="flavia.rainone at jboss.com">Flavia Rainone</a>
- * @version $Revision$
- */
-public class ClassFactory
-{
- public static final String PACKAGE = "org.test.classpool.proxy";
- private static final String CLASS = "Proxy";
- private static int counter = 0;
-
- public static Object create(Class<?> parent, ClassLoader loader) throws Exception
- {
- String packageName = parent.getPackage().getName();
- if (!packageName.startsWith("java.") && !packageName.startsWith("sun."))
- {
- packageName += ".";
- }
- else
- {
- packageName = PACKAGE + ".";
- }
- String className = packageName + CLASS + counter++;
- ClassPool pool = ClassPoolRepository.getInstance().getRegisteredCLs().get(loader);
- try
- {
- registerGeneratedClass(pool, className);
- CtClass ctClazz = pool.makeClass(packageName + CLASS + counter++);
- ctClazz.setSuperclass(pool.get(parent.getName()));
- Class<?> clazz = pool.toClass(ctClazz);
- Assert.assertSame(loader, clazz.getClassLoader());
- return clazz.newInstance();
- }
- catch(RuntimeException re)
- {
- unregisterGeneratedClass(pool, className);
- throw re;
- }
- }
-
- /**
- * Utility method to make a new class in a pool. It makes sure that the class is registered with the pool, so others can find it.
- */
- public static CtClass create(ClassPool pool, String name)
- {
- registerGeneratedClass(pool, name);
- return pool.makeClass(name);
- }
-
- /**
- * Utility method to make a new class in a pool. It makes sure that the class is registered with the pool, so others can find it.
- */
- public static CtClass createNested(CtClass outer, String name, boolean isStatic) throws CannotCompileException
- {
- final String classname = outer.getName() + "$" + name;
- try
- {
- registerGeneratedClass(outer.getClassPool(), classname);
- CtClass inner = outer.makeNestedClass(name, true);
- return inner;
- }
- catch (RuntimeException e)
- {
- unregisterGeneratedClass(outer.getClassPool(), classname);
- throw e;
- }
- }
-
- private static void registerGeneratedClass(ClassPool pool, String name)
- {
- if (pool != null && pool instanceof AbstractClassPool)
- {
- ((AbstractClassPool) pool).registerGeneratedClass(name);
- }
- }
-
- private static void unregisterGeneratedClass(ClassPool pool, String name)
- {
- if (pool != null && pool instanceof AbstractClassPool)
- {
- ((AbstractClassPool) pool).doneGeneratingClass(name);
- }
- }
-}
Deleted: projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderFactory.java
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderFactory.java 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderFactory.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -1,85 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, 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.classpool.support;
-
-import java.util.Collection;
-
-
-/**
- * Creates class loaders for {@link TestScenario}.
- *
- * @author <a href="mailto:flavia.rainone at jboss.org">Flavia Rainone</a>
- * @version $Revision$
- */
-
-public interface ClassLoaderFactory<I extends ClassLoaderInfo>
-{
- /**
- * Creates a class loader.
- *
- * @param classLoaderInfo information necessary for the creation of the class loader
- * @return the created class loader
- * @throws Exception if an unexpected error occurs
- */
- public ClassLoader create(I classLoaderInfo) throws Exception;
-
- /**
- * Destroys the created class loader.
- *
- * @param classLoaderInfo identifies the class loader to be destroyed
- * @throws Exception if an unexpected error occurs
- */
- public void destroy(I classLoaderInfo) throws Exception;
-
- /**
- * Destroys all class loaders that have been created by this factory so far.
- */
- public void destroyAll();
-
- /**
- * Retrieves a class loader that has been previously created.
- *
- * @param classLoaderInfo identifies the class loader to be retrieved
- * @return the class loader corresponding to {@code classLoaderInfo}
- * @throws Exception if an unexpected error occurs
- */
- public ClassLoader retrieve(I classLoaderInfo) throws Exception;
-
- /**
- * Retrieves all class loaders that have been created so far.
- *
- * @return a collection with all class loaders that have been created (excluding the ones
- * that have been destroyed)
- */
- public Collection<ClassLoader> retrieveAll();
-
- /**
- * Checks whether the class loader corresponding to {@code classLoaderInfo} is available
- * (i.e. whether is has been created but is not destroyed yet).
- *
- * @param classLoaderBuilder identifies the loader whose availability is going to be checked
- * @return {@code true} only if a class loader has been created for {@code classLoaderInfo}
- * and has not been destroyed
- * @throws Exception if an unexpected error occurs
- */
- public boolean isAvailable(I classLoaderInfo) throws Exception;
-}
\ No newline at end of file
Deleted: projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderInfo.java
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderInfo.java 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderInfo.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -1,31 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, 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.classpool.support;
-
-/**
- * Contains information necessary for the ClassLoaderFactory that will be used to
- * create the ClassLoaders.
- *
- * @author <a href="mailto:flavia.rainone at jboss.org">Flavia Rainone</a>
- * @version $Revision$
- */
-public interface ClassLoaderInfo {}
\ No newline at end of file
Deleted: projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -1,206 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, 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.classpool.support;
-
-import java.net.URL;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-import javassist.ClassPool;
-import javassist.CtClass;
-import javassist.NotFoundException;
-import junit.framework.Assert;
-
-import org.jboss.classpool.spi.ClassPoolRepository;
-import org.jboss.reflect.plugins.javassist.classpool.ClassPoolFactory;
-
-/**
- * This scenario is used by class pool test cases. All the operations performed using this
- * scenario are executed over CtClasses and ClassPools.
- *
- * @param <I> the ClassLoaderInfo that will be used to create class loaders for this scenario
- *
- * @author <a href="mailto:flavia.rainone at jboss.com">Flavia Rainone</a>
- * @version $Revision$
- */
-public class ClassPoolTestScenario<I extends ClassLoaderInfo> extends TestScenario<CtClass, ClassPool, I>
-{
- /** The ClassPool repository */
- protected ClassPoolRepository repository;
-
- /**
- * Constructor.
- *
- * @param classLoaderFactory creates class loaders for the loader scenario.
- * @param <I> the info that {@code classLoaderFactory} uses to create a class loader
- */
- public ClassPoolTestScenario(ClassLoaderFactory<I> classLoaderFactory)
- {
- super(classLoaderFactory);
- repository = ClassPoolRepository.getInstance();
- }
-
- @Override
- public ClassPool getLoader(CtClass clazz)
- {
- return assertRegisterClassLoader(clazz.getClassPool().getClassLoader());
- }
-
- @Override
- public ClassPool getLoader(ClassLoader classLoader)
- {
- return assertRegisterClassLoader(classLoader);
- }
-
- @Override
- public ClassLoader getClassLoader(ClassPool classPool)
- {
- return classPool.getClassLoader();
- }
-
- @Override
- public CtClass loadClass(ClassPool classPool, ClassPool expectedClassPool, String className)
- throws Exception
- {
- Class<?> clazz = assertLoadClass(classPool.getClassLoader(), expectedClassPool.getClassLoader(), className);
- CtClass ctClass = assertLoadCtClass(className, classPool, expectedClassPool);
- Assert.assertEquals(className, ctClass.getName());
- Assert.assertEquals("Class has been loaded by the wrong class loader: " + className,
- clazz.getClassLoader(), ctClass.getClassPool().getClassLoader());
- return ctClass;
- }
-
- @Override
- public void cannotLoadClass(ClassPool classPool, String className)
- {
- try
- {
- classPool.getCtClass(className);
- Assert.fail("Should not have been able to load " + className);
- }
- catch(NotFoundException expected){}
- }
-
- @Override
- public CtClass getMethodReturnType(CtClass clazz, String methodName) throws Exception
- {
- return clazz.getDeclaredMethod(methodName).getReturnType();
- }
-
- /********* HELPER METHODS ***************************************************************/
-
- private ClassPool assertRegisterClassLoader(ClassLoader loader)
- {
- ClassPool classPool = repository.registerClassLoader(loader);
- Assert.assertNotNull(classPool);
- assertBootstrap(classPool, String.class, Class.class, ClassLoader.class, Object.class,
- Collection.class, List.class, Map.class, URL.class);
- return classPool;
- }
-
- private void assertBootstrap(ClassPool classPool, Class<?>... classes)
- {
- assertSameClassPool(null, classPool, int.class, boolean.class, byte.class, short.class,
- long.class, float.class, double.class, char.class, void.class);
- // FIXME JBREFLECT-80
- //assertSameClassPool(ClassPoolFactory.getDefault(), classPool, classes);
-
- }
-
- private void assertSameClassPool(ClassPool expectedClassPool, ClassPool classPool,
- Class<?>... classes)
- {
- for (Class<?> clazz: classes)
- {
- CtClass ctClass = null;
- try
- {
- ctClass = classPool.getCtClass(clazz.getName());
- } catch (NotFoundException e)
- {
- e.printStackTrace();
- Assert.fail(e.getMessage());
- }
- Assert.assertSame("Class " + clazz.getName() + " loaded by unexpected class pool ",
- expectedClassPool, ctClass.getClassPool());
- }
- }
-
- protected CtClass assertLoadCtClass(String name, ClassPool initiating, ClassPool expected) throws Exception
- {
- CtClass clazz = initiating.get(name);
- if (expected != null)
- {
- Assert.assertSame(expected, clazz.getClassPool());
- }
- //Load twice to test both create and cache
- clazz = initiating.get(name);
- if (expected != null)
- {
- Assert.assertSame(expected, clazz.getClassPool());
- }
-
- assertLoadCtClassArray(name, clazz, initiating, expected);
-
- return clazz;
- }
-
- private void assertLoadCtClassArray(String name, CtClass clazz, ClassPool initiating, ClassPool expected) throws Exception
- {
- assertLoadCtClassArray(name, clazz, 1, initiating, expected);
- assertLoadCtClassArray(name, clazz, 2, initiating, expected);
- }
-
- private void assertLoadCtClassArray(String name, CtClass clazz, int dimensions, ClassPool initiating, ClassPool expected) throws Exception
- {
- String arrayName = name;
- for (int i = 0 ; i < dimensions ; i++)
- {
- arrayName = arrayName + "[]";
- }
- CtClass array = initiating.get(arrayName);
-
- if (expected != null)
- {
- Assert.assertSame(expected, array.getClassPool());
- }
-
- Assert.assertSame(clazz.getClassPool(), array.getClassPool());
-
- CtClass type = array;
- for (int i = 0 ; i < dimensions ; i++)
- {
- type = type.getComponentType();
- }
- Assert.assertSame(type, clazz);
- }
-
- @Override
- protected void preDestroy(Collection<ClassLoader> classLoaders)
- {
- for (ClassLoader classLoader: classLoaders)
- {
- repository.unregisterClassLoader(classLoader);
- }
- }
-}
Deleted: projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/SanityTestScenario.java
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/SanityTestScenario.java 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/SanityTestScenario.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -1,89 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, 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.classpool.support;
-
-
-import junit.framework.Assert;
-
-/**
- * This scenario is used by sanity tests, i.e., tests that check whether the class loaders
- * represented by class pools actually behave as expected.
- *
- * @param <I> the ClassLoaderInfo that will be used to create class loaders for this scenario
- *
- * @author <a href="mailto:flavia.rainone at jboss.com">Flavia Rainone</a>
- * @version $Revision$
- */
-public class SanityTestScenario<I extends ClassLoaderInfo> extends TestScenario<Class<?>, ClassLoader, I>
-{
- /**
- * Constructor.
- *
- * @param classLoaderFactory creates class loaders for the loader scenario.
- * @param <I> the info that {@code classLoaderFactory} uses to create a class loader
- */
- public SanityTestScenario(ClassLoaderFactory<I> classLoaderFactory)
- {
- super(classLoaderFactory);
- }
-
- @Override
- public ClassLoader getLoader(Class<?> clazz)
- {
- return clazz.getClassLoader();
- }
-
- @Override
- public ClassLoader getLoader(ClassLoader classLoader)
- {
- return classLoader;
- }
-
- @Override
- public ClassLoader getClassLoader(ClassLoader loader)
- {
- return loader;
- }
-
- @Override
- public Class<?> loadClass(ClassLoader loader, ClassLoader expected, String className) throws Exception
- {
- return assertLoadClass(loader, expected, className);
- }
-
- @Override
- public void cannotLoadClass(ClassLoader classLoader, String className)
- {
- try
- {
- classLoader.loadClass(className);
- Assert.fail("Should not have been able to load " + className);
- }
- catch(ClassNotFoundException expected){}
- }
-
- @Override
- public Class<?> getMethodReturnType(Class<?> clazz, String methodName) throws Exception
- {
- return clazz.getDeclaredMethod(methodName).getReturnType();
- }
-}
\ No newline at end of file
Deleted: projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/TestScenario.java
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/TestScenario.java 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/TestScenario.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -1,219 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, 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.classpool.support;
-
-import java.util.Collection;
-
-import junit.framework.Assert;
-
-
-/**
- * Abstracts the concept of loader (L) and classes (C).
- * This class allows the implementation of tests that don't rely on the actual type of
- * L and C.
- * @param <C> represents a class
- * @param <L> responsbile for loading a class
- * @param <I> the ClassLoaderInfo that will be used to create class loaders for this scenario
- *
- * @author <a href="mailto:flavia.rainone at jboss.com">Flavia Rainone</a>
- * @version $Revision$
- * @see SanityTestScenario
- * @see ClassPoolTestScenario
- */
-public abstract class TestScenario<C,L,I extends ClassLoaderInfo>
-{
- /** The classloader factory */
- private ClassLoaderFactory<I> classLoaderFactory;
-
- /**
- * Constructor.
- *
- * @param classLoaderFactory creates class loaders for the loader scenario.
- * @param <I> the info that {@code classLoaderFactory} uses to create a class loader
- */
- public TestScenario(ClassLoaderFactory<I> classLoaderFactory)
- {
- this.classLoaderFactory = classLoaderFactory;
- }
-
- /**
- * Creates a loader.
- *
- * @param classLoaderBuilder contains all the info necessary for the creation of a loader
- * @return the created loader
- * @throws Exception if an unexpected error occurs
- */
- public L createLoader(I classLoaderInfo) throws Exception
- {
- ClassLoader classLoader = classLoaderFactory.create(classLoaderInfo);
- return getLoader(classLoader);
- }
-
- /**
- * Destroys the loader corresponding to classLoaderBuilder.
- * This method can be invoked only after the loader has been
- * {@link #createLoader(I) created}.
- *
- * @param classLoaderInfo contains information regarding the loader to be destroyed.
- * @throws Exception if an unexpected error occurs
- * @see #createLoader(I)
- */
- public void destroyLoader(I classLoaderInfo) throws Exception
- {
- classLoaderFactory.destroy(classLoaderInfo);
- }
-
- /**
- * Destroy all loaders that have been created so far (except for loaders that have already
- * been {@link #destroyLoader(I) destroyed}).
- */
- public void destroyLoaders()
- {
- preDestroy(classLoaderFactory.retrieveAll());
- classLoaderFactory.destroyAll();
- }
-
- /**
- * Returns the loader corresponding to {@code classLoaderBuilder}. This method can be
- * invoked only after {@code #createLoader(I)}.
- *
- * @param classLoaderInfo contains all the info related to the loader to be retrieved
- * @return the loader equivalent to {@code classLoaderBuilder}
- * @throws Exception if an unexpected error occurs
- * @see {@link #createLoader(I)}
- */
- public L getLoader(I classLoaderInfo) throws Exception
- {
- ClassLoader classLoader = classLoaderFactory.retrieve(classLoaderInfo);
- return getLoader(classLoader);
- }
-
- /**
- * Checks that the loader corresponding to {@code classLoaderBuilder} is no longer
- * available on the test scenario.
- *
- * @param classLoaderInfo identifies the loader that is going to be verified
- * @throws Exception if an unexpected error occurs
- */
- public void cannotGetLoader(I classLoaderInfo) throws Exception
- {
- Assert.assertFalse("Class loader should not be available",
- classLoaderFactory.isAvailable(classLoaderInfo));
- }
-
- /**
- * Returns the loader that loaded {@code clazz}.
- *
- * @param clazz a class that has been previously loaded
- * @return the loader that loaded {@code clazz}
- */
- public abstract L getLoader(C clazz);
-
- /**
- * Returns the loader corresponding to {@code classLoader}.
- *
- * @param classLoader a class loader
- * @return the loader that represents {code classLoader}
- */
- public abstract L getLoader(ClassLoader classLoader);
-
- /**
- * Returns the class loader corresponding to {@code loader}.
- *
- * @param loader a loader
- * @return the class loader that is represented by loader
- */
- public abstract ClassLoader getClassLoader(L loader);
-
- /**
- * Checks that {@code loader} can load {@code className} and returns the loaded class.
- * Also verifies that the loader associated with the loaded class is {@code loader}.
- *
- * @param loader a loader
- * @param className the name of the class to be loaded
- * @return the class loaded by {@code loader}
- * @throws Exception if an unexpected error occurs
- */
- public C loadClass(L loader, String className) throws Exception
- {
- return loadClass(loader, loader, className);
- }
-
- /**
- * Checks that {@code loader} can load {@code className} and returns the loaded class.
- * Also verifies that the loader associated with the loaded class is {@code expected}.
- *
- * @param initiating the loader that will be used for loading {@code className}
- * @param expected the actual loader that is associated with the loaded class
- * @param className the name of the class to be loaded
- * @return the loaded class
- * @throws Exception if an unexpected error occurs
- */
- public abstract C loadClass(L initiating, L expected, String className) throws Exception;
-
- /**
- * Checks that {@code loader} cannot load class {@code className}.
- *
- * @param loader a loader
- * @param className the name of the class that can't be loaded by {@code loader}
- */
- public abstract void cannotLoadClass(L loader, String className);
-
- /**
- * Returns the return type of a method declared in {@code clazz}.
- *
- * @param clazz the class that contains the method
- * @param methodName the name of the method
- * @return the return type of the method
- * @throws Exception if an unexpected error occurs
- */
- public abstract C getMethodReturnType(C clazz, String methodName) throws Exception;
-
- /**
- * Asserts that {@code initiating} class loader can load {@code classname}.
- * Also verifies that the class loader associated with the loaded class is {@code expected}
- *
- * @param initiating the class loader that will be used for loading {@code className}
- * @param expected the actual class loader that is associated with the loaded class
- * @param className the name of the class to be loaded
- * @return the loaded class
- * @throws Exception if an unexpected error occurs
- */
- protected Class<?> assertLoadClass(ClassLoader initiating, ClassLoader expected, String className) throws Exception
- {
- Class<?> clazz = initiating.loadClass(className);
- if (expected != null)
- {
- Assert.assertSame(expected, clazz.getClassLoader());
- }
- return clazz;
- }
-
- /**
- * Is called prior to destroy the class loaders in {@code classLoaders} collection.
- *
- * @param classLoaders the class loaders that will be destroyed
- */
- protected void preDestroy(Collection<ClassLoader> classLoaders)
- {
- }
-}
\ No newline at end of file
Copied: projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassFactory.java (from rev 97737, projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassFactory.java)
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassFactory.java (rev 0)
+++ projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassFactory.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -0,0 +1,116 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.classpool.support;
+
+import javassist.CannotCompileException;
+import javassist.ClassPool;
+import javassist.CtClass;
+import junit.framework.Assert;
+
+import org.jboss.classpool.spi.AbstractClassPool;
+import org.jboss.classpool.spi.ClassPoolRepository;
+
+/**
+ * Creates classes using ClassPools.
+ *
+ * @author <a href="flavia.rainone at jboss.com">Flavia Rainone</a>
+ * @version $Revision$
+ */
+public class ClassFactory
+{
+ public static final String PACKAGE = "org.test.classpool.proxy";
+ private static final String CLASS = "Proxy";
+ private static int counter = 0;
+
+ public static Object create(Class<?> parent, ClassLoader loader) throws Exception
+ {
+ String packageName = parent.getPackage().getName();
+ if (!packageName.startsWith("java.") && !packageName.startsWith("sun."))
+ {
+ packageName += ".";
+ }
+ else
+ {
+ packageName = PACKAGE + ".";
+ }
+ String className = packageName + CLASS + counter++;
+ ClassPool pool = ClassPoolRepository.getInstance().getRegisteredCLs().get(loader);
+ try
+ {
+ registerGeneratedClass(pool, className);
+ CtClass ctClazz = pool.makeClass(packageName + CLASS + counter++);
+ ctClazz.setSuperclass(pool.get(parent.getName()));
+ Class<?> clazz = pool.toClass(ctClazz);
+ Assert.assertSame(loader, clazz.getClassLoader());
+ return clazz.newInstance();
+ }
+ catch(RuntimeException re)
+ {
+ unregisterGeneratedClass(pool, className);
+ throw re;
+ }
+ }
+
+ /**
+ * Utility method to make a new class in a pool. It makes sure that the class is registered with the pool, so others can find it.
+ */
+ public static CtClass create(ClassPool pool, String name)
+ {
+ registerGeneratedClass(pool, name);
+ return pool.makeClass(name);
+ }
+
+ /**
+ * Utility method to make a new class in a pool. It makes sure that the class is registered with the pool, so others can find it.
+ */
+ public static CtClass createNested(CtClass outer, String name, boolean isStatic) throws CannotCompileException
+ {
+ final String classname = outer.getName() + "$" + name;
+ try
+ {
+ registerGeneratedClass(outer.getClassPool(), classname);
+ CtClass inner = outer.makeNestedClass(name, true);
+ return inner;
+ }
+ catch (RuntimeException e)
+ {
+ unregisterGeneratedClass(outer.getClassPool(), classname);
+ throw e;
+ }
+ }
+
+ private static void registerGeneratedClass(ClassPool pool, String name)
+ {
+ if (pool != null && pool instanceof AbstractClassPool)
+ {
+ ((AbstractClassPool) pool).registerGeneratedClass(name);
+ }
+ }
+
+ private static void unregisterGeneratedClass(ClassPool pool, String name)
+ {
+ if (pool != null && pool instanceof AbstractClassPool)
+ {
+ ((AbstractClassPool) pool).doneGeneratingClass(name);
+ }
+ }
+}
Copied: projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderFactory.java (from rev 97737, projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderFactory.java)
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderFactory.java (rev 0)
+++ projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderFactory.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -0,0 +1,85 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.classpool.support;
+
+import java.util.Collection;
+
+
+/**
+ * Creates class loaders for {@link TestScenario}.
+ *
+ * @author <a href="mailto:flavia.rainone at jboss.org">Flavia Rainone</a>
+ * @version $Revision$
+ */
+
+public interface ClassLoaderFactory<I extends ClassLoaderInfo>
+{
+ /**
+ * Creates a class loader.
+ *
+ * @param classLoaderInfo information necessary for the creation of the class loader
+ * @return the created class loader
+ * @throws Exception if an unexpected error occurs
+ */
+ public ClassLoader create(I classLoaderInfo) throws Exception;
+
+ /**
+ * Destroys the created class loader.
+ *
+ * @param classLoaderInfo identifies the class loader to be destroyed
+ * @throws Exception if an unexpected error occurs
+ */
+ public void destroy(I classLoaderInfo) throws Exception;
+
+ /**
+ * Destroys all class loaders that have been created by this factory so far.
+ */
+ public void destroyAll();
+
+ /**
+ * Retrieves a class loader that has been previously created.
+ *
+ * @param classLoaderInfo identifies the class loader to be retrieved
+ * @return the class loader corresponding to {@code classLoaderInfo}
+ * @throws Exception if an unexpected error occurs
+ */
+ public ClassLoader retrieve(I classLoaderInfo) throws Exception;
+
+ /**
+ * Retrieves all class loaders that have been created so far.
+ *
+ * @return a collection with all class loaders that have been created (excluding the ones
+ * that have been destroyed)
+ */
+ public Collection<ClassLoader> retrieveAll();
+
+ /**
+ * Checks whether the class loader corresponding to {@code classLoaderInfo} is available
+ * (i.e. whether is has been created but is not destroyed yet).
+ *
+ * @param classLoaderBuilder identifies the loader whose availability is going to be checked
+ * @return {@code true} only if a class loader has been created for {@code classLoaderInfo}
+ * and has not been destroyed
+ * @throws Exception if an unexpected error occurs
+ */
+ public boolean isAvailable(I classLoaderInfo) throws Exception;
+}
\ No newline at end of file
Copied: projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderInfo.java (from rev 97737, projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassLoaderInfo.java)
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderInfo.java (rev 0)
+++ projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassLoaderInfo.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.classpool.support;
+
+/**
+ * Contains information necessary for the ClassLoaderFactory that will be used to
+ * create the ClassLoaders.
+ *
+ * @author <a href="mailto:flavia.rainone at jboss.org">Flavia Rainone</a>
+ * @version $Revision$
+ */
+public interface ClassLoaderInfo {}
\ No newline at end of file
Copied: projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java (from rev 97748, projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java)
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java (rev 0)
+++ projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/ClassPoolTestScenario.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -0,0 +1,206 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.classpool.support;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import javassist.ClassPool;
+import javassist.CtClass;
+import javassist.NotFoundException;
+import junit.framework.Assert;
+
+import org.jboss.classpool.spi.ClassPoolRepository;
+import org.jboss.reflect.plugins.javassist.classpool.ClassPoolFactory;
+
+/**
+ * This scenario is used by class pool test cases. All the operations performed using this
+ * scenario are executed over CtClasses and ClassPools.
+ *
+ * @param <I> the ClassLoaderInfo that will be used to create class loaders for this scenario
+ *
+ * @author <a href="mailto:flavia.rainone at jboss.com">Flavia Rainone</a>
+ * @version $Revision$
+ */
+public class ClassPoolTestScenario<I extends ClassLoaderInfo> extends TestScenario<CtClass, ClassPool, I>
+{
+ /** The ClassPool repository */
+ protected ClassPoolRepository repository;
+
+ /**
+ * Constructor.
+ *
+ * @param classLoaderFactory creates class loaders for the loader scenario.
+ * @param <I> the info that {@code classLoaderFactory} uses to create a class loader
+ */
+ public ClassPoolTestScenario(ClassLoaderFactory<I> classLoaderFactory)
+ {
+ super(classLoaderFactory);
+ repository = ClassPoolRepository.getInstance();
+ }
+
+ @Override
+ public ClassPool getLoader(CtClass clazz)
+ {
+ return assertRegisterClassLoader(clazz.getClassPool().getClassLoader());
+ }
+
+ @Override
+ public ClassPool getLoader(ClassLoader classLoader)
+ {
+ return assertRegisterClassLoader(classLoader);
+ }
+
+ @Override
+ public ClassLoader getClassLoader(ClassPool classPool)
+ {
+ return classPool.getClassLoader();
+ }
+
+ @Override
+ public CtClass loadClass(ClassPool classPool, ClassPool expectedClassPool, String className)
+ throws Exception
+ {
+ Class<?> clazz = assertLoadClass(classPool.getClassLoader(), expectedClassPool.getClassLoader(), className);
+ CtClass ctClass = assertLoadCtClass(className, classPool, expectedClassPool);
+ Assert.assertEquals(className, ctClass.getName());
+ Assert.assertEquals("Class has been loaded by the wrong class loader: " + className,
+ clazz.getClassLoader(), ctClass.getClassPool().getClassLoader());
+ return ctClass;
+ }
+
+ @Override
+ public void cannotLoadClass(ClassPool classPool, String className)
+ {
+ try
+ {
+ classPool.getCtClass(className);
+ Assert.fail("Should not have been able to load " + className);
+ }
+ catch(NotFoundException expected){}
+ }
+
+ @Override
+ public CtClass getMethodReturnType(CtClass clazz, String methodName) throws Exception
+ {
+ return clazz.getDeclaredMethod(methodName).getReturnType();
+ }
+
+ /********* HELPER METHODS ***************************************************************/
+
+ private ClassPool assertRegisterClassLoader(ClassLoader loader)
+ {
+ ClassPool classPool = repository.registerClassLoader(loader);
+ Assert.assertNotNull(classPool);
+ assertBootstrap(classPool, String.class, Class.class, ClassLoader.class, Object.class,
+ Collection.class, List.class, Map.class, URL.class);
+ return classPool;
+ }
+
+ private void assertBootstrap(ClassPool classPool, Class<?>... classes)
+ {
+ assertSameClassPool(null, classPool, int.class, boolean.class, byte.class, short.class,
+ long.class, float.class, double.class, char.class, void.class);
+ // FIXME JBREFLECT-80
+ //assertSameClassPool(ClassPoolFactory.getDefault(), classPool, classes);
+
+ }
+
+ private void assertSameClassPool(ClassPool expectedClassPool, ClassPool classPool,
+ Class<?>... classes)
+ {
+ for (Class<?> clazz: classes)
+ {
+ CtClass ctClass = null;
+ try
+ {
+ ctClass = classPool.getCtClass(clazz.getName());
+ } catch (NotFoundException e)
+ {
+ e.printStackTrace();
+ Assert.fail(e.getMessage());
+ }
+ Assert.assertSame("Class " + clazz.getName() + " loaded by unexpected class pool ",
+ expectedClassPool, ctClass.getClassPool());
+ }
+ }
+
+ protected CtClass assertLoadCtClass(String name, ClassPool initiating, ClassPool expected) throws Exception
+ {
+ CtClass clazz = initiating.get(name);
+ if (expected != null)
+ {
+ Assert.assertSame(expected, clazz.getClassPool());
+ }
+ //Load twice to test both create and cache
+ clazz = initiating.get(name);
+ if (expected != null)
+ {
+ Assert.assertSame(expected, clazz.getClassPool());
+ }
+
+ assertLoadCtClassArray(name, clazz, initiating, expected);
+
+ return clazz;
+ }
+
+ private void assertLoadCtClassArray(String name, CtClass clazz, ClassPool initiating, ClassPool expected) throws Exception
+ {
+ assertLoadCtClassArray(name, clazz, 1, initiating, expected);
+ assertLoadCtClassArray(name, clazz, 2, initiating, expected);
+ }
+
+ private void assertLoadCtClassArray(String name, CtClass clazz, int dimensions, ClassPool initiating, ClassPool expected) throws Exception
+ {
+ String arrayName = name;
+ for (int i = 0 ; i < dimensions ; i++)
+ {
+ arrayName = arrayName + "[]";
+ }
+ CtClass array = initiating.get(arrayName);
+
+ if (expected != null)
+ {
+ Assert.assertSame(expected, array.getClassPool());
+ }
+
+ Assert.assertSame(clazz.getClassPool(), array.getClassPool());
+
+ CtClass type = array;
+ for (int i = 0 ; i < dimensions ; i++)
+ {
+ type = type.getComponentType();
+ }
+ Assert.assertSame(type, clazz);
+ }
+
+ @Override
+ protected void preDestroy(Collection<ClassLoader> classLoaders)
+ {
+ for (ClassLoader classLoader: classLoaders)
+ {
+ repository.unregisterClassLoader(classLoader);
+ }
+ }
+}
Copied: projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/SanityTestScenario.java (from rev 97737, projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/SanityTestScenario.java)
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/SanityTestScenario.java (rev 0)
+++ projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/SanityTestScenario.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -0,0 +1,89 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.classpool.support;
+
+
+import junit.framework.Assert;
+
+/**
+ * This scenario is used by sanity tests, i.e., tests that check whether the class loaders
+ * represented by class pools actually behave as expected.
+ *
+ * @param <I> the ClassLoaderInfo that will be used to create class loaders for this scenario
+ *
+ * @author <a href="mailto:flavia.rainone at jboss.com">Flavia Rainone</a>
+ * @version $Revision$
+ */
+public class SanityTestScenario<I extends ClassLoaderInfo> extends TestScenario<Class<?>, ClassLoader, I>
+{
+ /**
+ * Constructor.
+ *
+ * @param classLoaderFactory creates class loaders for the loader scenario.
+ * @param <I> the info that {@code classLoaderFactory} uses to create a class loader
+ */
+ public SanityTestScenario(ClassLoaderFactory<I> classLoaderFactory)
+ {
+ super(classLoaderFactory);
+ }
+
+ @Override
+ public ClassLoader getLoader(Class<?> clazz)
+ {
+ return clazz.getClassLoader();
+ }
+
+ @Override
+ public ClassLoader getLoader(ClassLoader classLoader)
+ {
+ return classLoader;
+ }
+
+ @Override
+ public ClassLoader getClassLoader(ClassLoader loader)
+ {
+ return loader;
+ }
+
+ @Override
+ public Class<?> loadClass(ClassLoader loader, ClassLoader expected, String className) throws Exception
+ {
+ return assertLoadClass(loader, expected, className);
+ }
+
+ @Override
+ public void cannotLoadClass(ClassLoader classLoader, String className)
+ {
+ try
+ {
+ classLoader.loadClass(className);
+ Assert.fail("Should not have been able to load " + className);
+ }
+ catch(ClassNotFoundException expected){}
+ }
+
+ @Override
+ public Class<?> getMethodReturnType(Class<?> clazz, String methodName) throws Exception
+ {
+ return clazz.getDeclaredMethod(methodName).getReturnType();
+ }
+}
\ No newline at end of file
Copied: projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/TestScenario.java (from rev 97737, projects/jboss-classpool/trunk/classpool/src/main/java/org/jboss/test/classpool/support/TestScenario.java)
===================================================================
--- projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/TestScenario.java (rev 0)
+++ projects/jboss-classpool/trunk/classpool/src/test/java/org/jboss/test/classpool/support/TestScenario.java 2009-12-14 18:04:55 UTC (rev 97797)
@@ -0,0 +1,219 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.classpool.support;
+
+import java.util.Collection;
+
+import junit.framework.Assert;
+
+
+/**
+ * Abstracts the concept of loader (L) and classes (C).
+ * This class allows the implementation of tests that don't rely on the actual type of
+ * L and C.
+ * @param <C> represents a class
+ * @param <L> responsbile for loading a class
+ * @param <I> the ClassLoaderInfo that will be used to create class loaders for this scenario
+ *
+ * @author <a href="mailto:flavia.rainone at jboss.com">Flavia Rainone</a>
+ * @version $Revision$
+ * @see SanityTestScenario
+ * @see ClassPoolTestScenario
+ */
+public abstract class TestScenario<C,L,I extends ClassLoaderInfo>
+{
+ /** The classloader factory */
+ private ClassLoaderFactory<I> classLoaderFactory;
+
+ /**
+ * Constructor.
+ *
+ * @param classLoaderFactory creates class loaders for the loader scenario.
+ * @param <I> the info that {@code classLoaderFactory} uses to create a class loader
+ */
+ public TestScenario(ClassLoaderFactory<I> classLoaderFactory)
+ {
+ this.classLoaderFactory = classLoaderFactory;
+ }
+
+ /**
+ * Creates a loader.
+ *
+ * @param classLoaderBuilder contains all the info necessary for the creation of a loader
+ * @return the created loader
+ * @throws Exception if an unexpected error occurs
+ */
+ public L createLoader(I classLoaderInfo) throws Exception
+ {
+ ClassLoader classLoader = classLoaderFactory.create(classLoaderInfo);
+ return getLoader(classLoader);
+ }
+
+ /**
+ * Destroys the loader corresponding to classLoaderBuilder.
+ * This method can be invoked only after the loader has been
+ * {@link #createLoader(I) created}.
+ *
+ * @param classLoaderInfo contains information regarding the loader to be destroyed.
+ * @throws Exception if an unexpected error occurs
+ * @see #createLoader(I)
+ */
+ public void destroyLoader(I classLoaderInfo) throws Exception
+ {
+ classLoaderFactory.destroy(classLoaderInfo);
+ }
+
+ /**
+ * Destroy all loaders that have been created so far (except for loaders that have already
+ * been {@link #destroyLoader(I) destroyed}).
+ */
+ public void destroyLoaders()
+ {
+ preDestroy(classLoaderFactory.retrieveAll());
+ classLoaderFactory.destroyAll();
+ }
+
+ /**
+ * Returns the loader corresponding to {@code classLoaderBuilder}. This method can be
+ * invoked only after {@code #createLoader(I)}.
+ *
+ * @param classLoaderInfo contains all the info related to the loader to be retrieved
+ * @return the loader equivalent to {@code classLoaderBuilder}
+ * @throws Exception if an unexpected error occurs
+ * @see {@link #createLoader(I)}
+ */
+ public L getLoader(I classLoaderInfo) throws Exception
+ {
+ ClassLoader classLoader = classLoaderFactory.retrieve(classLoaderInfo);
+ return getLoader(classLoader);
+ }
+
+ /**
+ * Checks that the loader corresponding to {@code classLoaderBuilder} is no longer
+ * available on the test scenario.
+ *
+ * @param classLoaderInfo identifies the loader that is going to be verified
+ * @throws Exception if an unexpected error occurs
+ */
+ public void cannotGetLoader(I classLoaderInfo) throws Exception
+ {
+ Assert.assertFalse("Class loader should not be available",
+ classLoaderFactory.isAvailable(classLoaderInfo));
+ }
+
+ /**
+ * Returns the loader that loaded {@code clazz}.
+ *
+ * @param clazz a class that has been previously loaded
+ * @return the loader that loaded {@code clazz}
+ */
+ public abstract L getLoader(C clazz);
+
+ /**
+ * Returns the loader corresponding to {@code classLoader}.
+ *
+ * @param classLoader a class loader
+ * @return the loader that represents {code classLoader}
+ */
+ public abstract L getLoader(ClassLoader classLoader);
+
+ /**
+ * Returns the class loader corresponding to {@code loader}.
+ *
+ * @param loader a loader
+ * @return the class loader that is represented by loader
+ */
+ public abstract ClassLoader getClassLoader(L loader);
+
+ /**
+ * Checks that {@code loader} can load {@code className} and returns the loaded class.
+ * Also verifies that the loader associated with the loaded class is {@code loader}.
+ *
+ * @param loader a loader
+ * @param className the name of the class to be loaded
+ * @return the class loaded by {@code loader}
+ * @throws Exception if an unexpected error occurs
+ */
+ public C loadClass(L loader, String className) throws Exception
+ {
+ return loadClass(loader, loader, className);
+ }
+
+ /**
+ * Checks that {@code loader} can load {@code className} and returns the loaded class.
+ * Also verifies that the loader associated with the loaded class is {@code expected}.
+ *
+ * @param initiating the loader that will be used for loading {@code className}
+ * @param expected the actual loader that is associated with the loaded class
+ * @param className the name of the class to be loaded
+ * @return the loaded class
+ * @throws Exception if an unexpected error occurs
+ */
+ public abstract C loadClass(L initiating, L expected, String className) throws Exception;
+
+ /**
+ * Checks that {@code loader} cannot load class {@code className}.
+ *
+ * @param loader a loader
+ * @param className the name of the class that can't be loaded by {@code loader}
+ */
+ public abstract void cannotLoadClass(L loader, String className);
+
+ /**
+ * Returns the return type of a method declared in {@code clazz}.
+ *
+ * @param clazz the class that contains the method
+ * @param methodName the name of the method
+ * @return the return type of the method
+ * @throws Exception if an unexpected error occurs
+ */
+ public abstract C getMethodReturnType(C clazz, String methodName) throws Exception;
+
+ /**
+ * Asserts that {@code initiating} class loader can load {@code classname}.
+ * Also verifies that the class loader associated with the loaded class is {@code expected}
+ *
+ * @param initiating the class loader that will be used for loading {@code className}
+ * @param expected the actual class loader that is associated with the loaded class
+ * @param className the name of the class to be loaded
+ * @return the loaded class
+ * @throws Exception if an unexpected error occurs
+ */
+ protected Class<?> assertLoadClass(ClassLoader initiating, ClassLoader expected, String className) throws Exception
+ {
+ Class<?> clazz = initiating.loadClass(className);
+ if (expected != null)
+ {
+ Assert.assertSame(expected, clazz.getClassLoader());
+ }
+ return clazz;
+ }
+
+ /**
+ * Is called prior to destroy the class loaders in {@code classLoaders} collection.
+ *
+ * @param classLoaders the class loaders that will be destroyed
+ */
+ protected void preDestroy(Collection<ClassLoader> classLoaders)
+ {
+ }
+}
\ No newline at end of file
Modified: projects/jboss-classpool/trunk/jbosscl/pom.xml
===================================================================
--- projects/jboss-classpool/trunk/jbosscl/pom.xml 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/jbosscl/pom.xml 2009-12-14 18:04:55 UTC (rev 97797)
@@ -229,7 +229,14 @@
<groupId>org.jboss.cl</groupId>
<artifactId>jboss-classloading</artifactId>
</dependency>
+ <!-- Test-scoped dependencies -->
<dependency>
+ <groupId>org.jboss.classpool</groupId>
+ <artifactId>jboss-classpool</artifactId>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.jboss.cl</groupId>
<artifactId>jboss-classloading-vfs</artifactId>
<scope>test</scope>
@@ -248,6 +255,6 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
- </dependency>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
Modified: projects/jboss-classpool/trunk/pom.xml
===================================================================
--- projects/jboss-classpool/trunk/pom.xml 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/pom.xml 2009-12-14 18:04:55 UTC (rev 97797)
@@ -225,6 +225,12 @@
<version>${project.version}</version>
</dependency>
<dependency>
+ <groupId>org.jboss.classpool</groupId>
+ <artifactId>jboss-classpool</artifactId>
+ <type>test-jar</type>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>jboss</groupId>
<artifactId>jboss-test</artifactId>
<version>${version.org.jboss.test}</version>
Modified: projects/jboss-classpool/trunk/ucl/pom.xml
===================================================================
--- projects/jboss-classpool/trunk/ucl/pom.xml 2009-12-14 17:58:36 UTC (rev 97796)
+++ projects/jboss-classpool/trunk/ucl/pom.xml 2009-12-14 18:04:55 UTC (rev 97797)
@@ -82,7 +82,15 @@
<groupId>org.jboss.jbossas</groupId>
<artifactId>jboss-as-system-jmx</artifactId>
</dependency>
+ <!-- Test-scoped dependencies -->
<dependency>
+ <groupId>org.jboss.classpool</groupId>
+ <artifactId>jboss-classpool</artifactId>
+ <type>test-jar</type>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
More information about the jboss-cvs-commits
mailing list