Search in sources :

Example 6 with JavaClass

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

the class Module method filterSpringBeans.

private static Classes filterSpringBeans(JavaPackage source) {
    Map<Boolean, List<JavaClass>> collect = // 
    source.that(isConfiguration()).stream().flatMap(// 
    it -> it.getMethods().stream()).filter(// 
    SpringTypes::isAtBeanMethod).map(// 
    JavaMethod::getRawReturnType).collect(Collectors.groupingBy(it -> source.contains(it)));
    Classes repositories = source.that(isSpringDataRepository());
    Classes coreComponents = source.that(not(INTERFACES).and(isComponent()));
    Classes configurationProperties = source.that(isConfigurationProperties());
    return // 
    coreComponents.and(// 
    repositories).and(// 
    configurationProperties).and(// 
    collect.getOrDefault(true, Collections.emptyList())).and(collect.getOrDefault(false, Collections.emptyList()));
}
Also used : SourceCodeLocation(com.tngtech.archunit.core.domain.SourceCodeLocation) SpringTypes(org.moduliths.model.Types.SpringTypes) Arrays(java.util.Arrays) Getter(lombok.Getter) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) System(java.lang.System) Dependency(com.tngtech.archunit.core.domain.Dependency) RequiredArgsConstructor(lombok.RequiredArgsConstructor) JavaMember(com.tngtech.archunit.core.domain.JavaMember) JavaCodeUnit(com.tngtech.archunit.core.domain.JavaCodeUnit) JavaField(com.tngtech.archunit.core.domain.JavaField) JMoleculesTypes(org.moduliths.model.Types.JMoleculesTypes) JavaConstructor(com.tngtech.archunit.core.domain.JavaConstructor) Suppliers(com.tngtech.archunit.thirdparty.com.google.common.base.Suppliers) Map(java.util.Map) Nullable(org.springframework.lang.Nullable) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Predicates(com.tngtech.archunit.core.domain.JavaClass.Predicates) JavaXTypes(org.moduliths.model.Types.JavaXTypes) Types(org.moduliths.model.Types) Supplier(com.tngtech.archunit.thirdparty.com.google.common.base.Supplier) NonNull(lombok.NonNull) Predicate(java.util.function.Predicate) Collection(java.util.Collection) DescribedPredicate(com.tngtech.archunit.base.DescribedPredicate) Set(java.util.Set) EqualsAndHashCode(lombok.EqualsAndHashCode) Collectors(java.util.stream.Collectors) SpringDataTypes(org.moduliths.model.Types.SpringDataTypes) List(java.util.List) Stream(java.util.stream.Stream) Classes(org.moduliths.model.Classes) Optional(java.util.Optional) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) SpringTypes(org.moduliths.model.Types.SpringTypes) List(java.util.List) Classes(org.moduliths.model.Classes)

Example 7 with JavaClass

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

the class ArchitecturallyEvidentTypeUnitTest method discoversEventsListenedToForEventListener.

// #132
@Test
void discoversEventsListenedToForEventListener() {
    JavaClass listenerType = classes.getRequiredClass(SomeEventListener.class);
    // 
    assertThat(ArchitecturallyEvidentType.of(listenerType, classes).getReferenceTypes()).extracting(// 
    JavaClass::getFullName).containsExactly(Object.class.getName(), String.class.getName());
}
Also used : JavaClass(com.tngtech.archunit.core.domain.JavaClass) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest)

Example 8 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project arch-unit-build-plugin-core by societe-generale.

the class NoTestIgnoreRuleTest method notBeenIgnore.

