/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.test.classloading.test;
import java.net.URL;
import org.jboss.classloading.plugins.AbstractClassLoadingDomain;
import org.jboss.classloading.plugins.DelegatingDomainClassLoader;
import org.jboss.test.BaseTestCase;
import org.jboss.test.classloading.support.ClassLoaderStats;
import org.jboss.test.classloading.support.SimpleClassLoader;
/**
* Domain Test Case.
*
* @author <a href="adrian@jboss.com">Adrian Brock</a>
* @version $Revision: 1.3 $
*/
public class DomainTestCase extends BaseTestCase
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
public DomainTestCase(String name)
{
super(name);
}
// Public --------------------------------------------------------
public void testLoadClass() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
domain.loadClass("java.lang.String", false, null);
}
public void testLoadClassFailed() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
try
{
domain.loadClass("java.lang.rubbish", false, null);
fail("Should not be here!");
}
catch (ClassNotFoundException expected)
{
}
}
public void testLoadResource() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
URL resource = domain.loadResource("java/lang/String.class", null);
assertNotNull(resource);
}
public void testLoadResourceFailed() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
URL resource = domain.loadResource("java/lang/rubbish", null);
assertNull(resource);
}
public void testClassCache() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
SimpleClassLoader scl = new SimpleClassLoader();
scl.addClass("xyzzy.Hello", String.class);
domain.addDomainClassLoader(new DelegatingDomainClassLoader(scl));
domain.loadClass("xyzzy.Hello", false, null);
ClassLoaderStats stats = (ClassLoaderStats) scl.requests.get("xyzzy.Hello");
assertNotNull(stats);
assertEquals(1, stats.loadClass);
domain.loadClass("xyzzy.Hello", false, null);
assertEquals(1, stats.loadClass);
}
public void testClassCacheFailed() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
SimpleClassLoader scl = new SimpleClassLoader();
scl.addClass("xyzzy.Hello", String.class);
domain.addDomainClassLoader(new DelegatingDomainClassLoader(scl));
try
{
domain.loadClass("xyzzy.rubbish", false, null);
fail("Should not be here!");
}
catch (ClassNotFoundException expected)
{
}
ClassLoaderStats stats = (ClassLoaderStats) scl.requests.get("xyzzy.rubbish");
assertNotNull(stats);
assertEquals(1, stats.loadClass);
try
{
domain.loadClass("xyzzy.rubbish", false, null);
fail("Should not be here!");
}
catch (ClassNotFoundException expected)
{
}
assertEquals(1, stats.loadClass);
}
public void testClearClassCache() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
SimpleClassLoader scl = new SimpleClassLoader();
scl.addClass("xyzzy.Hello", String.class);
DelegatingDomainClassLoader ddcl = new DelegatingDomainClassLoader(scl);
domain.addDomainClassLoader(ddcl);
domain.loadClass("xyzzy.Hello", false, null);
ClassLoaderStats stats = (ClassLoaderStats) scl.requests.get("xyzzy.Hello");
assertNotNull(stats);
assertEquals(1, stats.loadClass);
domain.removeDomainClassLoader(ddcl);
try
{
domain.loadClass("xyzzy.Hello", false, null);
fail("Should not be here!");
}
catch (ClassNotFoundException expected)
{
}
domain.addDomainClassLoader(ddcl);
domain.loadClass("xyzzy.Hello", false, null);
assertEquals(2, stats.loadClass);
}
public void testResourceCache() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
SimpleClassLoader scl = new SimpleClassLoader();
scl.addResource("xyzzy/Hello", new URL("file:///"));
domain.addDomainClassLoader(new DelegatingDomainClassLoader(scl));
URL url = domain.loadResource("xyzzy/Hello", null);
assertNotNull(url);
ClassLoaderStats stats = (ClassLoaderStats) scl.requests.get("xyzzy/Hello");
assertNotNull(stats);
assertEquals(1, stats.getResource);
url = domain.loadResource("xyzzy/Hello", null);
assertNotNull(url);
assertEquals(1, stats.getResource);
}
public void testResourceCacheFailed() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
SimpleClassLoader scl = new SimpleClassLoader();
scl.addResource("xyzzy/Hello", new URL("file:///"));
domain.addDomainClassLoader(new DelegatingDomainClassLoader(scl));
URL url = domain.loadResource("xyzzy/rubbish", null);
assertNull(url);
ClassLoaderStats stats = (ClassLoaderStats) scl.requests.get("xyzzy/rubbish");
assertNotNull(stats);
assertEquals(1, stats.getResource);
url = domain.loadResource("xyzzy/rubbish", null);
assertNull(url);
assertEquals(1, stats.getResource);
}
public void testClearResourceCache() throws Exception
{
AbstractClassLoadingDomain domain = new AbstractClassLoadingDomain();
SimpleClassLoader scl = new SimpleClassLoader();
scl.addResource("xyzzy/Hello", new URL("file:///"));
DelegatingDomainClassLoader ddcl = new DelegatingDomainClassLoader(scl);
domain.addDomainClassLoader(ddcl);
URL url = domain.loadResource("xyzzy/Hello", null);
assertNotNull(url);
ClassLoaderStats stats = (ClassLoaderStats) scl.requests.get("xyzzy/Hello");
assertNotNull(stats);
assertEquals(1, stats.getResource);
domain.removeDomainClassLoader(ddcl);
url = domain.loadResource("xyzzy/Hello", null);
assertNull(url);
domain.addDomainClassLoader(ddcl);
url = domain.loadResource("xyzzy/Hello", null);
assertNotNull(url);
assertEquals(2, stats.getResource);
}
// TestCase overrides --------------------------------------------
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
protected void configureLogging()
{
//enableTrace("org.jboss.classloading");
}
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}