Skip to content

Commit c8d1a3d

Browse files
Ensure packages are added in runner checker
fix package loading to ensure only package-info is loaded, not the whole package Co-authored-by: John Burns <[email protected]>
1 parent e9b1f19 commit c8d1a3d

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

nebula-archrules-core/src/main/java/com/netflix/nebula/archrules/core/Runner.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@
22

33
import com.tngtech.archunit.core.domain.JavaClasses;
44
import com.tngtech.archunit.core.importer.ClassFileImporter;
5+
import com.tngtech.archunit.core.importer.Location;
6+
import com.tngtech.archunit.core.importer.Locations;
57
import com.tngtech.archunit.lang.ArchRule;
68
import com.tngtech.archunit.lang.ConditionEvents;
79
import com.tngtech.archunit.lang.EvaluationResult;
810

11+
import java.net.MalformedURLException;
12+
import java.net.URI;
13+
import java.net.URL;
14+
import java.util.*;
15+
import java.util.stream.Collectors;
16+
917
public class Runner {
18+
/**
19+
* Check a rule against some classes.
20+
* This can be invoked from the real Gradle plugin or unit tests for rules to ensure the same logic is observed there.
21+
*
22+
* @param rule the rule to run
23+
* @param classesToCheck the classes to run the rule against
24+
* @return the result, which contains information about failure
25+
*/
1026
public static EvaluationResult check(ArchRule rule, JavaClasses classesToCheck) {
1127
try {
1228
return rule.evaluate(classesToCheck);
@@ -24,16 +40,35 @@ public static EvaluationResult check(ArchRule rule, JavaClasses classesToCheck)
2440
}
2541

2642
/**
27-
* Check a rule against some classes.
28-
* This can be invoked from the real Gradle plugin or unit tests for rules to ensure the same logic is observed there.
43+
* Check a rule against some classes and their package-info
44+
* This intended to be used from unit tests for rules to ensure the same logic is observed there.
2945
*
3046
* @param rule the rule to run
3147
* @param classesToCheck the classes to run the rule against
3248
* @return the result, which contains information about failure
3349
*/
3450
public static EvaluationResult check(ArchRule rule, Class<?>... classesToCheck) {
51+
Set<Location> locs = Arrays.stream(classesToCheck)
52+
.map(Locations::ofClass)
53+
.flatMap(Collection::stream)
54+
.collect(Collectors.toSet());
55+
List<URL> uris = Arrays.stream(classesToCheck)
56+
.map(clazz -> clazz.getPackage().getName())
57+
.map(Locations::ofPackage)
58+
.flatMap(it -> it.stream().map(Location::asURI))
59+
.map(u -> URI.create(u.toASCIIString() + "package-info.class"))
60+
.map(uri -> {
61+
try {
62+
return uri.toURL();
63+
} catch (MalformedURLException e) {
64+
return null;
65+
}
66+
})
67+
.filter(Objects::nonNull)
68+
.collect(Collectors.toList());
69+
locs.addAll(Locations.of(uris));
3570
final JavaClasses classes = new ClassFileImporter()
36-
.importClasses(classesToCheck);
71+
.importLocations(locs);
3772
return check(rule, classes);
3873
}
3974
}

nebula-archrules-core/src/test/java/com/netflix/nebula/archrules/core/RunnerTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package com.netflix.nebula.archrules.core;
22

3+
import com.netflix.nebula.archrules.testpackage.TestDeprecated;
4+
import com.tngtech.archunit.core.domain.JavaClass;
5+
import com.tngtech.archunit.lang.ArchCondition;
36
import com.tngtech.archunit.lang.ArchRule;
7+
import com.tngtech.archunit.lang.ConditionEvents;
48
import com.tngtech.archunit.lang.EvaluationResult;
59
import com.tngtech.archunit.lang.Priority;
10+
import com.tngtech.archunit.lang.SimpleConditionEvent;
611
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
712
import org.junit.jupiter.api.Test;
813

914
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleName;
1015
import static org.assertj.core.api.Assertions.assertThat;
1116

1217
public class RunnerTest {
18+
private final ArchRule noDeprecatedRule = ArchRuleDefinition.classes().should()
19+
.notBeAnnotatedWith(Deprecated.class);
20+
1321
@Test
1422
public void test_pass() {
1523
final EvaluationResult result = Runner.check(noDeprecatedRule, PassingClass.class);
@@ -36,8 +44,6 @@ static class PassingClass {
3644
static class FailingClass {
3745
}
3846

39-
private final ArchRule noDeprecatedRule = ArchRuleDefinition.classes().should()
40-
.notBeAnnotatedWith(Deprecated.class);
4147
private final ArchRule smokeTestRule = ArchRuleDefinition.priority(Priority.MEDIUM)
4248
.classes().that(simpleName("SmokeTest"))
4349
.should().beAnnotatedWith(Deprecated.class)
@@ -63,4 +69,25 @@ public void test_smoke_fail() {
6369
assertThat(result.getFailureReport().getDetails())
6470
.contains(NoClassesMatchedEvent.NO_MATCH_MESSAGE);
6571
}
72+
73+
private final ArchCondition<JavaClass> notBeInDeprecatedPackage =
74+
new ArchCondition<JavaClass>("not be in a package marked with @Deprecated") {
75+
@Override
76+
public void check(JavaClass javaClass, ConditionEvents events) {
77+
boolean isInDeprecatedPackage = javaClass.getPackage().getPackageInfo().isAnnotatedWith(Deprecated.class);
78+
if (isInDeprecatedPackage) {
79+
String message = String.format("Class %s is in a package marked with @Deprecated", javaClass.getName());
80+
events.add(SimpleConditionEvent.violated(javaClass, message));
81+
}
82+
}
83+
};
84+
private final ArchRule noDeprecatedPackageRule = ArchRuleDefinition
85+
.classes().should(notBeInDeprecatedPackage);
86+
87+
@Test
88+
public void test_package_deprecated_rule() {
89+
final EvaluationResult result = Runner.check(noDeprecatedPackageRule, TestDeprecated.class);
90+
assertThat(result.hasViolation()).isTrue();
91+
}
92+
6693
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.netflix.nebula.archrules.testpackage;
2+
3+
public class TestDeprecated {
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@Deprecated
2+
package com.netflix.nebula.archrules.testpackage;

nebula-archrules-gradle-plugin/gradle.lockfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
cglib:cglib-nodep:3.2.2=testRuntimeClasspath
55
com.fasterxml.jackson.core:jackson-annotations:2.20=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
66
com.jayway.jsonpath:json-path:2.9.0=testCompileClasspath,testRuntimeClasspath
7-
com.netflix.nebula:archrules-deprecation:0.3.0=archRules
8-
com.netflix.nebula:archrules-joda:0.3.0=archRules
9-
com.netflix.nebula:archrules-testing-frameworks:0.3.0=archRules
10-
com.netflix.nebula:nebula-archrules-core:0.2.3=archRules
7+
com.netflix.nebula:archrules-deprecation:0.4.0=archRules
8+
com.netflix.nebula:archrules-joda:0.4.0=archRules
9+
com.netflix.nebula:archrules-testing-frameworks:0.4.0=archRules
10+
com.netflix.nebula:nebula-archrules-core:0.3.0=archRules
1111
com.netflix.nebula:nebula-test:11.9.0=testCompileClasspath,testRuntimeClasspath
1212
com.tngtech.archunit:archunit:1.4.1=archRules,compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
1313
net.bytebuddy:byte-buddy:1.17.7=testCompileClasspath,testRuntimeClasspath

0 commit comments

Comments
 (0)