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()));
}
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());
}
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();
}
};
}
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);
}
};
}
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());
}
Aggregations