From f76be8910660ceb996d8ddd6b58014e8741b5317 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Thu, 7 Feb 2002 10:36:42 +0000 Subject: [PATCH] Add in unit tests for the TypeFactory classes. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271202 13f79535-47bb-0310-9956-ffa450edef68 --- .../myrmidon/interfaces/type/MyType1.java | 22 +++++ .../myrmidon/interfaces/type/MyType2.java | 22 +++++ .../interfaces/type/TypeFactoryTest.java | 88 +++++++++++++++++++ .../myrmidon/interfaces/type/MyType1.java | 22 +++++ .../myrmidon/interfaces/type/MyType2.java | 22 +++++ .../interfaces/type/TypeFactoryTest.java | 88 +++++++++++++++++++ 6 files changed, 264 insertions(+) create mode 100644 proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType1.java create mode 100644 proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType2.java create mode 100644 proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java create mode 100644 proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType1.java create mode 100644 proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType2.java create mode 100644 proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType1.java b/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType1.java new file mode 100644 index 000000000..e64a7914c --- /dev/null +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType1.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.myrmidon.interfaces.type; + +/** + * A basic implementation of a type to test the type factory. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +public class MyType1 +{ + public boolean equals( final Object object ) + { + return object.getClass() == getClass(); + } +} diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType2.java b/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType2.java new file mode 100644 index 000000000..b652f6b84 --- /dev/null +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/MyType2.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.myrmidon.interfaces.type; + +/** + * A basic implementation of a type to test the type factory. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +public class MyType2 +{ + public boolean equals( final Object object ) + { + return object.getClass() == getClass(); + } +} diff --git a/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java b/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java new file mode 100644 index 000000000..6b3f8fad9 --- /dev/null +++ b/proposal/myrmidon/src/test/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.myrmidon.interfaces.type; + +import java.io.File; +import java.net.URL; +import junit.framework.TestCase; + +/** + * These are unit tests that test the basic operation of TypeFactorys. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +public class TypeFactoryTest + extends TestCase +{ + private final static String TYPE_NAME1 = "my-type1"; + private final static String TYPE_NAME2 = "my-type2"; + private final static Class TYPE_CLASS1 = MyType1.class; + private final static Class TYPE_CLASS2 = MyType2.class; + private final static String TYPE_CLASSNAME1 = TYPE_CLASS1.getName(); + private final static String TYPE_CLASSNAME2 = TYPE_CLASS2.getName(); + + private final static String TYPE_JAR = + "src/testcases/org/apache/myrmidon/interfaces/type/types.jar".replace( '/', File.separatorChar ); + + public TypeFactoryTest( final String name ) + { + super( name ); + } + + /** + * Make sure that you can load a basic type from DefaultTypeManager. + */ + public void testBasicType() + { + final ClassLoader classLoader = getClass().getClassLoader(); + final DefaultTypeFactory factory = new DefaultTypeFactory( classLoader ); + factory.addNameClassMapping( TYPE_NAME2, TYPE_CLASSNAME2 ); + + try + { + final Object type = factory.create( TYPE_NAME2 ); + final Class typeClass = type.getClass(); + assertEquals( "The type loaded for factory should be same class as in current classloader", + typeClass, TYPE_CLASS2 ); + } + catch( TypeException e ) + { + fail( "Unable to create Type due to " + e ); + } + } + + /** + * Make sure that when you load a type from a RelaodableTypeFactory + * that it is actually reloaded. + */ + public void testReloadingTypeFactory() + throws Exception + { + final File file = new File( TYPE_JAR ); + assertTrue( "Support Jar exists", file.exists() ); + + final URL[] classpath = new URL[]{file.toURL()}; + final ClassLoader classLoader = getClass().getClassLoader(); + final ReloadingTypeFactory factory = new ReloadingTypeFactory( classpath, null ); + factory.addNameClassMapping( TYPE_NAME1, TYPE_CLASSNAME1 ); + + try + { + final Object type = factory.create( TYPE_NAME1 ); + final Class typeClass = type.getClass(); + final boolean sameClass = typeClass == TYPE_CLASS1; + assertTrue( "The type loaded for factory should not be same class as in current classloader", + !sameClass ); + } + catch( TypeException e ) + { + fail( "Unable to create Type due to " + e ); + } + } +} diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType1.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType1.java new file mode 100644 index 000000000..e64a7914c --- /dev/null +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType1.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.myrmidon.interfaces.type; + +/** + * A basic implementation of a type to test the type factory. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +public class MyType1 +{ + public boolean equals( final Object object ) + { + return object.getClass() == getClass(); + } +} diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType2.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType2.java new file mode 100644 index 000000000..b652f6b84 --- /dev/null +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/MyType2.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.myrmidon.interfaces.type; + +/** + * A basic implementation of a type to test the type factory. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +public class MyType2 +{ + public boolean equals( final Object object ) + { + return object.getClass() == getClass(); + } +} diff --git a/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java b/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java new file mode 100644 index 000000000..6b3f8fad9 --- /dev/null +++ b/proposal/myrmidon/src/testcases/org/apache/myrmidon/interfaces/type/TypeFactoryTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.myrmidon.interfaces.type; + +import java.io.File; +import java.net.URL; +import junit.framework.TestCase; + +/** + * These are unit tests that test the basic operation of TypeFactorys. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +public class TypeFactoryTest + extends TestCase +{ + private final static String TYPE_NAME1 = "my-type1"; + private final static String TYPE_NAME2 = "my-type2"; + private final static Class TYPE_CLASS1 = MyType1.class; + private final static Class TYPE_CLASS2 = MyType2.class; + private final static String TYPE_CLASSNAME1 = TYPE_CLASS1.getName(); + private final static String TYPE_CLASSNAME2 = TYPE_CLASS2.getName(); + + private final static String TYPE_JAR = + "src/testcases/org/apache/myrmidon/interfaces/type/types.jar".replace( '/', File.separatorChar ); + + public TypeFactoryTest( final String name ) + { + super( name ); + } + + /** + * Make sure that you can load a basic type from DefaultTypeManager. + */ + public void testBasicType() + { + final ClassLoader classLoader = getClass().getClassLoader(); + final DefaultTypeFactory factory = new DefaultTypeFactory( classLoader ); + factory.addNameClassMapping( TYPE_NAME2, TYPE_CLASSNAME2 ); + + try + { + final Object type = factory.create( TYPE_NAME2 ); + final Class typeClass = type.getClass(); + assertEquals( "The type loaded for factory should be same class as in current classloader", + typeClass, TYPE_CLASS2 ); + } + catch( TypeException e ) + { + fail( "Unable to create Type due to " + e ); + } + } + + /** + * Make sure that when you load a type from a RelaodableTypeFactory + * that it is actually reloaded. + */ + public void testReloadingTypeFactory() + throws Exception + { + final File file = new File( TYPE_JAR ); + assertTrue( "Support Jar exists", file.exists() ); + + final URL[] classpath = new URL[]{file.toURL()}; + final ClassLoader classLoader = getClass().getClassLoader(); + final ReloadingTypeFactory factory = new ReloadingTypeFactory( classpath, null ); + factory.addNameClassMapping( TYPE_NAME1, TYPE_CLASSNAME1 ); + + try + { + final Object type = factory.create( TYPE_NAME1 ); + final Class typeClass = type.getClass(); + final boolean sameClass = typeClass == TYPE_CLASS1; + assertTrue( "The type loaded for factory should not be same class as in current classloader", + !sameClass ); + } + catch( TypeException e ) + { + fail( "Unable to create Type due to " + e ); + } + } +}