geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jensde...@apache.org
Subject incubator-geode git commit: GEODE-1350: Provide local fix for Category and Parameterized test issue
Date Fri, 13 May 2016 15:06:50 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/develop 819bf650b -> 973c86c7a


GEODE-1350: Provide local fix for Category and Parameterized test issue


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/973c86c7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/973c86c7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/973c86c7

Branch: refs/heads/develop
Commit: 973c86c7a2706f1737c39ffc4fa2feaf0fc08801
Parents: 819bf65
Author: Jens Deppe <jdeppe@pivotal.io>
Authored: Wed May 11 08:05:33 2016 -0700
Committer: Jens Deppe <jdeppe@pivotal.io>
Committed: Fri May 13 07:54:30 2016 -0700

----------------------------------------------------------------------
 .../CategoryWithParameterizedRunner.java        |  44 +++++
 .../CategoryWithParameterizedRunnerFactory.java |  42 +++++
 .../junit/runners/ExposedGetAnnotations.java    |  23 +++
 ...egoryWithParameterizedRunnerFactoryTest.java | 161 +++++++++++++++++++
 4 files changed, 270 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/973c86c7/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunner.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunner.java
b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunner.java
new file mode 100644
index 0000000..c96951b
--- /dev/null
+++ b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunner.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.test.junit.runners;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
+import org.junit.runners.parameterized.TestWithParameters;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CategoryWithParameterizedRunner extends BlockJUnit4ClassRunnerWithParameters
implements ExposedGetAnnotations {
+  public CategoryWithParameterizedRunner(TestWithParameters test) throws InitializationError
{
+    super(test);
+  }
+
+  @Override
+  public Annotation[] getRunnerAnnotations() {
+    Annotation[] allAnnotations = getTestClass().getAnnotations();
+    List<Annotation> annotationsWithoutRunWith = new ArrayList<>();
+    for (Annotation annotation: allAnnotations) {
+      if (!annotation.annotationType().equals(RunWith.class)) {
+        annotationsWithoutRunWith.add(annotation);
+      }
+    }
+    return annotationsWithoutRunWith.toArray(new Annotation[0]);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/973c86c7/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactory.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactory.java
b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactory.java
new file mode 100644
index 0000000..84882f0
--- /dev/null
+++ b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactory.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.test.junit.runners;
+
+import org.junit.runner.RunWith;
+import org.junit.runner.Runner;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
+import org.junit.runners.parameterized.ParametersRunnerFactory;
+import org.junit.runners.parameterized.TestWithParameters;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This class provides an early fix for JUnit issue <a href="https://github.com/junit-team/junit4/issues/751>751</a>.
+ *
+ * Once JUnit 4.13 is released, this class can be removed.
+ *
+ * See also <a href="https://issues.apache.org/jira/browse/GEODE-1350">GEODE-1350</a>
+ */
+public class CategoryWithParameterizedRunnerFactory implements ParametersRunnerFactory {
+  @Override
+  public Runner createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError
{
+    return new CategoryWithParameterizedRunner(test);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/973c86c7/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/ExposedGetAnnotations.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/ExposedGetAnnotations.java
b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/ExposedGetAnnotations.java
new file mode 100644
index 0000000..f5906f2
--- /dev/null
+++ b/geode-junit/src/main/java/com/gemstone/gemfire/test/junit/runners/ExposedGetAnnotations.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.test.junit.runners;
+
+import java.lang.annotation.Annotation;
+
+public interface ExposedGetAnnotations {
+  Annotation[] getRunnerAnnotations();
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/973c86c7/geode-junit/src/test/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactoryTest.java
----------------------------------------------------------------------
diff --git a/geode-junit/src/test/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactoryTest.java
b/geode-junit/src/test/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactoryTest.java
new file mode 100644
index 0000000..63a579b
--- /dev/null
+++ b/geode-junit/src/test/java/com/gemstone/gemfire/test/junit/runners/CategoryWithParameterizedRunnerFactoryTest.java
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.gemstone.gemfire.test.junit.runners;
+
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.Description;
+import org.junit.runner.JUnitCore;
+import org.junit.runner.Request;
+import org.junit.runner.Result;
+import org.junit.runner.RunWith;
+import org.junit.runner.Runner;
+import org.junit.runner.manipulation.Filter;
+import org.junit.runners.Parameterized;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
+import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory;
+import org.junit.runners.parameterized.TestWithParameters;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class CategoryWithParameterizedRunnerFactoryTest {
+
+  /**
+   * So much hacking in order to expose JUnit internals. I have no words...
+   */
+  public static class ExposedBlockJUnit4ClassRunnerWithParameters extends BlockJUnit4ClassRunnerWithParameters
implements ExposedGetAnnotations {
+    public ExposedBlockJUnit4ClassRunnerWithParameters(TestWithParameters test) throws InitializationError
{
+      super(test);
+    }
+
+    @Override
+    public Annotation[] getRunnerAnnotations() {
+      return super.getRunnerAnnotations();
+    }
+  }
+
+  public static class ExposedBlockJUnit4ClassRunnerWithParametersFactory extends
+      BlockJUnit4ClassRunnerWithParametersFactory {
+    public Runner createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError
{
+      return new ExposedBlockJUnit4ClassRunnerWithParameters(test);
+    }
+  }
+
+  public static class ExposedParameterized extends Parameterized {
+    public ExposedParameterized(Class<?> klass) throws Throwable {
+      super(klass);
+    }
+
+    @Override
+    protected List<Runner> getChildren() {
+      return super.getChildren();
+    }
+  }
+
+  @Category(UnitTest.class)
+  @RunWith(ExposedParameterized.class)
+  @Parameterized.UseParametersRunnerFactory(ExposedBlockJUnit4ClassRunnerWithParametersFactory.class)
+  public static class BrokenCategoryClass {
+    @Parameterized.Parameters
+    public static Iterable<String> getParams() {
+      return Arrays.asList("one", "two");
+    }
+
+    @Parameterized.Parameter
+    public String value;
+
+    @Test
+    public void insanity() {
+      assertTrue(true);
+    }
+  }
+
+  @Category(UnitTest.class)
+  @RunWith(ExposedParameterized.class)
+  @Parameterized.UseParametersRunnerFactory(CategoryWithParameterizedRunnerFactory.class)
+  public static class WorkingCategoryClass {
+    @Parameterized.Parameters
+    public static Iterable<String> getParams() {
+      return Arrays.asList("one", "two");
+    }
+
+    @Parameterized.Parameter
+    public String value;
+
+    @Test
+    public void insanity() {
+      assertTrue(true);
+    }
+  }
+
+  @Test
+  public void testBrokenCategoryAndParameterized() {
+    Request request = Request.aClass(BrokenCategoryClass.class);
+    ExposedParameterized runner = (ExposedParameterized) request.getRunner();
+    request = request.filterWith(new CategoryFilter((ExposedBlockJUnit4ClassRunnerWithParameters)
runner.getChildren().get(0)));
+    Result result = new JUnitCore().run(request);
+    assertEquals("Yeah!! This might actually mean we've upgraded to JUnit 4.13. Hurry up
already and delete this hack.", 1, result.getRunCount());
+  }
+
+  @Test
+  public void testWorkingCategoryAndParameterized() {
+    Request request = Request.aClass(WorkingCategoryClass.class);
+    ExposedParameterized runner = (ExposedParameterized) request.getRunner();
+    request = request.filterWith(new CategoryFilter((ExposedGetAnnotations) runner.getChildren().get(0)));
+    Result result = new JUnitCore().run(request);
+    assertEquals(2, result.getRunCount());
+  }
+
+  public static class CategoryFilter extends Filter {
+
+    private ExposedGetAnnotations runner;
+
+    public CategoryFilter(ExposedGetAnnotations runner) {
+      this.runner = runner;
+    }
+
+    @Override
+    public boolean shouldRun(Description description) {
+      if (description.getChildren().size() == 0) {
+        return true;
+      }
+
+      List<Annotation> runnerAnnotations = new ArrayList<>();
+      Collections.addAll(runnerAnnotations, runner.getRunnerAnnotations());
+      for (Annotation a : description.getAnnotations()) {
+        if (runnerAnnotations.contains(a)) {
+          return true;
+        }
+      }
+      return false;
+    }
+
+    @Override
+    public String describe() {
+      return CategoryFilter.class.getSimpleName();
+    }
+  }
+}


Mime
View raw message