This is an automated email from the ASF dual-hosted git repository. amashenkov pushed a commit to branch ignite-20086 in repository https://gitbox.apache.org/repos/asf/ignite-3.git
commit 8f25ffc9ea78917932e45ac8c972b3ce00958919 Author: amashenkov <andrey.mashen...@gmail.com> AuthorDate: Fri Jul 28 19:45:17 2023 +0300 Implement ArchUnit rule to detect wrong test classes hierarchy. --- .../ignite/internal/TestsExtendsBaseTestClass.java | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/modules/arch-test/src/test/java/org/apache/ignite/internal/TestsExtendsBaseTestClass.java b/modules/arch-test/src/test/java/org/apache/ignite/internal/TestsExtendsBaseTestClass.java new file mode 100644 index 0000000000..56b551f1f9 --- /dev/null +++ b/modules/arch-test/src/test/java/org/apache/ignite/internal/TestsExtendsBaseTestClass.java @@ -0,0 +1,71 @@ +/* + * 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.internal; + +import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.annotatedWith; + +import com.tngtech.archunit.base.DescribedPredicate; +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.junit.AnalyzeClasses; +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.lang.ArchRule; +import com.tngtech.archunit.lang.syntax.ArchRuleDefinition; +import org.apache.ignite.lang.IgniteTestImportOption; +import org.apache.ignite.lang.LocationProvider.RootLocationProvider; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; + +/** + * Tests that tests classes, which uses Mockito framework, extends BaseIgniteAbstractTest. Using BaseIgniteAbstractTest guarantees the + * Mockito resources will be released after tests finishes. + */ +@AnalyzeClasses( + packages = "org.apache.ignite", + importOptions = IgniteTestImportOption.class, + locations = RootLocationProvider.class) +public class TestsExtendsBaseTestClass { + @ArchTest + public static final ArchRule TEST_CLASS_WITH_MOCKS_EXTENDS_BASE_TEST_CLASS = ArchRuleDefinition + .classes() + // do not extend base class + .that().areNotAssignableTo("org.apache.ignite.internal.testframework.BaseIgniteAbstractTest") + // are classes with tests + .and().containAnyMethodsThat(annotatedWith(Test.class)).or(annotatedWith(ParameterizedTest.class)) + // uses Mockito framework + .and(new DescribedPredicate<>("uses Mockito framework") { + @Override + public boolean test(JavaClass javaClass) { + return javaClass.getDirectDependenciesFromSelf().stream() + .anyMatch(dep -> "org.mockito.Mockito".equals(dep.getTargetClass().getFullName())); + } + }) + .should() + .beAssignableTo("org.apache.ignite.internal.testframework.BaseIgniteAbstractTest") + .because("Test classes, which uses Mockito framework, must extends BaseIgniteAbstractTest"); + + //TODO: remove or enable. + public static final ArchRule TEST_CLASS_EXTENDS_BASE_TEST_CLASS = ArchRuleDefinition + .classes() + // do not extend base class + .that().areNotAssignableTo("org.apache.ignite.internal.testframework.BaseIgniteAbstractTest") + // are classes with tests + .and().containAnyMethodsThat(annotatedWith(Test.class)).or(annotatedWith(ParameterizedTest.class)) + .should() + .beAssignableTo("org.apache.ignite.internal.testframework.BaseIgniteAbstractTest") + .because("All test classes must extends BaseIgniteAbstractTest"); +}