Search in sources :

Example 1 with DescribedPredicate

use of com.tngtech.archunit.base.DescribedPredicate in project git-machete-intellij-plugin by VirtusLab.

the class ClassStructureTestSuite method actions_overriding_onUpdate_should_call_super_onUpdate.

@Test
public void actions_overriding_onUpdate_should_call_super_onUpdate() {
    classes().that().areAssignableTo(com.virtuslab.gitmachete.frontend.actions.base.BaseProjectDependentAction.class).and().areNotAssignableFrom(com.virtuslab.gitmachete.frontend.actions.base.BaseProjectDependentAction.class).and(new DescribedPredicate<JavaClass>("override onUpdate method") {

        @Override
        public boolean apply(JavaClass input) {
            return input.getMethods().stream().anyMatch(method -> method.getName().equals("onUpdate"));
        }
    }).should().callMethodWhere(new DescribedPredicate<JavaMethodCall>("name is onUpdate and owner is the direct superclass") {

        @Override
        public boolean apply(JavaMethodCall input) {
            // where is the method called from?
            JavaCodeUnit origin = input.getOrigin();
            // where is the method declared?
            AccessTarget.MethodCallTarget target = input.getTarget();
            if (origin.getName().equals("onUpdate") && target.getName().equals("onUpdate")) {
                return target.getOwner().equals(origin.getOwner().getSuperclass().orElse(null));
            }
            return false;
        }
    }).check(importedClasses);
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) AccessTarget(com.tngtech.archunit.core.domain.AccessTarget) Test(org.junit.Test)

Example 2 with DescribedPredicate

use of com.tngtech.archunit.base.DescribedPredicate in project sirius-components by eclipse-sirius.

the class AbstractServicesTests method nonAuditedRepositoryMethod.

private DescribedPredicate<JavaMethodCall> nonAuditedRepositoryMethod() {
    return new // $NON-NLS-1$
    DescribedPredicate<>(// $NON-NLS-1$
    "the repository method called has not been audited") {

        @Override
        public boolean apply(JavaMethodCall javaMethodCall) {
            JavaClass targetJavaClass = javaMethodCall.getTargetOwner();
            String fullName = targetJavaClass.getFullName();
            // $NON-NLS-1$
            boolean isRepository = fullName.endsWith("Repository");
            boolean hasBeenAudited = javaMethodCall.getTarget().isAnnotatedWith(Audited.class);
            return isRepository && !hasBeenAudited;
        }
    };
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall)

Example 3 with DescribedPredicate

use of com.tngtech.archunit.base.DescribedPredicate 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 4 with DescribedPredicate

use of com.tngtech.archunit.base.DescribedPredicate 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 5 with DescribedPredicate

use of com.tngtech.archunit.base.DescribedPredicate in project teammates by TEAMMATES.

the class ArchitectureTest method testArchitecture_logic_coreLogicCanOnlyAccessItsCorrespondingDb.

@Test
public void testArchitecture_logic_coreLogicCanOnlyAccessItsCorrespondingDb() {
    for (JavaClass coreLogicClass : forClasses(LOGIC_CORE_PACKAGE)) {
        String logicClassName = coreLogicClass.getSimpleName();
        if ("DataBundleLogic".equals(logicClassName)) {
            continue;
        }
        if (logicClassName.endsWith(TEST_FILE_SUFFIX)) {
            continue;
        }
        String dbClassName = logicClassName.replace("Logic", "Db");
        noClasses().that().resideInAPackage(includeSubpackages(LOGIC_CORE_PACKAGE)).and().doNotHaveSimpleName(logicClassName).and().doNotHaveSimpleName(logicClassName + TEST_FILE_SUFFIX).and().doNotHaveSimpleName("DataBundleLogic").should().accessClassesThat(new DescribedPredicate<>("") {

            @Override
            public boolean apply(JavaClass input) {
                return input.getPackageName().startsWith(STORAGE_API_PACKAGE) && input.getSimpleName().equals(dbClassName);
            }
        }).check(forClasses(LOGIC_CORE_PACKAGE, STORAGE_API_PACKAGE));
    }
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) Test(org.testng.annotations.Test)

Aggregations

DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 JavaClass (com.tngtech.archunit.core.domain.JavaClass)10 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)8 ArchCondition (com.tngtech.archunit.lang.ArchCondition)8 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)8 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 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)6 Collections (java.util.Collections)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)6 Test (org.junit.Test)3 JavaMember (com.tngtech.archunit.core.domain.JavaMember)2 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)2 EvaluationResult (com.tngtech.archunit.lang.EvaluationResult)2 AccessTarget (com.tngtech.archunit.core.domain.AccessTarget)1 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)1