Airblader commented on a change in pull request #17133: URL: https://github.com/apache/flink/pull/17133#discussion_r701627280
########## File path: flink-table/flink-table-common/src/test/java/org/apache/flink/table/architecture/ArchitectureTests.java ########## @@ -0,0 +1,100 @@ +/* + * 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.flink.table.architecture; + +import org.apache.flink.annotation.Experimental; +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.Public; +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.configuration.ConfigOption; +import org.apache.flink.table.factories.DynamicTableFactory; +import org.apache.flink.testutils.architecture.ApiRules; + +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaField; +import com.tngtech.archunit.core.domain.JavaModifier; +import com.tngtech.archunit.core.importer.ImportOption; +import com.tngtech.archunit.junit.AnalyzeClasses; +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.junit.ArchTests; +import com.tngtech.archunit.junit.ArchUnitRunner; +import com.tngtech.archunit.lang.ArchCondition; +import com.tngtech.archunit.lang.ArchRule; +import com.tngtech.archunit.lang.ConditionEvents; +import com.tngtech.archunit.lang.SimpleConditionEvent; +import org.junit.runner.RunWith; + +import java.util.Set; + +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; +import static org.apache.flink.testutils.architecture.Predicates.areAnnotatedWithAtLeastOneOf; +import static org.apache.flink.testutils.architecture.Predicates.fulfill; + +/** TODO. */ +@RunWith(ArchUnitRunner.class) +@AnalyzeClasses( + packages = "org.apache.flink.table", + importOptions = ImportOption.DoNotIncludeTests.class) +public class ArchitectureTests { + @ArchTest public static final ArchTests APIS = ArchTests.in(ApiRules.class); + + /** Ensure that all classes in the API package have a proper annotation. */ + @ArchTest + public static final ArchRule ANNOTATED_APIS = Review comment: In flink-table-common this test currently identifies a couple of classes (all exceptions) which are not annotated. ########## File path: flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/architecture/ApiRules.java ########## @@ -0,0 +1,93 @@ +/* + * 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.flink.testutils.architecture; + +import org.apache.flink.annotation.Public; +import org.apache.flink.annotation.PublicEvolving; + +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.lang.ArchRule; + +import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideOutsideOfPackage; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; +import static org.apache.flink.testutils.architecture.Predicates.areAnnotatedWithAtLeastOneOf; +import static org.apache.flink.testutils.architecture.Predicates.haveLeafArgumentTypes; +import static org.apache.flink.testutils.architecture.Predicates.haveLeafReturnTypes; + +/** Architecture rules for APIs. */ +public class ApiRules { + + /** + * Ensure that public methods marked {@link Public} only use return and argument types which are + * also marked {@link Public}. + * + * <p>Note that this is a stronger requirement than {@link + * #PUBLIC_EVOLVING_API_METHODS_USE_ONLY_PUBLIC_EVOLVING_API_TYPES} and ensures that {@link + * Public} APIs do not use {@link PublicEvolving} APIs. + */ + @ArchTest + public static final ArchRule PUBLIC_API_METHODS_USE_ONLY_PUBLIC_API_TYPES = + methods() + .that() + .areDeclaredInClassesThat() + .areAnnotatedWith(Public.class) + .and() + .arePublic() + .should( + haveLeafReturnTypes( + resideOutsideOfPackage("org.apache.flink..") + .or( + areAnnotatedWithAtLeastOneOf( + Public.class, Deprecated.class)))) + .andShould( + haveLeafArgumentTypes( + resideOutsideOfPackage("org.apache.flink..") + .or( + areAnnotatedWithAtLeastOneOf( + Public.class, Deprecated.class)))); + + /** + * Ensure that public methods marked {@link Public} or {@link PublicEvolving} only use return + * and argument types which are also marked {@link Public} or {@link PublicEvolving}. + */ + @ArchTest + public static final ArchRule PUBLIC_EVOLVING_API_METHODS_USE_ONLY_PUBLIC_EVOLVING_API_TYPES = Review comment: In flink-table-common this test currently identifies quite a few problems, e.g. a lot of methods in `Catalog` referencing non-annotated classes, or `SinkProvider` being PublicEvolving, but `Sink` being Experimental. ########## File path: flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/architecture/Predicates.java ########## @@ -0,0 +1,158 @@ +/* + * 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.flink.testutils.architecture; + +import com.tngtech.archunit.base.DescribedPredicate; +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.core.domain.JavaParameterizedType; +import com.tngtech.archunit.core.domain.JavaType; +import com.tngtech.archunit.core.domain.properties.HasName; +import com.tngtech.archunit.lang.ArchCondition; +import com.tngtech.archunit.lang.ConditionEvents; +import com.tngtech.archunit.lang.SimpleConditionEvent; + +import java.lang.annotation.Annotation; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** TODO. */ +public class Predicates { + + /** TODO. */ + public static <T extends HasName> ArchCondition<T> fulfill(DescribedPredicate<T> predicate) { + return new ArchCondition<T>(predicate.getDescription()) { + @Override + public void check(T item, ConditionEvents events) { + if (!predicate.apply(item)) { + final String message = + String.format( + "%s does not satisfy: %s", + item.getName(), predicate.getDescription()); + events.add(SimpleConditionEvent.violated(item, message)); + } + } + }; + } + + /** TODO. */ + @SafeVarargs + public static DescribedPredicate<JavaClass> areAnnotatedWithAtLeastOneOf( + Class<? extends Annotation>... annotations) { + return new DescribedPredicate<JavaClass>( + "be annotated with at least one of " + + Arrays.stream(annotations) + .map(Class::getSimpleName) + .collect(Collectors.joining(", "))) { + @Override + public boolean apply(JavaClass clazz) { + final JavaClass enclosingClazz = getEnclosingClass(clazz).getBaseComponentType(); Review comment: This is a bit of a Flink specialty, I guess, since we decorate only the top-level classes, not nested classes directly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org