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


##########
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:
   The regression test reaches into EnforceBytecodeVersion internals via 
reflection (getDeclaredMethod/setAccessible and field mutation). This is 
brittle (renames/visibility changes break tests) and makes the test less 
representative of how the rule is configured/used. Consider exposing a 
package-private helper (e.g., make filterDependencies package-private and add 
package-private setters for includes/excludes, or extract a small filter 
component) so the test can call it directly without reflective access.



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