diff --git a/src/etc/testcases/antunit/setupandteardown/bothsuite.xml b/src/etc/testcases/antunit/setupandteardown/bothsuite.xml new file mode 100644 index 0000000..524b20b --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/bothsuite.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/errorsetup.xml b/src/etc/testcases/antunit/setupandteardown/errorsetup.xml new file mode 100644 index 0000000..82fe26a --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/errorsetup.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/errorsuitesetup.xml b/src/etc/testcases/antunit/setupandteardown/errorsuitesetup.xml new file mode 100644 index 0000000..96ecb88 --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/errorsuitesetup.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/errorsuiteteardown.xml b/src/etc/testcases/antunit/setupandteardown/errorsuiteteardown.xml new file mode 100644 index 0000000..8960fff --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/errorsuiteteardown.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/errorteardown.xml b/src/etc/testcases/antunit/setupandteardown/errorteardown.xml new file mode 100644 index 0000000..56c4995 --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/errorteardown.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/failedtestsuite.xml b/src/etc/testcases/antunit/setupandteardown/failedtestsuite.xml new file mode 100644 index 0000000..7b2d96d --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/failedtestsuite.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/failuresetup.xml b/src/etc/testcases/antunit/setupandteardown/failuresetup.xml new file mode 100644 index 0000000..9f52d49 --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/failuresetup.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/failuresuitesetup.xml b/src/etc/testcases/antunit/setupandteardown/failuresuitesetup.xml new file mode 100644 index 0000000..c63c7fa --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/failuresuitesetup.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/failuresuiteteardown.xml b/src/etc/testcases/antunit/setupandteardown/failuresuiteteardown.xml new file mode 100644 index 0000000..839228c --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/failuresuiteteardown.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/failureteardown.xml b/src/etc/testcases/antunit/setupandteardown/failureteardown.xml new file mode 100644 index 0000000..47ac419 --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/failureteardown.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/nosuitesetup.xml b/src/etc/testcases/antunit/setupandteardown/nosuitesetup.xml new file mode 100644 index 0000000..84451e8 --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/nosuitesetup.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/antunit/setupandteardown/nosuiteteardown.xml b/src/etc/testcases/antunit/setupandteardown/nosuiteteardown.xml new file mode 100644 index 0000000..223be3e --- /dev/null +++ b/src/etc/testcases/antunit/setupandteardown/nosuiteteardown.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/etc/testcases/listener/plainlistener.xml b/src/etc/testcases/listener/plainlistener.xml old mode 100755 new mode 100644 diff --git a/src/etc/testcases/listener/xmllistener.xml b/src/etc/testcases/listener/xmllistener.xml old mode 100755 new mode 100644 diff --git a/src/etc/testcases/setupandteardown.xml b/src/etc/testcases/setupandteardown.xml index ad967f0..67a6a55 100644 --- a/src/etc/testcases/setupandteardown.xml +++ b/src/etc/testcases/setupandteardown.xml @@ -1,119 +1,281 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/ant/antunit/AntUnit.java b/src/main/org/apache/ant/antunit/AntUnit.java index abc9aa0..4c5cadd 100644 --- a/src/main/org/apache/ant/antunit/AntUnit.java +++ b/src/main/org/apache/ant/antunit/AntUnit.java @@ -82,6 +82,16 @@ public class AntUnit extends Task { * name of the magic tearDown target. */ private static final String TEARDOWN = "tearDown"; + + /** + * name of the magic suiteSetUp target. + */ + private static final String SUITESETUP = "suiteSetUp"; + + /** + * name of the magic suiteTearDown target. + */ + private static final String SUITETEARDOWN = "suiteTearDown"; /** * The build files to process. @@ -224,12 +234,47 @@ public class AntUnit extends Task { Map targets = newProject.getTargets(); boolean setUp = targets.containsKey(SETUP); boolean tearDown = targets.containsKey(TEARDOWN); + boolean suiteSetUp = targets.containsKey(SUITESETUP); + boolean suiteTearDown = targets.containsKey(SUITETEARDOWN); // start test newProject.fireBuildStarted(); Throwable caught = null; try { + if (suiteSetUp) { + boolean success = false; + try { + newProject.executeTarget(SUITESETUP); + success = true; + } catch (AssertionFailedException e) { + fireStartTest(SUITESETUP); + fireFail(SUITESETUP, e); + } catch (BuildException e) { + boolean failed = false; + fireStartTest(SUITESETUP); + + // try to see whether the BuildException masks + // an AssertionFailedException. If so, treat + // it as failure instead of error. + Throwable t = e.getCause(); + while (t != null && t instanceof BuildException) { + if (t instanceof AssertionFailedException) { + failed = true; + fireFail(SUITESETUP, (AssertionFailedException) t); + break; + } + t = ((BuildException) t).getCause(); + } + + if (!failed) { + fireError(SUITESETUP, e); + } + } + if (!success) { + return; + } + } Iterator iter = targets.keySet().iterator(); while (iter.hasNext()) { String name = (String) iter.next(); @@ -308,6 +353,35 @@ public class AntUnit extends Task { } catch (Throwable e) { caught = e; } finally { + if (suiteTearDown) { + try { + newProject.executeTarget(SUITETEARDOWN); + } catch (AssertionFailedException e) { + fireStartTest(SUITETEARDOWN); + fireFail(SUITETEARDOWN, e); + } catch (BuildException e) { + boolean failed = false; + fireStartTest(SUITETEARDOWN); + + // try to see whether the BuildException masks + // an AssertionFailedException. If so, treat + // it as failure instead of error. + Throwable t = e.getCause(); + while (t != null && t instanceof BuildException) { + if (t instanceof AssertionFailedException) { + failed = true; + fireFail(SUITETEARDOWN, (AssertionFailedException) t); + break; + } + t = ((BuildException) t).getCause(); + } + + if (!failed) { + fireError(SUITETEARDOWN, e); + } + } + } + newProject.fireBuildFinished(caught); newProject = null; } diff --git a/src/tests/junit/org/apache/ant/antunit/SetUpAndTearDownTest.java b/src/tests/junit/org/apache/ant/antunit/SetUpAndTearDownTest.java index 034be27..472214c 100644 --- a/src/tests/junit/org/apache/ant/antunit/SetUpAndTearDownTest.java +++ b/src/tests/junit/org/apache/ant/antunit/SetUpAndTearDownTest.java @@ -1,59 +1,202 @@ -/* - * 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; - -import org.apache.tools.ant.BuildFileTest; - -public class SetUpAndTearDownTest extends BuildFileTest { - - public SetUpAndTearDownTest(String name) { - super(name); - } - - public void setUp() throws Exception { - super.setUp(); - configureProject("src/etc/testcases/setupandteardown.xml"); - } - - - public void testBoth() { - executeTarget("testBoth"); - } - - public void testNoSetup() { - executeTarget("testNoSetup"); - } - - public void testNoTeardown() { - executeTarget("testNoTeardown"); - } - - public void testFailedTest() { - executeTarget("testFailedTest"); - } - - public void testFailedSetup() { - executeTarget("testFailedSetup"); - } - - public void testFailedTeardown() { - executeTarget("testFailedTeardown"); - } -} \ No newline at end of file +/* + * 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; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.apache.ant.antunit.listener.BaseAntUnitListener; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.BuildFileTest; +import org.apache.tools.ant.Project; + +public class SetUpAndTearDownTest extends BuildFileTest { + + public static class TestReportListener extends BaseAntUnitListener { + + private OutputStream out = null; + /** + * Helper to store intermediate output. + */ + private StringWriter inner; + /** + * Convenience layer on top of {@link #inner inner}. + */ + private PrintWriter wri; + + public TestReportListener() { + super(new BaseAntUnitListener.SendLogTo(SendLogTo.ANT_LOG), "txt"); + } + + /** + * Where to send the test report. + */ + public void setSendLogTo(BaseAntUnitListener.SendLogTo logTo) { + super.setSendLogTo(logTo); + } + + public void startTestSuite(Project testProject, String buildFile) { + super.startTestSuite(testProject, buildFile); + inner = new StringWriter(); + wri = new PrintWriter(inner); + out = getOut(buildFile); + } + + public void endTestSuite(Project testProject, String buildFile) { + if (out != null) { + try { + wri.close(); + out.write(inner.toString().getBytes()); + out.flush(); + } catch (IOException ioex) { + throw new BuildException("Unable to write output", ioex); + } finally { + close(out); + } + } + } + + public void endTest(String target) { + } + + public void addFailure(String target, AssertionFailedException ae) { + super.addFailure(target, ae); + wri.println("failure:" + target + "(" + ae.getMessage() + ")"); + } + + public void addError(String target, Throwable ae) { + super.addError(target, ae); + wri.println("error:" + target + "(" + ae.getMessage() + ")"); + } + + public void messageLogged(int level, String message) {} + } + + public SetUpAndTearDownTest(String name) { + super(name); + } + + public void setUp() throws Exception { + super.setUp(); + configureProject("src/etc/testcases/setupandteardown.xml"); + } + + + public void testBoth() { + executeTarget("testBoth"); + } + + public void testNoSetup() { + executeTarget("testNoSetup"); + } + + public void testNoTeardown() { + executeTarget("testNoTeardown"); + } + + public void testFailedTest() { + executeTarget("testFailedTest"); + System.out.println(getLog()); + } + + public void testFailureSetup() { + executeTarget("testFailureSetup"); + String log = getLog(); + int index = log.indexOf("Tests run: 3, Failures: 3, Errors: 0,"); + assertTrue("summary", index > -1); + } + + public void testErrorSetup() { + executeTarget("testErrorSetup"); + String log = getLog(); + int index = log.indexOf("Tests run: 3, Failures: 0, Errors: 3,"); + assertTrue("summary", index > -1); + } + + public void testFailureTeardown() { + executeTarget("testFailureTeardown"); + String log = getLog(); + int index = log.indexOf("Tests run: 3, Failures: 3, Errors: 0,"); + assertTrue("summary", index > -1); + } + + public void testErrorTeardown() { + executeTarget("testErrorTeardown"); + String log = getLog(); + int index = log.indexOf("Tests run: 3, Failures: 0, Errors: 3,"); + assertTrue("summary", index > -1); + } + + public void testBothSuite() { + executeTarget("testBothSuite"); + } + + public void testNoSuiteSetup() { + executeTarget("testNoSuiteSetUp"); + } + + public void testNoSuiteTeardown() { + executeTarget("testNoSuiteTearDown"); + } + + public void testFailedTestSuite() { + executeTarget("testFailedTestSuite"); + System.out.println(getLog()); + } + + public void testFailureSuiteSetUp() { + executeTarget("testFailureSuiteSetUp"); + String log = getLog(); + int index = log.indexOf("Tests run: 1, Failures: 1, Errors: 0,"); + assertTrue("summary", index > -1); + index = log.indexOf("failure:suiteSetUp(Expected failure)"); + assertTrue("testname", index > -1); + } + + public void testErrorSuiteSetUp() { + executeTarget("testErrorSuiteSetUp"); + String log = getLog(); + int index = log.indexOf("Tests run: 1, Failures: 0, Errors: 1,"); + assertTrue("summary", index > -1); + index = log.indexOf("error:suiteSetUp(Expected error)"); + assertTrue("testname", index > -1); + } + + public void testFailureSuiteTearDown() { + executeTarget("testFailureSuiteTearDown"); + String log = getLog(); + int index = log.indexOf("Tests run: 5, Failures: 1, Errors: 0,"); + assertTrue("summary", index > -1); + index = log.indexOf("failure:suiteTearDown(Expected failure)"); + assertTrue("testname", index > -1); + } + + public void testErrorSuiteTearDown() { + executeTarget("testErrorSuiteTearDown"); + String log = getLog(); + int index = log.indexOf("Tests run: 5, Failures: 0, Errors: 1,"); + assertTrue("summary", index > -1); + index = log.indexOf("error:suiteTearDown(Expected error)"); + assertTrue("testname", index > -1); + } + +} diff --git a/src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java b/src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java old mode 100755 new mode 100644 index 50b692d..57de373 --- a/src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java +++ b/src/tests/junit/org/apache/ant/antunit/listener/PlainListenerTest.java @@ -131,7 +131,6 @@ public class PlainListenerTest extends BuildFileTest { assertTrue("Second tearDown not present", index > -1); } - /* public void testSuiteSetUpTearDown() { executeTarget("showinfo"); String log = getLog(); @@ -144,5 +143,4 @@ public class PlainListenerTest extends BuildFileTest { index = log.indexOf("suiteTearDown", index + 1); assertTrue("suiteTearDown present more than once", index == -1); } - */ } \ No newline at end of file diff --git a/src/tests/junit/org/apache/ant/antunit/listener/XMLListenerTest.java b/src/tests/junit/org/apache/ant/antunit/listener/XMLListenerTest.java old mode 100755 new mode 100644 index 34a236e..2fb454c --- a/src/tests/junit/org/apache/ant/antunit/listener/XMLListenerTest.java +++ b/src/tests/junit/org/apache/ant/antunit/listener/XMLListenerTest.java @@ -66,11 +66,9 @@ public class XMLListenerTest extends BuildFileTest { executeTarget("setupteardown"); } - /* public void testSuiteSetUpTearDown() { executeTarget("suitesetupteardown"); } - */ public void testBadCharacters() { executeTarget("badcharacters");