You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2016/08/15 07:38:35 UTC
[06/30] ignite git commit: IGNITE-3659: Added special test suite to
handle ignored tests.
IGNITE-3659: Added special test suite to handle ignored tests.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/b9d9d84f
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/b9d9d84f
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/b9d9d84f
Branch: refs/heads/ignite-1926
Commit: b9d9d84f4fcaf7f025b480769b612fc63b5082f4
Parents: 00f47d7
Author: vozerov-gridgain <vo...@gridgain.com>
Authored: Tue Aug 9 14:04:47 2016 +0300
Committer: vozerov-gridgain <vo...@gridgain.com>
Committed: Tue Aug 9 14:04:47 2016 +0300
----------------------------------------------------------------------
.../ignite/testframework/IgniteTestSuite.java | 191 +++++++++++++++++++
.../apache/ignite/testsuites/IgniteIgnore.java | 35 ++++
.../testsuites/IgniteIgnoredTestSuite.java | 63 ++++++
3 files changed, 289 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/b9d9d84f/modules/core/src/test/java/org/apache/ignite/testframework/IgniteTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/IgniteTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testframework/IgniteTestSuite.java
new file mode 100644
index 0000000..2828065
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/IgniteTestSuite.java
@@ -0,0 +1,191 @@
+/*
+ * 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 org.apache.ignite.testframework;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import org.apache.ignite.testsuites.IgniteIgnore;
+import org.jetbrains.annotations.Nullable;
+import org.junit.internal.MethodSorter;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Base class for run junit tests.
+ * Test methods marked with @Ignored annotation won't be executed.
+ */
+public class IgniteTestSuite extends TestSuite {
+ /** Whether to execute only ignored tests. */
+ private final boolean ignoredOnly;
+
+ /**
+ * Constructor.
+ *
+ * @param name Name.
+ */
+ public IgniteTestSuite(String name) {
+ this(null, name);
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param theClass TestCase class
+ */
+ public IgniteTestSuite(Class<? extends TestCase> theClass) {
+ this(theClass, false);
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param theClass TestCase class
+ * @param ignoredOnly Whether to execute only ignored tests.
+ */
+ public IgniteTestSuite(Class<? extends TestCase> theClass, boolean ignoredOnly) {
+ this(theClass, null, ignoredOnly);
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param theClass TestCase class
+ * @param name Test suite name.
+ */
+ public IgniteTestSuite(Class<? extends TestCase> theClass, String name) {
+ this(theClass, name, false);
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param theClass TestCase class
+ * @param name Test suite name.
+ * @param ignoredOnly Whether to execute only ignored tests.
+ */
+ public IgniteTestSuite(@Nullable Class<? extends TestCase> theClass, @Nullable String name, boolean ignoredOnly) {
+ this.ignoredOnly = ignoredOnly;
+
+ if (theClass != null)
+ addTestsFromTestCase(theClass);
+
+ if (name != null)
+ setName(name);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void addTestSuite(Class<? extends TestCase> testClass) {
+ addTestSuite(testClass, false);
+ }
+
+ /**
+ * Add test class to the suite.
+ *
+ * @param testClass Test class.
+ * @param ignoredOnly Ignore only flag.
+ */
+ public void addTestSuite(Class<? extends TestCase> testClass, boolean ignoredOnly) {
+ addTest(new IgniteTestSuite(testClass, ignoredOnly));
+ }
+
+ /**
+ *
+ * @param theClass TestCase class
+ */
+ private void addTestsFromTestCase(Class<?> theClass) {
+ setName(theClass.getName());
+
+ try {
+ getTestConstructor(theClass);
+ }
+ catch (NoSuchMethodException ex) {
+ addTest(warning("Class " + theClass.getName() +
+ " has no public constructor TestCase(String name) or TestCase()"));
+
+ return;
+ }
+
+ if(!Modifier.isPublic(theClass.getModifiers()))
+ addTest(warning("Class " + theClass.getName() + " is not public"));
+ else {
+ Class superCls = theClass;
+
+ int testAdded = 0;
+
+ for(List<String> names = new ArrayList<>(); Test.class.isAssignableFrom(superCls);
+ superCls = superCls.getSuperclass()) {
+ Method[] methods = MethodSorter.getDeclaredMethods(superCls);
+
+ for (Method each : methods) {
+ if (addTestMethod(each, names, theClass))
+ testAdded++;
+ }
+ }
+
+ if(testAdded == 0)
+ addTest(warning("No tests found in " + theClass.getName()));
+ }
+ }
+
+ /**
+ * @param method test method
+ * @param names test name list
+ * @param theClass test class
+ */
+ private boolean addTestMethod(Method method, List<String> names, Class<?> theClass) {
+ String name = method.getName();
+
+ if(!names.contains(name) && canAddMethod(method)) {
+ if(!Modifier.isPublic(method.getModifiers()))
+ addTest(warning("Test method isn\'t public: " + method.getName() + "(" +
+ theClass.getCanonicalName() + ")"));
+ else {
+ names.add(name);
+
+ addTest(createTest(theClass, name));
+
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Check whether method should be ignored.
+ *
+ * @param method Method.
+ * @return {@code True} if it should be ignored.
+ */
+ protected boolean canAddMethod(Method method) {
+ boolean res = method.getParameterTypes().length == 0 && method.getName().startsWith("test")
+ && method.getReturnType().equals(Void.TYPE);
+
+ if (res) {
+ // If method signature and name matches check if it is ignored or not.
+ boolean hasIgnore = method.isAnnotationPresent(IgniteIgnore.class);
+
+ res = hasIgnore == ignoredOnly;
+ }
+
+ return res;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/b9d9d84f/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnore.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnore.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnore.java
new file mode 100644
index 0000000..ac9a885
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnore.java
@@ -0,0 +1,35 @@
+/*
+ * 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 org.apache.ignite.testsuites;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation which indicates that the test is ignored.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.METHOD, ElementType.TYPE})
+public @interface IgniteIgnore {
+ /**
+ * The optional reason why the test is ignored.
+ */
+ String value() default "";
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/b9d9d84f/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnoredTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnoredTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnoredTestSuite.java
new file mode 100644
index 0000000..c3ec5e4
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteIgnoredTestSuite.java
@@ -0,0 +1,63 @@
+/*
+ * 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 org.apache.ignite.testsuites;
+
+import junit.framework.TestSuite;
+import org.apache.ignite.testframework.IgniteTestSuite;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+/**
+ * Special test suite with ignored tests.
+ */
+public class IgniteIgnoredTestSuite extends TestSuite {
+ /**
+ * @return IgniteCache test suite.
+ * @throws Exception Thrown in case of the failure.
+ */
+ public static TestSuite suite() throws Exception {
+ IgniteTestSuite suite = new IgniteTestSuite("Ignite Ignored Test Suite");
+
+ suite.addTestSuite(SampleTestClass.class, true);
+
+ return suite;
+ }
+
+ /**
+ * Sample test class. To be removed once the very first really ignored test class is there.
+ */
+ public static class SampleTestClass extends GridCommonAbstractTest {
+ /**
+ * Test 1.
+ *
+ * @throws Exception If failed.
+ */
+ public void testMethod1() throws Exception {
+ System.out.println("Normal test method called.");
+ }
+
+ /**
+ * Test 2.
+ *
+ * @throws Exception If failed.
+ */
+ @IgniteIgnore
+ public void testMethod2() throws Exception {
+ System.out.println("Ignored method called.");
+ }
+ }
+}