gnodet-bot commented on code in PR #1160: URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4232429823
########## src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java: ########## @@ -0,0 +1,124 @@ +/* + * 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 javax.tools.Diagnostic; +import javax.tools.JavaFileObject; + +import java.util.List; +import java.util.Locale; + +import org.apache.maven.api.plugin.Log; +import org.apache.maven.impl.DefaultMessageBuilderFactory; +import org.junit.jupiter.api.Test; + +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.ArgumentMatchers.same; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class DiagnosticLoggerTest { + @Test + void fallsBackToKindAndCodeWhenDiagnosticMessageCannotBeFormatted() { + var logger = mock(Log.class); + @SuppressWarnings("unchecked") + Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class); + when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new RuntimeException("missing type")); + when(diagnostic.getKind()).thenReturn(Diagnostic.Kind.WARNING); + when(diagnostic.getSource()).thenReturn(null); + when(diagnostic.getLineNumber()).thenReturn(Diagnostic.NOPOS); + when(diagnostic.getColumnNumber()).thenReturn(Diagnostic.NOPOS); + when(diagnostic.getCode()).thenReturn("compiler.warn.has.been.deprecated"); + + var listener = new DiagnosticLogger(logger, new DefaultMessageBuilderFactory(), null, null); + listener.report(diagnostic); + + verify(logger).warn((String) + argThat(message -> ((String) message).contains("WARNING: compiler.warn.has.been.deprecated"))); + } + + @Test + void fallsBackToKindAndCodeWhenDiagnosticStringCannotBeFormatted() { + var logger = mock(Log.class); + @SuppressWarnings("unchecked") + Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class); + when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new RuntimeException("missing type")); + when(diagnostic.getKind()).thenReturn(Diagnostic.Kind.WARNING); + when(diagnostic.getSource()).thenReturn(null); + when(diagnostic.getLineNumber()).thenReturn(Diagnostic.NOPOS); + when(diagnostic.getColumnNumber()).thenReturn(Diagnostic.NOPOS); + when(diagnostic.getCode()).thenReturn("compiler.warn.has.been.deprecated"); + + var listener = new DiagnosticLogger(logger, new DefaultMessageBuilderFactory(), null, null); Review Comment: ⚠️ **Duplicate test — `fallsBackToKindAndCodeWhenDiagnosticStringCannotBeFormatted` is now identical to `fallsBackToKindAndCodeWhenDiagnosticMessageCannotBeFormatted`** Both methods used to differ by the `toString()` mock: the first stubbed `toString()` to return a string, the second stubbed it to throw. Removing both stubs collapsed them into identical setups and identical assertions — they now exercise exactly the same code path. The second test should be updated to cover the scenario it was originally designed for: when `diagnostic.toString()` also throws. Since the production code no longer calls `toString()` at all (it goes straight to `getKind() + code`), this test is now redundant. Either remove it or make it explicitly verify that no `toString()` call is made (e.g. using a strict mock or `verify(diagnostic, never()).toString()`). ########## src/it/MCOMPILER-1150/verify.groovy: ########## @@ -0,0 +1,22 @@ +/* + * 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. + */ + +def buildLog = new File(basedir, 'build.log') +assert buildLog.isFile() : 'Expected the Maven Invoker build log' +assert buildLog.text.contains('Cannot format compiler diagnostic; falling back to its string representation.') : 'Expected the diagnostic-formatting fallback to run' Review Comment: 🐛 **Bug: stale log message — IT will always fail** This assertion checks for the old log message `'falling back to its string representation.'`, but `DiagnosticLogger.java` was updated (in commit 87ac535) to emit `'falling back to its kind and code.'` instead. The integration test will fail whenever the fallback path is exercised on JDK 25+. ```suggestion assert buildLog.text.contains('Cannot format compiler diagnostic; falling back to its kind and code.') : 'Expected the diagnostic-formatting fallback to run' ``` -- 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]
