generated from it-at-m/oss-repository-en-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTestClassesEndWithTestCondition.java
More file actions
33 lines (26 loc) · 1.29 KB
/
TestClassesEndWithTestCondition.java
File metadata and controls
33 lines (26 loc) · 1.29 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
package de.muenchen.refarch.archunit.rules;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
public class TestClassesEndWithTestCondition extends ArchCondition<JavaMethod> {
TestClassesEndWithTestCondition() {
super("have top enclosing class name ending with `Test`");
}
public static final ArchCondition<JavaMethod> haveTopEnclosingClassEndingWithTest = new TestClassesEndWithTestCondition();
@Override
public void check(JavaMethod method, ConditionEvents events) {
final var topEnclosingClass = getTopEnclosingClass(method.getOwner());
if (!topEnclosingClass.getSimpleName().endsWith("Test")) {
events.add(SimpleConditionEvent.violated(method, "Method %s must be declared in a class whose simple name ends with 'Test' (found: %s)"
.formatted(method.getName(), topEnclosingClass.getSimpleName())));
}
}
private JavaClass getTopEnclosingClass(JavaClass item) {
while (item.getEnclosingClass().isPresent()) {
item = item.getEnclosingClass().orElseThrow();
}
return item;
}
}