Report properly BuildException generated when parsing antunit script from Junit4

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@758630 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Scokart Gilles 2009-03-26 13:32:52 +00:00
parent 1d3f040ac8
commit ef0dff3aad
5 changed files with 113 additions and 30 deletions

View File

@ -27,7 +27,6 @@ import java.util.Iterator;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestResult;
import junit.framework.TestSuite;
@ -49,7 +48,7 @@ public class AntUnitSuite extends TestSuite {
private final AntUnitScriptRunner antScriptRunner;
private final MultiProjectDemuxOutputStream stderr;
private final MultiProjectDemuxOutputStream stdout;
private final Test initializationReportingTest;
private final ErrorTestCase initializationReportingTest;
/**
* Create a JUnit TestSuite that when executed will run the given ant
@ -66,6 +65,7 @@ public class AntUnitSuite extends TestSuite {
* a name to the suite so that an IDE can reexecute this suite.
*/
public AntUnitSuite(File scriptFile, Class rootClass) {
setName(rootClass.getName()); //This name allows eclipse to reexecute the test
AntUnitScriptRunner createdScriptRunner = null;
try {
MyProjectFactory prjFactory = new MyProjectFactory(scriptFile);
@ -74,7 +74,7 @@ public class AntUnitSuite extends TestSuite {
antScriptRunner = null;
stdout = null;
stderr = null;
initializationReportingTest = error(e);
initializationReportingTest = new ErrorTestCase(e);
addTest(initializationReportingTest);
return;
}
@ -82,8 +82,6 @@ public class AntUnitSuite extends TestSuite {
initializationReportingTest = null;
stdout = new MultiProjectDemuxOutputStream(antScriptRunner, false);
stderr = new MultiProjectDemuxOutputStream(antScriptRunner, true);
setName(antScriptRunner.getName() + "[" + scriptFile + "]");
setName(rootClass.getName());// Allows eclipse to reexecute the test
List testTargets = antScriptRunner.getTestTartgets();
for (Iterator it = testTargets.iterator(); it.hasNext();) {
String target = (String) it.next();
@ -122,17 +120,6 @@ public class AntUnitSuite extends TestSuite {
runInContainer(testTartgets, notifier);
}
}
private static Test error(final BuildException ex) {
return new TestCase("warning") {
protected void runTest() throws BuildException {
throw ex;
}
};
}
/**
* @Override Run a single test target of the AntUnit suite. suiteSetUp,
@ -204,4 +191,13 @@ public class AntUnitSuite extends TestSuite {
}
}
public boolean hasAntInitError() {
return this.initializationReportingTest!=null;
}
public BuildException getAntInitialisationException() {
return hasAntInitError() ?
initializationReportingTest.getAntScriptError() :
null;
}
}

View File

@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.ant.antunit.junit3;
import org.apache.tools.ant.BuildException;
import junit.framework.TestCase;
/**
* A TestCase that will just report an error when running. This is pretty useful
* when an error is detected during initialization.
*/
public class ErrorTestCase extends TestCase {
/** The name we use for the error test case ('warning') */
public static final String NAME = "warning";
private final BuildException ex;
/**
* Creates a TestCase that will report the Ant BuildException when running.
* @param antScriptError The Ant BuildException that triggered the initialization
* failure
*/
public ErrorTestCase(BuildException antScriptError) {
super(NAME);
this.ex = antScriptError;
}
/**
* @overwrite
*/
protected void runTest() throws BuildException {
throw ex;
}
public BuildException getAntScriptError() {
return ex;
}
}

View File

@ -35,10 +35,13 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import junit.framework.TestCase;
import org.apache.ant.antunit.AntUnitExecutionNotifier;
import org.apache.ant.antunit.AssertionFailedException;
import org.apache.ant.antunit.junit3.AntUnitSuite;
import org.apache.ant.antunit.junit3.AntUnitTestCase;
import org.apache.ant.antunit.junit3.ErrorTestCase;
import org.junit.internal.runners.InitializationError;
import org.junit.runner.Description;
import org.junit.runner.Runner;
@ -64,18 +67,22 @@ public class AntUnitSuiteRunner extends Runner implements Filterable, Sortable {
private final Map/*<String, Description>*/ targetDescriptions = new HashMap();
private final List/*<String>*/ targetsOrder = new LinkedList();
private AntUnitSuiteRunner(AntUnitSuite suite, Class junitTestClass) {
private AntUnitSuiteRunner(AntUnitSuite suite, Class junitTestClass) throws InitializationError {
junit3Suite = suite;
Enumeration tests = suite.tests();
while (tests.hasMoreElements()) {
//TODO Handle the the case of FileNotFound.
//In that case the suite contains an error Test and we have
//a ClassCastException instead of a nice & clear error
//TODO Handle the possibility for the user to define suite of AntUnit scripts
AntUnitTestCase tc = (AntUnitTestCase) tests.nextElement();
Description tc_desc = Description.createTestDescription(junitTestClass, tc.getName());
targetDescriptions.put(tc.getTarget(), tc_desc);
targetsOrder.add(tc.getTarget());
if (suite.hasAntInitError()) {
throw new InitializationError(
new Throwable[] { suite.getAntInitialisationException() }
);
} else {
Enumeration tests = suite.tests();
while (tests.hasMoreElements()) {
TestCase nextTc = (TestCase) tests.nextElement();
//TODO Handle the possibility for the user to define suite of AntUnit scripts
AntUnitTestCase tc = (AntUnitTestCase) nextTc;
Description tc_desc = Description.createTestDescription(junitTestClass, tc.getName());
targetDescriptions.put(tc.getTarget(), tc_desc);
targetsOrder.add(tc.getTarget());
}
}
}

View File

@ -90,6 +90,7 @@ public class AntUnitSuiteTest extends TestCase {
TestResult testResult = new TestResult();
suite.run(testResult);
assertNotNull(suite.getName());
assertEquals(1 , testResult.errorCount());
TestFailure error = (TestFailure) testResult.errors().nextElement();
assertTrue("Unexpected error : " + error.exceptionMessage(),

View File

@ -29,6 +29,7 @@ import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.ant.antunit.junit3.AntUnitSuite;
import org.apache.ant.antunit.junit3.ErrorTestCase;
import org.apache.tools.ant.util.FileUtils;
import org.junit.internal.runners.InitializationError;
import org.junit.runner.Description;
@ -71,7 +72,6 @@ public class AntUnitSuiteRunnerTest extends TestCase {
JUnit4AntUnitRunnable.class);
final ArrayList tDescs = runner.getDescription().getChildren();
final int TEST_STARTED = 1, TEST_FINISHED = 2;
RunNotifier notifierMock = new RunNotifier() {
Description curTest = null;
@ -87,7 +87,6 @@ public class AntUnitSuiteRunnerTest extends TestCase {
curTest = description;
}
@Override
public void fireTestFinished(Description description) {
if (curTest == null) {
mockExcutionError += "Unexpected fireTestFinished("
@ -155,7 +154,19 @@ public class AntUnitSuiteRunnerTest extends TestCase {
}
}
public void testInvalidSuiteReferencingMissingFile() {
try {
AntUnitSuiteRunner runner = new AntUnitSuiteRunner(
JUnit4AntUnitRunnableRefferencingIncorrectFile.class);
fail("InitializationError expected");
} catch (InitializationError e) {
String msg = e.getCauses().get(0).getMessage();
assertTrue("Unexpected error : " + msg, msg.contains("FileNotFound"));
assertTrue("Unexpected error : " + msg, msg.contains("build_script_not_found.xml"));
}
}
public static class JUnit4AntUnitRunnable {
public static AntUnitSuite suite() {
File f = new File("src/etc/testcases/antunit/junit.xml");
@ -187,4 +198,13 @@ public class AntUnitSuiteRunnerTest extends TestCase {
}
}
public static class JUnit4AntUnitRunnableRefferencingIncorrectFile {
public static AntUnitSuite suite() {
File f = new File("build_script_not_found.xml");
return new AntUnitSuite(f,
JUnit4AntUnitRunnableWithNonStaticSuite.class);
}
}
}