elharo commented on code in PR #990:
URL: https://github.com/apache/maven-enforcer/pull/990#discussion_r3813355443


##########
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 {
+        Field field = EnforceBytecodeVersion.class.getDeclaredField(name);
+        field.setAccessible(true);
+        field.set(rule, value);
+    }
+
+    @SuppressWarnings("unchecked")
+    private static List<Dependency> filterDependencies(EnforceBytecodeVersion 
rule, List<Dependency> dependencies)
+            throws Exception {
+        Method method = 
EnforceBytecodeVersion.class.getDeclaredMethod("filterDependencies", 
List.class);
+        method.setAccessible(true);
+        return (List<Dependency>) method.invoke(rule, dependencies);
+    }

Review Comment:
   I agree with the diagnosis, but the fix should probably be to use an 
integration test rather than a unit test.



-- 
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]

Reply via email to