geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject [1/2] incubator-geode git commit: Latest changes for using DUnitTestRule.
Date Mon, 14 Sep 2015 17:35:38 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/feature/GEODE-217 7c27657a0 -> 435dc63c1


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/435dc63c/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestRule.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestRule.java
b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestRule.java
new file mode 100755
index 0000000..3d72b17
--- /dev/null
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/SerializableTestRule.java
@@ -0,0 +1,17 @@
+package com.gemstone.gemfire.test.junit.rules;
+
+import java.io.Serializable;
+
+import org.junit.rules.TestRule;
+
+/**
+ * Serializable version of JUnit TestRule. JUnit lifecycle is not
+ * executed in remote JVMs.
+ * 
+ * The simplest way to satisfy this interface is to apply <tt>transient</tt>
+ * to every instance field.
+ * 
+ * @author Kirk Lund
+ */
+public interface SerializableTestRule extends Serializable, TestRule {
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/435dc63c/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 0992fe9..e0af5c0 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
@@ -1,7 +1,5 @@
 package com.gemstone.gemfire.test.junit.rules;
 
-import java.io.Serializable;
-
 import org.junit.rules.TestWatcher;
 
 /**
@@ -11,5 +9,5 @@ import org.junit.rules.TestWatcher;
  * @author Kirk Lund
  */
 @SuppressWarnings("serial")
-public class SerializableTestWatcher extends TestWatcher implements Serializable {
+public class SerializableTestWatcher extends TestWatcher implements SerializableTestRule
{
 }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/435dc63c/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 814ecea..e2b0ce2 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
@@ -2,7 +2,6 @@ package com.gemstone.gemfire.test.junit.rules;
 
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.io.Serializable;
 import java.lang.reflect.Field;
 import java.util.concurrent.TimeUnit;
 
@@ -17,7 +16,7 @@ import org.junit.rules.Timeout;
  * @author Kirk Lund
  */
 @SuppressWarnings("serial")
-public class SerializableTimeout extends Timeout implements Serializable {
+public class SerializableTimeout extends Timeout implements SerializableTestRule {
 
   public static Builder builder() {
     return new Builder();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/435dc63c/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RuleAndClassRuleJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RuleAndClassRuleJUnitTest.java
b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RuleAndClassRuleJUnitTest.java
new file mode 100755
index 0000000..f02809c
--- /dev/null
+++ b/gemfire-junit/src/test/java/com/gemstone/gemfire/test/junit/rules/tests/RuleAndClassRuleJUnitTest.java
@@ -0,0 +1,122 @@
+package com.gemstone.gemfire.test.junit.rules.tests;
+
+import static com.gemstone.gemfire.test.junit.rules.tests.RunTest.*;
+import static org.assertj.core.api.Assertions.*;
+
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runner.Result;
+import org.junit.runners.model.Statement;
+
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class RuleAndClassRuleJUnitTest {
+
+  @Test
+  public void usingRuleAsRuleAndClassRuleShouldInvokeBeforeClass() {
+    Result result = runTest(UsingRuleAsRuleAndClassRule.class);
+    
+    assertThat(result.wasSuccessful()).isTrue();
+    assertThat(UsingRuleAsRuleAndClassRule.staticRule.beforeClassInvoked).isEqualTo(true);
+  }
+  
+  @Test
+  public void usingRuleAsRuleAndClassRuleShouldInvokeAfterClass() {
+    Result result = runTest(UsingRuleAsRuleAndClassRule.class);
+    
+    assertThat(result.wasSuccessful()).isTrue();
+    assertThat(UsingRuleAsRuleAndClassRule.staticRule.afterClassInvoked).isEqualTo(true);
+  }
+
+  @Test
+  public void usingRuleAsRuleAndClassRuleShouldInvokeBefore() {
+    Result result = runTest(UsingRuleAsRuleAndClassRule.class);
+    
+    assertThat(result.wasSuccessful()).isTrue();
+    assertThat(UsingRuleAsRuleAndClassRule.staticRule.beforeInvoked).isEqualTo(true);
+  }
+
+  @Test
+  public void usingRuleAsRuleAndClassRuleShouldInvokeAfter() {
+    Result result = runTest(UsingRuleAsRuleAndClassRule.class);
+    
+    assertThat(result.wasSuccessful()).isTrue();
+    assertThat(UsingRuleAsRuleAndClassRule.staticRule.afterInvoked).isEqualTo(true);
+  }
+
+  public static class SpyRule implements TestRule {
+    boolean beforeClassInvoked;
+    boolean afterClassInvoked;
+    boolean beforeInvoked;
+    boolean afterInvoked;
+    
+    @Override
+    public Statement apply(final Statement base, final Description description) {
+      if (description.isTest()) {
+        return statement(base);
+      } else if (description.isSuite()) {
+        return statementClass(base);
+      }
+      return base;
+    }
+
+    private Statement statement(final Statement base) {
+      return new Statement() {
+        @Override
+        public void evaluate() throws Throwable {
+          before();
+          try {
+            base.evaluate();
+          } finally {
+            after();
+          }
+        }
+      };
+    }
+    
+    private Statement statementClass(final Statement base) {
+      return new Statement() {
+        @Override
+        public void evaluate() throws Throwable {
+          beforeClass();
+          try {
+            base.evaluate();
+          } finally {
+            afterClass();
+          }
+        }
+      };
+    }
+    
+    private void beforeClass() {
+      this.beforeClassInvoked = true;
+    }
+    
+    private void afterClass() {
+      this.afterClassInvoked = true;
+    }
+    
+    private void before() {
+      this.beforeInvoked = true;
+    }
+    
+    private void after() {
+      this.afterInvoked = true;
+    }
+  };
+  
+  public static class UsingRuleAsRuleAndClassRule {
+    @ClassRule
+    public static SpyRule staticRule = new SpyRule();
+    @Rule
+    public SpyRule rule = staticRule;
+    @Test
+    public void doTest() throws Exception {
+    }
+  }
+}


Mime
View raw message