You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2015/08/26 01:17:47 UTC
[2/3] incubator-geode git commit: New rules and packages for test
rules
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-core/src/test/java/com/gemstone/gemfire/test/examples/CatchExceptionExampleJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/test/examples/CatchExceptionExampleJUnitTest.java b/gemfire-core/src/test/java/com/gemstone/gemfire/test/examples/CatchExceptionExampleJUnitTest.java
new file mode 100755
index 0000000..abfe0ce
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/test/examples/CatchExceptionExampleJUnitTest.java
@@ -0,0 +1,99 @@
+package com.gemstone.gemfire.test.examples;
+
+import static com.googlecode.catchexception.CatchException.*;
+import static com.googlecode.catchexception.apis.BDDCatchException.when;
+import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.*;
+import static org.assertj.core.api.BDDAssertions.*;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+/**
+ * Simple unit tests exercising Catch-Exception with AssertJ, Hamcrest and JUnit.
+ */
+@Category(UnitTest.class)
+public class CatchExceptionExampleJUnitTest {
+
+ @Test
+ public void catchExceptionShouldCatchException() {
+ List<?> myList = new ArrayList<Object>();
+
+ // when: we try to get the first element of the list
+ // then: catch the exception if any is thrown
+ catchException(myList).get(1);
+
+ // then: we expect an IndexOutOfBoundsException
+ assertThat(caughtException(), is(instanceOf(IndexOutOfBoundsException.class)));
+ }
+
+ @Test
+ public void verifyExceptionShouldCatchException() {
+ List<?> myList = new ArrayList<Object>();
+
+ // when: we try to get the first element of the list
+ // then: catch the exception if any is thrown
+ // then: we expect an IndexOutOfBoundsException
+ verifyException(myList, IndexOutOfBoundsException.class).get(1);
+ }
+
+ @Test
+ public void whenShouldCatchExceptionAndUseAssertJAssertion() {
+ // given: an empty list
+ List<?> myList = new ArrayList<Object>();
+
+ // when: we try to get the first element of the list
+ when(myList).get(1);
+
+ // then: we expect an IndexOutOfBoundsException
+ then(caughtException())
+ .isInstanceOf(IndexOutOfBoundsException.class)
+ .hasMessage("Index: 1, Size: 0")
+ .hasNoCause();
+ }
+
+ @Test
+ public void catchExceptionShouldCatchExceptionAndUseHamcrestAssertion() {
+ // given: an empty list
+ List<?> myList = new ArrayList<Object>();
+
+ // when: we try to get the first element of the list
+ catchException(myList).get(1);
+
+ // then: we expect an IndexOutOfBoundsException with message "Index: 1, Size: 0"
+ assertThat(caughtException(),
+ allOf(
+ instanceOf(IndexOutOfBoundsException.class),
+ hasMessage("Index: 1, Size: 0"),
+ hasNoCause()
+ )
+ );
+ }
+
+ @Test
+ public void shouldCatchFromThrowException() throws Exception {
+ String message = "error message";
+
+ catchException(this).throwException(message);
+
+ assertThat(caughtException(), is(instanceOf(Exception.class)));
+ }
+
+ @Test
+ public void shouldVerifyFromThrowException() throws Exception {
+ String message = "error message";
+
+ verifyException(this).throwException(message);
+ }
+
+ // fails if private
+ protected void throwException(final String message) throws Exception {
+ throw new Exception(message);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/IgnoreUntil.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/IgnoreUntil.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/IgnoreUntil.java
index 66ab929..0074f56 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/IgnoreUntil.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/IgnoreUntil.java
@@ -26,7 +26,7 @@ public @interface IgnoreUntil {
Class<? extends IgnoreCondition> condition() default DefaultIgnoreCondition.class;
- String deadline() default "1970-01-01";
+ String until() default "1970-01-01";
String value() default "";
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/Repeat.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/Repeat.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/Repeat.java
index b76d160..88e48c3 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/Repeat.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/Repeat.java
@@ -23,4 +23,5 @@ public @interface Repeat {
int value() default DEFAULT;
+ String property() default "";
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/IgnoreUntilRule.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/IgnoreUntilRule.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/IgnoreUntilRule.java
index fd35ad7..0f4b99d 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/IgnoreUntilRule.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/IgnoreUntilRule.java
@@ -81,7 +81,7 @@ public class IgnoreUntilRule implements TestRule {
protected boolean evaluate(IgnoreUntil conditionalIgnoreAnnotation, Description description) {
return (evaluateCondition(conditionalIgnoreAnnotation.condition(), description)
- || evaluateUntil(conditionalIgnoreAnnotation.deadline()));
+ || evaluateUntil(conditionalIgnoreAnnotation.until()));
}
protected boolean evaluateCondition(Class<? extends IgnoreCondition> ignoreConditionType, Description description) {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/RepeatRule.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/RepeatRule.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/RepeatRule.java
index ef66d48..8da22c4 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/RepeatRule.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/RepeatRule.java
@@ -44,7 +44,15 @@ public class RepeatRule implements TestRule {
int repetitions = DEFAULT_REPETITIONS;
if (repeat != null) {
- repetitions = repeat.value();
+ if (!"".equals(repeat.property())) {
+ repetitions = Integer.getInteger(repeat.property(), DEFAULT_REPETITIONS);
+ } else {
+ repetitions = repeat.value();
+ }
+ }
+
+ if (repetitions < 1) {
+ throw new IllegalArgumentException("Repeat value must be a positive integer");
}
return repetitions;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableExternalResource.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableExternalResource.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableExternalResource.java
new file mode 100755
index 0000000..5cc439c
--- /dev/null
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableExternalResource.java
@@ -0,0 +1,51 @@
+package com.gemstone.gemfire.test.junit.rules;
+
+import java.io.Serializable;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * Serializable version of ExternalResource JUnit Rule. JUnit lifecycle is not
+ * executed in remote JVMs. The after() callback has throws-clause that matches
+ * before().
+ *
+ * @author Kirk Lund
+ */
+@SuppressWarnings("serial")
+public abstract class SerializableExternalResource implements Serializable, TestRule {
+ public Statement apply(Statement base, Description description) {
+ return statement(base);
+ }
+
+ private Statement statement(final Statement base) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ before();
+ try {
+ base.evaluate();
+ } finally {
+ after();
+ }
+ }
+ };
+ }
+
+ /**
+ * Override to set up your specific external resource.
+ *
+ * @throws Throwable if setup fails (which will disable {@code after}
+ */
+ protected void before() throws Throwable {
+ // do nothing
+ }
+
+ /**
+ * Override to tear down your specific external resource.
+ */
+ protected void after() throws Throwable { // ExternalResource is missing this throws-clause
+ // do nothing
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTemporaryFolder.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTemporaryFolder.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTemporaryFolder.java
index 8f22629..ea335eb 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTemporaryFolder.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTemporaryFolder.java
@@ -8,6 +8,12 @@ import java.lang.reflect.Field;
import org.junit.rules.TemporaryFolder;
+/**
+ * Serializable version of TemporaryFolder JUnit Rule. JUnit lifecycle is not
+ * executed in remote JVMs.
+ *
+ * @author Kirk Lund
+ */
@SuppressWarnings("serial")
public class SerializableTemporaryFolder extends TemporaryFolder implements Serializable {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestName.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestName.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestName.java
index 38da244..d82d0d5 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestName.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestName.java
@@ -7,6 +7,12 @@ import java.lang.reflect.Field;
import org.junit.rules.TestName;
+/**
+ * Serializable version of TestName JUnit Rule. JUnit lifecycle is not
+ * executed in remote JVMs.
+ *
+ * @author Kirk Lund
+ */
@SuppressWarnings("serial")
public class SerializableTestName extends TestName implements Serializable {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestWatcher.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestWatcher.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestWatcher.java
index e873b41..0992fe9 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestWatcher.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestWatcher.java
@@ -4,6 +4,12 @@ import java.io.Serializable;
import org.junit.rules.TestWatcher;
+/**
+ * Serializable version of TestWatcher JUnit Rule. JUnit lifecycle is not
+ * executed in remote JVMs.
+ *
+ * @author Kirk Lund
+ */
@SuppressWarnings("serial")
public class SerializableTestWatcher extends TestWatcher implements Serializable {
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTimeout.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTimeout.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTimeout.java
index 45bd863..814ecea 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTimeout.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTimeout.java
@@ -10,6 +10,12 @@ import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;
import org.junit.rules.Timeout;
+/**
+ * Serializable version of Timeout JUnit Rule. JUnit lifecycle is not
+ * executed in remote JVMs.
+ *
+ * @author Kirk Lund
+ */
@SuppressWarnings("serial")
public class SerializableTimeout extends Timeout implements Serializable {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RepeatingTestCasesExampleJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RepeatingTestCasesExampleJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RepeatingTestCasesExampleJUnitTest.java
new file mode 100755
index 0000000..1e53199
--- /dev/null
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RepeatingTestCasesExampleJUnitTest.java
@@ -0,0 +1,75 @@
+package com.gemstone.gemfire.test.junit.rules.examples;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.gemstone.gemfire.test.junit.Repeat;
+import com.gemstone.gemfire.test.junit.rules.RepeatRule;
+
+/**
+ * The RepeatingTestCasesExampleTest class is a test suite of test cases testing the contract and functionality
+ * of the JUnit @Repeat annotation on a test suite class test case methods.
+ *
+ * @author John Blum
+ * @see org.junit.Test
+ * @see com.gemstone.gemfire.test.junit.Repeat
+ * @see com.gemstone.gemfire.test.junit.rules.RepeatRule
+ */
+public class RepeatingTestCasesExampleJUnitTest {
+
+ private static AtomicInteger repeatOnceCounter = new AtomicInteger(0);
+ private static AtomicInteger repeatOnlyOnceCounter = new AtomicInteger(0);
+ private static AtomicInteger repeatTenTimesCounter = new AtomicInteger(0);
+ private static AtomicInteger repeatTwiceCounter = new AtomicInteger(0);
+
+ @Rule
+ public RepeatRule repeatRule = new RepeatRule();
+
+ @BeforeClass
+ public static void setupBeforeClass() {
+ System.setProperty("tdd.example.test.case.two.repetitions", "2");
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() {
+ assertThat(repeatOnceCounter.get(), is(equalTo(1)));
+ assertThat(repeatOnlyOnceCounter.get(), is(equalTo(1)));
+ assertThat(repeatTenTimesCounter.get(), is(equalTo(10)));
+ assertThat(repeatTwiceCounter.get(), is(equalTo(2)));
+ }
+
+ @Test
+ @Repeat
+ public void repeatOnce() {
+ repeatOnceCounter.incrementAndGet();
+ assertThat(repeatOnceCounter.get() <= 1, is(true));
+ }
+
+ @Test
+ @Repeat(property = "tdd.example.test.case.with.non-existing.system.property")
+ public void repeatOnlyOnce() {
+ repeatOnlyOnceCounter.incrementAndGet();
+ assertThat(repeatOnlyOnceCounter.get() <= 1, is(true));
+ }
+
+ @Test
+ @Repeat(10)
+ public void repeatTenTimes() {
+ repeatTenTimesCounter.incrementAndGet();
+ assertThat(repeatTenTimesCounter.get() <= 10, is(true));
+ }
+
+ @Test
+ @Repeat(property = "tdd.example.test.case.two.repetitions")
+ public void repeatTwiceCounter() {
+ repeatTwiceCounter.incrementAndGet();
+ assertThat(repeatTwiceCounter.get() <= 2, is(true));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RetryRuleExampleJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RetryRuleExampleJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RetryRuleExampleJUnitTest.java
new file mode 100755
index 0000000..6ce4b94
--- /dev/null
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/examples/RetryRuleExampleJUnitTest.java
@@ -0,0 +1,28 @@
+package com.gemstone.gemfire.test.junit.rules.examples;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+import com.gemstone.gemfire.test.junit.rules.RetryRule;
+
+@Category(UnitTest.class)
+public class RetryRuleExampleJUnitTest {
+
+ @Rule
+ public final transient RetryRule retry = new RetryRule(2);
+
+ private static int count = 0;
+
+ @Test
+ public void unreliableTestWithRaceConditions() {
+ count++;
+ if (count < 2) {
+ assertThat(count, is(2)); // doomed to fail
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/ExpectedTimeoutRuleJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/ExpectedTimeoutRuleJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/ExpectedTimeoutRuleJUnitTest.java
index 8a14f67..27aa258 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/ExpectedTimeoutRuleJUnitTest.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/ExpectedTimeoutRuleJUnitTest.java
@@ -1,5 +1,6 @@
package com.gemstone.gemfire.test.junit.rules.tests;
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
import static org.hamcrest.core.StringContains.*;
import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsInstanceOf.*;
@@ -12,8 +13,6 @@ import java.util.concurrent.TimeoutException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.junit.runner.JUnitCore;
-import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -113,11 +112,6 @@ public class ExpectedTimeoutRuleJUnitTest {
assertThat(failure.getException().getMessage(), containsString("Expected test to throw (an instance of " + TimeoutException.class.getName() + " and exception with message a string containing \"" + FailsWhenTimeoutIsLate.message + "\")"));
}
- private static Result runTest(Class<?> test) {
- JUnitCore junitCore = new JUnitCore();
- return junitCore.run(Request.aClass(test).getRunner());
- }
-
public static class AbstractExpectedTimeoutRuleTest {
@Rule
public ExpectedTimeoutRule timeout = ExpectedTimeoutRule.none();
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/IgnoreUntilRuleJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/IgnoreUntilRuleJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/IgnoreUntilRuleJUnitTest.java
new file mode 100755
index 0000000..80724bb
--- /dev/null
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/IgnoreUntilRuleJUnitTest.java
@@ -0,0 +1,110 @@
+package com.gemstone.gemfire.test.junit.rules.tests;
+
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.hamcrest.core.StringContains.containsString;
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
+
+import com.gemstone.gemfire.test.junit.IgnoreUntil;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+import com.gemstone.gemfire.test.junit.rules.IgnoreUntilRule;
+
+/**
+ * Unit tests for IgnoreUntil JUnit Rule
+ *
+ * @author Kirk Lund
+ */
+@Category(UnitTest.class)
+public class IgnoreUntilRuleJUnitTest {
+
+ private static final String ASSERTION_ERROR_MESSAGE = "failing test";
+
+ @Test
+ public void shouldIgnoreWhenUntilIsInFuture() {
+ Result result = runTest(ShouldIgnoreWhenUntilIsInFuture.class);
+
+ assertTrue(result.wasSuccessful());
+ assertThat(ShouldIgnoreWhenUntilIsInFuture.count, is(0));
+ }
+
+ @Test
+ public void shouldExecuteWhenUntilIsInPast() {
+ Result result = runTest(ShouldExecuteWhenUntilIsInPast.class);
+
+ assertFalse(result.wasSuccessful());
+
+ List<Failure> failures = result.getFailures();
+ assertEquals("Failures: " + failures, 1, failures.size());
+
+ Failure failure = failures.get(0);
+ assertThat(failure.getException(), is(instanceOf(AssertionError.class)));
+ assertThat(failure.getException().getMessage(), containsString(ASSERTION_ERROR_MESSAGE));
+ assertThat(ShouldExecuteWhenUntilIsInPast.count, is(1));
+ }
+
+ @Test
+ public void shouldExecuteWhenUntilIsDefault() {
+ Result result = runTest(ShouldExecuteWhenUntilIsDefault.class);
+
+ assertFalse(result.wasSuccessful());
+
+ List<Failure> failures = result.getFailures();
+ assertEquals("Failures: " + failures, 1, failures.size());
+
+ Failure failure = failures.get(0);
+ assertThat(failure.getException(), is(instanceOf(AssertionError.class)));
+ assertThat(failure.getException().getMessage(), containsString(ASSERTION_ERROR_MESSAGE));
+ assertThat(ShouldExecuteWhenUntilIsDefault.count, is(1));
+ }
+
+ public static class ShouldIgnoreWhenUntilIsInFuture {
+ private static int count;
+
+ @Rule
+ public final IgnoreUntilRule ignoreUntilRule = new IgnoreUntilRule();
+
+ @Test
+ @IgnoreUntil(value = "description", until = "3000-01-01")
+ public void doTest() throws Exception {
+ count++;
+ fail(ASSERTION_ERROR_MESSAGE);
+ }
+ }
+
+ public static class ShouldExecuteWhenUntilIsInPast {
+ private static int count;
+
+ @Rule
+ public final IgnoreUntilRule ignoreUntilRule = new IgnoreUntilRule();
+
+ @Test
+ @IgnoreUntil(value = "description", until = "1980-01-01")
+ public void doTest() throws Exception {
+ count++;
+ fail(ASSERTION_ERROR_MESSAGE);
+ }
+ }
+
+ public static class ShouldExecuteWhenUntilIsDefault {
+ private static int count;
+
+ @Rule
+ public final IgnoreUntilRule ignoreUntilRule = new IgnoreUntilRule();
+
+ @Test
+ @IgnoreUntil(value = "description")
+ public void doTest() throws Exception {
+ count++;
+ fail(ASSERTION_ERROR_MESSAGE);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RepeatRuleJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RepeatRuleJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RepeatRuleJUnitTest.java
index 5e69f03..749b81b 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RepeatRuleJUnitTest.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RepeatRuleJUnitTest.java
@@ -1,5 +1,6 @@
package com.gemstone.gemfire.test.junit.rules.tests;
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
@@ -11,8 +12,6 @@ import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.junit.runner.JUnitCore;
-import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -54,19 +53,33 @@ public class RepeatRuleJUnitTest {
}
@Test
- public void failingTestShouldBeSkippedWhenRepeatIsZero() {
- Result result = runTest(FailingTestShouldBeSkippedWhenRepeatIsZero.class);
+ public void zeroValueShouldThrowIllegalArgumentException() {
+ Result result = runTest(ZeroValueShouldThrowIllegalArgumentException.class);
- assertTrue(result.wasSuccessful());
- assertThat(FailingTestShouldBeSkippedWhenRepeatIsZero.count, is(0));
- }
+ assertFalse(result.wasSuccessful());
+
+ List<Failure> failures = result.getFailures();
+ assertEquals("Failures: " + failures, 1, failures.size());
+ Failure failure = failures.get(0);
+ assertThat(failure.getException(), is(instanceOf(IllegalArgumentException.class)));
+ assertThat(failure.getException().getMessage(), containsString("Repeat value must be a positive integer"));
+ assertThat(ZeroValueShouldThrowIllegalArgumentException.count, is(0));
+ }
+
@Test
- public void passingTestShouldBeSkippedWhenRepeatIsZero() {
- Result result = runTest(PassingTestShouldBeSkippedWhenRepeatIsZero.class);
+ public void negativeValueShouldThrowIllegalArgumentException() {
+ Result result = runTest(NegativeValueShouldThrowIllegalArgumentException.class);
- assertTrue(result.wasSuccessful());
- assertThat(PassingTestShouldBeSkippedWhenRepeatIsZero.count, is(0));
+ assertFalse(result.wasSuccessful());
+
+ List<Failure> failures = result.getFailures();
+ assertEquals("Failures: " + failures, 1, failures.size());
+
+ Failure failure = failures.get(0);
+ assertThat(failure.getException(), is(instanceOf(IllegalArgumentException.class)));
+ assertThat(failure.getException().getMessage(), containsString("Repeat value must be a positive integer"));
+ assertThat(NegativeValueShouldThrowIllegalArgumentException.count, is(0));
}
@Test
@@ -138,11 +151,6 @@ public class RepeatRuleJUnitTest {
assertThat(PassingTestShouldPassThreeTimesWhenRepeatIsThree.count, is(3));
}
- private static Result runTest(Class<?> test) {
- JUnitCore junitCore = new JUnitCore();
- return junitCore.run(Request.aClass(test).getRunner());
- }
-
public static class FailingTestShouldFailOneTimeWhenRepeatIsUnused {
protected static int count = 0;
@@ -168,7 +176,7 @@ public class RepeatRuleJUnitTest {
}
}
- public static class FailingTestShouldBeSkippedWhenRepeatIsZero {
+ public static class ZeroValueShouldThrowIllegalArgumentException {
protected static int count = 0;
@Rule
@@ -178,7 +186,19 @@ public class RepeatRuleJUnitTest {
@Repeat(0)
public void doTest() throws Exception {
count++;
- fail(ASSERTION_ERROR_MESSAGE);
+ }
+ }
+
+ public static class NegativeValueShouldThrowIllegalArgumentException {
+ protected static int count = 0;
+
+ @Rule
+ public RepeatRule repeat = new RepeatRule();
+
+ @Test
+ @Repeat(-1)
+ public void doTest() throws Exception {
+ count++;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithErrorJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithErrorJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithErrorJUnitTest.java
index ce473e8..24d636b 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithErrorJUnitTest.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithErrorJUnitTest.java
@@ -1,5 +1,6 @@
package com.gemstone.gemfire.test.junit.rules.tests;
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.StringContains.containsString;
@@ -14,8 +15,6 @@ import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.junit.runner.JUnitCore;
-import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -122,11 +121,6 @@ public class RetryRuleGlobalWithErrorJUnitTest {
assertThat(PassesOnThirdAttempt.count, is(3));
}
- private static Result runTest(Class<?> test) {
- JUnitCore junitCore = new JUnitCore();
- return junitCore.run(Request.aClass(test).getRunner());
- }
-
public static class ZeroIsIllegal {
protected static int count;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithExceptionJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithExceptionJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithExceptionJUnitTest.java
index b67880c..b2a5840 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithExceptionJUnitTest.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleGlobalWithExceptionJUnitTest.java
@@ -1,5 +1,6 @@
package com.gemstone.gemfire.test.junit.rules.tests;
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.StringContains.containsString;
@@ -13,8 +14,6 @@ import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.junit.runner.JUnitCore;
-import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -121,11 +120,6 @@ public class RetryRuleGlobalWithExceptionJUnitTest {
assertThat(PassesOnThirdAttempt.count, is(3));
}
- private static Result runTest(Class<?> test) {
- JUnitCore junitCore = new JUnitCore();
- return junitCore.run(Request.aClass(test).getRunner());
- }
-
public static class CustomException extends Exception {
private static final long serialVersionUID = 1L;
public CustomException(final String message) {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithErrorJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithErrorJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithErrorJUnitTest.java
index b06575c..07a894c 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithErrorJUnitTest.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithErrorJUnitTest.java
@@ -1,5 +1,6 @@
package com.gemstone.gemfire.test.junit.rules.tests;
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.StringContains.containsString;
@@ -14,8 +15,6 @@ import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.junit.runner.JUnitCore;
-import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
@@ -101,11 +100,6 @@ public class RetryRuleLocalWithErrorJUnitTest {
assertThat(PassesOnThirdAttempt.count, is(3));
}
- private static Result runTest(Class<?> test) {
- JUnitCore junitCore = new JUnitCore();
- return junitCore.run(Request.aClass(test).getRunner());
- }
-
public static class FailsUnused {
protected static int count;
protected static String message;
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithExceptionJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithExceptionJUnitTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithExceptionJUnitTest.java
index 4cff4bb..46fd16c 100755
--- a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithExceptionJUnitTest.java
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RetryRuleLocalWithExceptionJUnitTest.java
@@ -1,5 +1,6 @@
package com.gemstone.gemfire.test.junit.rules.tests;
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.StringContains.containsString;
@@ -7,29 +8,18 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.List;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
-import org.junit.runner.JUnitCore;
-import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import com.gemstone.gemfire.test.junit.Retry;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
import com.gemstone.gemfire.test.junit.rules.RetryRule;
-import com.gemstone.gemfire.test.junit.rules.tests.RetryRuleLocalWithErrorJUnitTest.FailsOnSecondAttempt;
-import com.gemstone.gemfire.test.junit.rules.tests.RetryRuleLocalWithErrorJUnitTest.FailsOnThirdAttempt;
-import com.gemstone.gemfire.test.junit.rules.tests.RetryRuleLocalWithErrorJUnitTest.FailsUnused;
-import com.gemstone.gemfire.test.junit.rules.tests.RetryRuleLocalWithErrorJUnitTest.PassesOnSecondAttempt;
-import com.gemstone.gemfire.test.junit.rules.tests.RetryRuleLocalWithErrorJUnitTest.PassesOnThirdAttempt;
-import com.gemstone.gemfire.test.junit.rules.tests.RetryRuleLocalWithErrorJUnitTest.PassesUnused;
/**
* Unit tests for Retry JUnit Rule involving local scope (ie Rule affects
@@ -109,11 +99,6 @@ public class RetryRuleLocalWithExceptionJUnitTest {
assertThat(PassesOnThirdAttempt.count, is(3));
}
- private static Result runTest(Class<?> test) {
- JUnitCore junitCore = new JUnitCore();
- return junitCore.run(Request.aClass(test).getRunner());
- }
-
public static class CustomException extends Exception {
private static final long serialVersionUID = 1L;
public CustomException(final String message) {
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/89c0b437/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RunTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RunTest.java b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RunTest.java
new file mode 100755
index 0000000..7e286a6
--- /dev/null
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RunTest.java
@@ -0,0 +1,21 @@
+package com.gemstone.gemfire.test.junit.rules.tests;
+
+import org.junit.runner.JUnitCore;
+import org.junit.runner.Request;
+import org.junit.runner.Result;
+
+/**
+ * Used by Rule Unit Tests to execute Test Cases.
+ *
+ * @author Kirk Lund
+ */
+public class RunTest {
+
+ protected RunTest() {
+ }
+
+ public static Result runTest(Class<?> test) {
+ JUnitCore junitCore = new JUnitCore();
+ return junitCore.run(Request.aClass(test).getRunner());
+ }
+}