[testng] 83/160: Use composition instead of inheritance
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 3b2881b877fe7515d1f774f2f06755ceb56a09e7
Author: Julien Herr <julien.herr at alcatel-lucent.com>
Date: Fri Jun 5 01:22:43 2015 +0200
Use composition instead of inheritance
---
.../java/org/testng/IExpectedExceptionsHolder.java | 20 ++++
.../internal/AbstractExpectedExceptionsHolder.java | 108 -------------------
.../testng/internal/ExpectedExceptionsHolder.java | 117 +++++++++++++++------
src/main/java/org/testng/internal/Invoker.java | 12 ++-
...er.java => RegexpExpectedExceptionsHolder.java} | 15 ++-
5 files changed, 120 insertions(+), 152 deletions(-)
diff --git a/src/main/java/org/testng/IExpectedExceptionsHolder.java b/src/main/java/org/testng/IExpectedExceptionsHolder.java
new file mode 100644
index 0000000..18a174b
--- /dev/null
+++ b/src/main/java/org/testng/IExpectedExceptionsHolder.java
@@ -0,0 +1,20 @@
+package org.testng;
+
+public interface IExpectedExceptionsHolder {
+
+ /**
+ * Get the message in case the Throwable thrown by the test is not matching.
+ *
+ * @param ite The Throwable thrown by the test
+ * @return The message which will be displayed as test result
+ */
+ String getWrongExceptionMessage(Throwable ite);
+
+ /**
+ * Check if the Throwable thrown by the test is matching with the holder logic
+ *
+ * @param ite The Throwable thrown by the test
+ * @return true if the Throwable is matching with the holder logic, false otherwise
+ */
+ boolean isThrowableMatching(Throwable ite);
+}
diff --git a/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java b/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
deleted file mode 100644
index 0fa7260..0000000
--- a/src/main/java/org/testng/internal/AbstractExpectedExceptionsHolder.java
+++ /dev/null
@@ -1,108 +0,0 @@
-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(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();
- }
-
- // New syntax
- ITestAnnotation testAnnotation = finder.findAnnotation(method, ITestAnnotation.class);
- if (testAnnotation != null) {
- return testAnnotation.getExpectedExceptions();
- }
-
- return new Class<?>[0];
- }
-
- /**
- * @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 (!hasExpectedClasses()) {
- 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) && isExceptionMatches(ite)) {
- return true;
- }
- }
-
- return false;
- }
-
- public Throwable wrongException(Throwable ite) {
- if (!hasExpectedClasses()) {
- return ite;
- }
-
- if (isExceptionMatches(ite)) {
- return new TestException("Expected exception of " +
- getExpectedExceptionsPluralize()
- + " but got " + ite, ite);
- } else {
- return new TestException(getWrongExceptionMessage(ite), ite);
- }
- }
-
- protected abstract String getWrongExceptionMessage(Throwable ite);
-
- public TestException noException(ITestNGMethod testMethod) {
- if (!hasExpectedClasses()) {
- return null;
- }
- return new TestException("Method " + testMethod + " should have thrown an exception of "
- + getExpectedExceptionsPluralize());
- }
-
- private boolean hasExpectedClasses() {
- return expectedClasses != null && expectedClasses.length > 0;
- }
-
- 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 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
old mode 100755
new mode 100644
index 4c1a82d..2e08ae7
--- a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
+++ b/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
@@ -1,60 +1,107 @@
package org.testng.internal;
+import org.testng.IExpectedExceptionsHolder;
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.regex.Pattern;
+import java.util.Arrays;
-/**
- * A class that contains the expected exceptions and the message regular expression.
- * @author cbeust
- */
-public class ExpectedExceptionsHolder extends AbstractExpectedExceptionsHolder {
- public static final String DEFAULT_REGEXP = ".*";
+public class ExpectedExceptionsHolder {
- public ExpectedExceptionsHolder(IAnnotationFinder finder, ITestNGMethod method) {
- super(finder, method);
+ protected final IAnnotationFinder finder;
+ protected final ITestNGMethod method;
+ private final Class<?>[] expectedClasses;
+ private final IExpectedExceptionsHolder holder;
+
+ protected ExpectedExceptionsHolder(IAnnotationFinder finder, ITestNGMethod method, IExpectedExceptionsHolder holder) {
+ this.finder = finder;
+ this.method = method;
+ expectedClasses = findExpectedClasses(finder, method);
+ this.holder = holder;
+ }
+
+ private static Class<?>[] findExpectedClasses(IAnnotationFinder finder, ITestNGMethod method) {
+ IExpectedExceptionsAnnotation expectedExceptions =
+ finder.findAnnotation(method, IExpectedExceptionsAnnotation.class);
+ // Old syntax
+ if (expectedExceptions != null) {
+ return expectedExceptions.getValue();
+ }
+
+ // New syntax
+ ITestAnnotation testAnnotation = finder.findAnnotation(method, ITestAnnotation.class);
+ if (testAnnotation != null) {
+ return testAnnotation.getExpectedExceptions();
+ }
+
+ return new Class<?>[0];
}
/**
- * message / regEx .* other
- * null true false
- * non-null true match
+ * @param ite The exception that was just thrown
+ * @return true if the exception that was just thrown is part of the
+ * expected exceptions
*/
- @Override
- protected boolean isExceptionMatches(Throwable ite) {
- String messageRegExp = getRegExp();
+ public boolean isExpectedException(Throwable ite) {
+ if (!hasExpectedClasses()) {
+ return false;
+ }
- if (DEFAULT_REGEXP.equals(messageRegExp)) {
- return true;
+ // 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;
}
- final String message = ite.getMessage();
- return message != null && Pattern.compile(messageRegExp, Pattern.DOTALL).matcher(message).matches();
- }
+ Class<?> realExceptionClass= ite.getClass();
- protected String getWrongExceptionMessage(Throwable ite) {
- return "The exception was thrown with the wrong message:" +
- " expected \"" + getRegExp() + "\"" +
- " but got \"" + ite.getMessage() + "\"";
+ for (Class<?> exception : expectedClasses) {
+ if (exception.isAssignableFrom(realExceptionClass) && holder.isThrowableMatching(ite)) {
+ return true;
+ }
+ }
+
+ return false;
}
- private String getRegExp() {
- IExpectedExceptionsAnnotation expectedExceptions =
- finder.findAnnotation(method, IExpectedExceptionsAnnotation.class);
- if (expectedExceptions != null) {
- // Old syntax => default value
- return DEFAULT_REGEXP;
+ public Throwable wrongException(Throwable ite) {
+ if (!hasExpectedClasses()) {
+ return ite;
}
- // New syntax
- ITestAnnotation testAnnotation = finder.findAnnotation(method, ITestAnnotation.class);
- if (testAnnotation != null) {
- return testAnnotation.getExpectedExceptionsMessageRegExp();
+ if (holder.isThrowableMatching(ite)) {
+ return new TestException("Expected exception of " +
+ getExpectedExceptionsPluralize()
+ + " but got " + ite, ite);
+ } else {
+ return new TestException(holder.getWrongExceptionMessage(ite), ite);
}
+ }
+
+ public TestException noException(ITestNGMethod testMethod) {
+ if (!hasExpectedClasses()) {
+ return null;
+ }
+ return new TestException("Method " + testMethod + " should have thrown an exception of "
+ + getExpectedExceptionsPluralize());
+ }
- return DEFAULT_REGEXP;
+ private boolean hasExpectedClasses() {
+ return expectedClasses != null && expectedClasses.length > 0;
+ }
+
+ 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 921daaf..9fa4d57 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
- = new ExpectedExceptionsHolder(m_annotationFinder, tm);
+ = new ExpectedExceptionsHolder(m_annotationFinder, tm, new RegexpExpectedExceptionsHolder(m_annotationFinder, tm));
List<ITestResult> results = Lists.<ITestResult>newArrayList(testResult);
handleInvocationResults(tm, results, expectedExceptionClasses, false,
false /* collect results */, failureContext);
@@ -830,7 +830,8 @@ public class Invoker implements IInvoker {
tm.setId(ThreadUtil.currentThreadInfo());
ITestResult result = invokeMethod(instance, tm, parameterValues, parametersIndex, suite, params,
- testClass, beforeMethods, afterMethods, groupMethods, failureContext);
+ testClass, beforeMethods, afterMethods, groupMethods,
+ failureContext);
return result;
}
@@ -1069,7 +1070,9 @@ public class Invoker implements IInvoker {
int invocationCount = onlyOne ? 1 : testMethod.getInvocationCount();
- ExpectedExceptionsHolder expectedExceptionHolder = new ExpectedExceptionsHolder(m_annotationFinder, testMethod);
+ ExpectedExceptionsHolder expectedExceptionHolder =
+ new ExpectedExceptionsHolder(m_annotationFinder, testMethod,
+ new RegexpExpectedExceptionsHolder(m_annotationFinder, testMethod));
final ITestClass testClass= testMethod.getTestClass();
final List<ITestResult> result = Lists.newArrayList();
final FailureContext failure = new FailureContext();
@@ -1338,7 +1341,8 @@ public class Invoker implements IInvoker {
testContext));
}
- return runWorkers(testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite, parameters);
+ return runWorkers(testMethod, workers, testMethod.getThreadPoolSize(), groupMethods, suite,
+ parameters);
}
static class FailureContext {
diff --git a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java b/src/main/java/org/testng/internal/RegexpExpectedExceptionsHolder.java
similarity index 76%
copy from src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
copy to src/main/java/org/testng/internal/RegexpExpectedExceptionsHolder.java
index 4c1a82d..f1f1e93 100755
--- a/src/main/java/org/testng/internal/ExpectedExceptionsHolder.java
+++ b/src/main/java/org/testng/internal/RegexpExpectedExceptionsHolder.java
@@ -1,5 +1,6 @@
package org.testng.internal;
+import org.testng.IExpectedExceptionsHolder;
import org.testng.ITestNGMethod;
import org.testng.annotations.IExpectedExceptionsAnnotation;
import org.testng.annotations.ITestAnnotation;
@@ -11,11 +12,15 @@ import java.util.regex.Pattern;
* A class that contains the expected exceptions and the message regular expression.
* @author cbeust
*/
-public class ExpectedExceptionsHolder extends AbstractExpectedExceptionsHolder {
+public class RegexpExpectedExceptionsHolder implements IExpectedExceptionsHolder {
public static final String DEFAULT_REGEXP = ".*";
- public ExpectedExceptionsHolder(IAnnotationFinder finder, ITestNGMethod method) {
- super(finder, method);
+ private final IAnnotationFinder finder;
+ private final ITestNGMethod method;
+
+ public RegexpExpectedExceptionsHolder(IAnnotationFinder finder, ITestNGMethod method) {
+ this.finder = finder;
+ this.method = method;
}
/**
@@ -24,7 +29,7 @@ public class ExpectedExceptionsHolder extends AbstractExpectedExceptionsHolder {
* non-null true match
*/
@Override
- protected boolean isExceptionMatches(Throwable ite) {
+ public boolean isThrowableMatching(Throwable ite) {
String messageRegExp = getRegExp();
if (DEFAULT_REGEXP.equals(messageRegExp)) {
@@ -35,7 +40,7 @@ public class ExpectedExceptionsHolder extends AbstractExpectedExceptionsHolder {
return message != null && Pattern.compile(messageRegExp, Pattern.DOTALL).matcher(message).matches();
}
- protected String getWrongExceptionMessage(Throwable ite) {
+ public String getWrongExceptionMessage(Throwable ite) {
return "The exception was thrown with the wrong message:" +
" expected \"" + getRegExp() + "\"" +
" 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