public static ArchCondition<JavaClass> notBeenIgnore() {
    return new ArchCondition<JavaClass>(NO_JUNIT_IGNORE_VIOLATION_MESSAGE) {

        @Override
        @SuppressWarnings("squid:S1166")
        public void check(JavaClass item, ConditionEvents events) {
            // class level checks
            String violationMessageAtClassLevel = item.getName() + ", at class level";
            // class level checks
            addViolationEvent(buildViolationIfAnnotationWithNoValueFound(item, Ignore.class, violationMessageAtClassLevel), events);
            addViolationEvent(buildViolationIfAnnotationWithNoValueFound(item, Disabled.class, violationMessageAtClassLevel), events);
            // method level checks
            for (JavaMethod method : item.getMethods()) {
                String violationMessageAtMethodLevel = item.getName() + " - " + method.getName() + ", at method level";
                addViolationEvent(buildViolationIfAnnotationWithNoValueFound(method, Ignore.class, violationMessageAtMethodLevel), events);
                addViolationEvent(buildViolationIfAnnotationWithNoValueFound(method, Disabled.class, violationMessageAtMethodLevel), events);
            }
        }

        private void addViolationEvent(Optional<ConditionEvent> violation, ConditionEvents events) {
            if (violation.isPresent()) {
                events.add(violation.get());
            }
        }

        private Optional<ConditionEvent> buildViolationIfAnnotationWithNoValueFound(HasAnnotations item, Class annotation, String violationMessage) {
            try {
                if (item.getAnnotationOfType(annotation) != null) {
                    return Optional.of(SimpleConditionEvent.violated(item, violationMessage));
                }
            } catch (IllegalArgumentException e) {
            // if there's no Ignore annotation, IllegalArgument exception is thrown.
            // we swallow it, as it means there's no annotation at class level.
            }
            return Optional.empty();
        }
    };
}
Also used : HasAnnotations(com.tngtech.archunit.core.domain.properties.HasAnnotations) ConditionEvent(com.tngtech.archunit.lang.ConditionEvent) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) Ignore(org.junit.Ignore) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Optional(java.util.Optional) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaClass(com.tngtech.archunit.core.domain.JavaClass) Disabled(org.junit.jupiter.api.Disabled) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 9 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project arch-unit-build-plugin-core by societe-generale.

the class NoJodaTimeRuleTest method notUseJodaTime.

