mvanhorn commented on code in PR #4474:
URL: https://github.com/apache/logging-log4j2/pull/4474#discussion_r4574916515
##########
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:
Agreed, both pass on 2.x already since #4116. Dropped them in bdba13c, along
with the unused imports.
The same commit adds the status log check to `KeyValuePairTest` from your
other comment, so the test now pins the `A key must be provided for
KeyValuePair` error.
<!-- r:7bee2ad06016.dd0ef7a54484 -->
--
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]