codecholeric commented on a change in pull request #17133: URL: https://github.com/apache/flink/pull/17133#discussion_r753306333
########## File path: flink-architecture-tests/src/test/java/org/apache/flink/architecture/common/Conditions.java ########## @@ -0,0 +1,143 @@ +/* + * 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.architecture.common; + +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.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.apache.flink.architecture.common.SourcePredicates.isJavaClass; + +/** Common conditions for architecture tests. */ +public class Conditions { + + /** Generic condition to check fulfillment of a predicate. */ + 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)); + } + } + }; + } + + /** + * Tests leaf return types of a method against the given predicate. + * + * <p>Given some {@link JavaType}, "leaf" types are recursively determined as follows: + * + * <ul> + * <li>If the type is an array type, check its base component type. + * <li>If the type is a generic type, check the type itself and all of its type arguments. + * <li>Otherwise, check just the type itself. + * </ul> + */ + public static ArchCondition<JavaMethod> haveLeafReturnTypes( Review comment: Originally within ArchUnit I had also attempted to unit test these things with sort of stub imported objects, but eventually I switched over to simply creating test examples and using the real `ClassFileImporter` for imports. It's just more blackbox and the tests are still super fast. If you really want to mock-unit-test something like this without creating an example class you could ponder about making the signature ``` public static <T extends HasName.AndFullName & HasReturnType> ArchCondition<T> haveLeafReturnTypes(...) ``` Then you could simply mock the interface. But typically within ArchUnit we use tests with local classes where possible, like ``` @Test void example() { class SomeExample {...} new ClassFileImporter().importClass(SomeExample.class); } ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
