gnodet commented on code in PR #12694:
URL: https://github.com/apache/maven/pull/12694#discussion_r4057975199


##########
impl/maven-logging/src/test/java/org/apache/maven/slf4j/MavenJulHandlerTest.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.slf4j;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.logging.Level;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.slf4j.spi.LocationAwareLogger;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * Tests for {@link MavenJulHandler}, focused on the JUL→SLF4J level
+ * mapping and metadata lifecycle.
+ */
+class MavenJulHandlerTest {
+
+    /**
+     * Table test for JUL level → SLF4J level mapping.
+     * Verifies the mapping documented in the class Javadoc.
+     */
+    @ParameterizedTest(name = "JUL {0} -> SLF4J level {1}")
+    @CsvSource({
+        "FINEST, 0", // TRACE_INT = 0
+        "FINER, 10", // DEBUG_INT = 10
+        "FINE, 10", // DEBUG_INT = 10
+        "CONFIG, 20", // INFO_INT = 20
+        "INFO, 20", // INFO_INT = 20
+        "WARNING, 30", // WARN_INT = 30
+        "SEVERE, 40", // ERROR_INT = 40
+    })
+    void julLevelMapsToCorrectSlf4jLevel(String julLevelName, int 
expectedSlf4jLevel) throws Exception {
+        Level julLevel = Level.parse(julLevelName);
+        int actual = invokeJulLevelToSlf4j(julLevel);
+        assertEquals(
+                expectedSlf4jLevel, actual, "JUL " + julLevelName + " should 
map to SLF4J level " + expectedSlf4jLevel);
+    }
+
+    /**
+     * Verify that FINEST maps to TRACE (not DEBUG) — this is the key
+     * distinction for the TRACE/DEBUG separation.
+     */
+    @Test
+    void finestMapsToTrace() throws Exception {
+        assertEquals(
+                LocationAwareLogger.TRACE_INT,
+                invokeJulLevelToSlf4j(Level.FINEST),
+                "FINEST should map to TRACE, not DEBUG");
+    }
+
+    /**
+     * Verify that CONFIG maps to INFO (not DEBUG) — CONFIG is JUL's
+     * informational level for static configuration, not a debug level.
+     */
+    @Test
+    void configMapsToInfo() throws Exception {
+        assertEquals(LocationAwareLogger.INFO_INT, 
invokeJulLevelToSlf4j(Level.CONFIG), "CONFIG should map to INFO");
+    }
+
+    /**
+     * Verify that JUL metadata is null when no log event is being processed.
+     */
+    @Test
+    void julMetadataIsNullOutsidePublish() {
+        assertNull(MavenJulHandler.getJulMetadata(), "JUL metadata should be 
null outside a publish() call");
+    }
+
+    /**
+     * Verify that a recursive call to {@link MavenJulHandler#publish} from
+     * within a {@code publish()} call on the same thread is silently dropped
+     * instead of crashing with {@code IllegalStateException("Recursive 
update")}.
+     * <p>
+     * This is the reentrancy scenario that occurs when JLine's
+     * {@code StyleResolver} logs a JUL DEBUG event while
+     * {@code MavenSimpleLogger.renderLevel()} is lazily initialising ANSI
+     * colour strings during terminal construction.
+     */
+    @Test
+    void publishIsReentrantSafe() throws Exception {
+        MavenJulHandler handler = new MavenJulHandler();
+        java.util.logging.LogRecord outerRecord = new 
java.util.logging.LogRecord(Level.INFO, "outer");
+
+        // Simulate re-entrance: set IN_PUBLISH=true before calling publish().
+        // The reentrancy guard should short-circuit without throwing or 
logging.
+        java.util.logging.Logger julLogger =
+                java.util.logging.LogManager.getLogManager().getLogger("");
+        java.util.logging.Handler[] saved = julLogger.getHandlers();
+        for (java.util.logging.Handler h : saved) {
+            julLogger.removeHandler(h);
+        }
+
+        // The test verifies that publish() does not throw.
+        // We can't easily simulate the full SLF4J pipeline here, so we just
+        // call publish() with a null-logger-name record (which returns early
+        // before reaching SLF4J) after setting IN_PUBLISH to true, verifying
+        // the guard works.
+        java.lang.reflect.Field inPublishField = 
MavenJulHandler.class.getDeclaredField("IN_PUBLISH");
+        inPublishField.setAccessible(true);
+        @SuppressWarnings("unchecked")
+        ThreadLocal<Boolean> inPublish = (ThreadLocal<Boolean>) 
inPublishField.get(null);
+
+        // Simulate being inside publish()
+        inPublish.set(Boolean.TRUE);
+        try {
+            // A nested call should be dropped without throwing
+            handler.publish(outerRecord);
+            // If we reach here, the guard worked correctly
+        } finally {
+            inPublish.remove();
+            // Restore handlers
+            for (java.util.logging.Handler h : saved) {
+                julLogger.addHandler(h);
+            }
+        }

Review Comment:
   Fixed in b0a1155e82: added a package-private `setInPublishForTest(boolean)` 
method to `MavenJulHandler` so the test no longer needs reflection to access 
`IN_PUBLISH`. The unnecessary JUL root-handler save/restore noise is also 
removed — it was unrelated to the guard check.



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