This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 7377a89  Fix NPEs in StructuredDataLookup and improve relevant tests. 
(#720)
7377a89 is described below

commit 7377a8942c62db21c04e41fa79c6fb5b765aa6ae
Author: Arie van Deursen <[email protected]>
AuthorDate: Tue Feb 8 12:39:53 2022 +0100

    Fix NPEs in StructuredDataLookup and improve relevant tests. (#720)
    
    This commit strengthens the test suite for the `StructuredDataLookup` class 
so that it achieves full branch coverage.
    
    The corner cases revealed the problem that a lookup of a null key with a 
correct event led to a null pointer exception. This is inconsistent with the 
javadoc ("can be null") and the other lookup classes.
    
    The commit fixes the null pointer exception for the `key` by replacing 
`key.equals("id")` by `"id".equals(key)`, in two places.
    
    It also introduces explicit constants for the string literals "id" and 
"type".
---
 .../log4j/core/lookup/StructuredDataLookup.java    | 18 +++++++---
 .../core/lookup/StructuredDataLookupTest.java      | 40 +++++++++++++++++-----
 2 files changed, 45 insertions(+), 13 deletions(-)

diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java
index 379d6a9..c5dcf2d 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java
@@ -27,8 +27,18 @@ import 
org.apache.logging.log4j.message.StructuredDataMessage;
 public class StructuredDataLookup implements StrLookup {
 
     /**
+     * Key to obtain the id of a structured message.
+     */
+    public static final String ID_KEY = "id";
+
+    /**
+     * Key to obtain the type of a structured message.
+     */
+    public static final String TYPE_KEY = "type";
+
+    /**
      * Returns {@code null}. This Lookup plugin does not make sense outside 
the context of a LogEvent.
-     * @param key  the key to be looked up, may be null
+     * @param key  The key to be looked up, may be null.
      * @return {@code null}
      */
     @Override
@@ -39,7 +49,7 @@ public class StructuredDataLookup implements StrLookup {
     /**
      * Looks up the value for the key using the data in the LogEvent.
      * @param event The current LogEvent.
-     * @param key  the key to be looked up, may be null
+     * @param key  The key to be looked up, may be null.
      * @return The value associated with the key.
      */
     @Override
@@ -48,9 +58,9 @@ public class StructuredDataLookup implements StrLookup {
             return null;
         }
         final StructuredDataMessage msg = (StructuredDataMessage) 
event.getMessage();
-        if (key.equalsIgnoreCase("id")) {
+        if (ID_KEY.equalsIgnoreCase(key)) {
             return msg.getId().getName();
-        } else if (key.equalsIgnoreCase("type")) {
+        } else if (TYPE_KEY.equalsIgnoreCase(key)) {
             return msg.getType();
         }
         return msg.get(key);
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java
index 737ff41..92efd9e 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java
@@ -21,23 +21,45 @@ import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.StructuredDataMessage;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class StructuredDataLookupTest {
 
-    private static final String TESTKEY = "type";
-    private static final String TESTVAL = "Audit";
+    private StructuredDataLookup dataLookup;
+
+    @BeforeEach
+    public void setUp() {
+        dataLookup = new StructuredDataLookup();
+    }
 
     @Test
-    public void testLookup() {
-        final Message msg = new StructuredDataMessage("Test", "This is a 
test", "Audit");
+    public void testCorrectEvent() {
+        final Message msg = new StructuredDataMessage("TestId", "This is a 
test", "Audit");
         final LogEvent event = 
Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(msg).build();
-        final StrLookup lookup = new StructuredDataLookup();
-        String value = lookup.lookup(event, TESTKEY);
-        assertEquals(TESTVAL, value);
-        value = lookup.lookup("BadKey");
-        assertNull(value);
+
+        assertEquals("Audit", dataLookup.lookup(event, 
StructuredDataLookup.TYPE_KEY));
+        assertEquals("Audit", dataLookup.lookup(event, 
StructuredDataLookup.TYPE_KEY.toUpperCase()));
+        assertEquals("TestId", dataLookup.lookup(event, 
StructuredDataLookup.ID_KEY));
+        assertEquals("TestId", dataLookup.lookup(event, 
StructuredDataLookup.ID_KEY.toUpperCase()));
+        assertNull(dataLookup.lookup(event, "BadKey"));
+        assertNull(dataLookup.lookup(event, null));
+    }
+
+    @Test
+    public void testNullLookup() {
+        assertNull(dataLookup.lookup(null, null));
+        assertNull(dataLookup.lookup(null));
+    }
+
+    @Test void testWrongEvent() {
+        LogEvent mockEvent = mock(LogEvent.class);
+        // ensure message is not a StructuredDataMessage
+        when(mockEvent.getMessage()).thenReturn(mock(Message.class));
+        assertNull(dataLookup.lookup(mockEvent, "ignored"));
     }
 }

Reply via email to