From eef127ec791d5bf433f1cc0a66b4f9985c9aeaa5 Mon Sep 17 00:00:00 2001 From: Scokart Gilles Date: Sun, 18 Jan 2009 19:23:30 +0000 Subject: [PATCH] refactor duplicated code git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/antunit/trunk@735525 13f79535-47bb-0310-9956-ffa450edef68 --- src/main/org/apache/ant/antunit/AntUnit.java | 102 ++++++------------- 1 file changed, 29 insertions(+), 73 deletions(-) diff --git a/src/main/org/apache/ant/antunit/AntUnit.java b/src/main/org/apache/ant/antunit/AntUnit.java index 4c5cadd..ad94e4f 100644 --- a/src/main/org/apache/ant/antunit/AntUnit.java +++ b/src/main/org/apache/ant/antunit/AntUnit.java @@ -221,6 +221,8 @@ public class AntUnit extends Task { } } + + /** * Processes a single build file. */ @@ -251,25 +253,8 @@ public class AntUnit extends Task { 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); - } + fireFailOrError(SUITESETUP, e); } if (!success) { return; @@ -292,24 +277,7 @@ public class AntUnit extends Task { } catch (AssertionFailedException e) { fireFail(name, e); } catch (BuildException e) { - boolean failed = false; - - // 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(name, (AssertionFailedException) t); - break; - } - t = ((BuildException) t).getCause(); - } - - if (!failed) { - fireError(name, e); - } + fireFailOrError(name, e); } finally { // fire endTest here instead of the endTarget // event, otherwise an error would be @@ -324,24 +292,7 @@ public class AntUnit extends Task { } catch (final AssertionFailedException e) { fireFail(name, e); } catch (final BuildException e) { - boolean failed = false; - - // 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(name, (AssertionFailedException) t); - break; - } - t = ((BuildException) t).getCause(); - } - - if (!failed) { - fireError(name, e); - } + fireFailOrError(name, e); } } if (iter.hasNext()) { @@ -360,25 +311,8 @@ public class AntUnit extends Task { 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); - } + fireStartTest(SUITETEARDOWN); + fireFailOrError(SUITETEARDOWN, e); } } @@ -387,6 +321,28 @@ public class AntUnit extends Task { } } + + /** Report a failure or an exception for the test target name */ + private void fireFailOrError(String name, BuildException e) { + boolean failed = false; + // 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(name, (AssertionFailedException) t); + break; + } + t = ((BuildException) t).getCause(); + } + + if (!failed) { + fireError(name, e); + } + } + /** * Redirect output to new project instance. * @param outputToHandle the output to handle.