[testng] 77/160: Extract generic concept

Eugene Zhukov eugene-guest at moszumanska.debian.org
Tue Aug 18 10:22:23 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 c9cd68773ff11200e3ef2ace4987017ba3221d8d
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date:   Thu Jun 4 13:47:26 2015 +0200

    Extract generic concept
---
 ....java => AbstractExpectedExceptionsHolder.java} | 38 ++++--------
 .../testng/internal/ExpectedExceptionsHolder.java  | 69 +++-------------------
 2 files changed, 18 insertions(+), 89 deletions(-)

diff --git a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java b/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
old mode 100755
new mode 100644
similarity index 60%
copy from src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
copy to src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
index 46a3a4b..204d87c
--- a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
+++ b/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
@@ -4,19 +4,13 @@ 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 {
+public abstract class AbstractExpectedExceptionsHolder {
+
   private final Class<?>[] expectedClasses;
-  private final String messageRegExp;
 
-  public ExpectedExceptionsHolder(Class<?>[] expectedClasses, String messageRegExp) {
+  protected AbstractExpectedExceptionsHolder(Class<?>[] expectedClasses) {
     this.expectedClasses = expectedClasses;
-    this.messageRegExp = messageRegExp;
   }
 
   /**
@@ -38,7 +32,7 @@ public class ExpectedExceptionsHolder {
     Class<?> realExceptionClass= ite.getClass();
 
     for (Class<?> exception : expectedClasses) {
-      if (exception.isAssignableFrom(realExceptionClass) && messageRegExpMatches(ite)) {
+      if (exception.isAssignableFrom(realExceptionClass) && isExceptionMatches(ite)) {
         return true;
       }
     }
@@ -46,32 +40,18 @@ public class ExpectedExceptionsHolder {
     return false;
   }
 
-  /**
-   *   message / regEx  .*      other
-   *   null             true    false
-   *   non-null         true    match
-   */
-  private 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 wrongException(Throwable ite) {
-    if (messageRegExpMatches(ite)) {
+    if (isExceptionMatches(ite)) {
       return new TestException("Expected exception of " +
                                getExpectedExceptionsPluralize()
                                + " but got " + ite, ite);
     } else {
-      return new TestException("The exception was thrown with the wrong message:" +
-                               " expected \"" + messageRegExp + "\"" +
-                               " but got \"" + ite.getMessage() + "\"", ite);
+      return new TestException(getWrongExceptionMessage(ite), ite);
     }
   }
 
+  protected abstract String getWrongExceptionMessage(Throwable ite);
+
   public TestException noException(ITestNGMethod testMethod) {
     if (expectedClasses == null || expectedClasses.length == 0) {
       return null;
@@ -91,4 +71,6 @@ public class ExpectedExceptionsHolder {
     }
     return sb.toString();
   }
+
+  protected abstract boolean isExceptionMatches(Throwable ite);
 }
diff --git a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java b/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
index 46a3a4b..0894340 100755
--- a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
+++ b/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
@@ -10,48 +10,21 @@ import java.util.regex.Pattern;
  * A class that contains the expected exceptions and the message regular expression.
  * @author cbeust
  */
-public class ExpectedExceptionsHolder {
-  private final Class<?>[] expectedClasses;
+public class ExpectedExceptionsHolder extends AbstractExpectedExceptionsHolder {
   private final String messageRegExp;
 
   public ExpectedExceptionsHolder(Class<?>[] expectedClasses, String messageRegExp) {
-    this.expectedClasses = expectedClasses;
+    super(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) && messageRegExpMatches(ite)) {
-        return true;
-      }
-    }
-
-    return false;
-  }
-
-  /**
    *   message / regEx  .*      other
    *   null             true    false
    *   non-null         true    match
    */
-  private boolean messageRegExpMatches(Throwable ite) {
+  @Override
+  protected boolean isExceptionMatches(Throwable ite) {
     if (".*".equals(messageRegExp)) {
       return true;
     } else {
@@ -60,35 +33,9 @@ public class ExpectedExceptionsHolder {
     }
   }
 
-  public TestException wrongException(Throwable ite) {
-    if (messageRegExpMatches(ite)) {
-      return new TestException("Expected exception of " +
-                               getExpectedExceptionsPluralize()
-                               + " but got " + ite, ite);
-    } else {
-      return new TestException("The exception was thrown with the wrong message:" +
-                               " expected \"" + messageRegExp + "\"" +
-                               " but got \"" + ite.getMessage() + "\"", ite);
-    }
-  }
-
-  public TestException noException(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();
+  protected String getWrongExceptionMessage(Throwable ite) {
+    return "The exception was thrown with the wrong message:" +
+           " expected \"" + messageRegExp + "\"" +
+           " but got \"" + ite.getMessage() + "\"";
   }
 }

-- 
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