[jboss-cvs] JBossAS SVN: r62792 - in projects/microcontainer/trunk/classloader: src and 42 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu May 3 15:38:32 EDT 2007
Author: adrian at jboss.org
Date: 2007-05-03 15:38:31 -0400 (Thu, 03 May 2007)
New Revision: 62792
Added:
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/filter/PackageClassFilter.java
projects/microcontainer/trunk/classloader/src/resources/
projects/microcontainer/trunk/classloader/src/resources/tests/
projects/microcontainer/trunk/classloader/src/resources/tests/a/
projects/microcontainer/trunk/classloader/src/resources/tests/a/com/
projects/microcontainer/trunk/classloader/src/resources/tests/a/com/acme/
projects/microcontainer/trunk/classloader/src/resources/tests/a/com/acme/p1/
projects/microcontainer/trunk/classloader/src/resources/tests/a/com/acme/p1/testResource
projects/microcontainer/trunk/classloader/src/resources/tests/a/testResource
projects/microcontainer/trunk/classloader/src/resources/tests/b/
projects/microcontainer/trunk/classloader/src/resources/tests/b/com/
projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/
projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p1/
projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p1/testResource
projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p2/
projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p2/testResource
projects/microcontainer/trunk/classloader/src/resources/tests/b/testResource
projects/microcontainer/trunk/classloader/src/tests/TestDefaultPackage.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/support/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/support/TestClass.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateLoaderUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/ClassLoaderDomainTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MatchClassFilter.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MockLoader.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/NoMatchClassFilter.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/CustomParentLoaderUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/HierarchicalParentLoaderUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/ParentPolicyUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/FilterTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/FilterUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/PackageFilterUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/ClassLoaderPolicyTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClass.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClassLoaderPolicy.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestDelegateLoader.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/test/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/test/ClassLoaderPolicyUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/ResourceTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/tests/
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/tests/ResourceUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoaderSystemTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/SimpleMockClassLoaderPolicy.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemNoSecurityUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemUnitTestCase.java
Removed:
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PackageClassFilter.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoadingSystemTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemNoSecurityUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemUnitTestCase.java
Modified:
projects/microcontainer/trunk/classloader/.classpath
projects/microcontainer/trunk/classloader/pom.xml
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/JavaOnlyClassFilter.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PatternClassFilter.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/jdk/AbstractJDKChecker.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/loader/ClassLoaderToLoaderAdapter.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/system/ClassLoaderSystemBuilder.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderDomain.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderPolicy.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderSystem.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoader.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderPolicy.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderSystem.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseDelegateLoader.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderInformation.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderManager.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/jdk/JDKCheckerFactory.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderHelper.java
projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderPolicy.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/AbstractClassLoaderTest.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/ClassLoaderAllTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/test/ModifiedBootstrapUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/DelegateTestSuite.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/old/test/CircularLoadUnitTestCase.java
projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/MockClassLoaderDomain.java
Log:
[JBOSGI-18] [JBOSGI-20] [JBOSGI-21] - Finished the testing.
Implemented caching and blacklisting.
Resolved the TODOs.
Also made it so a parent domain can "look into" a parent domain
to find the loader which could avoid some deadlocks scenarios.
Modified: projects/microcontainer/trunk/classloader/.classpath
===================================================================
--- projects/microcontainer/trunk/classloader/.classpath 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/.classpath 2007-05-03 19:38:31 UTC (rev 62792)
@@ -2,6 +2,7 @@
<classpath>
<classpathentry kind="src" path="src/main"/>
<classpathentry kind="src" output="target/eclipse-classes-tests" path="src/tests"/>
+ <classpathentry kind="lib" path="src/resources/tests"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="M2_REPO/jboss/jboss-logging-log4j/2.0.2.GA/jboss-logging-log4j-2.0.2.GA.jar" sourcepath="M2_REPO/jboss/jboss-logging-log4j/2.0.2.GA/jboss-logging-log4j-2.0.2.GA-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/jboss/jboss-common-logging-spi/2.0.4.GA/jboss-common-logging-spi-2.0.4.GA.jar" sourcepath="M2_REPO/jboss/jboss-common-logging-spi/2.0.4.GA/jboss-common-logging-spi-2.0.4.GA-sources.jar"/>
Modified: projects/microcontainer/trunk/classloader/pom.xml
===================================================================
--- projects/microcontainer/trunk/classloader/pom.xml 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/pom.xml 2007-05-03 19:38:31 UTC (rev 62792)
@@ -11,6 +11,13 @@
<name>JBoss ClassLoader</name>
<url>http://www.jboss.com/products/jbossmc</url>
<description>JBoss ClassLoader</description>
+ <build>
+ <testResources>
+ <testResource>
+ <directory>src/resources/tests</directory>
+ </testResource>
+ </testResources>
+ </build>
<dependencies>
<!-- Global dependencies -->
<dependency>
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/ClassLoaderUtils.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -40,7 +40,6 @@
/**
* Check the class name makes sense
*
- * REVIEW other checks besides null and empty?
* @param className the class name
* @throws ClassNotFoundException for a malformed class name
*/
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/JavaOnlyClassFilter.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/JavaOnlyClassFilter.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/JavaOnlyClassFilter.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -37,7 +37,7 @@
*/
private JavaOnlyClassFilter()
{
- super(new String[] { "java\\..*", "javax\\..*" } );
+ super(new String[] { "java\\..+", "javax\\..+" } );
}
public String toString()
Deleted: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PackageClassFilter.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PackageClassFilter.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PackageClassFilter.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -1,81 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2007, 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.classloader.plugins.filter;
-
-import java.util.Arrays;
-
-/**
- * A class filter using regular expressions
- *
- * @author <a href="adrian at jboss.com">Adrian Brock</a>
- * @version $Revision: 1.1 $
- */
-public class PackageClassFilter extends PatternClassFilter
-{
- /** The patterns as regular expressions */
- private String[] packageNames;
-
- /**
- * Convert package names to patterns
- *
- * @param packageNames the package names
- * @return the patterns
- */
- private static String[] convertPackageNamesToPatterns(String[] packageNames)
- {
- if (packageNames == null)
- throw new IllegalArgumentException("Null package names");
-
- String[] patterns = new String[packageNames.length];
- for (int i = 0; i < packageNames.length; ++i)
- {
- if (packageNames[i] == null)
- throw new IllegalArgumentException("Null package name in " + Arrays.asList(packageNames));
-
- if (packageNames[i].length() == 0)
- // Base package - it is a match if there is no . in the class name
- patterns[i] = "[^.]*";
- else
- // Escape the dots in the package and match anything that has a single dot followed by non-dots
- patterns[i] = packageNames[i].replace(".", "\\.") + "\\.[^.]*";
- }
- return patterns;
- }
-
- /**
- * Create a new PackageClassFilter.
- *
- * @param packageNames the packageNames
- * @throws IllegalArgumentException for null packageNames
- */
- public PackageClassFilter(String[] packageNames)
- {
- super(convertPackageNamesToPatterns(packageNames));
- this.packageNames = packageNames;
- }
-
- @Override
- public String toString()
- {
- return Arrays.asList(packageNames).toString();
- }
-}
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PatternClassFilter.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PatternClassFilter.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PatternClassFilter.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -37,6 +37,18 @@
{
/** The patterns as regular expressions */
private Pattern[] patterns;
+
+ /**
+ * Create a new PatternClassFilter.
+ *
+ * @param patterns the patterns
+ * @return the filter
+ * @throws IllegalArgumentException for a null pattern
+ */
+ public static PatternClassFilter createPatternClassFilter(String... patterns)
+ {
+ return new PatternClassFilter(patterns);
+ }
/**
* Create a new PatternClassFilter.
@@ -73,6 +85,9 @@
public boolean matches(String className)
{
+ if (className == null)
+ return false;
+
for (int i = 0; i < patterns.length; ++i)
{
Matcher matcher = patterns[i].matcher(className);
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/jdk/AbstractJDKChecker.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/jdk/AbstractJDKChecker.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/jdk/AbstractJDKChecker.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -81,7 +81,7 @@
/**
* Whether the requesting class is from the JDK<p>
*
- * The default implementation returs true if the requesting class is not loaded from
+ * The default implementation returns true if the requesting class is not loaded from a
* {@link BaseClassLoader}
*
* @param name the name of the class being loaded
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/loader/ClassLoaderToLoaderAdapter.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/loader/ClassLoaderToLoaderAdapter.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/loader/ClassLoaderToLoaderAdapter.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -23,10 +23,16 @@
import java.io.IOException;
import java.net.URL;
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.Set;
import org.jboss.classloader.spi.Loader;
+import org.jboss.logging.Logger;
/**
* ClassLoaderToLoaderAdapter.
@@ -36,9 +42,15 @@
*/
public class ClassLoaderToLoaderAdapter implements Loader
{
+ /** The log */
+ private static final Logger log = Logger.getLogger(ClassLoaderToLoaderAdapter.class);
+
/** The classloader */
private ClassLoader classLoader;
+ /** The access control context of the creator of this adapter */
+ private AccessControlContext accessControlContext;
+
/**
* Create a new ClassLoaderToLoaderAdapter.
*
@@ -49,16 +61,71 @@
if (classLoader == null)
throw new IllegalArgumentException("Null classLoader");
this.classLoader = classLoader;
+ accessControlContext = AccessController.getContext();
}
- public URL getResource(String name, String resourceName)
+ public URL getResource(final String name, String resourceName)
{
- return classLoader.getResource(name);
+ URL url;
+ SecurityManager sm = System.getSecurityManager();
+ if (sm != null)
+ {
+ url = AccessController.doPrivileged(new PrivilegedAction<URL>()
+ {
+ public URL run()
+ {
+ return classLoader.getResource(name);
+ }
+ }, accessControlContext);
+
+ }
+ else
+ {
+ url = classLoader.getResource(name);
+ }
+
+ if (log.isTraceEnabled())
+ {
+ if (url != null)
+ log.trace("Resource " + name + " found in " + classLoader);
+ else
+ log.trace("Resource " + name + " NOT found in " + classLoader);
+ }
+ return url;
}
- public void getResources(String name, String resourceName, Set<URL> urls) throws IOException
+ public void getResources(final String name, String resourceName, Set<URL> urls) throws IOException
{
- Enumeration<URL> enumeration = classLoader.getResources(name);
+ Enumeration<URL> enumeration;
+ SecurityManager sm = System.getSecurityManager();
+ if (sm != null)
+ {
+ try
+ {
+ enumeration = AccessController.doPrivileged(new PrivilegedExceptionAction<Enumeration<URL>>()
+ {
+ public Enumeration<URL> run() throws Exception
+ {
+ return classLoader.getResources(name);
+ }
+ }, accessControlContext);
+ }
+ catch (PrivilegedActionException e)
+ {
+ Exception e1 = e.getException();
+ if (e1 instanceof RuntimeException)
+ throw (RuntimeException) e1;
+ if (e1 instanceof IOException)
+ throw (IOException) e1;
+ IOException e2 = new IOException("Unexpected error");
+ e2.initCause(e1);
+ throw e2;
+ }
+ }
+ else
+ {
+ enumeration = classLoader.getResources(name);
+ }
while (enumeration.hasMoreElements())
urls.add(enumeration.nextElement());
}
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/system/ClassLoaderSystemBuilder.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/system/ClassLoaderSystemBuilder.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/system/ClassLoaderSystemBuilder.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -21,19 +21,46 @@
*/
package org.jboss.classloader.plugins.system;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
import org.jboss.classloader.spi.ClassLoaderSystem;
/**
* ClassLoaderSystemBuilder.
*
- * TODO parameterize this
* @author <a href="adrian at jboss.com">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public class ClassLoaderSystemBuilder
{
/** The singleton */
- private static final ClassLoaderSystem singleton = new DefaultClassLoaderSystem();
+ private static final ClassLoaderSystem singleton;
+
+ static
+ {
+ singleton = AccessController.doPrivileged(new PrivilegedAction<ClassLoaderSystem>()
+ {
+ public ClassLoaderSystem run()
+ {
+ String className = System.getProperty(ClassLoaderSystem.class.getName(), DefaultClassLoaderSystem.class.getName());
+ try
+ {
+ Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
+ Object result = clazz.newInstance();
+ return ClassLoaderSystem.class.cast(result);
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw new Error("Unexpected error loading ClassLoaderSystem " + className, e);
+ }
+ }
+ });
+ }
/**
* Get the classloader system singleton
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderDomain.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderDomain.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderDomain.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -23,6 +23,8 @@
import java.io.IOException;
import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.util.Set;
import org.jboss.classloader.plugins.loader.ClassLoaderToLoaderAdapter;
@@ -153,11 +155,11 @@
if (filter.matches(resourceName))
{
if (trace)
- log.trace(this + " " + name + " matches parent beforeFilter=" + filter);
+ log.trace(this + " " + resourceName + " matches parent beforeFilter=" + filter);
return findLoaderFromParent(name, resourceName);
}
if (trace)
- log.trace(this + " " + name + " does NOT match parent beforeFilter=" + filter);
+ log.trace(this + " " + resourceName + " does NOT match parent beforeFilter=" + filter);
return null;
}
@@ -169,11 +171,11 @@
if (filter.matches(resourceName))
{
if (trace)
- log.trace(this + " " + name + " matches parent afterFilter=" + filter);
+ log.trace(this + " " + resourceName + " matches parent afterFilter=" + filter);
return findLoaderFromParent(name, resourceName);
}
if (trace)
- log.trace(this + " " + name + " does NOT match parent afterFilter=" + filter);
+ log.trace(this + " " + resourceName + " does NOT match parent afterFilter=" + filter);
return null;
}
@@ -197,8 +199,16 @@
}
if (trace)
- log.trace(this + " load from parent " + name + " parent=" + parent);
+ log.trace(this + " load from parent " + resourceName + " parent=" + parent);
+
+ // Recurse into parent domains
+ if (parentLoader instanceof ClassLoaderDomain)
+ {
+ ClassLoaderDomain parentDomain = (ClassLoaderDomain) parentLoader;
+ return parentDomain.findLoader(name, resourceName);
+ }
+ // A normal loader
if (parentLoader.getResource(name, resourceName) != null)
return parentLoader;
@@ -338,9 +348,17 @@
{
if (parent == null)
{
- ClassLoader classLoader = getParentClassLoader();
+ final ClassLoader classLoader = getParentClassLoader();
if (classLoader != null)
- parent = new ClassLoaderToLoaderAdapter(classLoader);
+ {
+ parent = AccessController.doPrivileged(new PrivilegedAction<Loader>()
+ {
+ public Loader run()
+ {
+ return new ClassLoaderToLoaderAdapter(classLoader);
+ }
+ });
+ }
}
}
}
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderPolicy.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderPolicy.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderPolicy.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -32,9 +32,9 @@
import java.util.List;
import java.util.Set;
-import org.jboss.classloader.plugins.filter.PackageClassFilter;
import org.jboss.classloader.spi.base.BaseClassLoaderPolicy;
import org.jboss.classloader.spi.filter.FilteredDelegateLoader;
+import org.jboss.classloader.spi.filter.PackageClassFilter;
import org.jboss.classloader.spi.jdk.JDKChecker;
import org.jboss.classloader.spi.jdk.JDKCheckerFactory;
import org.jboss.logging.Logger;
@@ -64,7 +64,7 @@
String[] packageNames = getPackageNames();
if (packageNames == null)
return null;
- return new FilteredDelegateLoader(this, new PackageClassFilter(packageNames));
+ return new FilteredDelegateLoader(this, PackageClassFilter.createPackageClassFilter(packageNames));
}
/**
@@ -112,6 +112,30 @@
}
/**
+ * Whether to cache<p>
+ *
+ * True by default
+ *
+ * @return true to cache
+ */
+ protected boolean isCachable()
+ {
+ return true;
+ }
+
+ /**
+ * Whether to cache misses<p>
+ *
+ * True by default
+ *
+ * @return true to cache misses
+ */
+ protected boolean isBlackListable()
+ {
+ return true;
+ }
+
+ /**
* Get the resource
*
* @param path the path
@@ -236,5 +260,4 @@
return ClassLoader.getSystemClassLoader();
}
}
-
}
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderSystem.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderSystem.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/ClassLoaderSystem.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -267,6 +267,18 @@
if (ClassLoaderSystem.DEFAULT_DOMAIN_NAME.equals(domain.getName()))
throw new IllegalArgumentException("Cannot unregister the default domain");
+ internalUnregisterDomain(domain);
+ }
+
+ /**
+ * Unregister a domain
+ *
+ * @param domain the domain
+ * @throws IllegalArgumentException for a null domain or if you attempt to unregister the default domain
+ * @throws IllegalStateException if a domain is not registered
+ */
+ private synchronized void internalUnregisterDomain(ClassLoaderDomain domain)
+ {
registeredDomains.remove(domain.getName());
super.unregisterDomain(domain);
@@ -301,7 +313,7 @@
public ClassLoader registerClassLoaderPolicy(ClassLoaderDomain domain, ClassLoaderPolicy policy)
{
if (isDomainRegistered(domain) == false)
- throw new IllegalArgumentException("Domain is not registered: " + domain);
+ throw new IllegalStateException("Domain is not registered: " + domain);
synchronized (this)
{
@@ -358,7 +370,7 @@
while (iterator.hasNext())
{
ClassLoaderDomain domain = iterator.next();
- unregisterDomain(domain);
+ internalUnregisterDomain(domain);
}
}
}
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoader.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoader.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoader.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -32,7 +32,10 @@
import java.security.SecureClassLoader;
import java.util.Enumeration;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@@ -63,6 +66,12 @@
/** Our Loader front end */
private DelegateLoader loader;
+ /** Our resource cache */
+ private Map<String, URL> resourceCache;
+
+ /** Our black list */
+ private Set<String> blackList;
+
/**
* Create a new ClassLoader with no parent.
*
@@ -82,6 +91,12 @@
loader = new DelegateLoader(policy);
+ if (basePolicy.isCachable())
+ resourceCache = new ConcurrentHashMap<String, URL>();
+
+ if (basePolicy.isBlackListable())
+ blackList = new CopyOnWriteArraySet<String>();
+
if (log.isTraceEnabled())
log.debug("Created " + this + " with policy " + policy);
}
@@ -279,9 +294,30 @@
{
if (trace)
log.trace(this + " get resource locally " + name);
+
+ // Do we already know the answer?
+ if (resourceCache != null)
+ {
+ URL url = resourceCache.get(name);
+ if (url != null)
+ {
+ if (trace)
+ log.trace(this + " got resource from cache " + name);
+ return url;
+ }
+ }
- return AccessController.doPrivileged(new PrivilegedAction<URL>()
+ // Is this resource blacklisted?
+ if (blackList != null && blackList.contains(name))
{
+ if (trace)
+ log.trace(this + " resource is blacklisted " + name);
+ return null;
+ }
+
+ // Ask the policy for the resource
+ URL result = AccessController.doPrivileged(new PrivilegedAction<URL>()
+ {
public URL run()
{
// Look for the resource
@@ -297,6 +333,16 @@
return result;
}
}, policy.getAccessControlContext());
+
+ // Cache what we found
+ if (resourceCache != null && result != null)
+ resourceCache.put(name, result);
+
+ // Blacklist when not found
+ if (blackList != null && result == null)
+ blackList.add(name);
+
+ return result;
}
/**
@@ -327,7 +373,6 @@
log.trace(this + " get resources locally " + name);
// Look for the resources
-
try
{
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>()
@@ -456,6 +501,10 @@
protected void shutdownClassLoader()
{
log.debug(toLongString() + " shutdown!");
+ if (resourceCache != null)
+ resourceCache.clear();
+ if (blackList != null)
+ blackList.clear();
}
/**
@@ -550,6 +599,8 @@
* Acquire the lock on the classloader fairly<p>
*
* This must be invoked with the monitor held
+ *
+ * @param trace whether trace is enabled
*/
private void acquireLockFairly(boolean trace)
{
@@ -569,8 +620,8 @@
{
if (lock.tryLock(0, TimeUnit.MICROSECONDS) == false)
{
- // REVIEW: If we've been spinning for more than a minute then there is probably something wrong?
- if (waits++ == 6)
+ // Two minutes should be long enough?
+ if (waits++ == 12)
throw new IllegalStateException("Waiting too long to get the classloader lock: " + this);
// Wait 10 seconds
if (trace)
@@ -602,7 +653,7 @@
/**
* Unlock
*
- * This method must be invoked with the monitor held
+ * @param trace whether trace is enabled
*/
private void unlock(boolean trace)
{
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderDomain.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -30,6 +30,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
import org.jboss.classloader.plugins.ClassLoaderUtils;
import org.jboss.classloader.spi.ClassLoaderPolicy;
@@ -43,7 +44,6 @@
* This class hides some of the implementation details and allows
* package access to the protected methods.
*
- * TODO add caching (needs to be per classloader when not AllExports policy)
* @author <a href="adrian at jboss.com">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
@@ -64,10 +64,33 @@
/** The classloaders by package name */
private Map<String, List<ClassLoaderInformation>> classLoadersByPackageName = new ConcurrentHashMap<String, List<ClassLoaderInformation>>();
+ /** The global class cache */
+ private Map<String, Loader> globalClassCache = new ConcurrentHashMap<String, Loader>();
+
+ /** The global class black list */
+ private Set<String> globalClassBlackList = new CopyOnWriteArraySet<String>();
+
+ /** The global resource cache */
+ private Map<String, URL> globalResourceCache = new ConcurrentHashMap<String, URL>();
+
+ /** The global resource black list */
+ private Set<String> globalResourceBlackList = new CopyOnWriteArraySet<String>();
+
/** Keep track of the added order */
private int order = 0;
/**
+ * Flush the internal caches
+ */
+ public void flushCaches()
+ {
+ globalClassCache.clear();
+ globalClassBlackList.clear();
+ globalResourceCache.clear();
+ globalResourceBlackList.clear();
+ }
+
+ /**
* Get the classloader system
*
* @return the classloader system
@@ -112,6 +135,8 @@
unregisterClassLoader(info.getClassLoader());
}
}
+
+ flushCaches();
}
/**
@@ -131,55 +156,12 @@
system.transform(className, byteCode, protectionDomain);
return byteCode;
}
-
- /**
- * Invoked before classloading is attempted to allow a preload attempt, e.g. from the parent
- *
- * @param name the class resource name
- * @param resourceName the resource name in dot notation
- * @return the loader if found or null otherwise
- */
- protected abstract Loader findBeforeLoader(String name, String resourceName);
/**
- * Invoked after classloading is attempted to allow a postload attempt, e.g. from the parent
- *
- * @param name the class resource name
- * @param resourceName the resource name in dot notation
- * @return the loader if found or null otherwise
- */
- protected abstract Loader findAfterLoader(String name, String resourceName);
-
- public Class<?> loadClass(String name)
- {
- try
- {
- return loadClass(null, name, true);
- }
- catch (ClassNotFoundException e)
- {
- return null;
- }
- }
-
- /**
* Load a class from the domain
*
* @param classLoader the classloader
* @param name the class name
- * @return the class
- * @throws ClassNotFoundException for any error
- */
- Class<?> loadClass(BaseClassLoader classLoader, String name) throws ClassNotFoundException
- {
- return loadClass(classLoader, name, false);
- }
-
- /**
- * Load a class from the domain
- *
- * @param classLoader the classloader
- * @param name the class name
* @param allExports whether we should look at all exports
* @return the class
* @throws ClassNotFoundException for any error
@@ -187,142 +169,115 @@
Class<?> loadClass(BaseClassLoader classLoader, String name, boolean allExports) throws ClassNotFoundException
{
boolean trace = log.isTraceEnabled();
-
- if (getClassLoaderSystem() == null)
- throw new IllegalStateException("Domain is not registered with a classloader system: " + toLongString());
String path = ClassLoaderUtils.classNameToPath(name);
-
- // Try the before attempt (e.g. from the parent)
- Loader loader = findBeforeLoader(path, name);
-
- if (loader == null)
+
+ Loader loader = findLoader(classLoader, path, name, allExports);
+ if (loader != null)
{
- // Work out the rules
- List<? extends DelegateLoader> delegates = null;
- BaseClassLoaderPolicy policy = null;
- if (classLoader != null)
+ Thread thread = Thread.currentThread();
+ ClassLoadingTask task = new ClassLoadingTask(name, classLoader, thread);
+ ClassLoaderManager.scheduleTask(task, loader, false);
+ return ClassLoaderManager.process(thread, task);
+ }
+ // Finally see whether this is the JDK assuming it can load its classes from any classloader
+ else if (classLoader != null)
+ {
+ BaseClassLoaderPolicy policy = classLoader.getPolicy();
+ ClassLoader hack = policy.isJDKRequest(name);
+ if (hack != null)
{
- policy = classLoader.getPolicy();
- delegates = policy.getDelegates();
- if (policy.isImportAll())
- allExports = true;
- }
-
- // Next we try the old "big ball of mud" model
- if (allExports)
- {
- String packageName = ClassLoaderUtils.getClassPackageName(name);
- List<ClassLoaderInformation> list = classLoadersByPackageName.get(packageName);
- if (list != null && list.isEmpty() == false)
- {
- if (trace)
- log.trace(this + " trying to load " + name + " from all exports " + list);
- for (ClassLoaderInformation info : list)
- {
- BaseDelegateLoader exported = info.getExported();
- if (exported.getResource(path, name) != null)
- {
- loader = exported;
- break;
- }
- }
- }
- }
-
- // Next we try the imports
- if (loader == null && delegates != null && delegates.isEmpty() == false)
- {
- for (DelegateLoader delegate : delegates)
- {
- if (trace)
- log.trace(this + " trying to load " + name + " from import " + delegate + " for " + classLoader);
- if (delegate.getResource(path, name) != null)
- {
- loader = delegate;
- break;
- }
- }
- }
-
- // Next use any requesting classloader, this will look at everything not just what it exports
- if (loader == null && classLoader != null)
- {
if (trace)
- log.trace(this + " trying to load " + name + " from requesting " + classLoader);
- if (classLoader.getResourceLocally(path, name) != null)
- loader = classLoader.getLoader();
- }
-
- // Try the after attempt (e.g. from the parent)
- if (loader == null)
- loader = findAfterLoader(path, name);
-
- // Finally see whether this is the JDK assuming it can load its classes from any classloader
- if (loader == null)
- {
- ClassLoader hack = policy.isJDKRequest(name);
- if (hack != null)
+ log.trace(this + " trying to load " + name + " using hack " + hack);
+ Class<?> result = hack.loadClass(name);
+ if (result != null)
{
if (trace)
- log.trace(this + " trying to load " + name + " using hack " + hack);
- Class<?> result = hack.loadClass(name);
- if (result != null)
- {
- if (trace)
- log.trace(this + " loaded from hack " + hack + " " + ClassLoaderUtils.classToString(result));
- return result;
- }
+ log.trace(this + " loaded from hack " + hack + " " + ClassLoaderUtils.classToString(result));
+ return result;
}
}
}
-
- if (loader != null)
- {
- Thread thread = Thread.currentThread();
- ClassLoadingTask task = new ClassLoadingTask(name, classLoader, thread);
- ClassLoaderManager.scheduleTask(task, loader, false);
- return ClassLoaderManager.process(thread, task);
- }
// Didn't find it
return null;
}
-
+
/**
- * Invoked before getResource is attempted to allow a preload attempt, e.g. from the parent
+ * Find a loader for a class
*
- * @param name the resource name
- * @param resourceName the name of the resource in dot notation
- * @return the url if found or null otherwise
+ * @param name the class resource name
+ * @param resourceName the resource name in dot notation
+ * @return the loader
*/
- protected abstract URL beforeGetResource(String name, String resourceName);
-
- /**
- * Invoked after getResource is attempted to allow a postload attempt, e.g. from the parent
- *
- * @param name the class name
- * @param resourceName the name of the resource in dot notation
- * @return the url if found or null otherwise
- */
- protected abstract URL afterGetResource(String name, String resourceName);
-
- public URL getResource(String name, String resourceName)
+ protected Loader findLoader(String name, String resourceName)
{
- return getResource(null, name, resourceName, true);
+ return findLoader(null, name, resourceName, true);
}
-
+
/**
- * Get a resource from the domain
+ * Find a loader for a class
*
* @param classLoader the classloader
- * @param name the resource name
- * @param resourceName the name of the resource in dot notation
- * @return the url
+ * @param name the class resource name
+ * @param resourceName the resource name in dot notation
+ * @param allExports whether we should look at all exports
+ * @return the loader
*/
- URL getResource(BaseClassLoader classLoader, String name, String resourceName)
+ Loader findLoader(BaseClassLoader classLoader, String name, String resourceName, boolean allExports)
{
- return getResource(classLoader, name, resourceName, false);
+ boolean trace = log.isTraceEnabled();
+ if (trace)
+ log.trace(this + " findLoader " + resourceName + " classLoader=" + classLoader + " allExports=" + allExports);
+
+ if (getClassLoaderSystem() == null)
+ throw new IllegalStateException("Domain is not registered with a classloader system: " + toLongString());
+
+ // Try the before attempt (e.g. from the parent)
+ Loader loader = findBeforeLoader(name, resourceName);
+ if (loader != null)
+ return loader;
+
+ // Work out the rules
+ ClassLoaderInformation info = null;
+ BaseClassLoaderPolicy policy = null;
+ if (classLoader != null)
+ {
+ info = infos.get(classLoader);
+ policy = classLoader.getPolicy();
+ if (policy.isImportAll())
+ allExports = true;
+ }
+
+ // Next we try the old "big ball of mud" model
+ if (allExports)
+ {
+ loader = findLoaderInExports(classLoader, name, resourceName, trace);
+ if (loader != null)
+ return loader;
+ }
+ else if (trace)
+ log.trace(this + " not loading " + resourceName + " from all exports");
+
+ // Next we try the imports
+ if (info != null)
+ {
+ loader = findLoaderInImports(info, name, resourceName, trace);
+ if (loader != null)
+ return loader;
+ }
+
+ // Next use any requesting classloader, this will look at everything not just what it exports
+ if (classLoader != null)
+ {
+ if (trace)
+ log.trace(this + " trying to load " + resourceName + " from requesting " + classLoader);
+ if (classLoader.getResourceLocally(name, resourceName) != null)
+ return classLoader.getLoader();
+ }
+
+ // Try the after attempt (e.g. from the parent)
+ return findAfterLoader(name, resourceName);
}
/**
@@ -347,12 +302,12 @@
return result;
// Work out the rules
- List<? extends DelegateLoader> delegates = null;
+ ClassLoaderInformation info = null;
BaseClassLoaderPolicy policy = null;
if (classLoader != null)
{
policy = classLoader.getPolicy();
- delegates = policy.getDelegates();
+ info = infos.get(classLoader);
if (policy.isImportAll())
allExports = true;
}
@@ -360,33 +315,19 @@
// Next we try the old "big ball of mud" model
if (allExports)
{
- String packageName = ClassLoaderUtils.getResourcePackageName(name);
- List<ClassLoaderInformation> list = classLoadersByPackageName.get(packageName);
- if (list != null && list.isEmpty() == false)
- {
- if (trace)
- log.trace(this + " trying to get resource " + name + " from all exports " + list);
- for (ClassLoaderInformation info : list)
- {
- BaseDelegateLoader loader = info.getExported();
- result = loader.getResource(name, resourceName);
- if (result != null)
- return result;
- }
- }
+ result = getResourceFromExports(classLoader, name, resourceName, trace);
+ if (result != null)
+ return result;
}
+ else if (trace)
+ log.trace(this + " not getting resource " + name + " from all exports");
// Next we try the imports
- if (delegates != null && delegates.isEmpty() == false)
+ if (info != null)
{
- for (DelegateLoader delegate : delegates)
- {
- if (trace)
- log.trace(this + " trying to get resource " + name + " from import " + delegate + " for " + classLoader);
- result = delegate.getResource(name, resourceName);
- if (result != null)
- return result;
- }
+ result = getResourceFromImports(info, name, resourceName, trace);
+ if (result != null)
+ return result;
}
// Finally use any requesting classloader
@@ -413,114 +354,444 @@
}
/**
- * Invoked before getResources is attempted to allow a preload attempt, e.g. from the parent
+ * Load resources from the domain
*
+ * @param classLoader the classloader
* @param name the resource name
* @param resourceName the name of the resource in dot notation
+ * @param allExports whether we should look at all exports
* @param urls the urls to add to
* @throws IOException for any error
*/
- protected abstract void beforeGetResources(String name, String resourceName, Set<URL> urls) throws IOException;
+ void getResources(BaseClassLoader classLoader, String name, String resourceName, Set<URL> urls, boolean allExports) throws IOException
+ {
+ boolean trace = log.isTraceEnabled();
+
+ if (getClassLoaderSystem() == null)
+ throw new IllegalStateException("Domain is not registered with a classloader system: " + toLongString());
+
+ // Try the before attempt
+ beforeGetResources(name, resourceName, urls);
+
+ // Work out the rules
+ ClassLoaderInformation info = null;
+ BaseClassLoaderPolicy policy = null;
+ if (classLoader != null)
+ {
+ policy = classLoader.getPolicy();
+ info = infos.get(classLoader);
+ if (policy.isImportAll())
+ allExports = true;
+ }
+
+ // Next we try the old "big ball of mud" model
+ if (allExports)
+ getResourcesFromExports(classLoader, name, resourceName, urls, trace);
+ else if (trace)
+ log.trace(this + " not getting resource " + name + " from all exports");
+
+ // Next we try the imports
+ if (info != null)
+ getResourcesFromImports(info, name, resourceName, urls, trace);
+
+ // Finally use any requesting classloader
+ if (classLoader != null)
+ {
+ if (trace)
+ log.trace(this + " trying to get resources " + name + " from requesting " + classLoader);
+ classLoader.getResourcesLocally(name, resourceName, urls);
+ }
+
+ // Try the after attempt
+ afterGetResources(name, resourceName, urls);
+ }
/**
- * Invoked after getResources is attempted to allow a postload attempt, e.g. from the parent
+ * Find a loader for class in exports
*
- * @param name the resource name
- * @param resourceName the name of the resource in dot notation
- * @param urls the urls to add to
- * @throws IOException for any error
+ * @param classLoader the classloader
+ * @param name the class resource name
+ * @param resourceName the resource name in dot notation
+ * @param trace whether trace is enabled
+ * @return the loader
*/
- protected abstract void afterGetResources(String name, String resourceName, Set<URL> urls) throws IOException;
-
- public void getResources(String name, String resourceName, Set<URL> urls) throws IOException
+ private Loader findLoaderInExports(BaseClassLoader classLoader, String name, String resourceName, boolean trace)
{
- getResources(null, name, resourceName, urls, true);
+ Loader loader = globalClassCache.get(name);
+ if (loader != null)
+ {
+ if (trace)
+ log.trace(this + " found in global class cache " + resourceName);
+
+ return loader;
+ }
+
+ if (globalClassBlackList.contains(name))
+ {
+ if (trace)
+ log.trace(this + " class is black listed " + resourceName);
+ return null;
+ }
+ boolean canCache = true;
+ boolean canBlackList = true;
+
+ String packageName = ClassLoaderUtils.getClassPackageName(resourceName);
+ List<ClassLoaderInformation> list = classLoadersByPackageName.get(packageName);
+ if (trace)
+ log.trace(this + " trying to load " + resourceName + " from all exports of package " + packageName + " " + list);
+ if (list != null && list.isEmpty() == false)
+ {
+ for (ClassLoaderInformation info : list)
+ {
+ BaseDelegateLoader exported = info.getExported();
+
+ // See whether the policies allow caching/blacklisting
+ BaseClassLoaderPolicy loaderPolicy = exported.getPolicy();
+ if (loaderPolicy.isCachable() == false)
+ canCache = false;
+ if (loaderPolicy.isBlackListable() == false)
+ canBlackList = false;
+
+ if (exported.getResource(name, resourceName) != null)
+ {
+ if (canCache)
+ globalClassCache.put(name, exported);
+ return exported;
+ }
+ }
+ }
+ // Here is not found in the exports so can we blacklist it?
+ if (canBlackList)
+ globalClassBlackList.add(name);
+
+ return null;
}
/**
- * Get a resource from the domain
+ * Load a resource from the exports
*
* @param classLoader the classloader
* @param name the resource name
* @param resourceName the name of the resource in dot notation
- * @param urls the urls to add to
- * @throws IOException for any error
+ * @param trace whether trace is enabled
+ * @return the url
*/
- void getResources(BaseClassLoader classLoader, String name, String resourceName, Set<URL> urls) throws IOException
+ private URL getResourceFromExports(BaseClassLoader classLoader, String name, String resourceName, boolean trace)
{
- getResources(classLoader, name, resourceName, urls, false);
+ URL result = globalResourceCache.get(name);
+ if (result != null)
+ {
+ if (trace)
+ log.trace(this + " got resource from cache " + name);
+ }
+
+ if (globalResourceBlackList.contains(name))
+ {
+ if (trace)
+ log.trace(this + " resource is black listed, not looking at exports " + name);
+ return null;
+ }
+ boolean canCache = true;
+ boolean canBlackList = true;
+
+ String packageName = ClassLoaderUtils.getResourcePackageName(name);
+ List<ClassLoaderInformation> list = classLoadersByPackageName.get(packageName);
+ if (trace)
+ log.trace(this + " trying to get resource " + name + " from all exports " + list);
+ if (list != null && list.isEmpty() == false)
+ {
+ for (ClassLoaderInformation info : list)
+ {
+ BaseDelegateLoader loader = info.getExported();
+
+ // See whether the policies allow caching/blacklisting
+ BaseClassLoaderPolicy loaderPolicy = loader.getPolicy();
+ if (loaderPolicy.isCachable() == false)
+ canCache = false;
+ if (loaderPolicy.isBlackListable() == false)
+ canBlackList = false;
+
+ result = loader.getResource(name, resourceName);
+ if (result != null)
+ {
+ if (canCache)
+ globalResourceCache.put(name, result);
+ return result;
+ }
+ }
+ }
+ // Here is not found in the exports so can we blacklist it?
+ if (canBlackList)
+ globalResourceBlackList.add(name);
+ return null;
}
/**
- * Load a resource from the domain
+ * Load resources from the exports
*
* @param classLoader the classloader
* @param name the resource name
* @param resourceName the name of the resource in dot notation
- * @param allExports whether we should look at all exports
* @param urls the urls to add to
+ * @param trace whether trace is enabled
* @throws IOException for any error
*/
- void getResources(BaseClassLoader classLoader, String name, String resourceName, Set<URL> urls, boolean allExports) throws IOException
+ void getResourcesFromExports(BaseClassLoader classLoader, String name, String resourceName, Set<URL> urls, boolean trace) throws IOException
{
- boolean trace = log.isTraceEnabled();
+ String packageName = ClassLoaderUtils.getResourcePackageName(name);
+ List<ClassLoaderInformation> list = classLoadersByPackageName.get(packageName);
+ if (trace)
+ log.trace(this + " trying to get resources " + name + " from all exports " + list);
+ if (list != null && list.isEmpty() == false)
+ {
+ for (ClassLoaderInformation info : list)
+ {
+ BaseDelegateLoader loader = info.getExported();
+ loader.getResources(name, resourceName, urls);
+ }
+ }
+ }
- if (getClassLoaderSystem() == null)
- throw new IllegalStateException("Domain is not registered with a classloader system: " + toLongString());
-
- // Try the before attempt
- beforeGetResources(name, resourceName, urls);
-
- // Work out the rules
- List<? extends DelegateLoader> delegates = null;
- BaseClassLoaderPolicy policy = null;
- if (classLoader != null)
+ /**
+ * Find a loader for a class in imports
+ *
+ * @param info the classloader information
+ * @param name the class resource name
+ * @param resourceName the resource name in dot notation
+ * @param trace whether trace is enabled
+ * @return the loader
+ */
+ Loader findLoaderInImports(ClassLoaderInformation info, String name, String resourceName, boolean trace)
+ {
+ List<? extends DelegateLoader> delegates = info.getDelegates();
+ if (delegates == null || delegates.isEmpty())
{
- policy = classLoader.getPolicy();
- delegates = policy.getDelegates();
- if (policy.isImportAll())
- allExports = true;
+ if (trace)
+ log.trace(this + " not loading " + resourceName + " from imports it has no delegates");
+ return null;
}
- // Next we try the old "big ball of mud" model
- if (allExports)
+ Loader loader = info.getCachedLoader(name);
+ if (loader != null)
{
- String packageName = ClassLoaderUtils.getResourcePackageName(name);
- List<ClassLoaderInformation> list = classLoadersByPackageName.get(packageName);
- if (list != null && list.isEmpty() == false)
+ if (trace)
+ log.trace(this + " found in import cache " + name);
+ return loader;
+ }
+
+ if (info.isBlackListedClass(name))
+ {
+ if (trace)
+ log.trace(this + " class is black listed in imports " + name);
+ return null;
+ }
+
+ for (DelegateLoader delegate : delegates)
+ {
+ if (trace)
+ log.trace(this + " trying to load " + resourceName + " from import " + delegate + " for " + info.getClassLoader());
+ if (delegate.getResource(name, resourceName) != null)
{
- if (trace)
- log.trace(this + " trying to get resources " + name + " from all exports " + list);
- for (ClassLoaderInformation info : list)
- {
- BaseDelegateLoader loader = info.getExported();
- loader.getResources(name, resourceName, urls);
- }
+ info.cacheLoader(name, delegate);
+ return delegate;
}
}
+ info.blackListClass(name);
+ return null;
+ }
+
+ /**
+ * Load a resource from the imports
+ *
+ * @param info the classloader information
+ * @param name the resource name
+ * @param resourceName the name of the resource in dot notation
+ * @param trace whether trace is enabled
+ * @return the url
+ */
+ private URL getResourceFromImports(ClassLoaderInformation info, String name, String resourceName, boolean trace)
+ {
+ List<? extends DelegateLoader> delegates = info.getDelegates();
+ if (delegates == null || delegates.isEmpty())
+ {
+ if (trace)
+ log.trace(this + " not getting resource " + name + " from imports it has no delegates");
+ return null;
+ }
+
+ URL url = info.getCachedResource(name);
+ if (url != null)
+ {
+ if (trace)
+ log.trace(this + " found resource in import cache " + name);
+ return url;
+ }
- // Next we try the imports
- if (delegates != null && delegates.isEmpty() == false)
+ if (info.isBlackListedResource(name))
{
- for (DelegateLoader delegate : delegates)
+ if (trace)
+ log.trace(this + " resource is black listed in imports " + name);
+ return null;
+ }
+
+ if (trace)
+ log.trace(this + " trying to get resource " + name + " from imports " + delegates + " for " + info.getClassLoader());
+
+ for (DelegateLoader delegate : delegates)
+ {
+ URL result = delegate.getResource(name, resourceName);
+ if (result != null)
{
- if (trace)
- log.trace(this + " trying to get resources " + name + " from import " + delegate + " for " + classLoader);
- delegate.getResources(name, resourceName, urls);
+ info.cacheResource(name, result);
+ return result;
}
}
-
- // Finally use any requesting classloader
- if (classLoader != null)
+ info.blackListResource(name);
+ return null;
+ }
+
+ /**
+ * Load resources from the imports
+ *
+ * @param info the classloader info
+ * @param name the resource name
+ * @param resourceName the name of the resource in dot notation
+ * @param urls the urls to add to
+ * @param trace whether trace is enabled
+ * @throws IOException for any error
+ */
+ void getResourcesFromImports(ClassLoaderInformation info, String name, String resourceName, Set<URL> urls, boolean trace) throws IOException
+ {
+ List<? extends DelegateLoader> delegates = info.getDelegates();
+ if (delegates == null || delegates.isEmpty())
{
if (trace)
- log.trace(this + " trying to get resources " + name + " from requesting " + classLoader);
- classLoader.getResourcesLocally(name, resourceName, urls);
+ log.trace(this + " not getting resource " + name + " from imports it has no delegates");
+ return;
}
+ if (trace)
+ log.trace(this + " trying to get resources " + name + " from imports " + delegates + " for " + info.getClassLoader());
+ for (DelegateLoader delegate : delegates)
+ delegate.getResources(name, resourceName, urls);
+ }
- // Try the after attempt
- afterGetResources(name, resourceName, urls);
+ /**
+ * Invoked before classloading is attempted to allow a preload attempt, e.g. from the parent
+ *
+ * @param name the class resource name
+ * @param resourceName the resource name in dot notation
+ * @return the loader if found or null otherwise
+ */
+ protected abstract Loader findBeforeLoader(String name, String resourceName);
+
+ /**
+ * Invoked after classloading is attempted to allow a postload attempt, e.g. from the parent
+ *
+ * @param name the class resource name
+ * @param resourceName the resource name in dot notation
+ * @return the loader if found or null otherwise
+ */
+ protected abstract Loader findAfterLoader(String name, String resourceName);
+
+ /**
+ * Invoked before getResources is attempted to allow a preload attempt, e.g. from the parent
+ *
+ * @param name the resource name
+ * @param resourceName the name of the resource in dot notation
+ * @param urls the urls to add to
+ * @throws IOException for any error
+ */
+ protected abstract void beforeGetResources(String name, String resourceName, Set<URL> urls) throws IOException;
+
+ /**
+ * Invoked after getResources is attempted to allow a postload attempt, e.g. from the parent
+ *
+ * @param name the resource name
+ * @param resourceName the name of the resource in dot notation
+ * @param urls the urls to add to
+ * @throws IOException for any error
+ */
+ protected abstract void afterGetResources(String name, String resourceName, Set<URL> urls) throws IOException;
+
+ /**
+ * Invoked before getResource is attempted to allow a preload attempt, e.g. from the parent
+ *
+ * @param name the resource name
+ * @param resourceName the name of the resource in dot notation
+ * @return the url if found or null otherwise
+ */
+ protected abstract URL beforeGetResource(String name, String resourceName);
+
+ /**
+ * Invoked after getResource is attempted to allow a postload attempt, e.g. from the parent
+ *
+ * @param name the class name
+ * @param resourceName the name of the resource in dot notation
+ * @return the url if found or null otherwise
+ */
+ protected abstract URL afterGetResource(String name, String resourceName);
+
+ public Class<?> loadClass(String name)
+ {
+ try
+ {
+ return loadClass(null, name, true);
+ }
+ catch (ClassNotFoundException e)
+ {
+ return null;
+ }
}
+
+ /**
+ * Load a class from the domain
+ *
+ * @param classLoader the classloader
+ * @param name the class name
+ * @return the class
+ * @throws ClassNotFoundException for any error
+ */
+ Class<?> loadClass(BaseClassLoader classLoader, String name) throws ClassNotFoundException
+ {
+ return loadClass(classLoader, name, false);
+ }
+
+ public URL getResource(String name, String resourceName)
+ {
+ return getResource(null, name, resourceName, true);
+ }
+
+ /**
+ * Get a resource from the domain
+ *
+ * @param classLoader the classloader
+ * @param name the resource name
+ * @param resourceName the name of the resource in dot notation
+ * @return the url
+ */
+ URL getResource(BaseClassLoader classLoader, String name, String resourceName)
+ {
+ return getResource(classLoader, name, resourceName, false);
+ }
+
+ public void getResources(String name, String resourceName, Set<URL> urls) throws IOException
+ {
+ getResources(null, name, resourceName, urls, true);
+ }
+
+ /**
+ * Get a resource from the domain
+ *
+ * @param classLoader the classloader
+ * @param name the resource name
+ * @param resourceName the name of the resource in dot notation
+ * @param urls the urls to add to
+ * @throws IOException for any error
+ */
+ void getResources(BaseClassLoader classLoader, String name, String resourceName, Set<URL> urls) throws IOException
+ {
+ getResources(classLoader, name, resourceName, urls, false);
+ }
/**
* A long version of toString()
@@ -557,6 +828,7 @@
{
// nothing
}
+
/**
* Invoked after adding a classloader policy
*
@@ -578,6 +850,7 @@
{
// nothing
}
+
/**
* Invoked after adding a classloader policy
*
@@ -588,16 +861,6 @@
{
// nothing
}
-
- /**
- * Remove a classloader
- *
- * @param classLoader the classloader
- */
- protected void removeClassLoader(BaseClassLoader classLoader)
- {
- // nothing
- }
/**
* Get the parent classloader
@@ -641,7 +904,6 @@
infos.put(classLoader, info);
// Index the packages
- // TODO Test base package and add support for dynamic packages to the policy
String[] packageNames = policy.getPackageNames();
if (packageNames != null && info.getExported() != null)
{
@@ -657,6 +919,8 @@
log.trace("Registered " + policy + " as providing package=" + packageName);
}
}
+
+ flushCaches();
}
try
@@ -712,6 +976,8 @@
}
}
}
+
+ flushCaches();
}
try
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderPolicy.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderPolicy.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderPolicy.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -140,6 +140,20 @@
}
/**
+ * Whether to cache<p>
+ *
+ * @return true to cache
+ */
+ protected abstract boolean isCachable();
+
+ /**
+ * Whether to cache misses<p>
+ *
+ * @return true to cache misses
+ */
+ protected abstract boolean isBlackListable();
+
+ /**
* Check whether this a request from the jdk if it is return the relevant classloader
*
* @param name the class name
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderSystem.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderSystem.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseClassLoaderSystem.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -108,6 +108,8 @@
{
if (domain == null)
throw new IllegalArgumentException("Null domain");
+ if (policy == null)
+ throw new IllegalArgumentException("Null policy");
return AccessController.doPrivileged(new PrivilegedAction<BaseClassLoader>()
{
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseDelegateLoader.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseDelegateLoader.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/BaseDelegateLoader.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -62,10 +62,14 @@
public Class<?> loadClass(String className)
{
- BaseClassLoader classLoader = delegate.getClassLoader();
- if (classLoader == null)
+ BaseClassLoader classLoader;
+ try
{
- log.warn("Not loading from policy that has no classLoader: " + toLongString(), new Throwable("STACKTRACE"));
+ classLoader = delegate.getClassLoader();
+ }
+ catch (IllegalStateException e)
+ {
+ log.warn("Not loading from policy that has no classLoader: " + toLongString());
return null;
}
return classLoader.loadClassLocally(className);
@@ -73,10 +77,14 @@
public URL getResource(String name, String resourceName)
{
- BaseClassLoader classLoader = delegate.getClassLoader();
- if (classLoader == null)
+ BaseClassLoader classLoader;
+ try
{
- log.warn("Not loading from policy that has no classLoader: " + toLongString(), new Throwable("STACKTRACE"));
+ classLoader = delegate.getClassLoader();
+ }
+ catch (IllegalStateException e)
+ {
+ log.warn("Not loading from policy that has no classLoader: " + toLongString());
return null;
}
return classLoader.getResourceLocally(name, resourceName);
@@ -84,10 +92,14 @@
public void getResources(String name, String resourceName, Set<URL> urls) throws IOException
{
- BaseClassLoader classLoader = delegate.getClassLoader();
- if (classLoader == null)
+ BaseClassLoader classLoader;
+ try
{
- log.warn("Not loading from policy that has no classLoader: " + toLongString(), new Throwable("STACKTRACE"));
+ classLoader = delegate.getClassLoader();
+ }
+ catch (IllegalStateException e)
+ {
+ log.warn("Not loading from policy that has no classLoader: " + toLongString());
return;
}
classLoader.getResourcesLocally(name, resourceName, urls);
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderInformation.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderInformation.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderInformation.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -21,6 +21,16 @@
*/
package org.jboss.classloader.spi.base;
+import java.net.URL;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+import org.jboss.classloader.spi.DelegateLoader;
+import org.jboss.classloader.spi.Loader;
+
/**
* ClassLoaderInformation.
*
@@ -38,9 +48,24 @@
/** The order */
private int order;
+ /** The delegates */
+ private List<? extends DelegateLoader> delegates;
+
/** The exports of the classloader */
private BaseDelegateLoader exported;
+ /** The class cache */
+ private Map<String, Loader> classCache;
+
+ /** The class black list */
+ private Set<String> classBlackList;
+
+ /** The resource cache */
+ private Map<String, URL> resourceCache;
+
+ /** The resource black list */
+ private Set<String> resourceBlackList;
+
/**
* Create a new ClassLoaderInformation.
*
@@ -59,9 +84,52 @@
this.policy = policy;
this.order = order;
this.exported = policy.getExported();
+ this.delegates = policy.getDelegates();
+
+ boolean canCache = policy.isCachable();
+ boolean canBlackList = policy.isBlackListable();
+ if (delegates != null && delegates.isEmpty() == false)
+ {
+ for (DelegateLoader delegate : delegates)
+ {
+ BaseDelegateLoader baseDelegate = delegate;
+ BaseClassLoaderPolicy delegatePolicy = baseDelegate.getPolicy();
+ if (delegatePolicy.isCachable() == false)
+ canCache = false;
+ if (delegatePolicy.isBlackListable() == false)
+ canBlackList = false;
+ }
+ }
+
+ if (canCache)
+ {
+ classCache = new ConcurrentHashMap<String, Loader>();
+ resourceCache = new ConcurrentHashMap<String, URL>();
+ }
+
+ if (canBlackList)
+ {
+ classBlackList = new CopyOnWriteArraySet<String>();
+ resourceBlackList = new CopyOnWriteArraySet<String>();
+ }
}
/**
+ * Flush the caches
+ */
+ public void flushCaches()
+ {
+ if (classCache != null)
+ classCache.clear();
+ if (classBlackList != null)
+ classBlackList.clear();
+ if (resourceCache != null)
+ resourceCache.clear();
+ if (resourceBlackList != null)
+ resourceBlackList.clear();
+ }
+
+ /**
* Get the classLoader.
*
* @return the classLoader.
@@ -100,7 +168,115 @@
{
return exported;
}
+
+ /**
+ * Get the delegates.
+ *
+ * @return the delegates.
+ */
+ public List<? extends DelegateLoader> getDelegates()
+ {
+ return delegates;
+ }
+ /**
+ * Get the cached loader for a class
+ *
+ * @param name the class name
+ * @return any cached loader
+ */
+ public Loader getCachedLoader(String name)
+ {
+ if (classCache != null)
+ return classCache.get(name);
+ return null;
+ }
+
+ /**
+ * Cache a loader for a class
+ *
+ * @param name the class name
+ * @param loader the cached loader
+ */
+ public void cacheLoader(String name, Loader loader)
+ {
+ if (classCache != null)
+ classCache.put(name, loader);
+ }
+
+ /**
+ * Check whether this is a black listed class
+ *
+ * @param name the class name
+ * @return true when black listed
+ */
+ public boolean isBlackListedClass(String name)
+ {
+ if (classBlackList != null)
+ return classBlackList.contains(name);
+ return false;
+ }
+
+ /**
+ * Blacklist a class
+ *
+ * @param name the class name to black list
+ */
+ public void blackListClass(String name)
+ {
+ if (classBlackList != null)
+ classBlackList.add(name);
+ }
+
+ /**
+ * Get the cached url for a resource
+ *
+ * @param name the resource name
+ * @return any cached url
+ */
+ public URL getCachedResource(String name)
+ {
+ if (resourceCache != null)
+ return resourceCache.get(name);
+ return null;
+ }
+
+ /**
+ * Cache a url for a resource
+ *
+ * @param name the resource name
+ * @param url the cached url
+ */
+ public void cacheResource(String name, URL url)
+ {
+ if (resourceCache != null)
+ resourceCache.put(name, url);
+ }
+
+ /**
+ * Check whether this is a black listed resource
+ *
+ * @param name the resource name
+ * @return true when black listed
+ */
+ public boolean isBlackListedResource(String name)
+ {
+ if (resourceBlackList != null)
+ return resourceBlackList.contains(name);
+ return false;
+ }
+
+ /**
+ * Blacklist a resource
+ *
+ * @param name the resource name to black list
+ */
+ public void blackListResource(String name)
+ {
+ if (resourceBlackList != null)
+ resourceBlackList.add(name);
+ }
+
@Override
public String toString()
{
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderManager.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderManager.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/base/ClassLoaderManager.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -167,7 +167,9 @@
}
// Assert that loadedClass is not null
else
- throw new IllegalStateException("ClassLoadingTask.loadedTask is null, name: " + task.getClassName());
+ {
+ throw new ClassNotFoundException("Failed to load class " + task.getClassName());
+ }
}
return loadedClass;
}
@@ -365,8 +367,8 @@
boolean gotLock = classLoader.attemptLock();
if (gotLock == false)
{
- // REVIEW: If we've been spinning for more than a minute then there is probably something wrong?
- if (waits++ == 6)
+ // Two minutes should be long enough?
+ if (waits++ == 12)
throw new IllegalStateException("Waiting too long to get the registration lock for classLoader " + classLoader);
// Wait 10 seconds
if (trace)
Copied: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/filter/PackageClassFilter.java (from rev 62723, projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/plugins/filter/PackageClassFilter.java)
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/filter/PackageClassFilter.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/filter/PackageClassFilter.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.spi.filter;
+
+import java.util.Arrays;
+
+import org.jboss.classloader.plugins.filter.PatternClassFilter;
+
+/**
+ * A class filter using regular expressions
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class PackageClassFilter extends PatternClassFilter
+{
+ /** The patterns as regular expressions */
+ private String[] packageNames;
+
+ /**
+ * Convert package names to patterns
+ *
+ * @param packageNames the package names
+ * @return the patterns
+ */
+ private static String[] convertPackageNamesToPatterns(String[] packageNames)
+ {
+ if (packageNames == null)
+ throw new IllegalArgumentException("Null package names");
+
+ String[] patterns = new String[packageNames.length];
+ for (int i = 0; i < packageNames.length; ++i)
+ {
+ if (packageNames[i] == null)
+ throw new IllegalArgumentException("Null package name in " + Arrays.asList(packageNames));
+
+ if (packageNames[i].length() == 0)
+ // Base package - it is a match if there is no . in the class name
+ patterns[i] = "[^.]*";
+ else
+ // Escape the dots in the package and match anything that has a single dot followed by non-dots
+ patterns[i] = packageNames[i].replace(".", "\\.") + "\\.[^.]+";
+ }
+ return patterns;
+ }
+
+ /**
+ * Create a new package class filter
+ *
+ * @param packageNames the package names
+ * @return the filter
+ * @throws IllegalArgumentException for null packageNames
+ */
+ public static PackageClassFilter createPackageClassFilter(String... packageNames)
+ {
+ return new PackageClassFilter(packageNames);
+ }
+
+ /**
+ * Create a new PackageClassFilter.
+ *
+ * @param packageNames the packageNames
+ * @throws IllegalArgumentException for null packageNames
+ */
+ public PackageClassFilter(String[] packageNames)
+ {
+ super(convertPackageNamesToPatterns(packageNames));
+ this.packageNames = packageNames;
+ }
+
+ @Override
+ public String toString()
+ {
+ return Arrays.asList(packageNames).toString();
+ }
+}
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/jdk/JDKCheckerFactory.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/jdk/JDKCheckerFactory.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/spi/jdk/JDKCheckerFactory.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -21,20 +21,51 @@
*/
package org.jboss.classloader.spi.jdk;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
import org.jboss.classloader.plugins.jdk.AbstractJDKChecker;
/**
* JDKCheckerFactory.
*
- * TODO parameterize this
+ *
* @author <a href="adrian at jboss.com">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public class JDKCheckerFactory
{
/** The checker */
- private static JDKChecker checker = new AbstractJDKChecker();
-
+ private static final JDKChecker checker;
+
+ static
+ {
+ checker = AccessController.doPrivileged(new PrivilegedAction<JDKChecker>()
+ {
+ public JDKChecker run()
+ {
+ // Decide what default checker to use based on the JDK (not implemented - YAGNI?)
+ String defaultChecker = AbstractJDKChecker.class.getName();
+
+ String className = System.getProperty(JDKChecker.class.getName(), defaultChecker);
+ try
+ {
+ Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
+ Object result = clazz.newInstance();
+ return JDKChecker.class.cast(result);
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw new Error("Unexpected error loading JDKChecker " + className, e);
+ }
+ }
+ });
+ }
+
/**
* Retrieve the checker for the JDK we are running on
*
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderHelper.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderHelper.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderHelper.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -92,6 +92,31 @@
}
});
}
+
+ /**
+ * Register a mock classloader policy<p>
+ *
+ * No security problem here. The user needs access to a ClassLoaderSystem
+ * to register a classloader with it.
+ *
+ * @param system the system
+ * @param domain the domain
+ * @param policy the policy
+ * @return the classloader
+ */
+ public static ClassLoader registerMockClassLoader(final ClassLoaderSystem system, final ClassLoaderDomain domain, final MockClassLoaderPolicy policy)
+ {
+ return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>()
+ {
+ public ClassLoader run()
+ {
+ if (domain == null)
+ return system.registerClassLoaderPolicy(policy);
+ else
+ return system.registerClassLoaderPolicy(domain, policy);
+ }
+ });
+ }
/**
* Check whether a class has the expected classloader
Modified: projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderPolicy.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderPolicy.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/main/org/jboss/classloader/test/support/MockClassLoaderPolicy.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -48,6 +48,9 @@
/** The logical name of the policy */
private String name;
+ /** The prefix */
+ private String prefix = "";
+
/** The delegates */
private List<? extends DelegateLoader> delegates;
@@ -134,6 +137,26 @@
}
/**
+ * Get the prefix.
+ *
+ * @return the prefix.
+ */
+ public String getPrefix()
+ {
+ return prefix;
+ }
+
+ /**
+ * Set the prefix.
+ *
+ * @param prefix the prefix.
+ */
+ public void setPrefix(String prefix)
+ {
+ this.prefix = prefix;
+ }
+
+ /**
* Get the paths to expose
*
* @return the paths
@@ -295,7 +318,7 @@
for (int i = 0; i < paths.length; ++i)
{
if (path.startsWith(paths[i]))
- return getClass().getClassLoader().getResource(path);
+ return getClass().getClassLoader().getResource(prefix + path);
}
return null;
}
@@ -335,7 +358,7 @@
{
if (path.startsWith(paths[i]))
{
- Enumeration<URL> enumeration = parent.getResources(path);
+ Enumeration<URL> enumeration = parent.getResources(prefix + path);
while (enumeration.hasMoreElements())
urls.add(enumeration.nextElement());
}
@@ -380,6 +403,8 @@
public void toLongString(StringBuilder builder)
{
builder.append(" name=").append(name);
+ if (prefix.length() > 0)
+ builder.append(" prefix=").append(prefix);
if (paths != null)
builder.append(" paths=").append(Arrays.asList(paths));
if (included != null)
Added: projects/microcontainer/trunk/classloader/src/resources/tests/a/com/acme/p1/testResource
===================================================================
--- projects/microcontainer/trunk/classloader/src/resources/tests/a/com/acme/p1/testResource (rev 0)
+++ projects/microcontainer/trunk/classloader/src/resources/tests/a/com/acme/p1/testResource 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1 @@
+a/com/acme/p1/testResource
\ No newline at end of file
Added: projects/microcontainer/trunk/classloader/src/resources/tests/a/testResource
===================================================================
--- projects/microcontainer/trunk/classloader/src/resources/tests/a/testResource (rev 0)
+++ projects/microcontainer/trunk/classloader/src/resources/tests/a/testResource 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1 @@
+a/testResource
\ No newline at end of file
Added: projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p1/testResource
===================================================================
--- projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p1/testResource (rev 0)
+++ projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p1/testResource 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1 @@
+b/com/acme/p1/testResource
\ No newline at end of file
Added: projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p2/testResource
===================================================================
--- projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p2/testResource (rev 0)
+++ projects/microcontainer/trunk/classloader/src/resources/tests/b/com/acme/p2/testResource 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1 @@
+b/com/acme/p2/testResource
\ No newline at end of file
Added: projects/microcontainer/trunk/classloader/src/resources/tests/b/testResource
===================================================================
--- projects/microcontainer/trunk/classloader/src/resources/tests/b/testResource (rev 0)
+++ projects/microcontainer/trunk/classloader/src/resources/tests/b/testResource 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1 @@
+b/testResource
\ No newline at end of file
Added: projects/microcontainer/trunk/classloader/src/tests/TestDefaultPackage.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/TestDefaultPackage.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/TestDefaultPackage.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.
+ */
+
+/**
+ * TestDefaultPackage
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestDefaultPackage
+{
+}
Modified: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/AbstractClassLoaderTest.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/AbstractClassLoaderTest.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/AbstractClassLoaderTest.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -26,6 +26,7 @@
import org.jboss.classloader.spi.ClassLoaderPolicy;
import org.jboss.classloader.spi.ClassLoaderSystem;
import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.filter.ClassFilter;
import org.jboss.classloader.test.support.MockClassLoaderHelper;
import org.jboss.classloader.test.support.MockClassLoaderPolicy;
import org.jboss.test.AbstractTestCaseWithSetup;
@@ -122,6 +123,8 @@
protected void assertClassLoader(Class<?> clazz, ClassLoader expected)
{
+ if (expected == null)
+ return;
boolean result = MockClassLoaderHelper.isExpectedClassLoader(clazz, expected);
assertTrue(ClassLoaderUtils.classToString(clazz) + " should have expected classloader=" + expected, result);
}
@@ -252,6 +255,22 @@
}
}
+ protected void assertFilterMatches(String test, ClassFilter filter)
+ {
+ getLog().debug("Checking " + test + " expect it to match filter=" + filter);
+ boolean result = filter.matches(test);
+ getLog().debug("Checked " + test + " result was " + result + " for filter=" + filter);
+ assertTrue("Expected " + test + " to match " + filter, result);
+ }
+
+ protected void assertFilterNoMatch(String test, ClassFilter filter)
+ {
+ getLog().debug("Checking " + test + " expect it NOT to match filter=" + filter);
+ boolean result = filter.matches(test);
+ getLog().debug("Checked " + test + " result was " + result + " for filter=" + filter);
+ assertFalse("Expected " + test + " NOT to match " + filter, result);
+ }
+
protected void configureLogging()
{
//enableTrace("org.jboss.classloader");
Modified: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/ClassLoaderAllTestSuite.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/ClassLoaderAllTestSuite.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/ClassLoaderAllTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -27,8 +27,12 @@
import org.jboss.test.classloader.bootstrap.BootstrapTestSuite;
import org.jboss.test.classloader.delegate.DelegateTestSuite;
+import org.jboss.test.classloader.domain.ClassLoaderDomainTestSuite;
+import org.jboss.test.classloader.filter.FilterTestSuite;
import org.jboss.test.classloader.old.OldTestSuite;
-import org.jboss.test.classloader.system.ClassLoadingSystemTestSuite;
+import org.jboss.test.classloader.policy.test.ClassLoaderPolicyUnitTestCase;
+import org.jboss.test.classloader.resources.ResourceTestSuite;
+import org.jboss.test.classloader.system.ClassLoaderSystemTestSuite;
/**
* ClassLoader All Test Suite.
@@ -57,10 +61,14 @@
{
TestSuite suite = new TestSuite("ClassLoader All Tests");
- suite.addTest(ClassLoadingSystemTestSuite.suite());
+ suite.addTest(ClassLoaderSystemTestSuite.suite());
+ suite.addTest(ClassLoaderDomainTestSuite.suite());
+ suite.addTest(ClassLoaderPolicyUnitTestCase.suite());
suite.addTest(BootstrapTestSuite.suite());
+ suite.addTest(OldTestSuite.suite());
+ suite.addTest(FilterTestSuite.suite());
suite.addTest(DelegateTestSuite.suite());
- suite.addTest(OldTestSuite.suite());
+ suite.addTest(ResourceTestSuite.suite());
return suite;
}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/support/TestClass.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/support/TestClass.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/support/TestClass.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.bootstrap.support;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+
+/**
+ * TestClass.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestClass
+{
+ public Object doSomething() throws Exception
+ {
+ Constructor constructor = getClass().getConstructor((Class[]) null);
+
+ Object object = constructor.newInstance((Object[]) null);
+
+ Method method = getClass().getMethod("doSomethingElse", (Class[]) null);
+ return method.invoke(object, (Object[]) null);
+ }
+
+ public ClassLoader doSomethingElse()
+ {
+ return getClass().getClassLoader();
+ }
+}
Modified: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/test/ModifiedBootstrapUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/test/ModifiedBootstrapUnitTestCase.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/bootstrap/test/ModifiedBootstrapUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -21,6 +21,8 @@
*/
package org.jboss.test.classloader.bootstrap.test;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
import java.util.List;
import javax.naming.Context;
@@ -28,7 +30,10 @@
import junit.framework.Test;
import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+import org.jboss.test.classloader.bootstrap.support.TestClass;
/**
* ModifiedBootstrapUnitTestCase.
@@ -68,6 +73,23 @@
testNotBootstrapClass(ClassLoaderDomain.class);
}
+ public void testReflection() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(TestClass.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ Class clazz = assertLoadClass(TestClass.class, classLoader);
+ Constructor constructor = clazz.getConstructor((Class[]) null);
+
+ Object object = constructor.newInstance((Object[]) null);
+
+ Method method = clazz.getMethod("doSomething", (Class[]) null);
+ Object result = method.invoke(object, (Object[]) null);
+ assertEquals(classLoader, result);
+ }
+
protected void testBootstrapClass(Class<?> clazz) throws Exception
{
ClassLoader classLoader = createClassLoaderSystemWithModifiedBootstrapAndMockClassLoader();
Modified: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/DelegateTestSuite.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/DelegateTestSuite.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/DelegateTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -25,6 +25,7 @@
import junit.framework.TestSuite;
import junit.textui.TestRunner;
+import org.jboss.test.classloader.delegate.test.DelegateLoaderUnitTestCase;
import org.jboss.test.classloader.delegate.test.DelegateUnitTestCase;
/**
@@ -54,6 +55,7 @@
{
TestSuite suite = new TestSuite("Delegate Tests");
+ suite.addTest(DelegateLoaderUnitTestCase.suite());
suite.addTest(DelegateUnitTestCase.suite());
return suite;
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateLoaderUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateLoaderUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateLoaderUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,101 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.delegate.test;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.DelegateLoader;
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.spi.filter.FilteredDelegateLoader;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+import org.jboss.test.classloader.delegate.support.a.TestA1;
+
+/**
+ * DelegateUnitTestCase
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class DelegateLoaderUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(DelegateLoaderUnitTestCase.class);
+ }
+
+ public DelegateLoaderUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testLoadClassNoFilter() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(TestA1.class);
+ system.registerClassLoaderPolicy(policy);
+
+ FilteredDelegateLoader delegate = new FilteredDelegateLoader(policy);
+ assertLoadClass(TestA1.class, delegate);
+ }
+
+ public void testLoadClassFiltered() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(TestA1.class);
+ system.registerClassLoaderPolicy(policy);
+
+ FilteredDelegateLoader delegate = new FilteredDelegateLoader(policy, ClassFilter.NOTHING);
+ assertLoadClassFail(TestA1.class, delegate);
+ }
+
+ public void testLoadClassPolicyNotRegistered() throws Exception
+ {
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(TestA1.class);
+
+ FilteredDelegateLoader delegate = new FilteredDelegateLoader(policy);
+ assertLoadClassFail(TestA1.class, delegate);
+ }
+
+ protected Class<?> assertLoadClass(Class<?> reference, DelegateLoader delegate) throws Exception
+ {
+ Class<?> result = delegate.loadClass(reference.getName());
+ assertNotNull("Should have loaded " + reference.getName() + " from " + delegate);
+ getLog().debug("Loaded " + ClassLoaderUtils.classToString(result) + " from " + delegate);
+ return result;
+ }
+
+ protected void assertLoadClassFail(Class<?> reference, DelegateLoader delegate) throws Exception
+ {
+ Class<?> result = delegate.loadClass(reference.getName());
+ String message = "Should not have loaded " + ClassLoaderUtils.classToString(result) + " from " + delegate;
+ getLog().error(message);
+ assertNull(message, result);
+ }
+}
Modified: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateUnitTestCase.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/delegate/test/DelegateUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -33,6 +33,7 @@
import org.jboss.classloader.spi.ClassLoaderDomain;
import org.jboss.classloader.spi.ClassLoaderSystem;
import org.jboss.classloader.spi.DelegateLoader;
+import org.jboss.classloader.spi.ParentPolicy;
import org.jboss.classloader.spi.filter.FilteredDelegateLoader;
import org.jboss.classloader.test.support.MockClassLoaderPolicy;
import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
@@ -117,8 +118,8 @@
ClassLoader b = system.registerClassLoaderPolicy(pb);
Class<?> testAbstractFactoryClass = assertLoadClass(TestAbstractFactory.class, a);
- Method method = testAbstractFactoryClass.getMethod("getInstance", null);
- Object instance = method.invoke(null, null);
+ Method method = testAbstractFactoryClass.getMethod("getInstance", (Class[]) null);
+ Object instance = method.invoke(null, (Object[]) null);
Class<?> testFactoryImplementationClass = assertLoadClass(TestFactoryImplementation.class, b);
assertClassEquality(testFactoryImplementationClass, instance.getClass());
}
@@ -141,7 +142,7 @@
Class<?> delegate = assertLoadClass(TestADelegateClassLoaderDomain.class, a, false);
- Method method = delegate.getMethod("getSomething", null);
+ Method method = delegate.getMethod("getSomething", (Class[]) null);
fromA = method.getReturnType();
assertNotNull(fromA);
assertClassLoader(fromA, b);
@@ -169,12 +170,66 @@
ClassLoader b = system.registerClassLoaderPolicy(pb);
Class<?> testAbstractFactoryClass = assertLoadClass(TestAbstractFactory.class, b, a);
- Method method = testAbstractFactoryClass.getMethod("getInstance", null);
- Object instance = method.invoke(null, null);
+ Method method = testAbstractFactoryClass.getMethod("getInstance", (Class[]) null);
+ Object instance = method.invoke(null, (Object[]) null);
Class<?> testFactoryImplementationClass = assertLoadClass(TestFactoryImplementation.class, a, b);
assertClassEquality(testFactoryImplementationClass, instance.getClass());
}
-
+
+ public void testLoadClassFromDefaultPackage() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" });
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ assertLoadClass("TestDefaultPackage", classLoader);
+ }
+
+ public void testLoadClassFromDefaultPackageFromDelegate() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" });
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setDelegates(Collections.singletonList(new FilteredDelegateLoader(policy)));
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader2 = system.registerClassLoaderPolicy(policy2);
+
+ assertLoadClass("TestDefaultPackage", classLoader2, classLoader);
+ }
+
+ public void testLoadClassFromDefaultPackageFromExports() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" });
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setImportAll(true);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader2 = system.registerClassLoaderPolicy(policy2);
+
+ assertLoadClass("TestDefaultPackage", classLoader2, classLoader);
+ }
+
+ public void testLoadClassFromDefaultPackageFromParent() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent", ParentPolicy.BEFORE_BUT_JAVA_ONLY);
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" });
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(parent, policy);
+ ClassLoader classLoader2 = system.registerClassLoaderPolicy(child, policy2);
+
+ assertLoadClass("TestDefaultPackage", classLoader2, classLoader);
+ }
+
public void testAbstractFactoryConcurrent() throws Exception
{
for (int i = 0; i < 10; ++i)
@@ -219,8 +274,8 @@
Class<?> testAbstractFactoryClass = assertLoadClass(TestAbstractFactory.class, a);
try
{
- Method method = testAbstractFactoryClass.getMethod("getInstance", null);
- method.invoke(null, null);
+ Method method = testAbstractFactoryClass.getMethod("getInstance", (Class[]) null);
+ method.invoke(null, (Object[]) null);
}
catch (Exception e)
{
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/ClassLoaderDomainTestSuite.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/ClassLoaderDomainTestSuite.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/ClassLoaderDomainTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.domain;
+
+import org.jboss.test.classloader.domain.test.CustomParentLoaderUnitTestCase;
+import org.jboss.test.classloader.domain.test.HierarchicalParentLoaderUnitTestCase;
+import org.jboss.test.classloader.domain.test.ParentPolicyUnitTestCase;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * ClassLoaderDomain Test Suite.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class ClassLoaderDomainTestSuite extends TestSuite
+{
+ /**
+ * For running the testsuite from the command line
+ *
+ * @param args the command line args
+ */
+ public static void main(String[] args)
+ {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create the testsuite
+ *
+ * @return the testsuite
+ */
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("ClassLoader Domain Tests");
+
+ suite.addTest(ParentPolicyUnitTestCase.suite());
+ suite.addTest(CustomParentLoaderUnitTestCase.suite());
+ suite.addTest(HierarchicalParentLoaderUnitTestCase.suite());
+
+ return suite;
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MatchClassFilter.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MatchClassFilter.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MatchClassFilter.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.domain.support;
+
+import org.jboss.classloader.spi.filter.ClassFilter;
+
+/**
+ * MatchClassFilter.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class MatchClassFilter implements ClassFilter
+{
+ String className;
+
+ public boolean filtered = false;
+
+ public MatchClassFilter(Class clazz)
+ {
+ this.className = clazz.getName();
+ }
+
+ public boolean matches(String className)
+ {
+ if (this.className.equals(className))
+ filtered = true;
+ return true;
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MockLoader.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MockLoader.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/MockLoader.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.domain.support;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.jboss.classloader.spi.Loader;
+
+/**
+ * CustomLoader.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class MockLoader implements Loader
+{
+ public Set<String> getResource = new HashSet<String>();
+ public Set<String> getResources = new HashSet<String>();
+ public Set<String> loadClass = new HashSet<String>();
+
+ public URL getResource(String name, String resourceName)
+ {
+ getResource.add(name);
+ return getClass().getClassLoader().getResource(name);
+ }
+
+ public void getResources(String name, String resourceName, Set<URL> urls) throws IOException
+ {
+ // Nothing
+ }
+
+ public Class<?> loadClass(String className)
+ {
+ loadClass.add(className);
+ try
+ {
+ return getClass().getClassLoader().loadClass(className);
+ }
+ catch (ClassNotFoundException e)
+ {
+ return null;
+ }
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/NoMatchClassFilter.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/NoMatchClassFilter.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/support/NoMatchClassFilter.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.domain.support;
+
+import org.jboss.classloader.spi.filter.ClassFilter;
+
+/**
+ * NoMatchClassFilter.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class NoMatchClassFilter implements ClassFilter
+{
+ String className;
+
+ public boolean filtered = false;
+
+ public NoMatchClassFilter(Class clazz)
+ {
+ this.className = clazz.getName();
+ }
+
+ public boolean matches(String className)
+ {
+ if (this.className.equals(className))
+ {
+ filtered = true;
+ return false;
+ }
+ return true;
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/CustomParentLoaderUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/CustomParentLoaderUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/CustomParentLoaderUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,161 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.domain.test;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.Loader;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.base.BaseClassLoaderDomain;
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+import org.jboss.test.classloader.domain.support.MockLoader;
+import org.jboss.test.classloader.domain.support.NoMatchClassFilter;
+
+/**
+ * ParentPolicyUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class CustomParentLoaderUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(CustomParentLoaderUnitTestCase.class);
+ }
+
+ public CustomParentLoaderUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testCustomLoaderBefore() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockLoader loader = new MockLoader();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.BEFORE, loader);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(MockLoader.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(MockLoader.class, classLoader, null, true);
+ checkGetResource(loader, MockLoader.class);
+ checkLoadClass(loader, MockLoader.class);
+ }
+
+ public void testCustomLoaderBeforeNotFound() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockLoader loader = new MockLoader();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.BEFORE, loader);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(ClassLoaderDomain.class, classLoader);
+ checkGetResource(loader, ClassLoaderDomain.class, BaseClassLoaderDomain.class, Loader.class, Object.class);
+ checkLoadClass(loader);
+ }
+
+ public void testCustomLoaderAfterNotReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockLoader loader = new MockLoader();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.AFTER, loader);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(MockLoader.class, classLoader);
+ checkGetResource(loader);
+ checkLoadClass(loader);
+ }
+
+ public void testCustomLoaderAfterReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockLoader loader = new MockLoader();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.AFTER, loader);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(MockLoader.class, classLoader, null, true);
+ checkGetResource(loader, MockLoader.class);
+ checkLoadClass(loader, MockLoader.class);
+ }
+
+ public void testCustomLoaderFiltered() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockLoader loader = new MockLoader();
+ NoMatchClassFilter filter = new NoMatchClassFilter(MockLoader.class);
+ ParentPolicy parentPolicy = new ParentPolicy(filter, ClassFilter.NOTHING);
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", parentPolicy, loader);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(MockLoader.class, classLoader);
+ checkGetResource(loader);
+ checkLoadClass(loader);
+ assertTrue("Should have been filtered", filter.filtered);
+ }
+
+ protected void checkGetResource(MockLoader loader, Class... classes)
+ {
+ if (classes == null || classes.length == 0)
+ {
+ assertEmpty(loader.getResources);
+ return;
+ }
+ Set<String> resourceNames = new HashSet<String>();
+ for (Class clazz : classes)
+ resourceNames.add(ClassLoaderUtils.classNameToPath(clazz.getName()));
+ assertEquals(resourceNames, loader.getResource);
+ }
+
+ protected void checkLoadClass(MockLoader loader, Class... classes)
+ {
+ if (classes == null || classes.length == 0)
+ {
+ assertEmpty(loader.loadClass);
+ return;
+ }
+ Set<String> classNames = new HashSet<String>();
+ for (Class clazz : classes)
+ classNames.add(clazz.getName());
+ assertEquals(classNames, loader.loadClass);
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/HierarchicalParentLoaderUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/HierarchicalParentLoaderUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/HierarchicalParentLoaderUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,136 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.domain.test;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.Loader;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+import org.jboss.test.classloader.domain.support.MockLoader;
+import org.jboss.test.classloader.domain.support.NoMatchClassFilter;
+
+/**
+ * ParentPolicyUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class HierarchicalParentLoaderUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(HierarchicalParentLoaderUnitTestCase.class);
+ }
+
+ public HierarchicalParentLoaderUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testHierarchyBefore() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent", ParentPolicy.BEFORE_BUT_JAVA_ONLY);
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+
+ MockClassLoaderPolicy parentPolicy = createMockClassLoaderPolicy();
+ parentPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader parentClassLoader = system.registerClassLoaderPolicy(parent, parentPolicy);
+
+ MockClassLoaderPolicy childPolicy = createMockClassLoaderPolicy();
+ childPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, childPolicy);
+
+ assertLoadClass(MockLoader.class, classLoader, parentClassLoader);
+ }
+
+ public void testHierarchyBeforeNotFound() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent", ParentPolicy.BEFORE_BUT_JAVA_ONLY);
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+
+ MockClassLoaderPolicy childPolicy = createMockClassLoaderPolicy();
+ childPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, childPolicy);
+
+ assertLoadClass(MockLoader.class, classLoader);
+ }
+
+ public void testHierarchyAfterNotReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent", ParentPolicy.BEFORE_BUT_JAVA_ONLY);
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.AFTER_BUT_JAVA_BEFORE, parent);
+
+ MockClassLoaderPolicy parentPolicy = createMockClassLoaderPolicy();
+ parentPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ system.registerClassLoaderPolicy(parent, parentPolicy);
+
+ MockClassLoaderPolicy childPolicy = createMockClassLoaderPolicy();
+ childPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, childPolicy);
+
+ assertLoadClass(MockLoader.class, classLoader);
+ }
+
+ public void testHierarchyAfterReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent", ParentPolicy.BEFORE_BUT_JAVA_ONLY);
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.AFTER_BUT_JAVA_BEFORE, parent);
+
+ MockClassLoaderPolicy parentPolicy = createMockClassLoaderPolicy();
+ parentPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader parentClassLoader = system.registerClassLoaderPolicy(parent, parentPolicy);
+
+ MockClassLoaderPolicy childPolicy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, childPolicy);
+
+ assertLoadClass(MockLoader.class, classLoader, parentClassLoader);
+ }
+
+ public void testHierarchyFiltered() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ NoMatchClassFilter filter = new NoMatchClassFilter(MockLoader.class);
+ ParentPolicy pp = new ParentPolicy(filter, ClassFilter.NOTHING);
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent", ParentPolicy.BEFORE_BUT_JAVA_ONLY);
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", pp, parent);
+
+ MockClassLoaderPolicy parentPolicy = createMockClassLoaderPolicy();
+ parentPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ system.registerClassLoaderPolicy(parent, parentPolicy);
+
+ MockClassLoaderPolicy childPolicy = createMockClassLoaderPolicy();
+ childPolicy.setPathsAndPackageNames(MockLoader.class, Loader.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, childPolicy);
+
+ assertLoadClass(MockLoader.class, classLoader);
+ assertTrue("Should have been filtered", filter.filtered);
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/ParentPolicyUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/ParentPolicyUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/domain/test/ParentPolicyUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,230 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.domain.test;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+import org.jboss.test.classloader.domain.support.MatchClassFilter;
+import org.jboss.test.classloader.domain.support.NoMatchClassFilter;
+
+/**
+ * ParentPolicyUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ParentPolicyUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(ParentPolicyUnitTestCase.class);
+ }
+
+ public ParentPolicyUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testNoParentLoadsFromJBossClassLoadersClassLoader() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.BEFORE, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader, null, true);
+ }
+
+ public void testBefore() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.BEFORE, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader, null, true);
+ }
+
+ public void testBeforeButJavaOnly() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.BEFORE_BUT_JAVA_ONLY, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader);
+ }
+
+ public void testBeforeButJavaOnlyNotFound() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.BEFORE_BUT_JAVA_ONLY, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClassFail(ClassLoaderDomain.class, classLoader);
+ }
+
+ public void testAfterNotReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.AFTER, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader);
+ }
+
+ public void testAfterReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.AFTER, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader, null, true);
+ }
+
+ public void testAfterButJavaBeforeNotReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.AFTER_BUT_JAVA_BEFORE, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader);
+ }
+
+ public void testAfterButJavaBeforeReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", ParentPolicy.AFTER_BUT_JAVA_BEFORE, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader, null, true);
+ }
+
+ public void testBeforeFilteredNotMatched() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ NoMatchClassFilter filter = new NoMatchClassFilter(ClassLoaderDomain.class);
+ ParentPolicy parentPolicy = new ParentPolicy(filter, ClassFilter.NOTHING);
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", parentPolicy, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader);
+ assertTrue("Should have been filtered", filter.filtered);
+ }
+
+ public void testBeforeFilteredMatched() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MatchClassFilter filter = new MatchClassFilter(ClassLoaderDomain.class);
+ ParentPolicy parentPolicy = new ParentPolicy(filter, ClassFilter.NOTHING);
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", parentPolicy, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader, null, true);
+ assertTrue("Should have been filtered", filter.filtered);
+ }
+
+ public void testAfterFilteredNotReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ NoMatchClassFilter filter = new NoMatchClassFilter(ClassLoaderDomain.class);
+ ParentPolicy parentPolicy = new ParentPolicy(ClassFilter.JAVA_ONLY, filter);
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", parentPolicy, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader);
+ assertFalse("Should NOT have been filtered", filter.filtered);
+ }
+
+ public void testAfterFilteredReachedNotMatched() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ NoMatchClassFilter filter = new NoMatchClassFilter(ClassLoaderDomain.class);
+ ParentPolicy parentPolicy = new ParentPolicy(ClassFilter.JAVA_ONLY, filter);
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", parentPolicy, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClassFail(ClassLoaderDomain.class, classLoader);
+ assertTrue("Should have been filtered", filter.filtered);
+ }
+
+ public void testAfterFilteredReachedMatched() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MatchClassFilter filter = new MatchClassFilter(ClassLoaderDomain.class);
+ ParentPolicy parentPolicy = new ParentPolicy(ClassFilter.JAVA_ONLY, filter);
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", parentPolicy, null);
+
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ assertLoadClass(Object.class, classLoader, null, true);
+ assertLoadClass(ClassLoaderDomain.class, classLoader, null, true);
+ assertTrue("Should have been filtered", filter.filtered);
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/FilterTestSuite.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/FilterTestSuite.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/FilterTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.filter;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import org.jboss.test.classloader.filter.test.FilterUnitTestCase;
+import org.jboss.test.classloader.filter.test.PackageFilterUnitTestCase;
+
+/**
+ * Filter Test Suite.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class FilterTestSuite extends TestSuite
+{
+ /**
+ * For running the testsuite from the command line
+ *
+ * @param args the command line args
+ */
+ public static void main(String[] args)
+ {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create the testsuite
+ *
+ * @return the testsuite
+ */
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("Filter Tests");
+
+ suite.addTest(FilterUnitTestCase.suite());
+ suite.addTest(PackageFilterUnitTestCase.suite());
+
+ return suite;
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/FilterUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/FilterUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/FilterUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,84 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.filter.test;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+
+/**
+ * FilterUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class FilterUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(FilterUnitTestCase.class);
+ }
+
+ public FilterUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testEverything() throws Exception
+ {
+ ClassFilter filter = ClassFilter.EVERYTHING;
+ assertFilterMatches("gibberish", filter);
+ assertFilterMatches("", filter);
+ assertFilterMatches(null, filter);
+ }
+
+ public void testNothing() throws Exception
+ {
+ ClassFilter filter = ClassFilter.NOTHING;
+ assertFilterNoMatch("gibberish", filter);
+ assertFilterNoMatch("", filter);
+ assertFilterNoMatch(null, filter);
+ }
+
+ public void testJavaOnly() throws Exception
+ {
+ ClassFilter filter = ClassFilter.JAVA_ONLY;
+ assertFilterMatches("java.x", filter);
+ assertFilterMatches("java.lang.Object", filter);
+ assertFilterMatches("java.lang.ref.Method", filter);
+ assertFilterMatches("java.util.Collection", filter);
+ assertFilterMatches("javax.x", filter);
+ assertFilterMatches("javax.naming.Context", filter);
+ assertFilterNoMatch("java.", filter);
+ assertFilterNoMatch("java", filter);
+ assertFilterNoMatch("javaa.", filter);
+ assertFilterNoMatch("javaa.whatever", filter);
+ assertFilterNoMatch("javax", filter);
+ assertFilterNoMatch("javax.", filter);
+ assertFilterNoMatch("javaxa.", filter);
+ assertFilterNoMatch("javaxa.whatever", filter);
+ assertFilterNoMatch("gibberish", filter);
+ assertFilterNoMatch("", filter);
+ assertFilterNoMatch(null, filter);
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/PackageFilterUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/PackageFilterUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/filter/test/PackageFilterUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,85 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.filter.test;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.spi.filter.PackageClassFilter;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+
+/**
+ * PackageFilterUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class PackageFilterUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(PackageFilterUnitTestCase.class);
+ }
+
+ public PackageFilterUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testNothing() throws Exception
+ {
+ ClassFilter filter = PackageClassFilter.createPackageClassFilter();
+ assertFilterNoMatch("gibberish", filter);
+ assertFilterNoMatch("", filter);
+ assertFilterNoMatch(null, filter);
+ }
+
+ public void testJavaLang() throws Exception
+ {
+ ClassFilter filter = PackageClassFilter.createPackageClassFilter("java.lang");
+ assertFilterMatches("java.lang.Object", filter);
+ assertFilterMatches("java.lang.X", filter);
+ assertFilterNoMatch("java.lang", filter);
+ assertFilterNoMatch("java.lang.", filter);
+ assertFilterNoMatch("java.lang.reflect.Method", filter);
+ assertFilterNoMatch("gibberish", filter);
+ assertFilterNoMatch("", filter);
+ assertFilterNoMatch(null, filter);
+ }
+
+ public void testJavaLangAndJavaLangReflect() throws Exception
+ {
+ ClassFilter filter = PackageClassFilter.createPackageClassFilter("java.lang", "java.lang.reflect");
+ assertFilterMatches("java.lang.Object", filter);
+ assertFilterMatches("java.lang.X", filter);
+ assertFilterMatches("java.lang.reflect.Method", filter);
+ assertFilterMatches("java.lang.reflect.X", filter);
+ assertFilterMatches("java.lang.reflect", filter);
+ assertFilterNoMatch("java.lang", filter);
+ assertFilterNoMatch("java.lang.", filter);
+ assertFilterNoMatch("java.lang.reflect.", filter);
+ assertFilterNoMatch("java.lang.reflect.subpackage.Something", filter);
+ assertFilterNoMatch("gibberish", filter);
+ assertFilterNoMatch("", filter);
+ assertFilterNoMatch(null, filter);
+ }
+}
Modified: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/old/test/CircularLoadUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/old/test/CircularLoadUnitTestCase.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/old/test/CircularLoadUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -89,13 +89,13 @@
// Load and create an instance of the UserOfBase class
Class<?> userOfBaseClass = assertLoadClass(UserOfBase.class, cl0);
- Constructor ctor0 = userOfBaseClass.getConstructor(null);
- Object userOfBase = ctor0.newInstance(null);
+ Constructor ctor0 = userOfBaseClass.getConstructor((Class[]) null);
+ Object userOfBase = ctor0.newInstance((Object[]) null);
// Load and create an instance of the Support class
Class<?> supportClass = assertLoadClass(Support.class, cl1);
- Constructor ctor1 = supportClass.getConstructor(null);
- Object support = ctor1.newInstance(null);
+ Constructor ctor1 = supportClass.getConstructor((Class[]) null);
+ Object support = ctor1.newInstance((Object[]) null);
// Now invoke UserOfBase.testBase(Support)
Class[] sig = { supportClass };
@@ -169,8 +169,8 @@
assertLoadClass(Class0.class, cl0);
assertLoadClass(Class2.class, cl1, cl0);
Class<?> base = assertLoadClass(Base.class, cl0);
- Method run = base.getMethod("run", null);
- run.invoke(null, null);
+ Method run = base.getMethod("run", (Class[]) null);
+ run.invoke(null, (Object[]) null);
}
public void testUCLOwner() throws Exception
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/ClassLoaderPolicyTestSuite.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/ClassLoaderPolicyTestSuite.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/ClassLoaderPolicyTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.policy;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import org.jboss.test.classloader.policy.test.ClassLoaderPolicyUnitTestCase;
+
+/**
+ * ClassLoaderPolicy Test Suite.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class ClassLoaderPolicyTestSuite extends TestSuite
+{
+ /**
+ * For running the testsuite from the command line
+ *
+ * @param args the command line args
+ */
+ public static void main(String[] args)
+ {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create the testsuite
+ *
+ * @return the testsuite
+ */
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("ClassLoader Policy Tests");
+
+ suite.addTest(ClassLoaderPolicyUnitTestCase.suite());
+
+ return suite;
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClass.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClass.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClass.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.policy.support;
+
+/**
+ * TestClass.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestClass
+{
+
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClassLoaderPolicy.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClassLoaderPolicy.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestClassLoaderPolicy.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,155 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.policy.support;
+
+import java.io.FilePermission;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.CodeSource;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.security.ProtectionDomain;
+import java.security.cert.Certificate;
+import java.util.List;
+import java.util.Set;
+
+import javax.naming.Context;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.classloader.spi.ClassLoaderPolicy;
+import org.jboss.classloader.spi.DelegateLoader;
+import org.jboss.classloader.spi.PackageInformation;
+
+/**
+ * TestClassLoaderPolicy.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestClassLoaderPolicy extends ClassLoaderPolicy
+{
+ public static String testClassPath = ClassLoaderUtils.classNameToPath(TestClass.class.getName());
+ public static URL sealBase;
+ public static URL codeSourceURL;
+ public String getResourceInvoked;
+ public String getResourceAsStreamInvoked;
+ public String getResourcesInvoked;
+
+ public List<? extends DelegateLoader> delegates;
+ public String[] packageNames;
+ public boolean isImportAll = false;
+ public DelegateLoader exported;
+
+ static
+ {
+ try
+ {
+ sealBase = new URL("http://localhost:9090");
+ codeSourceURL = new URL("http://localhost:8080");
+ }
+ catch (MalformedURLException e)
+ {
+ throw new Error("Unexpected", e);
+ }
+ }
+
+ @Override
+ protected List<? extends DelegateLoader> getDelegates()
+ {
+ return delegates;
+ }
+
+ @Override
+ public String[] getPackageNames()
+ {
+ return packageNames;
+ }
+
+ @Override
+ protected DelegateLoader getExported()
+ {
+ if (exported != null)
+ return exported;
+ return super.getExported();
+ }
+
+ @Override
+ protected boolean isImportAll()
+ {
+ return isImportAll;
+ }
+
+ @Override
+ public PackageInformation getPackageInformation(String packageName)
+ {
+ PackageInformation pi = new PackageInformation(ClassLoaderUtils.getClassPackageName(TestClass.class.getName()));
+ pi.specTitle = "SpecTitle";
+ pi.specVendor = "SpecVendor";
+ pi.specVersion = "SpecVersion";
+ pi.implTitle = "ImplTitle";
+ pi.implVendor = "ImplVendor";
+ pi.implVersion = "ImplVersion";
+ pi.sealBase = sealBase;
+ return pi;
+ }
+
+ @Override
+ protected ProtectionDomain getProtectionDomain(String className, String path)
+ {
+ CodeSource codeSource = new CodeSource(codeSourceURL, (Certificate[]) null);
+ PermissionCollection permissions = new Permissions();
+ permissions.add(new FilePermission("<<ALL FILES>>", "read"));
+ return new ProtectionDomain(codeSource, permissions);
+ }
+
+ @Override
+ public URL getResource(String path)
+ {
+ getResourceInvoked = path;
+ if (testClassPath.equals(path))
+ return getClass().getClassLoader().getResource(path);
+ return null;
+ }
+
+ @Override
+ public InputStream getResourceAsStream(String path)
+ {
+ getResourceAsStreamInvoked = path;
+ return super.getResourceAsStream(path);
+ }
+
+ @Override
+ public void getResources(String name, Set<URL> urls) throws IOException
+ {
+ getResourcesInvoked = name;
+ }
+
+ @Override
+ protected ClassLoader isJDKRequest(String name)
+ {
+ if (Context.class.getName().equals(name))
+ return getClass().getClassLoader();
+ return super.isJDKRequest(name);
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestDelegateLoader.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestDelegateLoader.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/support/TestDelegateLoader.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.policy.support;
+
+import java.net.URL;
+
+import org.jboss.classloader.spi.ClassLoaderPolicy;
+import org.jboss.classloader.spi.DelegateLoader;
+
+/**
+ * TestDelegateLoader.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class TestDelegateLoader extends DelegateLoader
+{
+ public String getResourceInvoked;
+ public String loadClassInvoked;
+
+ public TestDelegateLoader(ClassLoaderPolicy delegate)
+ {
+ super(delegate);
+ }
+
+ @Override
+ public Class<?> loadClass(String className)
+ {
+ loadClassInvoked = className;
+ return null;
+ }
+
+ public URL getResource(String name, String resourceName)
+ {
+ getResourceInvoked = name;
+ return TestClassLoaderPolicy.codeSourceURL;
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/test/ClassLoaderPolicyUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/test/ClassLoaderPolicyUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/policy/test/ClassLoaderPolicyUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,261 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.policy.test;
+
+import java.io.FilePermission;
+import java.net.URL;
+import java.security.CodeSource;
+import java.security.PermissionCollection;
+import java.security.ProtectionDomain;
+import java.util.Collections;
+
+import javax.naming.Context;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.filter.ClassFilter;
+import org.jboss.classloader.spi.filter.FilteredDelegateLoader;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.AbstractClassLoaderTest;
+import org.jboss.test.classloader.policy.support.TestClass;
+import org.jboss.test.classloader.policy.support.TestClassLoaderPolicy;
+import org.jboss.test.classloader.policy.support.TestDelegateLoader;
+
+/**
+ * ClassLoaderSystemUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassLoaderPolicyUnitTestCase extends AbstractClassLoaderTest
+{
+ public static Test suite()
+ {
+ return suite(ClassLoaderPolicyUnitTestCase.class);
+ }
+
+ public ClassLoaderPolicyUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testLoadClassInvoked() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ Class<?> clazz = assertLoadClass(TestClass.class, classLoader);
+ assertEquals("getResourceAsStream should have been invoked", TestClassLoaderPolicy.testClassPath, policy.getResourceAsStreamInvoked);
+ assertEquals("getResource should have been invoked", TestClassLoaderPolicy.testClassPath, policy.getResourceInvoked);
+
+ Package pkge = clazz.getPackage();
+ getLog().debug("Package " + pkge);
+ assertEquals(ClassLoaderUtils.getClassPackageName(TestClass.class.getName()), pkge.getName());
+ assertEquals("SpecTitle", pkge.getSpecificationTitle());
+ assertEquals("SpecVendor", pkge.getSpecificationVendor());
+ assertEquals("SpecVersion", pkge.getSpecificationVersion());
+ assertEquals("ImplTitle", pkge.getImplementationTitle());
+ assertEquals("ImplVendor", pkge.getImplementationVendor());
+ assertEquals("ImplVersion", pkge.getImplementationVersion());
+ assertTrue("Package should be sealed ", pkge.isSealed());
+ assertTrue("URL should be sealed ", pkge.isSealed(TestClassLoaderPolicy.sealBase));
+
+ ProtectionDomain pd = clazz.getProtectionDomain();
+ CodeSource cs = pd.getCodeSource();
+ assertEquals(TestClassLoaderPolicy.codeSourceURL, cs.getLocation());
+
+ PermissionCollection permissions = pd.getPermissions();
+ URL url = getClass().getClassLoader().getResource("java/lang/Object.class");
+ String file = url.getFile();
+ FilePermission fp = new FilePermission(file, "read");
+ assertTrue("Should have read permission", permissions.implies(fp));
+ RuntimePermission rp = new RuntimePermission("createClassLoader");
+ assertFalse("Shouldn't have classloader permissions", permissions.implies(rp));
+ }
+
+ public void testGetResourceInvoked() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ classLoader.getResource("test");
+ assertEquals("getResource() should have been invoked", "test", policy.getResourceInvoked);
+ }
+
+ public void testGetResourcesInvoked() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ classLoader.getResources("test");
+ assertEquals("getResources() should have been invoked", "test", policy.getResourcesInvoked);
+ }
+
+ public void testIsJDKRequest() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ // Hide java
+ ParentPolicy parentPolicy = new ParentPolicy(ClassFilter.NOTHING, ClassFilter.NOTHING);
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test", parentPolicy);
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(domain, policy);
+
+ // Can't see java
+ assertLoadClassFail(Object.class, classLoader);
+
+ // Should be able to load javax.naming.Context
+ assertLoadClass(Context.class, classLoader, null, true);
+ }
+
+ public void testNotImportAll() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ MockClassLoaderPolicy mock = createMockClassLoaderPolicy();
+ mock.setPathsAndPackageNames(ClassLoaderDomain.class);
+ system.registerClassLoaderPolicy(mock);
+
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ assertLoadClassFail(ClassLoaderDomain.class, classLoader);
+ assertNotNull("Should have tried to load through us ", policy.getResourceInvoked);
+ }
+
+ public void testImportAll() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ MockClassLoaderPolicy mock = createMockClassLoaderPolicy();
+ mock.setPathsAndPackageNames(ClassLoaderDomain.class);
+ ClassLoader mockClassLoader = system.registerClassLoaderPolicy(mock);
+
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ policy.isImportAll = true;
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ assertLoadClass(ClassLoaderDomain.class, classLoader, mockClassLoader);
+ assertNull("Should NOT have tried to load through us ", policy.getResourceInvoked);
+ }
+
+ public void testPackageIndexed() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ MockClassLoaderPolicy mock = createMockClassLoaderPolicy();
+ mock.setImportAll(true);
+ ClassLoader mockClassLoader = system.registerClassLoaderPolicy(mock);
+
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ policy.packageNames = new String[] { ClassLoaderUtils.getClassPackageName(ClassLoaderDomain.class.getName())};
+ system.registerClassLoaderPolicy(policy);
+
+ assertLoadClassFail(ClassLoaderDomain.class, mockClassLoader);
+ assertNotNull("Should have tried to load through us ", policy.getResourceInvoked);
+ }
+
+ public void testPackageNotIndexed() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ MockClassLoaderPolicy mock = createMockClassLoaderPolicy();
+ mock.setImportAll(true);
+ ClassLoader mockClassLoader = system.registerClassLoaderPolicy(mock);
+
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ system.registerClassLoaderPolicy(policy);
+
+ assertLoadClassFail(ClassLoaderDomain.class, mockClassLoader);
+ assertNull("Should NOT have tried to load through us ", policy.getResourceInvoked);
+ }
+
+ public void testDelegateInvoked() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ TestClassLoaderPolicy policy1 = new TestClassLoaderPolicy();
+ TestClassLoaderPolicy policy2 = new TestClassLoaderPolicy();
+ policy1.delegates = Collections.singletonList(new FilteredDelegateLoader(policy2));
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy1);
+ system.registerClassLoaderPolicy(policy2);
+
+ assertLoadClassFail(ClassLoaderDomain.class, classLoader);
+ assertNotNull("Should have tried policy1 ", policy1.getResourceInvoked);
+ assertNotNull("Should have tried policy2 ", policy2.getResourceInvoked);
+ }
+
+ public void testDelegateInvokedSuccessfully() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ TestClassLoaderPolicy policy1 = new TestClassLoaderPolicy();
+ TestClassLoaderPolicy policy2 = new TestClassLoaderPolicy();
+ policy1.delegates = Collections.singletonList(new FilteredDelegateLoader(policy2));
+ ClassLoader classLoader1 = system.registerClassLoaderPolicy(policy1);
+ ClassLoader classLoader2 = system.registerClassLoaderPolicy(policy2);
+
+ assertLoadClass(TestClass.class, classLoader1, classLoader2);
+ assertNull("Should NOT have tried policy1 ", policy1.getResourceInvoked);
+ assertNotNull("Should have tried policy2 ", policy2.getResourceInvoked);
+ }
+
+ public void testNonDelegateNotInvoked() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ TestClassLoaderPolicy policy1 = new TestClassLoaderPolicy();
+ TestClassLoaderPolicy policy2 = new TestClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy1);
+ system.registerClassLoaderPolicy(policy2);
+
+ assertLoadClassFail(ClassLoaderDomain.class, classLoader);
+ assertNotNull("Should have tried policy1 ", policy1.getResourceInvoked);
+ assertNull("Should NOT have tried policy2 ", policy2.getResourceInvoked);
+ }
+
+ public void testExportLoaderInvoked() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystemWithModifiedBootstrap();
+
+ MockClassLoaderPolicy mock = createMockClassLoaderPolicy();
+ mock.setImportAll(true);
+ ClassLoader mockClassLoader = system.registerClassLoaderPolicy(mock);
+
+ TestClassLoaderPolicy policy = new TestClassLoaderPolicy();
+ policy.packageNames = new String[] { ClassLoaderUtils.getClassPackageName(ClassLoaderDomain.class.getName())};
+ TestDelegateLoader exported = new TestDelegateLoader(policy);
+ policy.exported = exported;
+ system.registerClassLoaderPolicy(policy);
+
+ assertLoadClassFail(ClassLoaderDomain.class, mockClassLoader);
+ assertNotNull("Should have tried to the export delegate ", exported.getResourceInvoked);
+ assertNotNull("Should have tried to the export delegate to load the class ", exported.loadClassInvoked);
+ assertNull("Should NOT have tried to load through us ", policy.getResourceInvoked);
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/ResourceTestSuite.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/ResourceTestSuite.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/ResourceTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.resources;
+
+import org.jboss.test.classloader.resources.tests.ResourceUnitTestCase;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Resource Test Suite.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class ResourceTestSuite extends TestSuite
+{
+ /**
+ * For running the testsuite from the command line
+ *
+ * @param args the command line args
+ */
+ public static void main(String[] args)
+ {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create the testsuite
+ *
+ * @return the testsuite
+ */
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("Resource Tests");
+
+ suite.addTest(ResourceUnitTestCase.suite());
+
+ return suite;
+ }
+}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/tests/ResourceUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/tests/ResourceUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/resources/tests/ResourceUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,528 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.resources.tests;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloader.spi.filter.FilteredDelegateLoader;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+
+/**
+ * ClassLoaderSystemUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ResourceUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(ResourceUnitTestCase.class);
+ }
+
+ public ResourceUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testGetResource() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ assertGetResource("a/", "com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourceFromDefaultPackage() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ assertGetResource("a/", "testResource", classLoader);
+ }
+
+ public void testGetResourceFromDelegate() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setDelegates(Collections.singletonList(new FilteredDelegateLoader(policy)));
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResource("a/", "com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourceFromDefaultPackageFromDelegate() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setDelegates(Collections.singletonList(new FilteredDelegateLoader(policy)));
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResource("a/", "testResource", classLoader);
+ }
+
+ public void testGetResourceUsingAllImports() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setImportAll(true);
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResource("a/", "com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourceFromDefaultPackageUsingAllImports() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" } );
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setImportAll(true);
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResource("a/", "testResource", classLoader);
+ }
+
+ public void testGetResourceNotFound() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResourceFail("com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourceFromParentBefore() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+ system.registerClassLoaderPolicy(parent, policy);
+
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("com/acme/p1");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+
+ assertGetResource("a/", "com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourceFromParentAfter() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+ system.registerClassLoaderPolicy(parent, policy);
+
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.AFTER, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+
+ assertGetResource("a/", "com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourceFromParentAfterNotReached() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+ system.registerClassLoaderPolicy(parent, policy);
+
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.AFTER, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("com/acme/p1");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+
+ assertGetResource("b/", "com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourceFromDefaultPackageFromParent() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" } );
+ system.registerClassLoaderPolicy(parent, policy);
+
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("com/acme/p1");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+
+ assertGetResource("a/", "testResource", classLoader);
+ }
+
+ public void testGetResources() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ assertGetResources("com/acme/p1/testResource", classLoader, "a/");
+ }
+
+ public void testGetResourcesFromDefaultPackage() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+
+ assertGetResources("testResource", classLoader, "a/");
+ }
+
+ public void testGetResourcesMultiple() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setImportAll(true);
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("com/acme/p1");
+ policy2.setPackageNames(new String[] { "com.acme.p1" });
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+ system.registerClassLoaderPolicy(policy2);
+
+ assertGetResources("com/acme/p1/testResource", classLoader, "a/", "b/");
+ }
+
+ public void testGetResourcesFromDefaultPackageMultiple() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setImportAll(true);
+ policy.setPrefix("a/");
+ policy.setPath("");
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("");
+ policy2.setPackageNames(new String[] { "" });
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy);
+ system.registerClassLoaderPolicy(policy2);
+
+ assertGetResources("testResource", classLoader, "a/", "b/");
+ }
+
+ public void testGetResourcesFromDelegate() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setDelegates(Collections.singletonList(new FilteredDelegateLoader(policy)));
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResources("com/acme/p1/testResource", classLoader, "a/");
+ }
+
+ public void testGetResourcesFromDefaultPackageFromDelegate() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setDelegates(Collections.singletonList(new FilteredDelegateLoader(policy)));
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResources("testResource", classLoader, "a/");
+ }
+
+ public void testGetResourcesUsingAllImports() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setImportAll(true);
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResources("com/acme/p1/testResource", classLoader, "a/");
+ }
+
+ public void testGetResourcesFromDefaultPackageUsingAllImports() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" } );
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setImportAll(true);
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResources("testResource", classLoader, "a/");
+ }
+
+ public void testGetResourcesNotFound() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+
+ system.registerClassLoaderPolicy(policy);
+ ClassLoader classLoader = system.registerClassLoaderPolicy(policy2);
+
+ assertGetResourcesFail("com/acme/p1/testResource", classLoader);
+ }
+
+ public void testGetResourcesFromParentBefore() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+ system.registerClassLoaderPolicy(parent, policy);
+
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("com/acme/p1");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+
+ assertGetResources("com/acme/p1/testResource", classLoader, "a/", "b/");
+ }
+
+ public void testGetResourcesFromParentAfter() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("com/acme/p1");
+ policy.setPackageNames(new String[] { "com.acme.p1" } );
+ system.registerClassLoaderPolicy(parent, policy);
+
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.AFTER, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("com/acme/p1");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+
+ assertGetResources("com/acme/p1/testResource", classLoader, "a/", "b/");
+ }
+
+ public void testGetResourcesFromDefaultPackageFromParent() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain parent = system.createAndRegisterDomain("parent");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ policy.setPrefix("a/");
+ policy.setPath("");
+ policy.setPackageNames(new String[] { "" } );
+ system.registerClassLoaderPolicy(parent, policy);
+
+ ClassLoaderDomain child = system.createAndRegisterDomain("child", ParentPolicy.BEFORE, parent);
+ MockClassLoaderPolicy policy2 = createMockClassLoaderPolicy();
+ policy2.setPrefix("b/");
+ policy2.setPath("");
+ ClassLoader classLoader = system.registerClassLoaderPolicy(child, policy2);
+
+ assertGetResources("testResource", classLoader, "a/", "b/");
+ }
+
+ protected URL assertGetResource(String prefix, String resourceName, ClassLoader classLoader) throws Exception
+ {
+ URL url = classLoader.getResource(resourceName);
+ assertNotNull("Should have got resource prefix=" + prefix + " resourceName " + resourceName + " from " + classLoader, url);
+ SecurityManager sm = suspendSecurity();
+ try
+ {
+ InputStream is = url.openStream();
+ try
+ {
+ InputStreamReader reader = new InputStreamReader(is);
+ char[] chars = new char[1000];
+ int count = 0;
+ int read = reader.read(chars);
+ while (read != -1)
+ {
+ count += read;
+ read = reader.read(chars, read, 1000 - read);
+ }
+ String string = new String(chars, 0, count);
+ assertEquals("Should have read the correct resource", prefix + resourceName, string);
+ }
+ finally
+ {
+ is.close();
+ }
+ }
+ finally
+ {
+ resumeSecurity(sm);
+ }
+ return url;
+ }
+
+ protected void assertGetResourceFail(String resourceName, ClassLoader classLoader) throws Exception
+ {
+ URL url = classLoader.getResource(resourceName);
+ assertNull("Should NOT have got resource " + resourceName + " from " + classLoader, url);
+ }
+
+ protected Enumeration<URL> assertGetResources(String resourceName, ClassLoader classLoader, String... prefixes) throws Exception
+ {
+ Enumeration<URL> urls = classLoader.getResources(resourceName);
+
+ HashSet<String> foundResources = new HashSet<String>();
+ SecurityManager sm = suspendSecurity();
+ try
+ {
+ while (urls.hasMoreElements())
+ {
+ URL url = urls.nextElement();
+ InputStream is = url.openStream();
+ try
+ {
+ InputStreamReader reader = new InputStreamReader(is);
+ char[] chars = new char[1000];
+ int count = 0;
+ int read = reader.read(chars);
+ while (read != -1)
+ {
+ count += read;
+ read = reader.read(chars, read, 1000 - read);
+ }
+ String string = new String(chars, 0, count);
+ foundResources.add(string);
+ }
+ finally
+ {
+ is.close();
+ }
+ }
+ }
+ finally
+ {
+ resumeSecurity(sm);
+ }
+
+ HashSet<String> expectedResources = new HashSet<String>();
+ for (String prefix : prefixes)
+ expectedResources.add(prefix + resourceName);
+
+ assertEquals(expectedResources, foundResources);
+
+ return urls;
+ }
+
+ protected void assertGetResourcesFail(String resourceName, ClassLoader classLoader) throws Exception
+ {
+ Enumeration<URL> urls = classLoader.getResources(resourceName);
+ if (urls.hasMoreElements())
+ {
+ HashSet<URL> found = new HashSet<URL>();
+ while (urls.hasMoreElements())
+ found.add(urls.nextElement());
+ fail("Should NOT have got resources " + resourceName + " from " + classLoader + " found " + urls);
+ }
+ }
+}
Copied: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoaderSystemTestSuite.java (from rev 62723, projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoadingSystemTestSuite.java)
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoaderSystemTestSuite.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoaderSystemTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.system;
+
+import org.jboss.test.classloader.system.test.ClassLoaderSystemNoSecurityUnitTestCase;
+import org.jboss.test.classloader.system.test.ClassLoaderSystemUnitTestCase;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * ClassLoaderSystem Test Suite.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class ClassLoaderSystemTestSuite extends TestSuite
+{
+ /**
+ * For running the testsuite from the command line
+ *
+ * @param args the command line args
+ */
+ public static void main(String[] args)
+ {
+ TestRunner.run(suite());
+ }
+
+ /**
+ * Create the testsuite
+ *
+ * @return the testsuite
+ */
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("ClassLoader System Tests");
+
+ suite.addTest(ClassLoaderSystemNoSecurityUnitTestCase.suite());
+ suite.addTest(ClassLoaderSystemUnitTestCase.suite());
+
+ return suite;
+ }
+}
Deleted: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoadingSystemTestSuite.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoadingSystemTestSuite.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/ClassLoadingSystemTestSuite.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -1,63 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2007, 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.classloader.system;
-
-import org.jboss.test.classloader.system.test.ClassLoadingSystemNoSecurityUnitTestCase;
-import org.jboss.test.classloader.system.test.ClassLoadingSystemUnitTestCase;
-
-import junit.framework.Test;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
-
-/**
- * ClassLoadingSystem Test Suite.
- *
- * @author <a href="adrian at jboss.com">Adrian Brock</a>
- * @version $Revision: 37459 $
- */
-public class ClassLoadingSystemTestSuite extends TestSuite
-{
- /**
- * For running the testsuite from the command line
- *
- * @param args the command line args
- */
- public static void main(String[] args)
- {
- TestRunner.run(suite());
- }
-
- /**
- * Create the testsuite
- *
- * @return the testsuite
- */
- public static Test suite()
- {
- TestSuite suite = new TestSuite("ClassLoading System Tests");
-
- suite.addTest(ClassLoadingSystemNoSecurityUnitTestCase.suite());
- suite.addTest(ClassLoadingSystemUnitTestCase.suite());
-
- return suite;
- }
-}
Modified: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/MockClassLoaderDomain.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/MockClassLoaderDomain.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/MockClassLoaderDomain.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -37,6 +37,7 @@
{
public List<ClassLoader> added = new CopyOnWriteArrayList<ClassLoader>();
public List<ClassLoader> removed = new CopyOnWriteArrayList<ClassLoader>();
+ public boolean shutdown = false;
public MockClassLoaderDomain()
{
@@ -57,4 +58,10 @@
{
removed.add(classLoader);
}
+
+ protected void shutdownDomain()
+ {
+ shutdown = true;
+ super.shutdownDomain();
+ }
}
Added: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/SimpleMockClassLoaderPolicy.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/SimpleMockClassLoaderPolicy.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/support/SimpleMockClassLoaderPolicy.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.system.support;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Set;
+
+import org.jboss.classloader.spi.ClassLoaderPolicy;
+
+/**
+ * SimpleMockClassLoaderPolicy.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class SimpleMockClassLoaderPolicy extends ClassLoaderPolicy
+{
+ public boolean shutdown = false;
+
+ public URL getResource(String path)
+ {
+ return null;
+ }
+
+ public void getResources(String name, Set<URL> urls) throws IOException
+ {
+ }
+
+ protected synchronized void shutdownPolicy()
+ {
+ shutdown = true;
+ super.shutdownPolicy();
+ }
+}
Copied: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemNoSecurityUnitTestCase.java (from rev 62723, projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemNoSecurityUnitTestCase.java)
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemNoSecurityUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemNoSecurityUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,130 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.system.test;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.test.classloader.AbstractClassLoaderTest;
+import org.jboss.test.classloader.system.support.MockClassLoaderDomain;
+import org.jboss.test.classloader.system.support.MockClassLoaderSystem;
+import org.jboss.test.classloader.system.support.SimpleMockClassLoaderPolicy;
+
+/**
+ * ClassLoaderSystemUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassLoaderSystemNoSecurityUnitTestCase extends AbstractClassLoaderTest
+{
+ public static Test suite()
+ {
+ return suite(ClassLoaderSystemNoSecurityUnitTestCase.class);
+ }
+
+ public ClassLoaderSystemNoSecurityUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testGetInstance() throws Exception
+ {
+ ClassLoaderSystem instance = ClassLoaderSystem.getInstance();
+ assertNotNull(instance);
+
+ ClassLoaderSystem instance2 = ClassLoaderSystem.getInstance();
+ assertTrue("Should be the same instance", instance == instance2);
+ }
+
+ public void testClassLoaderPolicyShutdownFromClassLoaderSystemShutdown() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ SimpleMockClassLoaderPolicy policy = new SimpleMockClassLoaderPolicy();
+ MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
+ system.registerClassLoaderPolicy(domain, policy);
+
+ assertFalse(policy.shutdown);
+ system.shutdown();
+ assertTrue("Policy should be shutdown", policy.shutdown);
+ }
+
+ public void testClassLoaderPolicyShutdownFromClassLoaderDomainUnregister() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ SimpleMockClassLoaderPolicy policy = new SimpleMockClassLoaderPolicy();
+ MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
+ system.registerClassLoaderPolicy(domain, policy);
+
+ assertFalse(policy.shutdown);
+ system.unregisterDomain(domain);
+ assertTrue("Policy should be shutdown", policy.shutdown);
+ }
+
+ public void testClassLoaderPolicyShutdownFromClassLoaderPolicyUnregister() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ SimpleMockClassLoaderPolicy policy = new SimpleMockClassLoaderPolicy();
+ MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
+ system.registerClassLoaderPolicy(domain, policy);
+
+ assertFalse(policy.shutdown);
+ system.unregisterClassLoaderPolicy(policy);
+ assertTrue("Policy should be shutdown", policy.shutdown);
+ }
+
+ public void testClassLoaderPolicyShutdownFromClassLoaderUnregister() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ SimpleMockClassLoaderPolicy policy = new SimpleMockClassLoaderPolicy();
+ MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
+ ClassLoader cl = system.registerClassLoaderPolicy(domain, policy);
+
+ assertFalse(policy.shutdown);
+ system.unregisterClassLoader(cl);
+ assertTrue("Policy should be shutdown", policy.shutdown);
+ }
+
+ public void testUnregisterClassLoaderNotRegistered() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ ClassLoader cl = new URLClassLoader(new URL[0]);
+
+ try
+ {
+ system.unregisterClassLoader(cl);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ protected MockClassLoaderSystem createMockClassLoaderSystem()
+ {
+ return new MockClassLoaderSystem();
+ }
+}
Copied: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemUnitTestCase.java (from rev 62723, projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemUnitTestCase.java)
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemUnitTestCase.java (rev 0)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoaderSystemUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -0,0 +1,618 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, 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.classloader.system.test;
+
+import java.security.AccessControlException;
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.test.support.MockClassLoaderHelper;
+import org.jboss.classloader.test.support.MockClassLoaderPolicy;
+import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
+import org.jboss.test.classloader.system.support.MockClassLoaderDomain;
+import org.jboss.test.classloader.system.support.MockClassLoaderSystem;
+
+/**
+ * ClassLoaderSystemUnitTestCase.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassLoaderSystemUnitTestCase extends AbstractClassLoaderTestWithSecurity
+{
+ public static Test suite()
+ {
+ return suite(ClassLoaderSystemUnitTestCase.class);
+ }
+
+ public ClassLoaderSystemUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testGetInstance() throws Exception
+ {
+ try
+ {
+ ClassLoaderSystem.getInstance();
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(AccessControlException.class, e);
+ }
+ }
+
+ public void testDefaultDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ assertTrue(system.isRegistered(ClassLoaderSystem.DEFAULT_DOMAIN_NAME));
+ ClassLoaderDomain domain = system.getDefaultDomain();
+ assertNotNull(domain);
+ assertEquals(ClassLoaderSystem.DEFAULT_DOMAIN_NAME, domain.getName());
+ assertTrue(system.isRegistered(ClassLoaderSystem.DEFAULT_DOMAIN_NAME));
+ assertTrue(system.isDomainRegistered(domain));
+ }
+
+ public void testRegisterDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ assertFalse(system.isRegistered("mock"));
+ MockClassLoaderDomain domain = new MockClassLoaderDomain("mock");
+ assertFalse(system.isDomainRegistered(domain));
+
+ system.registerDomain(domain);
+ assertTrue(system.isRegistered("mock"));
+ assertTrue(system.isDomainRegistered(domain));
+ }
+
+ public void testRegisterDomains() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ assertFalse(system.isRegistered("a"));
+ MockClassLoaderDomain a = new MockClassLoaderDomain("a");
+ assertFalse(system.isDomainRegistered(a));
+
+ assertFalse(system.isRegistered("b"));
+ MockClassLoaderDomain b = new MockClassLoaderDomain("b");
+ assertFalse(system.isDomainRegistered(a));
+
+ system.registerDomain(a);
+ system.registerDomain(b);
+
+ assertTrue(system.isRegistered("a"));
+ assertTrue(system.isDomainRegistered(a));
+ assertTrue(system.isRegistered("b"));
+ assertTrue(system.isDomainRegistered(b));
+ }
+
+ public void testRegisterNullDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ try
+ {
+ system.registerDomain(null);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ }
+
+ public void testRegisterDefaultDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ MockClassLoaderDomain domain = new MockClassLoaderDomain(ClassLoaderSystem.DEFAULT_DOMAIN_NAME);
+ try
+ {
+ system.registerDomain(domain);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testRegisterDuplicateDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ assertFalse(system.isRegistered("a"));
+ MockClassLoaderDomain a = new MockClassLoaderDomain("a");
+ assertFalse(system.isDomainRegistered(a));
+
+ system.registerDomain(a);
+
+ assertTrue(system.isRegistered("a"));
+ assertTrue(system.isDomainRegistered(a));
+
+ try
+ {
+ system.registerDomain(a);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testRegisterDuplicateDomainName() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ assertFalse(system.isRegistered("a"));
+ MockClassLoaderDomain a = new MockClassLoaderDomain("a");
+ assertFalse(system.isDomainRegistered(a));
+
+ MockClassLoaderDomain b = new MockClassLoaderDomain("a");
+
+ system.registerDomain(a);
+
+ assertTrue(system.isRegistered("a"));
+ assertTrue(system.isDomainRegistered(a));
+
+ try
+ {
+ system.registerDomain(b);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testUnregisterDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ MockClassLoaderDomain domain = new MockClassLoaderDomain("mock");
+
+ system.registerDomain(domain);
+ assertTrue(system.isRegistered("mock"));
+ assertTrue(system.isDomainRegistered(domain));
+
+ system.unregisterDomain(domain);
+ assertFalse(system.isRegistered("mock"));
+ assertFalse(system.isDomainRegistered(domain));
+ }
+
+ public void testUnregisterWrongDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ MockClassLoaderDomain domain = new MockClassLoaderDomain("mock");
+
+ system.registerDomain(domain);
+ assertTrue(system.isRegistered("mock"));
+ assertTrue(system.isDomainRegistered(domain));
+
+ MockClassLoaderDomain notDomain = new MockClassLoaderDomain("mock");
+
+ try
+ {
+ system.unregisterDomain(notDomain);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ assertTrue(system.isRegistered("mock"));
+ assertTrue(system.isDomainRegistered(domain));
+ }
+
+ public void testUnregisterDefaultDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ ClassLoaderDomain domain = system.getDefaultDomain();
+ try
+ {
+ system.unregisterDomain(domain);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ assertTrue(system.isRegistered(ClassLoaderSystem.DEFAULT_DOMAIN_NAME));
+ assertTrue(system.isDomainRegistered(domain));
+ }
+
+ public void testUnregisterNullDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+
+ try
+ {
+ system.unregisterDomain(null);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ }
+
+ public void testUnregisterUnregisteredDomain() throws Exception
+ {
+ ClassLoaderSystem system = createClassLoaderSystem();
+ ClassLoaderDomain domain = system.createAndRegisterDomain("test");
+ system.unregisterDomain(domain);
+ try
+ {
+ system.unregisterDomain(domain);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testDomainShutdownFromClassLoaderSystemShutdown() throws Exception
+ {
+ ClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = new MockClassLoaderDomain();
+ system.registerDomain(domain);
+
+ assertFalse(domain.shutdown);
+ system.shutdown();
+ assertTrue("Domain should be shutdown", domain.shutdown);
+ }
+
+ public void testDomainShutdownFromUnregister() throws Exception
+ {
+ ClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = new MockClassLoaderDomain();
+ system.registerDomain(domain);
+
+ assertFalse(domain.shutdown);
+ system.unregisterDomain(domain);
+ assertTrue("Domain should be shutdown", domain.shutdown);
+ }
+
+ public void testRegisterClassLoaderDefaultDomain() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = (MockClassLoaderDomain) system.getDefaultDomain();
+ ClassLoader cl1 = createAndRegisterMockClassLoader(system);
+
+ List<ClassLoader> expected = new ArrayList<ClassLoader>();
+ expected.add(cl1);
+
+ assertEquals(expected, domain.added);
+ assertEmpty(domain.removed);
+
+ ClassLoader cl2 = createAndRegisterMockClassLoader(system, domain);
+ expected.add(cl2);
+
+ assertEquals(expected, domain.added);
+ assertEmpty(domain.removed);
+ }
+
+ public void testRegisterClassLoaderExplicitDomain() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
+ ClassLoader cl1 = createAndRegisterMockClassLoader(system, domain);
+
+ List<ClassLoader> expected = new ArrayList<ClassLoader>();
+ expected.add(cl1);
+
+ assertEquals(expected, domain.added);
+ assertEmpty(domain.removed);
+
+ ClassLoader cl2 = createAndRegisterMockClassLoader(system, domain);
+ expected.add(cl2);
+
+ assertEquals(expected, domain.added);
+ assertEmpty(domain.removed);
+ }
+
+ public void testRegisterNullClassLoaderPolicyDefaultDomain()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ try
+ {
+ system.registerClassLoaderPolicy(null);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ }
+
+ public void testRegisterNullClassLoaderPolicyExplicitDomain()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = new MockClassLoaderDomain();
+ system.registerDomain(domain);
+ try
+ {
+ system.registerClassLoaderPolicy(domain, null);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ }
+
+ public void testRegisterClassLoaderPolicyNullDomain()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ try
+ {
+ system.registerClassLoaderPolicy(null, policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ }
+
+ public void testRegisterClassLoaderPolicyWithNotRegisteredDomain()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = new MockClassLoaderDomain();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ try
+ {
+ system.registerClassLoaderPolicy(domain, policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testRegisterClassLoaderPolicyWithUnregisteredDomain()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = new MockClassLoaderDomain();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ system.registerDomain(domain);
+ system.unregisterDomain(domain);
+ try
+ {
+ system.registerClassLoaderPolicy(domain, policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testRegisterClassLoaderPolicyTwiceDefaultDomain()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ system.registerClassLoaderPolicy(policy);
+ try
+ {
+ system.registerClassLoaderPolicy(policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testRegisterClassLoaderPolicyTwiceExplicitDomain()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = new MockClassLoaderDomain();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ system.registerDomain(domain);
+ system.registerClassLoaderPolicy(domain, policy);
+ try
+ {
+ system.registerClassLoaderPolicy(domain, policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testRegisterClassLoaderPolicyTwiceDifferentDomains()
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain1 = new MockClassLoaderDomain("domain1");
+ MockClassLoaderDomain domain2 = new MockClassLoaderDomain("domain2");
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ system.registerDomain(domain1);
+ system.registerDomain(domain2);
+ system.registerClassLoaderPolicy(domain1, policy);
+ try
+ {
+ system.registerClassLoaderPolicy(domain2, policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testUnregisterClassLoaderDefaultDomain() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = (MockClassLoaderDomain) system.getDefaultDomain();
+ ClassLoader cl1 = createAndRegisterMockClassLoader(system);
+
+ List<ClassLoader> expected = new ArrayList<ClassLoader>();
+ expected.add(cl1);
+
+ assertEquals(expected, domain.added);
+ assertEmpty(domain.removed);
+
+ system.unregisterClassLoader(cl1);
+
+ assertEquals(expected, domain.added);
+ assertEquals(expected, domain.removed);
+ }
+
+ public void testUnregisterClassLoaderExplicitDomain() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
+ ClassLoader cl1 = createAndRegisterMockClassLoader(system, domain);
+
+ List<ClassLoader> expected = new ArrayList<ClassLoader>();
+ expected.add(cl1);
+
+ assertEquals(expected, domain.added);
+ assertEmpty(domain.removed);
+
+ system.unregisterClassLoader(cl1);
+
+ assertEquals(expected, domain.added);
+ assertEquals(expected, domain.removed);
+ }
+
+ public void testUnregisterClassLoaderPolicyExplicitDomain() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
+ ClassLoader cl1 = MockClassLoaderHelper.registerMockClassLoader(system, domain, policy);
+
+ List<ClassLoader> expected = new ArrayList<ClassLoader>();
+ expected.add(cl1);
+
+ assertEquals(expected, domain.added);
+ assertEmpty(domain.removed);
+
+ system.unregisterClassLoaderPolicy(policy);
+
+ assertEquals(expected, domain.added);
+ assertEquals(expected, domain.removed);
+ }
+
+ public void testUnregisterNullClassLoader() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+
+ try
+ {
+ system.unregisterClassLoader(null);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ }
+
+ public void testUnregisterNullClassLoaderPolicy() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+
+ try
+ {
+ system.unregisterClassLoaderPolicy(null);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalArgumentException.class, e);
+ }
+ }
+
+ public void testUnregisterClassLoaderUnregistered() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ ClassLoader cl = system.registerClassLoaderPolicy(policy);
+ system.unregisterClassLoaderPolicy(policy);
+
+ try
+ {
+ system.unregisterClassLoader(cl);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testUnregisterClassLoaderPolicyNotRegistered() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+
+ try
+ {
+ system.unregisterClassLoaderPolicy(policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ public void testUnregisterClassLoaderPolicyUnregistered() throws Exception
+ {
+ MockClassLoaderSystem system = createMockClassLoaderSystem();
+ MockClassLoaderPolicy policy = createMockClassLoaderPolicy();
+ system.registerClassLoaderPolicy(policy);
+ system.unregisterClassLoaderPolicy(policy);
+
+ try
+ {
+ system.unregisterClassLoaderPolicy(policy);
+ fail("Should not be here!");
+ }
+ catch (Exception e)
+ {
+ checkThrowable(IllegalStateException.class, e);
+ }
+ }
+
+ protected MockClassLoaderSystem createMockClassLoaderSystem()
+ {
+ return new MockClassLoaderSystem();
+ }
+}
Deleted: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemNoSecurityUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemNoSecurityUnitTestCase.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemNoSecurityUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -1,55 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2007, 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.classloader.system.test;
-
-import junit.framework.Test;
-
-import org.jboss.classloader.spi.ClassLoaderSystem;
-import org.jboss.test.classloader.AbstractClassLoaderTest;
-
-/**
- * ClassLoadingSystemUnitTestCase.
- *
- * @author <a href="adrian at jboss.com">Adrian Brock</a>
- * @version $Revision: 1.1 $
- */
-public class ClassLoadingSystemNoSecurityUnitTestCase extends AbstractClassLoaderTest
-{
- public static Test suite()
- {
- return suite(ClassLoadingSystemNoSecurityUnitTestCase.class);
- }
-
- public ClassLoadingSystemNoSecurityUnitTestCase(String name)
- {
- super(name);
- }
-
- public void testGetInstance() throws Exception
- {
- ClassLoaderSystem instance = ClassLoaderSystem.getInstance();
- assertNotNull(instance);
-
- ClassLoaderSystem instance2 = ClassLoaderSystem.getInstance();
- assertTrue("Should be the same instance", instance == instance2);
- }
-}
Deleted: projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemUnitTestCase.java 2007-05-03 19:34:51 UTC (rev 62791)
+++ projects/microcontainer/trunk/classloader/src/tests/org/jboss/test/classloader/system/test/ClassLoadingSystemUnitTestCase.java 2007-05-03 19:38:31 UTC (rev 62792)
@@ -1,307 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2007, 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.classloader.system.test;
-
-import java.security.AccessControlException;
-import java.util.ArrayList;
-import java.util.List;
-
-import junit.framework.Test;
-
-import org.jboss.classloader.spi.ClassLoaderDomain;
-import org.jboss.classloader.spi.ClassLoaderSystem;
-import org.jboss.test.classloader.AbstractClassLoaderTestWithSecurity;
-import org.jboss.test.classloader.system.support.MockClassLoaderDomain;
-import org.jboss.test.classloader.system.support.MockClassLoaderSystem;
-
-/**
- * ClassLoadingSystemUnitTestCase.
- *
- * @author <a href="adrian at jboss.com">Adrian Brock</a>
- * @version $Revision: 1.1 $
- */
-public class ClassLoadingSystemUnitTestCase extends AbstractClassLoaderTestWithSecurity
-{
- public static Test suite()
- {
- return suite(ClassLoadingSystemUnitTestCase.class);
- }
-
- public ClassLoadingSystemUnitTestCase(String name)
- {
- super(name);
- }
-
- public void testGetInstance() throws Exception
- {
- try
- {
- ClassLoaderSystem.getInstance();
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(AccessControlException.class, e);
- }
- }
-
- public void testDefaultDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
- assertTrue(system.isRegistered(ClassLoaderSystem.DEFAULT_DOMAIN_NAME));
- ClassLoaderDomain domain = system.getDefaultDomain();
- assertNotNull(domain);
- assertEquals(ClassLoaderSystem.DEFAULT_DOMAIN_NAME, domain.getName());
- assertTrue(system.isRegistered(ClassLoaderSystem.DEFAULT_DOMAIN_NAME));
- assertTrue(system.isDomainRegistered(domain));
- }
-
- public void testRegisterDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- assertFalse(system.isRegistered("mock"));
- MockClassLoaderDomain domain = new MockClassLoaderDomain("mock");
- assertFalse(system.isDomainRegistered(domain));
-
- system.registerDomain(domain);
- assertTrue(system.isRegistered("mock"));
- assertTrue(system.isDomainRegistered(domain));
- }
-
- public void testRegisterDomains() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- assertFalse(system.isRegistered("a"));
- MockClassLoaderDomain a = new MockClassLoaderDomain("a");
- assertFalse(system.isDomainRegistered(a));
-
- assertFalse(system.isRegistered("b"));
- MockClassLoaderDomain b = new MockClassLoaderDomain("b");
- assertFalse(system.isDomainRegistered(a));
-
- system.registerDomain(a);
- system.registerDomain(b);
-
- assertTrue(system.isRegistered("a"));
- assertTrue(system.isDomainRegistered(a));
- assertTrue(system.isRegistered("b"));
- assertTrue(system.isDomainRegistered(b));
- }
-
- public void testRegisterNullDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- try
- {
- system.registerDomain(null);
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(IllegalArgumentException.class, e);
- }
- }
-
- public void testRegisterDefaultDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- MockClassLoaderDomain domain = new MockClassLoaderDomain(ClassLoaderSystem.DEFAULT_DOMAIN_NAME);
- try
- {
- system.registerDomain(domain);
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(IllegalStateException.class, e);
- }
- }
-
- public void testRegisterDuplicateDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- assertFalse(system.isRegistered("a"));
- MockClassLoaderDomain a = new MockClassLoaderDomain("a");
- assertFalse(system.isDomainRegistered(a));
-
- system.registerDomain(a);
-
- assertTrue(system.isRegistered("a"));
- assertTrue(system.isDomainRegistered(a));
-
- try
- {
- system.registerDomain(a);
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(IllegalStateException.class, e);
- }
- }
-
- public void testRegisterDuplicateDomainName() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- assertFalse(system.isRegistered("a"));
- MockClassLoaderDomain a = new MockClassLoaderDomain("a");
- assertFalse(system.isDomainRegistered(a));
-
- MockClassLoaderDomain b = new MockClassLoaderDomain("a");
-
- system.registerDomain(a);
-
- assertTrue(system.isRegistered("a"));
- assertTrue(system.isDomainRegistered(a));
-
- try
- {
- system.registerDomain(b);
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(IllegalStateException.class, e);
- }
- }
-
- public void testUnregisterDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- MockClassLoaderDomain domain = new MockClassLoaderDomain("mock");
-
- system.registerDomain(domain);
- assertTrue(system.isRegistered("mock"));
- assertTrue(system.isDomainRegistered(domain));
-
- system.unregisterDomain(domain);
- assertFalse(system.isRegistered("mock"));
- assertFalse(system.isDomainRegistered(domain));
- }
-
- public void testUnregisterWrongDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- MockClassLoaderDomain domain = new MockClassLoaderDomain("mock");
-
- system.registerDomain(domain);
- assertTrue(system.isRegistered("mock"));
- assertTrue(system.isDomainRegistered(domain));
-
- MockClassLoaderDomain notDomain = new MockClassLoaderDomain("mock");
-
- try
- {
- system.unregisterDomain(notDomain);
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(IllegalStateException.class, e);
- }
- assertTrue(system.isRegistered("mock"));
- assertTrue(system.isDomainRegistered(domain));
- }
-
- public void testUnregisterDefaultDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- ClassLoaderDomain domain = system.getDefaultDomain();
- try
- {
- system.unregisterDomain(domain);
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(IllegalArgumentException.class, e);
- }
- assertTrue(system.isRegistered(ClassLoaderSystem.DEFAULT_DOMAIN_NAME));
- assertTrue(system.isDomainRegistered(domain));
- }
-
- public void testUnregisterNullDomain() throws Exception
- {
- ClassLoaderSystem system = createClassLoaderSystem();
-
- try
- {
- system.unregisterDomain(null);
- fail("Should not be here!");
- }
- catch (Exception e)
- {
- checkThrowable(IllegalArgumentException.class, e);
- }
- }
-
- public void testRegisterClassLoaderExplicitDomain() throws Exception
- {
- MockClassLoaderSystem system = createMockClassLoaderSystem();
- MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
- ClassLoader cl1 = createAndRegisterMockClassLoader(system, domain);
-
- List<ClassLoader> expected = new ArrayList<ClassLoader>();
- expected.add(cl1);
-
- assertEquals(expected, domain.added);
- assertEmpty(domain.removed);
-
- ClassLoader cl2 = createAndRegisterMockClassLoader(system, domain);
- expected.add(cl2);
-
- assertEquals(expected, domain.added);
- assertEmpty(domain.removed);
- }
-
- public void testUnregisterClassLoaderExplicitDomain() throws Exception
- {
- MockClassLoaderSystem system = createMockClassLoaderSystem();
- MockClassLoaderDomain domain = system.createAndRegisterDomain("mock");
- ClassLoader cl1 = createAndRegisterMockClassLoader(system, domain);
-
- List<ClassLoader> expected = new ArrayList<ClassLoader>();
- expected.add(cl1);
-
- assertEquals(expected, domain.added);
- assertEmpty(domain.removed);
-
- system.unregisterClassLoader(cl1);
-
- assertEquals(expected, domain.added);
- assertEquals(expected, domain.removed);
- }
-
- protected MockClassLoaderSystem createMockClassLoaderSystem()
- {
- return new MockClassLoaderSystem();
- }
-}
More information about the jboss-cvs-commits
mailing list