Skip to content

Commit

Permalink
Added more tests for correct use of trace links in api
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Nov 29, 2024
1 parent 6586bb6 commit 8be7d03
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;

import java.util.List;

import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.junit.AnalyzeClasses;
Expand All @@ -19,6 +21,21 @@ public boolean test(JavaClass clazz) {
}
};

private static final DescribedPredicate<List<JavaClass>> isSubclassOfTraceLinkList = new DescribedPredicate<>("subclass of " + TraceLink.class) {
@Override
public boolean test(List<JavaClass> clazzes) {
return clazzes.stream().anyMatch(clazz -> clazz.isAssignableTo(TraceLink.class));
}
};

private static final DescribedPredicate<List<JavaClass>> isSubclassOfTraceLinkListButNotTracelink = new DescribedPredicate<>(
"subclass of " + TraceLink.class + " without base class") {
@Override
public boolean test(List<JavaClass> clazzes) {
return clazzes.stream().anyMatch(clazz -> clazz.isAssignableTo(TraceLink.class) && !clazz.isEquivalentTo(TraceLink.class));
}
};

@ArchTest
static final ArchRule onlyTraceLinkAsReturnType = methods().that()
.haveRawReturnType(isSubclassOfTraceLink)
Expand All @@ -27,4 +44,13 @@ public boolean test(JavaClass clazz) {
.should()
.haveRawReturnType(TraceLink.class)
.because("the specific subclasses of TraceLink shall not be used as return type in non-private methods");

@ArchTest
static final ArchRule onlyTraceLinkAsParameterType = methods().that()
.haveRawParameterTypes(isSubclassOfTraceLinkList)
.and()
.areNotPrivate()
.should()
.notHaveRawParameterTypes(isSubclassOfTraceLinkListButNotTracelink)
.because("the specific subclasses of TraceLink shall not be used as parameter type in non-private methods");
}

0 comments on commit 8be7d03

Please sign in to comment.