elharo commented on code in PR #990:
URL: https://github.com/apache/maven-enforcer/pull/990#discussion_r3838495370
##########
enforcer-rules/src/test/java/org/apache/maven/enforcer/rules/dependency/EnforceBytecodeVersionTest.java:
##########
@@ -18,17 +18,72 @@
*/
package org.apache.maven.enforcer.rules.dependency;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
import java.util.stream.Stream;
+import org.eclipse.aether.artifact.DefaultArtifact;
+import org.eclipse.aether.graph.Dependency;
+import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.params.provider.Arguments.arguments;
+import static org.mockito.Mockito.mock;
class EnforceBytecodeVersionTest {
+ @Test
+ void filterDependenciesExcludesMatchesWithoutIncludes() throws Exception {
+ EnforceBytecodeVersion rule = newRule();
+ setField(rule, "excludes",
Collections.singletonList("org.example:excluded"));
+
+ Dependency included = dependency("org.example", "included");
+ Dependency excluded = dependency("org.example", "excluded");
+
+ assertEquals(Collections.singletonList(included),
filterDependencies(rule, Arrays.asList(included, excluded)));
+ }
+
+ @Test
+ void filterDependenciesIncludesOverrideExcludes() throws Exception {
+ EnforceBytecodeVersion rule = newRule();
+ setField(rule, "excludes", Collections.singletonList("org.example:*"));
+ setField(rule, "includes",
Collections.singletonList("org.example:included"));
+
+ Dependency included = dependency("org.example", "included");
+ Dependency excluded = dependency("org.example", "excluded");
+
+ assertEquals(Collections.singletonList(included),
filterDependencies(rule, Arrays.asList(included, excluded)));
+ }
+
+ private static EnforceBytecodeVersion newRule() {
+ return new EnforceBytecodeVersion(
+ mock(org.apache.maven.execution.MavenSession.class),
mock(ResolverUtil.class));
+ }
+
+ private static Dependency dependency(String groupId, String artifactId) {
+ return new Dependency(new DefaultArtifact(groupId + ":" + artifactId +
":jar:1.0"), "compile");
+ }
+
+ private static void setField(EnforceBytecodeVersion rule, String name,
List<String> value) throws Exception {
Review Comment:
don't use reflection
##########
enforcer-rules/src/main/java/org/apache/maven/enforcer/rules/dependency/EnforceBytecodeVersion.java:
##########
@@ -528,7 +528,7 @@ private List<Dependency>
filterDependencies(List<Dependency> dependencies) {
Predicate<Dependency> includeExcludeMatcher = d -> true;
if (includes != null || excludes != null) {
ArtifactMatcher artifactMatcher = new ArtifactMatcher(excludes,
includes);
- includeExcludeMatcher = d ->
artifactMatcher.match(ArtifactUtils.toArtifact(d));
+ includeExcludeMatcher = d ->
!artifactMatcher.match(ArtifactUtils.toArtifact(d));
Review Comment:
It's not obvious from the code whether the not is needed or not. The word
"filter" is ambiguous. It needs comments explaining what is expected that
leaves no room for interpretation.
Avoiding lambdas would also make this clearer. As is I can't really tell
what it's doing or what it's intended to do which is how we ended up with a bug
in the first place.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]