protected static ArchCondition<JavaClass> notUseJodaTime() {
    return new ArchCondition<JavaClass>("not use Joda time ") {

        @Override
        public void check(JavaClass item, ConditionEvents events) {
            List<JavaField> classesWithJodaTimeFields = item.getAllFields().stream().filter(this::isJodaTimeField).collect(toList());
            for (JavaField field : classesWithJodaTimeFields) {
                events.add(SimpleConditionEvent.violated(field, NO_JODA_VIOLATION_MESSAGE + " - class: " + field.getOwner().getName() + " - field name: " + field.getName()));
            }
            List<JavaMethodCall> methodsUsingJodaTimeInternally = item.getCodeUnits().stream().filter(codeUnit -> codeUnit instanceof JavaMethod).flatMap(method -> method.getMethodCallsFromSelf().stream()).filter(method -> method instanceof JavaMethodCall).filter(this::isMethodUsingJodaTimeInternally).collect(toList());
            for (JavaMethodCall methodCall : methodsUsingJodaTimeInternally) {
                events.add(SimpleConditionEvent.violated(methodCall.getOriginOwner(), NO_JODA_VIOLATION_MESSAGE + " - class: " + methodCall.getOriginOwner().getName() + " - method: " + methodCall.getTarget().getOwner().getSimpleName() + "." + methodCall.getTarget().getName() + " - line: " + methodCall.getLineNumber()));
            }
        }

        private boolean isJodaTimeField(JavaField field) {
            return field.getRawType().getName().startsWith(JODATIME_PACKAGE_PREFIX);
        }

        private boolean isMethodUsingJodaTimeInternally(JavaMethodCall javaMethodCall) {
            return javaMethodCall.getTarget().getFullName().startsWith(JODATIME_PACKAGE_PREFIX);
        }
    };
}
Also used : JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) Collection(java.util.Collection) JavaField(com.tngtech.archunit.core.domain.JavaField) ArchUtils(com.societegenerale.commons.plugin.utils.ArchUtils) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents) SimpleConditionEvent(com.tngtech.archunit.lang.SimpleConditionEvent) ArchRuleDefinition.classes(com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ScopePathProvider(com.societegenerale.commons.plugin.service.ScopePathProvider) JavaField(com.tngtech.archunit.core.domain.JavaField) JavaClass(com.tngtech.archunit.core.domain.JavaClass) ArchCondition(com.tngtech.archunit.lang.ArchCondition) JavaMethod(com.tngtech.archunit.core.domain.JavaMethod) JavaMethodCall(com.tngtech.archunit.core.domain.JavaMethodCall) ConditionEvents(com.tngtech.archunit.lang.ConditionEvents)

Example 10 with JavaClass

use of com.tngtech.archunit.core.domain.JavaClass in project arch-unit-build-plugin-core by societe-generale.

the class ArchUtilsTest method shouldIgnoreClassesFromConfiguredPaths.

@Test
public void shouldIgnoreClassesFromConfiguredPaths() {
    JavaClasses classes = ArchUtils.importAllClassesInPackage(new RootClassFolder("./target"), "");
    assertThat(classes).isNotEmpty();
    JavaClass classToExclude = classes.stream().filter(c -> c.getSource().get().getUri().toString().contains("ClassToExclude")).findFirst().get();
    assertThat(classToExclude).as("when no exclusion pattern configured, ClassToExclude should be found").isNotNull();
    JavaClasses classesWithTestClassesExclusions = ArchUtils.importAllClassesInPackage(new RootClassFolder("./target"), "", Arrays.asList("test-classes"));
    assertThat(containsClassWithPattern(classesWithTestClassesExclusions, "ClassToExclude")).as("when 'test-classes' pattern configured, ClassToExclude should still be found").isTrue();
    assertThat(classes.size()).as("There should be less classes loaded when we apply the test-classes exclusion").isGreaterThan(classesWithTestClassesExclusions.size());
    JavaClasses classesWithTestClassesAndSpecificExclusions = ArchUtils.importAllClassesInPackage(new RootClassFolder("./target"), "", Arrays.asList("test-classes", "ClassToExclude"));
    assertThat(containsClassWithPattern(classesWithTestClassesAndSpecificExclusions, "ClassToExclude")).as("when 'ClassToExclude' pattern configured, ClassToExclude should not  be found").isFalse();
    assertThat(classesWithTestClassesAndSpecificExclusions.size() + 1).as("with a specific exclusion; we should have one less class than without").isEqualTo(classesWithTestClassesExclusions.size());
}
Also used : JavaClasses(com.tngtech.archunit.core.domain.JavaClasses) JavaClass(com.tngtech.archunit.core.domain.JavaClass) RootClassFolder(com.societegenerale.commons.plugin.model.RootClassFolder) Test(org.junit.Test)

Aggregations

JavaClass (com.tngtech.archunit.core.domain.JavaClass)234 Test (org.junit.Test)183 JavaClasses (com.tngtech.archunit.core.domain.JavaClasses)65 JavaMethod (com.tngtech.archunit.core.domain.JavaMethod)42 ConditionEvents (com.tngtech.archunit.lang.ConditionEvents)25 ArchCondition (com.tngtech.archunit.lang.ArchCondition)24 SimpleConditionEvent (com.tngtech.archunit.lang.SimpleConditionEvent)20 JavaMethodCall (com.tngtech.archunit.core.domain.JavaMethodCall)19 List (java.util.List)19 JavaFieldAccess (com.tngtech.archunit.core.domain.JavaFieldAccess)16 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)15 DescribedPredicate (com.tngtech.archunit.base.DescribedPredicate)12 ClassFileImporter (com.tngtech.archunit.core.importer.ClassFileImporter)12 Serializable (java.io.Serializable)12 JavaField (com.tngtech.archunit.core.domain.JavaField)11 Test (org.junit.jupiter.api.Test)11 JavaCodeUnit (com.tngtech.archunit.core.domain.JavaCodeUnit)10 JavaConstructor (com.tngtech.archunit.core.domain.JavaConstructor)10 Collectors (java.util.stream.Collectors)10 JavaType (com.tngtech.archunit.core.domain.JavaType)9