From: David S. <ds...@us...> - 2006-02-15 22:56:15
|
Update of /cvsroot/junit/junit/junit/framework In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13390/junit/framework Modified Files: TestSuite.java TestFailure.java TestResult.java TestCase.java ComparisonFailure.java AssertionFailedError.java Assert.java Added Files: JUnit4TestCaseFacade.java JUnit4TestAdapter.java JUnit4TestAdapterCache.java Log Message: Merged with branch, Kent will make final changes and launch. Index: TestSuite.java =================================================================== RCS file: /cvsroot/junit/junit/junit/framework/TestSuite.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- TestSuite.java 23 Oct 2004 22:05:36 -0000 1.15 +++ TestSuite.java 15 Feb 2006 22:55:32 -0000 1.16 @@ -6,7 +6,9 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Enumeration; +import java.util.List; import java.util.Vector; /** @@ -41,8 +43,8 @@ * ...as the moon sets over the early morning Merlin, Oregon * mountains, our intrepid adventurers type... */ - static public Test createTest(Class theClass, String name) { - Constructor constructor; + static public Test createTest(Class<? extends TestCase> theClass, String name) { + Constructor<? extends TestCase> constructor; try { constructor= getTestConstructor(theClass); } catch (NoSuchMethodException e) { @@ -71,7 +73,7 @@ * Gets a constructor which takes a single String as * its argument or a no arg constructor. */ - public static Constructor getTestConstructor(Class theClass) throws NoSuchMethodException { + public static Constructor<? extends TestCase> getTestConstructor(Class<? extends TestCase> theClass) throws NoSuchMethodException { Class[] args= { String.class }; try { return theClass.getConstructor(args); @@ -86,6 +88,7 @@ */ public static Test warning(final String message) { return new TestCase("warning") { + @Override protected void runTest() { fail(message); } @@ -100,11 +103,11 @@ PrintWriter writer= new PrintWriter(stringWriter); t.printStackTrace(writer); return stringWriter.toString(); - } + private String fName; - private Vector fTests= new Vector(10); + private Vector<Test> fTests= new Vector<Test>(10); // Cannot convert this to List because it is used directly by some test runners /** * Constructs an empty TestSuite. @@ -115,10 +118,10 @@ /** * Constructs a TestSuite from the given class. Adds all the methods * starting with "test" as test cases to the suite. - * Parts of this method was written at 2337 meters in the Hueffihuette, + * Parts of this method were written at 2337 meters in the Hueffihuette, * Kanton Uri */ - public TestSuite(final Class theClass) { + public TestSuite(final Class<? extends TestCase> theClass) { fName= theClass.getName(); try { getTestConstructor(theClass); // Avoid generating multiple error messages @@ -133,12 +136,10 @@ } Class superClass= theClass; - Vector names= new Vector(); + List<String> names= new ArrayList<String>(); while (Test.class.isAssignableFrom(superClass)) { - Method[] methods= superClass.getDeclaredMethods(); - for (int i= 0; i < methods.length; i++) { - addTestMethod(methods[i], names, theClass); - } + for (Method each : superClass.getDeclaredMethods()) + addTestMethod(each, names, theClass); superClass= superClass.getSuperclass(); } if (fTests.size() == 0) @@ -149,7 +150,7 @@ * Constructs a TestSuite from the given class with the given name. * @see TestSuite#TestSuite(Class) */ - public TestSuite(Class theClass, String name) { + public TestSuite(Class<? extends TestCase> theClass, String name) { this(theClass); setName(name); } @@ -165,16 +166,16 @@ * Constructs a TestSuite from the given array of classes. * @param classes */ - public TestSuite (Class[] classes) { - for (int i= 0; i < classes.length; i++) - addTest(new TestSuite(classes[i])); + public TestSuite (Class<? extends TestCase>... classes) { + for (Class<? extends TestCase> each : classes) + addTest(new TestSuite(each)); } /** * Constructs a TestSuite from the given array of classes with the given name. * @see TestSuite#TestSuite(Class[]) */ - public TestSuite(Class[] classes, String name) { + public TestSuite(Class<? extends TestCase>[] classes, String name) { this(classes); setName(name); } @@ -183,13 +184,13 @@ * Adds a test to the suite. */ public void addTest(Test test) { - fTests.addElement(test); + fTests.add(test); } /** * Adds the tests from the given class to the suite */ - public void addTestSuite(Class testClass) { + public void addTestSuite(Class<? extends TestCase> testClass) { addTest(new TestSuite(testClass)); } @@ -198,10 +199,8 @@ */ public int countTestCases() { int count= 0; - for (Enumeration e= tests(); e.hasMoreElements(); ) { - Test test= (Test)e.nextElement(); - count= count + test.countTestCases(); - } + for (Test each : fTests) + count+= each.countTestCases(); return count; } @@ -218,11 +217,10 @@ * Runs the tests and collects their result in a TestResult. */ public void run(TestResult result) { - for (Enumeration e= tests(); e.hasMoreElements(); ) { + for (Test each : fTests) { if (result.shouldStop() ) break; - Test test= (Test)e.nextElement(); - runTest(test, result); + runTest(each, result); } } @@ -242,7 +240,7 @@ * Returns the test at the given index */ public Test testAt(int index) { - return (Test)fTests.elementAt(index); + return fTests.get(index); } /** @@ -255,19 +253,20 @@ /** * Returns the tests as an enumeration */ - public Enumeration tests() { + public Enumeration<Test> tests() { return fTests.elements(); } /** */ + @Override public String toString() { if (getName() != null) return getName(); return super.toString(); } - private void addTestMethod(Method m, Vector names, Class theClass) { + private void addTestMethod(Method m, List<String> names, Class<? extends TestCase> theClass) { String name= m.getName(); if (names.contains(name)) return; @@ -276,7 +275,7 @@ addTest(warning("Test method isn't public: "+m.getName())); return; } - names.addElement(name); + names.add(name); addTest(createTest(theClass, name)); } Index: TestFailure.java =================================================================== RCS file: /cvsroot/junit/junit/junit/framework/TestFailure.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- TestFailure.java 22 Jun 2002 00:28:03 -0000 1.3 +++ TestFailure.java 15 Feb 2006 22:55:32 -0000 1.4 @@ -36,6 +36,7 @@ /** * Returns a short description of the failure. */ + @Override public String toString() { StringBuffer buffer= new StringBuffer(); buffer.append(fFailedTest+": "+fThrownException.getMessage()); Index: TestResult.java =================================================================== RCS file: /cvsroot/junit/junit/junit/framework/TestResult.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- TestResult.java 9 Sep 2004 21:53:54 -0000 1.5 +++ TestResult.java 15 Feb 2006 22:55:32 -0000 1.6 @@ -1,7 +1,9 @@ package junit.framework; +import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; -import java.util.Vector; +import java.util.List; /** * A <code>TestResult</code> collects the results of executing @@ -13,16 +15,16 @@ * @see Test */ public class TestResult extends Object { - protected Vector fFailures; - protected Vector fErrors; - protected Vector fListeners; + protected List<TestFailure> fFailures; + protected List<TestFailure> fErrors; + protected List<TestListener> fListeners; protected int fRunTests; private boolean fStop; public TestResult() { - fFailures= new Vector(); - fErrors= new Vector(); - fListeners= new Vector(); + fFailures= new ArrayList<TestFailure>(); + fErrors= new ArrayList<TestFailure>(); + fListeners= new ArrayList<TestListener>(); fRunTests= 0; fStop= false; } @@ -31,46 +33,45 @@ * caused the error. */ public synchronized void addError(Test test, Throwable t) { - fErrors.addElement(new TestFailure(test, t)); - for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) { - ((TestListener)e.nextElement()).addError(test, t); - } + fErrors.add(new TestFailure(test, t)); + for (TestListener each : cloneListeners()) + each.addError(test, t); } /** * Adds a failure to the list of failures. The passed in exception * caused the failure. */ public synchronized void addFailure(Test test, AssertionFailedError t) { - fFailures.addElement(new TestFailure(test, t)); - for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) { - ((TestListener)e.nextElement()).addFailure(test, t); - } + fFailures.add(new TestFailure(test, t)); + for (TestListener each : cloneListeners()) + each.addFailure(test, t); } /** * Registers a TestListener */ public synchronized void addListener(TestListener listener) { - fListeners.addElement(listener); + fListeners.add(listener); } /** * Unregisters a TestListener */ public synchronized void removeListener(TestListener listener) { - fListeners.removeElement(listener); + fListeners.remove(listener); } /** * Returns a copy of the listeners. */ - private synchronized Vector cloneListeners() { - return (Vector)fListeners.clone(); + private synchronized List<TestListener> cloneListeners() { + List<TestListener> result= new ArrayList<TestListener>(); + result.addAll(fListeners); + return result; } /** * Informs the result that a test was completed. */ public void endTest(Test test) { - for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) { - ((TestListener)e.nextElement()).endTest(test); - } + for (TestListener each : cloneListeners()) + each.endTest(test); } /** * Gets the number of detected errors. @@ -81,9 +82,11 @@ /** * Returns an Enumeration for the errors */ - public synchronized Enumeration errors() { - return fErrors.elements(); + public synchronized Enumeration<TestFailure> errors() { + return Collections.enumeration(fErrors); } + + /** * Gets the number of detected failures. */ @@ -93,9 +96,10 @@ /** * Returns an Enumeration for the failures */ - public synchronized Enumeration failures() { - return fFailures.elements(); + public synchronized Enumeration<TestFailure> failures() { + return Collections.enumeration(fFailures); } + /** * Runs a TestCase. */ @@ -147,9 +151,8 @@ synchronized(this) { fRunTests+= count; } - for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) { - ((TestListener)e.nextElement()).startTest(test); - } + for (TestListener each : cloneListeners()) + each.startTest(test); } /** * Marks that the test run should stop. Index: TestCase.java =================================================================== RCS file: /cvsroot/junit/junit/junit/framework/TestCase.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- TestCase.java 24 Oct 2004 07:37:06 -0000 1.18 +++ TestCase.java 15 Feb 2006 22:55:32 -0000 1.19 @@ -145,7 +145,7 @@ * @exception Throwable if any exception is thrown */ protected void runTest() throws Throwable { - assertNotNull(fName); // Some VMs crash when calling getMethod(null,null); + assertNotNull("TestCase.fName cannot be null", fName); // Some VMs crash when calling getMethod(null,null); Method runMethod= null; try { // use getMethod to get all public inherited @@ -161,7 +161,7 @@ } try { - runMethod.invoke(this, (Object[])new Class[0]); + runMethod.invoke(this); } catch (InvocationTargetException e) { e.fillInStackTrace(); @@ -187,6 +187,7 @@ /** * Returns a string representation of the test case */ + @Override public String toString() { return getName() + "(" + getClass().getName() + ")"; } Index: ComparisonFailure.java =================================================================== RCS file: /cvsroot/junit/junit/junit/framework/ComparisonFailure.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- ComparisonFailure.java 24 Oct 2004 06:41:48 -0000 1.9 +++ ComparisonFailure.java 15 Feb 2006 22:55:32 -0000 1.10 @@ -30,6 +30,7 @@ * * @see java.lang.Throwable#getMessage() */ + @Override public String getMessage() { return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage()); } Index: AssertionFailedError.java =================================================================== RCS file: /cvsroot/junit/junit/junit/framework/AssertionFailedError.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- AssertionFailedError.java 11 Sep 2004 13:49:07 -0000 1.2 +++ AssertionFailedError.java 15 Feb 2006 22:55:32 -0000 1.3 @@ -3,13 +3,14 @@ /** * Thrown when an assertion failed. */ -public class AssertionFailedError extends Error { +public class AssertionFailedError extends AssertionError { private static final long serialVersionUID= 1L; - - public AssertionFailedError () { + + public AssertionFailedError() { } - public AssertionFailedError (String message) { - super (message); + + public AssertionFailedError(String message) { + super(message); } } \ No newline at end of file Index: Assert.java =================================================================== RCS file: /cvsroot/junit/junit/junit/framework/Assert.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -d -r1.21 -r1.22 --- Assert.java 17 Nov 2004 22:06:08 -0000 1.21 +++ Assert.java 15 Feb 2006 22:55:32 -0000 1.22 @@ -105,18 +105,15 @@ assertEquals(null, expected, actual, delta); } /** - * Asserts that two floats are equal concerning a delta. If they are not - * an AssertionFailedError is thrown with the given message. If the expected - * value is infinity then the delta value is ignored. + * Asserts that two floats are equal concerning a positive delta. If they + * are not an AssertionFailedError is thrown with the given message. If the + * expected value is infinity then the delta value is ignored. */ static public void assertEquals(String message, float expected, float actual, float delta) { - // handle infinity specially since subtracting to infinite values gives NaN and the - // the following test fails - if (Float.isInfinite(expected)) { - if (!(expected == actual)) + if (Float.compare(expected, actual) == 0) + return; + if (!(Math.abs(expected - actual) <= delta)) failNotEquals(message, new Float(expected), new Float(actual)); - } else if (!(Math.abs(expected-actual) <= delta)) - failNotEquals(message, new Float(expected), new Float(actual)); } /** * Asserts that two floats are equal concerning a delta. If the expected @@ -280,7 +277,7 @@ fail(format(message, expected, actual)); } - static String format(String message, Object expected, Object actual) { + public static String format(String message, Object expected, Object actual) { String formatted= ""; if (message != null) formatted= message+" "; |