-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceTest.java
More file actions
48 lines (40 loc) · 2.25 KB
/
ServiceTest.java
File metadata and controls
48 lines (40 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package fr.arthurrousseau.archunit;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
import com.tngtech.archunit.core.domain.properties.HasName;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import com.tngtech.archunit.library.freeze.FreezingArchRule;
import org.springframework.stereotype.Service;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
@AnalyzeClasses(packages = "fr.arthurrousseau.archunit")
public class ServiceTest {
@ArchTest
public static final ArchRule SERVICES_SHOULD_CALL_LOGGER_RULE = FreezingArchRule.freeze(methods().that()
.areDeclaredInClassesThat(
DescribedPredicate.or(
HasName.Predicates.nameEndingWith("ServiceImpl"),
CanBeAnnotated.Predicates.annotatedWith(Service.class)
))
.should(new ArchCondition<>("call logger") {
@Override
public void check(JavaMethod javaMethod, ConditionEvents conditionEvents) {
var callsLogger = javaMethod.getCallsFromSelf().stream().anyMatch(it -> it.getTargetOwner().getName().equals("org.slf4j.Logger"));
if (!callsLogger) {
var message = String.format("Method %s.%s() doesn't log anything", javaMethod.getOwner().getName(), javaMethod.getName());
conditionEvents.add(SimpleConditionEvent.violated(javaMethod, message));
}
}
}));
@ArchTest
public static final ArchRule SERVICE_RULE = classes().that().areAnnotatedWith(Service.class).or().haveSimpleNameEndingWith("ServiceImpl")
.should().resideInAPackage("..service.impl..")
.andShould().haveSimpleNameEndingWith("ServiceImpl")
.andShould().beAnnotatedWith(Service.class);
}