aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/internal/runners/statements
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/junit/internal/runners/statements')
-rw-r--r--src/main/java/org/junit/internal/runners/statements/ExpectException.java38
-rw-r--r--src/main/java/org/junit/internal/runners/statements/Fail.java17
-rw-r--r--src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java71
-rw-r--r--src/main/java/org/junit/internal/runners/statements/InvokeMethod.java22
-rw-r--r--src/main/java/org/junit/internal/runners/statements/RunAfters.java43
-rw-r--r--src/main/java/org/junit/internal/runners/statements/RunBefores.java30
6 files changed, 221 insertions, 0 deletions
diff --git a/src/main/java/org/junit/internal/runners/statements/ExpectException.java b/src/main/java/org/junit/internal/runners/statements/ExpectException.java
new file mode 100644
index 0000000..ddfef07
--- /dev/null
+++ b/src/main/java/org/junit/internal/runners/statements/ExpectException.java
@@ -0,0 +1,38 @@
+/**
+ *
+ */
+package org.junit.internal.runners.statements;
+
+import org.junit.internal.AssumptionViolatedException;
+import org.junit.runners.model.Statement;
+
+public class ExpectException extends Statement {
+ private Statement fNext;
+ private final Class<? extends Throwable> fExpected;
+
+ public ExpectException(Statement next, Class<? extends Throwable> expected) {
+ fNext= next;
+ fExpected= expected;
+ }
+
+ @Override
+ public void evaluate() throws Exception {
+ boolean complete = false;
+ try {
+ fNext.evaluate();
+ complete = true;
+ } catch (AssumptionViolatedException e) {
+ throw e;
+ } catch (Throwable e) {
+ if (!fExpected.isAssignableFrom(e.getClass())) {
+ String message= "Unexpected exception, expected<"
+ + fExpected.getName() + "> but was<"
+ + e.getClass().getName() + ">";
+ throw new Exception(message, e);
+ }
+ }
+ if (complete)
+ throw new AssertionError("Expected exception: "
+ + fExpected.getName());
+ }
+} \ No newline at end of file
diff --git a/src/main/java/org/junit/internal/runners/statements/Fail.java b/src/main/java/org/junit/internal/runners/statements/Fail.java
new file mode 100644
index 0000000..e7d0d5c
--- /dev/null
+++ b/src/main/java/org/junit/internal/runners/statements/Fail.java
@@ -0,0 +1,17 @@
+package org.junit.internal.runners.statements;
+
+import org.junit.runners.model.Statement;
+
+
+public class Fail extends Statement {
+ private final Throwable fError;
+
+ public Fail(Throwable e) {
+ fError= e;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ throw fError;
+ }
+}
diff --git a/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java b/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java
new file mode 100644
index 0000000..bff7c72
--- /dev/null
+++ b/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java
@@ -0,0 +1,71 @@
+/**
+ *
+ */
+package org.junit.internal.runners.statements;
+
+import org.junit.runners.model.Statement;
+
+public class FailOnTimeout extends Statement {
+ private final Statement fOriginalStatement;
+
+ private final long fTimeout;
+
+ public FailOnTimeout(Statement originalStatement, long timeout) {
+ fOriginalStatement= originalStatement;
+ fTimeout= timeout;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ StatementThread thread= evaluateStatement();
+ if (!thread.fFinished)
+ throwExceptionForUnfinishedThread(thread);
+ }
+
+ private StatementThread evaluateStatement() throws InterruptedException {
+ StatementThread thread= new StatementThread(fOriginalStatement);
+ thread.start();
+ thread.join(fTimeout);
+ thread.interrupt();
+ return thread;
+ }
+
+ private void throwExceptionForUnfinishedThread(StatementThread thread)
+ throws Throwable {
+ if (thread.fExceptionThrownByOriginalStatement != null)
+ throw thread.fExceptionThrownByOriginalStatement;
+ else
+ throwTimeoutException(thread);
+ }
+
+ private void throwTimeoutException(StatementThread thread) throws Exception {
+ Exception exception= new Exception(String.format(
+ "test timed out after %d milliseconds", fTimeout));
+ exception.setStackTrace(thread.getStackTrace());
+ throw exception;
+ }
+
+ private static class StatementThread extends Thread {
+ private final Statement fStatement;
+
+ private boolean fFinished= false;
+
+ private Throwable fExceptionThrownByOriginalStatement= null;
+
+ public StatementThread(Statement statement) {
+ fStatement= statement;
+ }
+
+ @Override
+ public void run() {
+ try {
+ fStatement.evaluate();
+ fFinished= true;
+ } catch (InterruptedException e) {
+ //don't log the InterruptedException
+ } catch (Throwable e) {
+ fExceptionThrownByOriginalStatement= e;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/java/org/junit/internal/runners/statements/InvokeMethod.java b/src/main/java/org/junit/internal/runners/statements/InvokeMethod.java
new file mode 100644
index 0000000..e2e81e1
--- /dev/null
+++ b/src/main/java/org/junit/internal/runners/statements/InvokeMethod.java
@@ -0,0 +1,22 @@
+/**
+ *
+ */
+package org.junit.internal.runners.statements;
+
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+
+public class InvokeMethod extends Statement {
+ private final FrameworkMethod fTestMethod;
+ private Object fTarget;
+
+ public InvokeMethod(FrameworkMethod testMethod, Object target) {
+ fTestMethod= testMethod;
+ fTarget= target;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ fTestMethod.invokeExplosively(fTarget);
+ }
+} \ No newline at end of file
diff --git a/src/main/java/org/junit/internal/runners/statements/RunAfters.java b/src/main/java/org/junit/internal/runners/statements/RunAfters.java
new file mode 100644
index 0000000..475ec72
--- /dev/null
+++ b/src/main/java/org/junit/internal/runners/statements/RunAfters.java
@@ -0,0 +1,43 @@
+/**
+ *
+ */
+package org.junit.internal.runners.statements;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.MultipleFailureException;
+import org.junit.runners.model.Statement;
+
+public class RunAfters extends Statement {
+ private final Statement fNext;
+
+ private final Object fTarget;
+
+ private final List<FrameworkMethod> fAfters;
+
+ public RunAfters(Statement next, List<FrameworkMethod> afters, Object target) {
+ fNext= next;
+ fAfters= afters;
+ fTarget= target;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ List<Throwable> errors = new ArrayList<Throwable>();
+ try {
+ fNext.evaluate();
+ } catch (Throwable e) {
+ errors.add(e);
+ } finally {
+ for (FrameworkMethod each : fAfters)
+ try {
+ each.invokeExplosively(fTarget);
+ } catch (Throwable e) {
+ errors.add(e);
+ }
+ }
+ MultipleFailureException.assertEmpty(errors);
+ }
+} \ No newline at end of file
diff --git a/src/main/java/org/junit/internal/runners/statements/RunBefores.java b/src/main/java/org/junit/internal/runners/statements/RunBefores.java
new file mode 100644
index 0000000..66a34e1
--- /dev/null
+++ b/src/main/java/org/junit/internal/runners/statements/RunBefores.java
@@ -0,0 +1,30 @@
+/**
+ *
+ */
+package org.junit.internal.runners.statements;
+
+import java.util.List;
+
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+
+public class RunBefores extends Statement {
+ private final Statement fNext;
+
+ private final Object fTarget;
+
+ private final List<FrameworkMethod> fBefores;
+
+ public RunBefores(Statement next, List<FrameworkMethod> befores, Object target) {
+ fNext= next;
+ fBefores= befores;
+ fTarget= target;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ for (FrameworkMethod before : fBefores)
+ before.invokeExplosively(fTarget);
+ fNext.evaluate();
+ }
+} \ No newline at end of file