Search in sources :

Example 1 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass 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 2 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass 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 3 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project gradle by gradle.

the class ArchUnitFixtureTest method checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes.

@NotNull
private ConditionEvent checkThatMethodHasOnlyAllowedArgumentTypesOrReturnTypes(String methodName, Class<?>... arguments) {
    ArchCondition<JavaMethod> archCondition = haveOnlyArgumentsOrReturnTypesThatAre(resideInAnyPackage("java.lang").or(primitive).or(resideInAnyPackage("java.util")).as("allowed"));
    JavaClass javaClass = new ClassFileImporter().importClass(AllowedMethodTypesClass.class);
    JavaMethod validMethod = javaClass.getMethod(methodName, arguments);
    ConditionEvents events = new ConditionEvents();
    archCondition.check(validMethod, events);
    assertThat(events).hasSize(1);
    return events.iterator().next();
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass 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)

Example 5 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project solarthing by wildmountainfarms.

the class ArchTest method testClassesThatMustImplementEquals.

@Test
void testClassesThatMustImplementEquals() {
    JavaClasses importedClasses = new ClassFileImporter().importPackages("me.retrodaredevil.solarthing");
    Collection<JavaClass> classesToCheck = new ArrayList<>();
    // we want to be able to compare all execution reasons
    classesToCheck.addAll(importedClasses.get(ExecutionReason.class).getAllSubclasses());
    // required for OpenSourceExecutionReason
    classesToCheck.addAll(importedClasses.get(OpenSourcePacket.class).getAllSubclasses());
    classesToCheck.addAll(importedClasses.get(ActivePeriod.class).getAllSubclasses());
    // DeleteAlterPacket uses this, and DeleteAlterPacket must implement equals correctly
    classesToCheck.addAll(importedClasses.get(UpdateToken.class).getAllSubclasses());
    for (JavaClass javaClass : classesToCheck) {
        if (javaClass.isInterface()) {
            continue;
        }
        boolean doesClassOverrideEquals = javaClass.getMethods().stream().anyMatch(method -> "equals".equals(method.getName()));
        if (!doesClassOverrideEquals) {
            fail("equals is not implemented for " + javaClass);
        }
    }
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArrayList(java.util.ArrayList) ClassFileImporter(com.tngtech.archunit.core.importer.ClassFileImporter) Test(org.junit.jupiter.api.Test)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)205 Test (org.junit.Test)173 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)56 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)25 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)17 ArchCondition (com.tngtech.archunit.lang.ArchCondition)16 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)15 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)14 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)12 List (java.util.List)11 Serializable (java.io.Serializable)10 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)9 JavaType (com.tngtech.archunit.core.domain.JavaType)9 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)9 SourcePredicates.isJavaClass (org.apache.flink.architecture.common.SourcePredicates.isJavaClass)9 JavaEnumConstant (com.tngtech.archunit.core.domain.JavaEnumConstant)8 JavaAnnotation (com.tngtech.archunit.core.domain.JavaAnnotation)7 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)7 JavaParameterizedType (com.tngtech.archunit.core.domain.JavaParameterizedType)7