gnodet-bot commented on code in PR #1158:
URL: 
https://github.com/apache/maven-compiler-plugin/pull/1158#discussion_r4605886838


##########
src/test/java/org/apache/maven/plugin/compiler/ByteCodeTransformerTest.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.spi.ToolProvider;
+
+import org.apache.maven.api.plugin.Log;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ModuleVisitor;
+import org.objectweb.asm.Opcodes;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Tests for {@link ByteCodeTransformer#patchJdkModuleVersion}.
+ *
+ * <p>On JDK 24+, surefire prepends {@code META-INF/versions/24/} on the test 
classpath,
+ * so this test exercises the {@code java.lang.classfile}-backed 
implementation.
+ * On JDK 17-23, it exercises the ASM fallback.
+ *
+ * @see <a 
href="https://issues.apache.org/jira/browse/MCOMPILER-542";>MCOMPILER-542</a>
+ */
+class ByteCodeTransformerTest {
+
+    private static final Log LOG = mock(Log.class);
+
+    @Test
+    void returnsNullForNonModuleClass(@TempDir Path tempDir) throws Exception {
+        byte[] bytes = compileRegularClass(tempDir);
+        assertNull(
+                ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", LOG),
+                "Regular class has no ModuleAttribute -> null");
+    }
+
+    @Test
+    void patchesJdkRequiresVersionToTarget(@TempDir Path tempDir) throws 
Exception {
+        byte[] bytes = compileModuleInfo(
+                tempDir, "jdkRequires", "module com.example { requires 
java.base; requires java.logging; }");
+        byte[] result = ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", 
LOG);
+        // javac on JDK 21+ emits requires version attributes for jdk modules.
+        // On JDK 25+ the compiler always emits requires version attributes
+        assertNotNull(result, "javac on JDK 25+ should emit requires version 
attributes");
+        assertTrue(result.length > 0, "Patched result must be non-empty");
+        Map<String, String> versions = readRequiresVersions(result);
+        for (Map.Entry<String, String> entry : versions.entrySet()) {
+            String mod = entry.getKey();
+            if (mod.startsWith("java.") || mod.startsWith("jdk.")) {
+                assertEquals(
+                        "21", entry.getValue(), "JDK module " + mod + " 
requires version should be patched to '21'");
+            }
+        }
+    }
+
+    @Test
+    void patchedBytesAreValidClassFile(@TempDir Path tempDir) throws Exception 
{
+        byte[] bytes = compileModuleInfo(tempDir, "valid", "module com.example 
{ requires java.base; }");
+        byte[] result = ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", 
LOG);
+        if (result != null) {
+            assertTrue(result.length >= 4, "Patched result must be at least 4 
bytes");
+            assertTrue(
+                    result[0] == (byte) 0xCA
+                            && result[1] == (byte) 0xFE
+                            && result[2] == (byte) 0xBA
+                            && result[3] == (byte) 0xBE,
+                    "Patched result must start with CAFEBABE");
+        }
+    }
+
+    @Test
+    void doesNotThrowOnEmptyModule(@TempDir Path tempDir) throws Exception {
+        byte[] bytes = compileModuleInfo(tempDir, "empty", "module com.example 
{ }");
+        ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", LOG);

Review Comment:
   🔍 **Nit:** `doesNotThrowOnEmptyModule` only asserts that no exception is 
thrown, but doesn't assert the return value of `patchJdkModuleVersion`. On JDK 
25+, an empty `module com.example {}` may still receive implicit `requires 
java.base` with a version attribute, making the return non-null — or not. 
Either way, documenting the expected return via an assertion (or at minimum a 
comment explaining why the return value is intentionally ignored) would make 
the test contract explicit.
   
   ```suggestion
           // For an empty module, javac may or may not emit requires version 
attributes for
           // the implicit java.base dependency — either outcome is acceptable; 
we only verify
           // that no exception is thrown during transformation.
           ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", LOG);
   ```



##########
src/test/java/org/apache/maven/plugin/compiler/ByteCodeTransformerTest.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.spi.ToolProvider;
+
+import org.apache.maven.api.plugin.Log;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ModuleVisitor;
+import org.objectweb.asm.Opcodes;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Tests for {@link ByteCodeTransformer#patchJdkModuleVersion}.
+ *
+ * <p>On JDK 24+, surefire prepends {@code META-INF/versions/24/} on the test 
classpath,
+ * so this test exercises the {@code java.lang.classfile}-backed 
implementation.
+ * On JDK 17-23, it exercises the ASM fallback.

Review Comment:
   🔍 **Nit:** The Javadoc says 'On JDK 17-23, it exercises the ASM fallback', 
but the enforcer at `pom.xml` requires JDK `[25,)`. This test can never run on 
JDK 17-23, so this branch of the documentation is dead. Consider removing or 
updating the comment to reflect the actual enforced minimum.



##########
src/test/java/org/apache/maven/plugin/compiler/ByteCodeTransformerTest.java:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.spi.ToolProvider;
+
+import org.apache.maven.api.plugin.Log;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.ModuleVisitor;
+import org.objectweb.asm.Opcodes;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Tests for {@link ByteCodeTransformer#patchJdkModuleVersion}.
+ *
+ * <p>On JDK 24+, surefire prepends {@code META-INF/versions/24/} on the test 
classpath,
+ * so this test exercises the {@code java.lang.classfile}-backed 
implementation.
+ * On JDK 17-23, it exercises the ASM fallback.
+ *
+ * @see <a 
href="https://issues.apache.org/jira/browse/MCOMPILER-542";>MCOMPILER-542</a>
+ */
+class ByteCodeTransformerTest {
+
+    private static final Log LOG = mock(Log.class);
+
+    @Test
+    void returnsNullForNonModuleClass(@TempDir Path tempDir) throws Exception {
+        byte[] bytes = compileRegularClass(tempDir);
+        assertNull(
+                ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", LOG),
+                "Regular class has no ModuleAttribute -> null");
+    }
+
+    @Test
+    void patchesJdkRequiresVersionToTarget(@TempDir Path tempDir) throws 
Exception {
+        byte[] bytes = compileModuleInfo(
+                tempDir, "jdkRequires", "module com.example { requires 
java.base; requires java.logging; }");
+        byte[] result = ByteCodeTransformer.patchJdkModuleVersion(bytes, "21", 
LOG);
+        // javac on JDK 21+ emits requires version attributes for jdk modules.
+        // On JDK 25+ the compiler always emits requires version attributes
+        assertNotNull(result, "javac on JDK 25+ should emit requires version 
attributes");

Review Comment:
   🔍 **Nit:** Assertion failure message describes a javac implementation detail 
rather than what actually failed. If this assertion trips, the message points 
to _why_ (javac emits version attributes on JDK 25+) rather than _what went 
wrong_.
   
   Consider:
   ```suggestion
           assertNotNull(result, "patchJdkModuleVersion returned null — 
expected non-null result when javac emits requires version attributes");
   ```



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