Search in sources :

Example 6 with ConditionEvents

use of com.tngtech.archunit.lang.ConditionEvents in project ArchUnit by TNG.

the class SessionBeanRulesTest method haveAUniqueImplementation.

private static ArchCondition<JavaClass> haveAUniqueImplementation() {
    return new ArchCondition<JavaClass>("have a unique implementation") {

        @Override
        public void check(JavaClass businessInterface, ConditionEvents events) {
            events.add(new SimpleConditionEvent(businessInterface, businessInterface.getAllSubclasses().size() <= 1, describe(businessInterface)));
        }

        private String describe(JavaClass businessInterface) {
            return String.format("%s is implemented by %s", businessInterface.getSimpleName(), joinNamesOf(businessInterface.getAllSubclasses()));
        }

        private String joinNamesOf(Set<JavaClass> implementations) {
            if (implementations.isEmpty()) {
                return "";
            }
            Deque<JavaClass> toJoin = new LinkedList<>(implementations);
            StringBuilder sb = new StringBuilder(toJoin.pollFirst().getSimpleName());
            for (JavaClass javaClass : toJoin) {
                sb.append(", ").append(javaClass.getSimpleName());
            }
            return sb.toString();
        }
    };
}
Also used : SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) Set(java.util.Set) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) LinkedList(java.util.LinkedList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 7 with ConditionEvents

use of com.tngtech.archunit.lang.ConditionEvents in project flink-mirror by flink-ci.

the class Conditions method haveLeafArgumentTypes.

/**
 * Tests leaf argument types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafArgumentTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf argument types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            final List<JavaClass> leafArgumentTypes = method.getParameterTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
            for (JavaClass leafType : leafArgumentTypes) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Argument leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaType(com.tngtech.archunit.core.domain.JavaType) Collectors(java.util.stream.Collectors) HasName(com.tngtech.archunit.core.domain.properties.HasName) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Stream(java.util.stream.Stream) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Collections(java.util.Collections) JavaParameterizedType(com.tngtech.archunit.core.domain.JavaParameterizedType) SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 8 with ConditionEvents

use of com.tngtech.archunit.lang.ConditionEvents in project flink by apache.

the class Conditions method haveLeafReturnTypes.

/**
 * Tests leaf return types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafReturnTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf return types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            for (JavaClass leafType : getLeafTypes(method.getReturnType())) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Returned leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 9 with ConditionEvents

use of com.tngtech.archunit.lang.ConditionEvents in project flink by apache.

the class Conditions method haveLeafArgumentTypes.

/**
 * Tests leaf argument types of a method against the given predicate.
 *
 * <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
 */
public static ArchCondition<JavaMethod> haveLeafArgumentTypes(DescribedPredicate<JavaClass> typePredicate) {
    return new ArchCondition<JavaMethod>("have leaf argument types" + typePredicate.getDescription()) {

        @Override
        public void check(JavaMethod method, ConditionEvents events) {
            final List<JavaClass> leafArgumentTypes = method.getParameterTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
            for (JavaClass leafType : leafArgumentTypes) {
                if (!isJavaClass(leafType)) {
                    continue;
                }
                if (!typePredicate.apply(leafType)) {
                    final String message = String.format("%s: Argument leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
                    events.add(SimpleConditionEvent.violated(method, message));
                }
            }
        }
    };
}
Also used : SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaType(com.tngtech.archunit.core.domain.JavaType) Collectors(java.util.stream.Collectors) HasName(com.tngtech.archunit.core.domain.properties.HasName) List(java.util.List) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) Stream(java.util.stream.Stream) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Collections(java.util.Collections) JavaParameterizedType(com.tngtech.archunit.core.domain.JavaParameterizedType) SourcePredicates.isJavaClass(org.apache.flink.architecture.common.SourcePredicates.isJavaClass) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 10 with ConditionEvents

use of com.tngtech.archunit.lang.ConditionEvents in project taskana by Taskana.

the class ArchitectureTest method notUseCurrentTimestampSqlFunction.

private static ArchCondition<JavaClass> notUseCurrentTimestampSqlFunction() {
    Function<JavaMethod, List<String>> getSqlStringsFromMethod = CheckedFunction.wrap((method) -> {
        List<String> values = new ArrayList<>();
        final Optional<Select> selectAnnotation = method.tryGetAnnotationOfType(Select.class);
        final Optional<Update> updateAnnotation = method.tryGetAnnotationOfType(Update.class);
        final Optional<Insert> insertAnnotation = method.tryGetAnnotationOfType(Insert.class);
        final Optional<Delete> deleteAnnotation = method.tryGetAnnotationOfType(Delete.class);
        final Optional<SelectProvider> selectProviderAnnotation = method.tryGetAnnotationOfType(SelectProvider.class);
        final Optional<UpdateProvider> updateProviderAnnotation = method.tryGetAnnotationOfType(UpdateProvider.class);
        final Optional<InsertProvider> insertProviderAnnotation = method.tryGetAnnotationOfType(InsertProvider.class);
        final Optional<DeleteProvider> deleteProviderAnnotation = method.tryGetAnnotationOfType(DeleteProvider.class);
        if (selectAnnotation.isPresent()) {
            values.addAll(Arrays.asList(selectAnnotation.get().value()));
        }
        if (updateAnnotation.isPresent()) {
            values.addAll(Arrays.asList(updateAnnotation.get().value()));
        }
        if (insertAnnotation.isPresent()) {
            values.addAll(Arrays.asList(insertAnnotation.get().value()));
        }
        if (deleteAnnotation.isPresent()) {
            values.addAll(Arrays.asList(deleteAnnotation.get().value()));
        }
        if (selectProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(selectProviderAnnotation.get().type(), selectProviderAnnotation.get().method()));
        }
        if (updateProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(updateProviderAnnotation.get().type(), updateProviderAnnotation.get().method()));
        }
        if (insertProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(insertProviderAnnotation.get().type(), insertProviderAnnotation.get().method()));
        }
        if (deleteProviderAnnotation.isPresent()) {
            values.add(executeStaticProviderMethod(deleteProviderAnnotation.get().type(), deleteProviderAnnotation.get().method()));
        }
        return values;
    });
    return new ArchCondition<>("not use the SQL function 'CURRENT_TIMESTAMP'") {

        @Override
        public void check(JavaClass javaClass, ConditionEvents events) {
            for (JavaMethod method : javaClass.getAllMethods()) {
                List<String> sqlStrings = getSqlStringsFromMethod.apply(method);
                if (sqlStrings.isEmpty() && !method.tryGetAnnotationOfType(SelectProvider.class).isPresent()) {
                    String message = String.format("Method '%s#%s' does not contain any MyBatis SQL annotation", javaClass.getName(), method.getName());
                    events.add(SimpleConditionEvent.violated(javaClass, message));
                }
                if (sqlStrings.stream().anyMatch(s -> s.contains("CURRENT_TIMESTAMP"))) {
                    String message = String.format("Method '%s#%s' uses 'CURRENT_TIMESTAMP' SQL function", javaClass.getName(), method.getName());
                    events.add(SimpleConditionEvent.violated(javaClass, message));
                }
            }
        }
    };
}
Also used : Delete(org.apache.ibatis.annotations.Delete) SelectProvider(org.apache.ibatis.annotations.SelectProvider) ArchCondition(com.tngtech.archunit.lang.ArchCondition) ArrayList(java.util.ArrayList) Update(org.apache.ibatis.annotations.Update) Insert(org.apache.ibatis.annotations.Insert) DeleteProvider(org.apache.ibatis.annotations.DeleteProvider) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) List(java.util.List) ArrayList(java.util.ArrayList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) InsertProvider(org.apache.ibatis.annotations.InsertProvider) UpdateProvider(org.apache.ibatis.annotations.UpdateProvider) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Select(org.apache.ibatis.annotations.Select)

Aggregations

ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)53 ArchCondition (com.tngtech.archunit.lang.ArchCondition)31 JavaClass (com.tngtech.archunit.core.domain.JavaClass)25 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)22 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)22 Test (org.junit.Test)15 List (java.util.List)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)8 JavaMember (com.tngtech.archunit.core.domain.JavaMember)7 Collectors (java.util.stream.Collectors)7 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)6 JavaType (com.tngtech.archunit.core.domain.JavaType)6 HasName (com.tngtech.archunit.core.domain.properties.HasName)6 Collections (java.util.Collections)6 Stream (java.util.stream.Stream)6 JavaField (com.tngtech.archunit.core.domain.JavaField)5 ConditionEvent (com.tngtech.archunit.lang.ConditionEvent)4 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)4 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)3