ramanathan1504 commented on code in PR #4474:
URL: https://github.com/apache/logging-log4j2/pull/4474#discussion_r4573235905
##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/builder/ConfigurationBuilderTest.java:
##########
@@ -116,4 +120,37 @@ void testXmlConstructing() {
final String xmlConfiguration = builder.toXmlConfiguration();
assertEquals(expectedXml, xmlConfiguration);
}
+
+ @Test
+ void testNewKeyValuePairRejectsNullOrEmptyKey() {
+ final ConfigurationBuilder<?> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
+ assertThrows(IllegalArgumentException.class, () ->
builder.newKeyValuePair(null, "v"));
+ assertThrows(IllegalArgumentException.class, () ->
builder.newKeyValuePair("", "v"));
+ assertNotNull(builder.newKeyValuePair(" ", "v"));
+ }
+
+ @Test
+ void testNewKeyValuePairOmitsNullValue() {
+ final ConfigurationBuilder<?> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
+ builder.add(builder.newAppender("List", "List")
+ .add(builder.newLayout("GelfLayout")
+ .setAttribute("host", "my-host")
+ .addComponent(builder.newKeyValuePair("k", null))));
+ final String xml = builder.toXmlConfiguration();
+ assertTrue(xml.contains("key=\"k\""));
+ assertFalse(xml.contains("value="));
+ assertFalse(xml.contains("value=\"null\""));
+ }
+
+ @Test
+ void testSetKeyNullRemovesKeyAttribute() {
+ final ConfigurationBuilder<?> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
+ builder.add(builder.newAppender("List", "List")
+ .add(builder.newLayout("GelfLayout")
+ .setAttribute("host", "my-host")
+ .addComponent(builder.newKeyValuePair("k",
"v").setKey(null))));
+ final String xml = builder.toXmlConfiguration();
+ assertTrue(xml.contains("value=\"v\""));
+ assertFalse(xml.contains("key="));
+ }
Review Comment:
These two tests pass on `2.x` without this change, since #4116 already drops
null attributes. Can we drop them so the PR only tests the key check? `./mvnw
spotless:apply` then removes the unused `assertFalse` and `assertTrue` imports.
```suggestion
```
##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/KeyValuePairTest.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.logging.log4j.core.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
+import org.apache.logging.log4j.core.test.appender.ListAppender;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link KeyValuePair}.
+ */
+class KeyValuePairTest {
+
+ @Test
+ void testRejectsNullOrEmptyKey() {
+ assertThrows(IllegalArgumentException.class, () -> new
KeyValuePair(null, "v"));
+ assertThrows(IllegalArgumentException.class, () -> new
KeyValuePair("", "v"));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> KeyValuePair.newBuilder().setValue("v").build());
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> KeyValuePair.newBuilder().setKey("").build());
+ }
+
+ @Test
+ void testPreservesKeyAndValue() {
+ final KeyValuePair pair = new KeyValuePair("extraField", "extraValue");
+ assertEquals("extraField", pair.getKey());
+ assertEquals("extraValue", pair.getValue());
+
+ final KeyValuePair built = KeyValuePair.newBuilder()
+ .setKey("extraField")
+ .setValue("extraValue")
+ .build();
+ assertEquals("extraField", built.getKey());
+ assertEquals("extraValue", built.getValue());
+ }
+
+ @Test
+ void testAcceptsNullValueAndWhitespaceKey() {
+ final KeyValuePair nullValue = new KeyValuePair("k", null);
+ assertEquals("k", nullValue.getKey());
+ assertNull(nullValue.getValue());
+
+ final KeyValuePair built =
+ KeyValuePair.newBuilder().setKey("k").setValue(null).build();
+ assertEquals("k", built.getKey());
+ assertNull(built.getValue());
+
+ final KeyValuePair space = new KeyValuePair(" ", "v");
+ assertEquals(" ", space.getKey());
+ assertEquals("v", space.getValue());
+ }
+
+ @Test
+ void testKeylessPairIsOmittedFromGelfLayout() throws Exception {
+ final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ + "<Configuration status=\"OFF\" name=\"KeyValuePairTest\">"
+ + "<Appenders>"
+ + "<List name=\"List\">"
+ + "<GelfLayout host=\"myhost\" compressionType=\"OFF\">"
+ + "<KeyValuePair value=\"bar\"/>"
+ + "</GelfLayout>"
+ + "</List>"
+ + "</Appenders>"
+ + "<Loggers>"
+ + "<Root level=\"info\">"
+ + "<AppenderRef ref=\"List\"/>"
+ + "</Root>"
+ + "</Loggers>"
+ + "</Configuration>";
+ final byte[] bytes = xml.getBytes(StandardCharsets.UTF_8);
+ try (final LoggerContext context = new
LoggerContext("KeyValuePairTest")) {
+ final XmlConfiguration configuration =
+ new XmlConfiguration(context, new ConfigurationSource(new
ByteArrayInputStream(bytes)));
+ context.start(configuration);
+ final ListAppender list = configuration.getAppender("List");
+ assertNotNull(list);
+ assertTrue(list.isStarted());
+ context.getLogger(KeyValuePairTest.class).info("hello");
+ assertEquals(1, list.getMessages().size());
+ final String payload = list.getMessages().get(0);
+ assertTrue(payload.contains("\"short_message\":\"hello\""));
+ assertFalse(payload.contains("bar"));
+ assertFalse(payload.contains("null"));
Review Comment:
No test fails when the `@Required` line in `KeyValuePair` is removed.
Without it the status log shows a stack trace and `No factory method found`
instead of `A key must be provided for KeyValuePair`. Can we check the status
log here? The test then needs `@UsingStatusListener`, a `ListStatusListener
listener` parameter, and the imports for `Level`, `ListStatusListener` and
`UsingStatusListener`.
```suggestion
assertFalse(payload.contains("null"));
assertTrue(listener.findStatusData(Level.ERROR)
.anyMatch(data -> "A key must be provided for
KeyValuePair"
.equals(data.getMessage().getFormattedMessage())));
```
##########
src/changelog/.2.x.x/3472_reject_null_KeyValuePair_key.xml:
##########
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns="https://logging.apache.org/xml/ns"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="
+ https://logging.apache.org/xml/ns
+ https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="fixed">
+ <issue id="3472"
link="https://github.com/apache/logging-log4j2/issues/3472"/>
+ <description format="asciidoc">
+ Reject a `KeyValuePair` with a `null` or empty key during construction and
configuration.
Review Comment:
Can we shorten this to one line under 64 characters?
```suggestion
Reject a `KeyValuePair` without a key at configuration time.
```
--
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]