diff --git a/build.xml b/build.xml index d42515972..17746a08d 100644 --- a/build.xml +++ b/build.xml @@ -14,215 +14,165 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - - - + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + test.time" description="Run all unit test cases"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/default.properties b/default.properties index 2c7ec7edf..7670def15 100644 --- a/default.properties +++ b/default.properties @@ -19,6 +19,9 @@ # The pathname of the "junit.jar" JAR file junit.jar = ${junit.home}/junit-3.8.1.jar +# Whether or not to fork tests +junit.fork = true + # The name of this component component.name = commons-lang @@ -52,9 +55,6 @@ source.home = src/java # The base directory for unit test sources test.home = src/test -# Whether or not to fork tests -test.fork = true - # Should Java compilations set the 'debug' compiler option? compile.debug = true diff --git a/src/test/org/apache/commons/lang/ClassUtilsTest.java b/src/test/org/apache/commons/lang/ClassUtilsTest.java index dccfebacd..f1f05da6f 100644 --- a/src/test/org/apache/commons/lang/ClassUtilsTest.java +++ b/src/test/org/apache/commons/lang/ClassUtilsTest.java @@ -399,11 +399,18 @@ public void testPrimitivesToWrappers() { * @return the resulting class loader */ public static ClassLoader newSystemClassLoader() throws SecurityException, IllegalArgumentException { - ClassLoader scl = ClassLoader.getSystemClassLoader(); - if (!(scl instanceof URLClassLoader)) { + ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); + ClassLoader myClassLoader = ClassUtilsTest.class.getClassLoader(); + if (!(myClassLoader instanceof URLClassLoader)) { fail("Need a better test set up."); } - URLClassLoader urlScl = (URLClassLoader) scl; + if (!(systemClassLoader instanceof URLClassLoader)) { + fail("Need a better test set up."); + } + if (!myClassLoader.equals(systemClassLoader)) { + fail("Need a better test set up?"); + } + URLClassLoader urlScl = (URLClassLoader) myClassLoader; return URLClassLoader.newInstance(urlScl.getURLs(), null); } diff --git a/src/test/org/apache/commons/lang/enum/EnumTest.java b/src/test/org/apache/commons/lang/enum/EnumTest.java index 7a8dd1060..05e01edf0 100644 --- a/src/test/org/apache/commons/lang/enum/EnumTest.java +++ b/src/test/org/apache/commons/lang/enum/EnumTest.java @@ -13,10 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.commons.lang.enum; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -27,12 +30,13 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; + import org.apache.commons.lang.ClassUtilsTest; import org.apache.commons.lang.SerializationUtils; /** * Test cases for the {@link Enum} class. - * + * * @author Stephen Colebourne * @author Gary D. Gregory * @version $Id$ @@ -40,6 +44,8 @@ public final class EnumTest extends TestCase { + private static final String ENUM_CLASS_NAME = "org.apache.commons.lang.enum.ColorEnum"; + public EnumTest(String name) { super(name); } @@ -66,11 +72,13 @@ public void testCompareTo() { try { ColorEnum.RED.compareTo(null); fail(); - } catch (NullPointerException ex) {} + } catch (NullPointerException ex) { + } try { ColorEnum.RED.compareTo(new Object()); fail(); - } catch (ClassCastException ex) {} + } catch (ClassCastException ex) { + } } public void testEquals() { @@ -101,12 +109,11 @@ public void testIterator() { public void testList() { List list = new ArrayList(ColorEnum.getEnumList()); - + assertNotNull(list); - - assertEquals( list.size(), - ColorEnum.getEnumMap().keySet().size()); - + + assertEquals(list.size(), ColorEnum.getEnumMap().keySet().size()); + Iterator it = list.iterator(); assertSame(ColorEnum.RED, it.next()); assertSame(ColorEnum.GREEN, it.next()); @@ -115,7 +122,7 @@ public void testList() { public void testMap() { Map map = new HashMap(ColorEnum.getEnumMap()); - + assertNotNull(map); assertTrue(map.containsValue(ColorEnum.RED)); assertTrue(map.containsValue(ColorEnum.GREEN)); @@ -123,8 +130,7 @@ public void testMap() { assertSame(ColorEnum.RED, map.get("Red")); assertSame(ColorEnum.GREEN, map.get("Green")); assertSame(ColorEnum.BLUE, map.get("Blue")); - assertEquals( map.keySet().size(), - ColorEnum.getEnumList().size()); + assertEquals(map.keySet().size(), ColorEnum.getEnumList().size()); } public void testGet() { @@ -234,7 +240,7 @@ public void testOperationList() { assertNotNull(list); assertEquals(2, list.size()); assertEquals(list.size(), OperationEnum.getEnumMap().keySet().size()); - + Iterator it = list.iterator(); assertSame(OperationEnum.PLUS, it.next()); assertSame(OperationEnum.MINUS, it.next()); @@ -244,7 +250,7 @@ public void testOperationMap() { Map map = OperationEnum.getEnumMap(); assertNotNull(map); assertEquals(map.keySet().size(), OperationEnum.getEnumList().size()); - + assertTrue(map.containsValue(OperationEnum.PLUS)); assertTrue(map.containsValue(OperationEnum.MINUS)); assertSame(OperationEnum.PLUS, map.get("Plus")); @@ -255,19 +261,19 @@ public void testOperationCalculation() { assertEquals(3, OperationEnum.PLUS.eval(1, 2)); assertEquals(-1, OperationEnum.MINUS.eval(1, 2)); } - - //----------------------------------------------------------------------- + + // ----------------------------------------------------------------------- public void testExtended1Get() { assertSame(Extended1Enum.ALPHA, Extended1Enum.getEnum("Alpha")); assertSame(Extended1Enum.BETA, Extended1Enum.getEnum("Beta")); assertSame(null, Extended1Enum.getEnum("Gamma")); assertSame(null, Extended1Enum.getEnum("Delta")); } - + public void testExtended2Get() { assertSame(Extended1Enum.ALPHA, Extended2Enum.ALPHA); assertSame(Extended1Enum.BETA, Extended2Enum.BETA); - + assertSame(Extended2Enum.ALPHA, Extended2Enum.getEnum("Alpha")); assertSame(Extended2Enum.BETA, Extended2Enum.getEnum("Beta")); assertSame(Extended2Enum.GAMMA, Extended2Enum.getEnum("Gamma")); @@ -278,7 +284,7 @@ public void testExtended3Get() { assertSame(Extended2Enum.ALPHA, Extended3Enum.ALPHA); assertSame(Extended2Enum.BETA, Extended3Enum.BETA); assertSame(Extended2Enum.GAMMA, Extended3Enum.GAMMA); - + assertSame(Extended3Enum.ALPHA, Extended3Enum.getEnum("Alpha")); assertSame(Extended3Enum.BETA, Extended3Enum.getEnum("Beta")); assertSame(Extended3Enum.GAMMA, Extended3Enum.getEnum("Gamma")); @@ -295,11 +301,11 @@ public void testExtendedSerialization() { public void testExtendedToString() { assertEquals("Extended1Enum[Alpha]", Extended1Enum.ALPHA.toString()); assertEquals("Extended1Enum[Beta]", Extended1Enum.BETA.toString()); - + assertEquals("Extended1Enum[Alpha]", Extended2Enum.ALPHA.toString()); assertEquals("Extended1Enum[Beta]", Extended2Enum.BETA.toString()); assertEquals("Extended2Enum[Gamma]", Extended2Enum.GAMMA.toString()); - + assertEquals("Extended1Enum[Alpha]", Extended3Enum.ALPHA.toString()); assertEquals("Extended1Enum[Beta]", Extended3Enum.BETA.toString()); assertEquals("Extended2Enum[Gamma]", Extended3Enum.GAMMA.toString()); @@ -311,7 +317,7 @@ public void testExtended1List() { assertNotNull(list); assertEquals(2, list.size()); assertEquals(list.size(), Extended1Enum.getEnumMap().keySet().size()); - + Iterator it = list.iterator(); assertSame(Extended1Enum.ALPHA, it.next()); assertSame(Extended1Enum.BETA, it.next()); @@ -322,7 +328,7 @@ public void testExtended2List() { assertNotNull(list); assertEquals(3, list.size()); assertEquals(list.size(), Extended2Enum.getEnumMap().keySet().size()); - + Iterator it = list.iterator(); assertSame(Extended2Enum.ALPHA, it.next()); assertSame(Extended2Enum.BETA, it.next()); @@ -334,7 +340,7 @@ public void testExtended3List() { assertNotNull(list); assertEquals(4, list.size()); assertEquals(list.size(), Extended3Enum.getEnumMap().keySet().size()); - + Iterator it = list.iterator(); assertSame(Extended3Enum.ALPHA, it.next()); assertSame(Extended3Enum.BETA, it.next()); @@ -346,7 +352,7 @@ public void testExtended1Map() { Map map = Extended1Enum.getEnumMap(); assertNotNull(map); assertEquals(map.keySet().size(), Extended1Enum.getEnumList().size()); - + assertTrue(map.containsValue(Extended1Enum.ALPHA)); assertTrue(map.containsValue(Extended1Enum.BETA)); assertSame(Extended1Enum.ALPHA, map.get("Alpha")); @@ -357,7 +363,7 @@ public void testExtended2Map() { Map map = Extended2Enum.getEnumMap(); assertNotNull(map); assertEquals(map.keySet().size(), Extended2Enum.getEnumList().size()); - + assertTrue(map.containsValue(Extended2Enum.ALPHA)); assertTrue(map.containsValue(Extended2Enum.BETA)); assertTrue(map.containsValue(Extended2Enum.GAMMA)); @@ -370,7 +376,7 @@ public void testExtended3Map() { Map map = Extended3Enum.getEnumMap(); assertNotNull(map); assertEquals(map.keySet().size(), Extended3Enum.getEnumList().size()); - + assertTrue(map.containsValue(Extended3Enum.ALPHA)); assertTrue(map.containsValue(Extended3Enum.BETA)); assertTrue(map.containsValue(Extended3Enum.GAMMA)); @@ -381,10 +387,10 @@ public void testExtended3Map() { assertSame(Extended3Enum.DELTA, map.get("Delta")); } - //----------------------------------------------------------------------- + // ----------------------------------------------------------------------- public void testNested() { List list = new ArrayList(Nest.ColorEnum.getEnumList()); - assertEquals(3, list.size()); // all is well + assertEquals(3, list.size()); // all is well Iterator it = list.iterator(); assertSame(Nest.ColorEnum.RED, it.next()); assertSame(Nest.ColorEnum.GREEN, it.next()); @@ -396,7 +402,7 @@ public void testNested() { public void testNestedBroken() { List list = new ArrayList(NestBroken.ColorEnum.getEnumList()); try { - assertEquals(0, list.size()); // no enums!!! + assertEquals(0, list.size()); // no enums!!! // this is BROKEN because the enum constants are defined in a DIFFERENT // class from getEnumList(). Once NestBroken class is referenced, // and thus class loaded with its enum constants, the getEnumList works: @@ -406,7 +412,7 @@ public void testNestedBroken() { } new NestBroken(); list = new ArrayList(NestBroken.ColorEnum.getEnumList()); - assertEquals(3, list.size()); // all is well!!! + assertEquals(3, list.size()); // all is well!!! Iterator it = list.iterator(); assertSame(NestBroken.RED, it.next()); assertSame(NestBroken.GREEN, it.next()); @@ -415,7 +421,7 @@ public void testNestedBroken() { public void testNestedLinked() { List list = new ArrayList(NestLinked.ColorEnum.getEnumList()); - assertEquals(3, list.size()); // all is well + assertEquals(3, list.size()); // all is well Iterator it = list.iterator(); assertSame(NestLinked.RED, it.next()); assertSame(NestLinked.GREEN, it.next()); @@ -426,7 +432,7 @@ public void testNestedLinked() { public void testNestedReferenced() { List list = new ArrayList(NestReferenced.ColorEnum.getEnumList()); - assertEquals(3, list.size()); // all is well + assertEquals(3, list.size()); // all is well Iterator it = list.iterator(); assertSame(NestReferenced.RED, it.next()); assertSame(NestReferenced.GREEN, it.next()); @@ -435,7 +441,7 @@ public void testNestedReferenced() { // the SAME class as the getEnumList(). The references in the outer class // are just extra references. } - + public void testColorEnumEqualsWithDifferentClassLoaders() throws SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { this.testEqualsTrueWithDifferentClassLoaders(ColorEnum.BLUE); @@ -481,7 +487,21 @@ Object getColorEnum(ClassLoader classLoader, String color) throws ClassNotFoundE // set up: assertNotNull(classLoader); assertFalse(classLoader.equals(ColorEnum.class.getClassLoader())); - Class otherColorEnumClass = classLoader.loadClass("org.apache.commons.lang.enum.ColorEnum"); + Class otherColorEnumClass = null; + try { + otherColorEnumClass = classLoader.loadClass(ENUM_CLASS_NAME); + } catch (ClassNotFoundException e) { + // Dump some information to help debug class loader issues under different JREs, Ant, Eclipse. + System.err.println("Could not load " + ENUM_CLASS_NAME + " from the class loader " + classLoader); + URLClassLoader urlCl = (URLClassLoader) classLoader; + URL[] urls = urlCl.getURLs(); + System.err.println("Class loader has " + urls.length + " URLs:"); + for (int i = 0; i < urls.length; i++) { + System.err.println("URL[" + i + "] = " + urls[i]); + } + e.printStackTrace(); + throw e; + } assertNotNull(otherColorEnumClass); assertNotNull(otherColorEnumClass.getClassLoader()); assertTrue(classLoader.equals(otherColorEnumClass.getClassLoader())); diff --git a/src/test/org/apache/commons/lang/enums/EnumTest.java b/src/test/org/apache/commons/lang/enums/EnumTest.java index c41692f61..92ca822fa 100644 --- a/src/test/org/apache/commons/lang/enums/EnumTest.java +++ b/src/test/org/apache/commons/lang/enums/EnumTest.java @@ -17,6 +17,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -29,6 +31,7 @@ import junit.framework.TestSuite; import org.apache.commons.lang.ClassUtilsTest; import org.apache.commons.lang.SerializationUtils; +import org.apache.commons.lang.enum.ColorEnum; /** * Test cases for the {@link Enum} class. @@ -40,6 +43,8 @@ public final class EnumTest extends TestCase { + private static final String ENUMS_CLASS_NAME = "org.apache.commons.lang.enums.ColorEnum"; + public EnumTest(String name) { super(name); } @@ -481,7 +486,21 @@ Object getColorEnum(ClassLoader classLoader, String color) throws ClassNotFoundE // set up: assertNotNull(classLoader); assertFalse(classLoader.equals(ColorEnum.class.getClassLoader())); - Class otherColorEnumClass = classLoader.loadClass("org.apache.commons.lang.enums.ColorEnum"); + Class otherColorEnumClass = null; + try { + otherColorEnumClass = classLoader.loadClass(ENUMS_CLASS_NAME); + } catch (ClassNotFoundException e) { + // Dump some information to help debug class loader issues under different JREs, Ant, Eclipse. + System.err.println("Could not load " + ENUMS_CLASS_NAME + " from the class loader " + classLoader); + URLClassLoader urlCl = (URLClassLoader) classLoader; + URL[] urls = urlCl.getURLs(); + System.err.println("Class loader has " + urls.length + " URLs:"); + for (int i = 0; i < urls.length; i++) { + System.err.println("URL[" + i + "] = " + urls[i]); + } + e.printStackTrace(); + throw e; + } assertNotNull(otherColorEnumClass); assertNotNull(otherColorEnumClass.getClassLoader()); assertTrue(classLoader.equals(otherColorEnumClass.getClassLoader()));