Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A4E7F200B68 for ; Fri, 19 Aug 2016 13:48:23 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id A3BEB160AAC; Fri, 19 Aug 2016 11:48:23 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D4252160ACE for ; Fri, 19 Aug 2016 13:48:21 +0200 (CEST) Received: (qmail 79775 invoked by uid 500); 19 Aug 2016 11:48:20 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 78763 invoked by uid 99); 19 Aug 2016 11:48:19 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 19 Aug 2016 11:48:19 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D3877EEF45; Fri, 19 Aug 2016 11:48:18 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Fri, 19 Aug 2016 11:48:36 -0000 Message-Id: <324e8eae02d542988ee5b1843d4a7c0b@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [19/53] [abbrv] ignite git commit: IGNITE-3659: Added special test suite to handle ignored tests. archived-at: Fri, 19 Aug 2016 11:48:23 -0000 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-3299 Commit: b9d9d84f4fcaf7f025b480769b612fc63b5082f4 Parents: 00f47d7 Author: vozerov-gridgain Authored: Tue Aug 9 14:04:47 2016 +0300 Committer: vozerov-gridgain 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 theClass) { + this(theClass, false); + } + + /** + * Constructor. + * + * @param theClass TestCase class + * @param ignoredOnly Whether to execute only ignored tests. + */ + public IgniteTestSuite(Class theClass, boolean ignoredOnly) { + this(theClass, null, ignoredOnly); + } + + /** + * Constructor. + * + * @param theClass TestCase class + * @param name Test suite name. + */ + public IgniteTestSuite(Class 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 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 testClass) { + addTestSuite(testClass, false); + } + + /** + * Add test class to the suite. + * + * @param testClass Test class. + * @param ignoredOnly Ignore only flag. + */ + public void addTestSuite(Class 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 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 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."); + } + } +}