[testng] 78/160: Add more logic in ExceptionsHolder

Eugene Zhukov eugene-guest at moszumanska.debian.org
Tue Aug 18 10:22:24 UTC 2015


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 bd67aa402ca1c85a9a2f35fd81fa8c2a2582f25f
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date:   Thu Jun 4 15:06:00 2015 +0200

    Add more logic in ExceptionsHolder
---
 .../internal/AbstractExpectedExceptionsHolder.java | 25 ++++++++++++-
 .../testng/internal/ExpectedExceptionsHolder.java  | 43 ++++++++++++++++------
 src/main/java/org/testng/internal/Invoker.java     |  5 +--
 .../java/org/testng/internal/MethodHelper.java     | 30 ---------------
 4 files changed, 57 insertions(+), 46 deletions(-)

diff --git a/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java b/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
index 204d87c..183547d 100644
--- a/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
+++ b/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
@@ -2,15 +2,36 @@ package org.testng.internal;
 
 import org.testng.ITestNGMethod;
 import org.testng.TestException;
+import org.testng.annotations.IExpectedExceptionsAnnotation;
+import org.testng.annotations.ITestAnnotation;
+import org.testng.internal.annotations.IAnnotationFinder;
 
 import java.util.Arrays;
 
 public abstract class AbstractExpectedExceptionsHolder {
 
+  protected final IAnnotationFinder finder;
+  protected final ITestNGMethod method;
   private final Class<?>[] expectedClasses;
 
-  protected AbstractExpectedExceptionsHolder(Class<?>[] expectedClasses) {
-    this.expectedClasses = expectedClasses;
+  protected AbstractExpectedExceptionsHolder(IAnnotationFinder finder, ITestNGMethod method) {
+    this.finder = finder;
+    this.method = method;
+    expectedClasses = findExpectedClasses(finder, method);
+  }
+
+  private static Class<?>[] findExpectedClasses(IAnnotationFinder finder, ITestNGMethod method) {
+    IExpectedExceptionsAnnotation expectedExceptions = finder.findAnnotation(method, IExpectedExceptionsAnnotation.class);
+    // Old syntax
+    if (expectedExceptions != null) {
+      return expectedExceptions.getValue();
+    } else { // New syntax
+      ITestAnnotation testAnnotation = finder.findAnnotation(method, ITestAnnotation.class);
+      if (testAnnotation != null) {
+        return testAnnotation.getExpectedExceptions();
+      }
+    }
+    return new Class<?>[0];
   }
 
   /**
diff --git a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java b/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
index 0894340..1a7ea7c 100755
--- a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
+++ b/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
@@ -1,9 +1,10 @@
 package org.testng.internal;
 
 import org.testng.ITestNGMethod;
-import org.testng.TestException;
+import org.testng.annotations.IExpectedExceptionsAnnotation;
+import org.testng.annotations.ITestAnnotation;
+import org.testng.internal.annotations.IAnnotationFinder;
 
-import java.util.Arrays;
 import java.util.regex.Pattern;
 
 /**
@@ -11,11 +12,10 @@ import java.util.regex.Pattern;
  * @author cbeust
  */
 public class ExpectedExceptionsHolder extends AbstractExpectedExceptionsHolder {
-  private final String messageRegExp;
+  public static final String DEFAULT_REGEXP = ".*";
 
-  public ExpectedExceptionsHolder(Class<?>[] expectedClasses, String messageRegExp) {
-    super(expectedClasses);
-    this.messageRegExp = messageRegExp;
+  public ExpectedExceptionsHolder(IAnnotationFinder finder, ITestNGMethod method) {
+    super(finder, method);
   }
 
   /**
@@ -25,17 +25,38 @@ public class ExpectedExceptionsHolder extends AbstractExpectedExceptionsHolder {
    */
   @Override
   protected boolean isExceptionMatches(Throwable ite) {
-    if (".*".equals(messageRegExp)) {
+    String messageRegExp = getRegExp();
+
+    if (DEFAULT_REGEXP.equals(messageRegExp)) {
       return true;
-    } else {
-      final String message = ite.getMessage();
-      return message != null && Pattern.compile(messageRegExp, Pattern.DOTALL).matcher(ite.getMessage()).matches();
     }
+
+    final String message = ite.getMessage();
+    return message != null && Pattern.compile(messageRegExp, Pattern.DOTALL).matcher(message).matches();
   }
 
   protected String getWrongExceptionMessage(Throwable ite) {
     return "The exception was thrown with the wrong message:" +
-           " expected \"" + messageRegExp + "\"" +
+           " expected \"" + getRegExp() + "\"" +
            " but got \"" + ite.getMessage() + "\"";
   }
+
+  private String getRegExp() {
+    String messageRegExp = DEFAULT_REGEXP;
+    IExpectedExceptionsAnnotation expectedExceptions =
+        finder.findAnnotation(method, IExpectedExceptionsAnnotation.class);
+
+    if (expectedExceptions == null) {
+      // New syntax
+      ITestAnnotation testAnnotation = finder.findAnnotation(method, ITestAnnotation.class);
+      if (testAnnotation != null) {
+        Class<?>[] ee = testAnnotation.getExpectedExceptions();
+        if (ee.length > 0) {
+          messageRegExp = testAnnotation.getExpectedExceptionsMessageRegExp();
+        }
+      }
+    } // else: Old syntax => keep default value
+
+    return messageRegExp;
+  }
 }
diff --git a/src/main/java/org/testng/internal/Invoker.java b/src/main/java/org/testng/internal/Invoker.java
index 9660043..109b45e 100644
--- a/src/main/java/org/testng/internal/Invoker.java
+++ b/src/main/java/org/testng/internal/Invoker.java
@@ -683,7 +683,7 @@ public class Invoker implements IInvoker {
       testResult.setEndMillis(System.currentTimeMillis());
 
       ExpectedExceptionsHolder expectedExceptionClasses
-          = MethodHelper.findExpectedExceptions(m_annotationFinder, tm.getMethod());
+          = new ExpectedExceptionsHolder(m_annotationFinder, tm);
       List<ITestResult> results = Lists.<ITestResult>newArrayList(testResult);
       handleInvocationResults(tm, results, expectedExceptionClasses, false,
           false /* collect results */, failureContext);
@@ -1069,8 +1069,7 @@ public class Invoker implements IInvoker {
 
     int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();
 
-    ExpectedExceptionsHolder expectedExceptionHolder =
-        MethodHelper.findExpectedExceptions(m_annotationFinder, testMethod.getMethod());
+    ExpectedExceptionsHolder expectedExceptionHolder = new ExpectedExceptionsHolder(m_annotationFinder, testMethod);
     final ITestClass testClass= testMethod.getTestClass();
     final List<ITestResult> result = Lists.newArrayList();
     final FailureContext failure = new FailureContext();
diff --git a/src/main/java/org/testng/internal/MethodHelper.java b/src/main/java/org/testng/internal/MethodHelper.java
index 068853a..b7545e8 100644
--- a/src/main/java/org/testng/internal/MethodHelper.java
+++ b/src/main/java/org/testng/internal/MethodHelper.java
@@ -154,36 +154,6 @@ public class MethodHelper {
     return null;
   }
 
-  /**
-   * Read the expected exceptions, if any (need to handle both the old and new
-   * syntax)
-   */
-  protected static ExpectedExceptionsHolder findExpectedExceptions(IAnnotationFinder finder,
-      Method method) {
-    ExpectedExceptionsHolder result = null;
-    IExpectedExceptionsAnnotation expectedExceptions =
-      (IExpectedExceptionsAnnotation) finder.findAnnotation(method,
-        IExpectedExceptionsAnnotation.class);
-    // Old syntax
-    if (expectedExceptions != null) {
-      result = new ExpectedExceptionsHolder(expectedExceptions.getValue(), ".*");
-    }
-    else {
-      // New syntax
-      ITestAnnotation testAnnotation =
-        (ITestAnnotation) finder.findAnnotation(method, ITestAnnotation.class);
-      if (testAnnotation != null) {
-        Class<?>[] ee = testAnnotation.getExpectedExceptions();
-        if (ee.length > 0) {
-          result = new ExpectedExceptionsHolder(ee,
-              testAnnotation.getExpectedExceptionsMessageRegExp());
-        }
-      }
-    }
-
-    return result;
-  }
-
   protected static boolean isEnabled(Class<?> objectClass, IAnnotationFinder finder) {
     ITestAnnotation testClassAnnotation = AnnotationHelper.findTest(finder, objectClass);
     return isEnabled(testClassAnnotation);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/testng.git



More information about the pkg-java-commits mailing list