/*******************************************************************************
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.apache.kato.tests.junit;
import java.util.Iterator;
import javax.tools.diagnostics.image.CorruptDataException;
import javax.tools.diagnostics.runtime.java.JavaClass;
import javax.tools.diagnostics.runtime.java.JavaClassLoader;
import javax.tools.diagnostics.runtime.java.JavaObject;
public class JavaClassLoaderTest extends AbstractImageTestcase
{
private JavaClassLoader _loader;
protected void setUp() throws Exception
{
_loader = defaultJavaClassLoader();
super.setUp();
}
/**
* Ensure that the classes iterator is non-null
*/
public void testGetDefinedClasses()
{
Iterator iter = _loader.getDefinedClasses().iterator();
assertNotNull(iter);
}
/**
* Ensure that the cached classes iterator is non-null
*/
public void testGetCachedClasses()
{
Iterator iter = _loader.getCachedClasses().iterator();
assertNotNull(iter);
}
/**
*
* Grab any class, then try looking up a class with that name since we know it is there
*/
public void testFindClass()
{
JavaClass oneClass = (JavaClass) _loader.getDefinedClasses().get(0);
try {
String name = oneClass.getName();
JavaClass foundClass = _loader.findClass(name);
//make sure that it came out
assertNotNull(foundClass);
//classes might not be the same instance but they should be comparable to each other if they are the same entity
assertTrue(foundClass.equals(oneClass));
} catch (CorruptDataException e) {
}
}
/**
*
* Ensure that the class loader object exists and is an instance of java.lang.ClassLoader
* or some class that inherits from it
*/
public void testGetObject()
{
try {
JavaObject loader = _loader.getObject();
boolean didMatch = false;
JavaClass oneClass = loader.getJavaClass();
while ((!didMatch) && (null != oneClass)) {
if (oneClass.getName().equals("java/lang/ClassLoader")) {
didMatch = true;
}
oneClass = oneClass.getSuperclass();
}
assertTrue(didMatch);
} catch (CorruptDataException e) {
//technically, this is valid
}
}
/**
* Verify that the equals call doesn't throw
*/
public void testEquals()
{
try{
assertTrue(_loader.equals(_loader));
} catch (Throwable t) {
assertTrue(false);
}
}
/**
* Verify that hashCode() doesn't throw and returns non-zero (technically zero is ok but it will be
* flagged here to ensure that we aren't doing anything bad to create the hashcode)
*/
public void testHashCode()
{
try {
assertTrue(0 != _loader.hashCode());
} catch (Throwable t) {
assertTrue(false);
}
}
}