This is an automated email from the git hooks/post-receive script. eugene-guest pushed a commit to annotated tag testng-6.9.5 in repository testng.
commit c55504f189ef27cef6a662084c9353daec31afc9 Author: Julien Herr <[email protected]> Date: Thu Jun 4 11:08:28 2015 +0200 Move expected exceptions holder logic methods to ExpectedExceptionsHolder --- .../testng/internal/ExpectedExceptionsHolder.java | 82 +++++++++++++++++++++- src/main/java/org/testng/internal/Invoker.java | 78 ++------------------ 2 files changed, 87 insertions(+), 73 deletions(-) diff --git a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java b/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java index 71ba629..8fbd6fa 100755 --- a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java +++ b/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java @@ -1,16 +1,94 @@ package org.testng.internal; +import org.testng.ITestNGMethod; +import org.testng.TestException; + +import java.util.Arrays; +import java.util.regex.Pattern; + /** * A class that contains the expected exceptions and the message regular expression. * @author cbeust */ public class ExpectedExceptionsHolder { - Class<?>[] expectedClasses; - String messageRegExp; + private final Class<?>[] expectedClasses; + private final String messageRegExp; public ExpectedExceptionsHolder(Class<?>[] expectedClasses, String messageRegExp) { this.expectedClasses = expectedClasses; this.messageRegExp = messageRegExp; } + /** + * @param ite The exception that was just thrown + * @return true if the exception that was just thrown is part of the + * expected exceptions + */ + public boolean isExpectedException(Throwable ite) { + if (expectedClasses == null) { + return false; + } + + // TestException is the wrapper exception that TestNG will be throwing when an exception was + // expected but not thrown + if (ite.getClass() == TestException.class) { + return false; + } + + Class<?> realExceptionClass= ite.getClass(); + + for (Class<?> exception : expectedClasses) { + if (exception.isAssignableFrom(realExceptionClass)) { + return true; + } + } + + return false; + } + + /** + * message / regEx .* other + * null true false + * non-null true match + */ + public boolean messageRegExpMatches(Throwable ite) { + if (".*".equals(messageRegExp)) { + return true; + } else { + final String message = ite.getMessage(); + return message != null && Pattern.compile(messageRegExp, Pattern.DOTALL).matcher(ite.getMessage()).matches(); + } + } + + public TestException buildTestException(Throwable ite) { + return new TestException("The exception was thrown with the wrong message:" + + " expected \"" + messageRegExp + "\"" + + " but got \"" + ite.getMessage() + "\"", ite); + } + + public TestException buildTestExceptionPluralize(Throwable ite) { + return new TestException("Expected exception of " + + getExpectedExceptionsPluralize() + + " but got " + ite, ite); + } + + public TestException buildTestExceptionPluralize(ITestNGMethod testMethod) { + if (expectedClasses == null || expectedClasses.length == 0) { + return null; + } + return new TestException("Method " + testMethod + " should have thrown an exception of " + + getExpectedExceptionsPluralize()); + } + + private String getExpectedExceptionsPluralize() { + StringBuilder sb = new StringBuilder(); + if (expectedClasses.length > 1) { + sb.append("any of types "); + sb.append(Arrays.toString(expectedClasses)); + } else { + sb.append("type "); + sb.append(expectedClasses[0]); + } + return sb.toString(); + } } diff --git a/src/main/java/org/testng/internal/Invoker.java b/src/main/java/org/testng/internal/Invoker.java index 8678a98..00e696f 100644 --- a/src/main/java/org/testng/internal/Invoker.java +++ b/src/main/java/org/testng/internal/Invoker.java @@ -1371,25 +1371,19 @@ public class Invoker implements IInvoker { if (ite != null) { // Invocation caused an exception, see if the method was annotated with @ExpectedException - if (isExpectedException(ite, expectedExceptionsHolder)) { - if (messageRegExpMatches(expectedExceptionsHolder.messageRegExp, ite)) { + if (expectedExceptionsHolder != null && expectedExceptionsHolder.isExpectedException(ite)) { + if (expectedExceptionsHolder.messageRegExpMatches(ite)) { testResult.setStatus(ITestResult.SUCCESS); status= ITestResult.SUCCESS; } else { - testResult.setThrowable( - new TestException("The exception was thrown with the wrong message:" + - " expected \"" + expectedExceptionsHolder.messageRegExp + "\"" + - " but got \"" + ite.getMessage() + "\"", ite)); + testResult.setThrowable(expectedExceptionsHolder.buildTestException(ite)); status= ITestResult.FAILURE; } } else if (isSkipExceptionAndSkip(ite)){ status = ITestResult.SKIP; } else if (expectedExceptionsHolder != null) { - testResult.setThrowable( - new TestException("Expected exception of " + - getExpectedExceptionsPluralize(expectedExceptionsHolder) - + " but got " + ite, ite)); + testResult.setThrowable(expectedExceptionsHolder.buildTestExceptionPluralize(ite)); status= ITestResult.FAILURE; } else { handleException(ite, testMethod, testResult, failure.count++); @@ -1400,11 +1394,9 @@ public class Invoker implements IInvoker { // No exception thrown, make sure we weren't expecting one else if(status != ITestResult.SKIP && expectedExceptionsHolder != null) { - Class<?>[] classes = expectedExceptionsHolder.expectedClasses; - if (classes != null && classes.length > 0) { - testResult.setThrowable( - new TestException("Method " + testMethod + " should have thrown an exception of " - + getExpectedExceptionsPluralize(expectedExceptionsHolder))); + TestException exception = expectedExceptionsHolder.buildTestExceptionPluralize(testMethod); + if (exception != null) { + testResult.setThrowable(exception); status= ITestResult.FAILURE; } } @@ -1435,36 +1427,10 @@ public class Invoker implements IInvoker { removeResultsToRetryFromResult(resultsToRetry, result, failure); } - private String getExpectedExceptionsPluralize(final ExpectedExceptionsHolder holder) { - StringBuilder sb = new StringBuilder(); - if (holder.expectedClasses.length > 1) { - sb.append("any of types "); - sb.append(Arrays.toString(holder.expectedClasses)); - } else { - sb.append("type "); - sb.append(holder.expectedClasses[0]); - } - return sb.toString(); - } - private boolean isSkipExceptionAndSkip(Throwable ite) { return SkipException.class.isAssignableFrom(ite.getClass()) && ((SkipException) ite).isSkip(); } - /** - * message / regEx .* other - * null true false - * non-null true match - */ - private boolean messageRegExpMatches(String messageRegExp, Throwable ite) { - if (".*".equals(messageRegExp)) { - return true; - } else { - final String message = ite.getMessage(); - return message != null && Pattern.compile(messageRegExp, Pattern.DOTALL).matcher(ite.getMessage()).matches(); - } - } - private void removeResultsToRetryFromResult(List<ITestResult> resultsToRetry, List<ITestResult> result, FailureContext failure) { if (resultsToRetry != null) { @@ -1656,36 +1622,6 @@ public class Invoker implements IInvoker { } - /** - * @param ite The exception that was just thrown - * @param exceptionHolder Expected exceptions holder for this - * test method - * @return true if the exception that was just thrown is part of the - * expected exceptions - */ - private boolean isExpectedException(Throwable ite, ExpectedExceptionsHolder exceptionHolder) { - if (exceptionHolder == null || exceptionHolder.expectedClasses == null) { - return false; - } - - // TestException is the wrapper exception that TestNG will be throwing when an exception was - // expected but not thrown - if (ite.getClass() == TestException.class) { - return false; - } - - Class<?>[] exceptions = exceptionHolder.expectedClasses; - Class<?> realExceptionClass= ite.getClass(); - - for (Class<?> exception : exceptions) { - if (exception.isAssignableFrom(realExceptionClass)) { - return true; - } - } - - return false; - } - static interface Predicate<K, T> { boolean isTrue(K k, T v); } -- Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git _______________________________________________ pkg-java-commits mailing list [email protected] http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-java-commits

