This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/main by this push:
new c5680fe76c Port `log4j-jndi-test` changes from `2.x` (#2163)
c5680fe76c is described below
commit c5680fe76c26fcc3bb5df66c569f805b3b51817b
Author: Volkan Yazıcı <[email protected]>
AuthorDate: Wed Jan 10 16:45:22 2024 +0100
Port `log4j-jndi-test` changes from `2.x` (#2163)
---
.../log4j/jndi/lookup/InterpolatorTest.java | 84 ++++++++++++++++++++--
.../log4j/jndi/lookup/JndiDisabledLookupTest.java | 36 ++--------
.../logging/log4j/jndi/lookup/JndiLookupTest.java | 3 +-
3 files changed, 86 insertions(+), 37 deletions(-)
diff --git
a/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/InterpolatorTest.java
b/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/InterpolatorTest.java
index 3d07d58154..58b68c088a 100644
---
a/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/InterpolatorTest.java
+++
b/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/InterpolatorTest.java
@@ -16,27 +16,35 @@
*/
package org.apache.logging.log4j.jndi.lookup;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
+import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
+import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.ThreadContext;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.lookup.Interpolator;
import org.apache.logging.log4j.core.lookup.MapLookup;
import org.apache.logging.log4j.core.lookup.StrLookup;
import org.apache.logging.log4j.jndi.test.junit.JndiRule;
+import org.apache.logging.log4j.message.StringMapMessage;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
/**
- *
+ * Tests {@link Interpolator}.
*/
public class InterpolatorTest {
@@ -48,7 +56,7 @@ public class InterpolatorTest {
private static final String TEST_CONTEXT_NAME = "app-1";
@ClassRule
- public static RuleChain rules = RuleChain.outerRule(new ExternalResource()
{
+ public static final RuleChain RULES = RuleChain.outerRule(new
ExternalResource() {
@Override
protected void before() throws Throwable {
System.setProperty(TESTKEY, TESTVAL);
@@ -66,6 +74,28 @@ public class InterpolatorTest {
.around(new JndiRule(
JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX +
TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME));
+ @Test
+ public void testGetDefaultLookup() {
+ final Map<String, String> map = new HashMap<>();
+ map.put(TESTKEY, TESTVAL);
+ final MapLookup defaultLookup = new MapLookup(map);
+ final Interpolator interpolator = new Interpolator(defaultLookup);
+ assertEquals(getLookupMap(defaultLookup), getLookupMap((MapLookup)
interpolator.getDefaultLookup()));
+ assertSame(defaultLookup, interpolator.getDefaultLookup());
+ }
+
+ private static Map<String, String> getLookupMap(final MapLookup lookup) {
+ try {
+ final Field mapField = lookup.getClass().getDeclaredField("map");
+ mapField.setAccessible(true);
+ @SuppressWarnings("unchecked")
+ Map<String, String> map = (Map<String, String>)
mapField.get(lookup);
+ return map;
+ } catch (final Exception error) {
+ throw new RuntimeException(error);
+ }
+ }
+
@Test
public void testLookup() {
final Map<String, String> map = new HashMap<>();
@@ -117,4 +147,50 @@ public class InterpolatorTest {
assertLookupNotEmpty(lookup, "java:locale");
assertLookupNotEmpty(lookup, "java:hw");
}
+
+ @Test
+ public void testInterpolatorMapMessageWithNoPrefix() {
+ final HashMap<String, String> configProperties = new HashMap<>();
+ configProperties.put("key", "configProperties");
+ final Interpolator interpolator = new Interpolator(configProperties);
+ final HashMap<String, String> map = new HashMap<>();
+ map.put("key", "mapMessage");
+ final LogEvent event = Log4jLogEvent.newBuilder()
+ .setLoggerName(getClass().getName())
+ .setLoggerFqcn(Logger.class.getName())
+ .setLevel(Level.INFO)
+ .setMessage(new StringMapMessage(map))
+ .build();
+ assertEquals("configProperties", interpolator.lookup(event, "key"));
+ }
+
+ @Test
+ public void testInterpolatorMapMessageWithNoPrefixConfigDoesntMatch() {
+ final Interpolator interpolator = new
Interpolator(Collections.emptyMap());
+ final HashMap<String, String> map = new HashMap<>();
+ map.put("key", "mapMessage");
+ final LogEvent event = Log4jLogEvent.newBuilder()
+ .setLoggerName(getClass().getName())
+ .setLoggerFqcn(Logger.class.getName())
+ .setLevel(Level.INFO)
+ .setMessage(new StringMapMessage(map))
+ .build();
+ assertNull(interpolator.lookup(event, "key"), "Values without a map
prefix should not match MapMessages");
+ }
+
+ @Test
+ public void testInterpolatorMapMessageWithMapPrefix() {
+ final HashMap<String, String> configProperties = new HashMap<>();
+ configProperties.put("key", "configProperties");
+ final Interpolator interpolator = new Interpolator(configProperties);
+ final HashMap<String, String> map = new HashMap<>();
+ map.put("key", "mapMessage");
+ final LogEvent event = Log4jLogEvent.newBuilder()
+ .setLoggerName(getClass().getName())
+ .setLoggerFqcn(Logger.class.getName())
+ .setLevel(Level.INFO)
+ .setMessage(new StringMapMessage(map))
+ .build();
+ assertEquals("mapMessage", interpolator.lookup(event, "map:key"));
+ }
}
diff --git
a/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiDisabledLookupTest.java
b/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiDisabledLookupTest.java
index d8124e917e..b797972821 100644
---
a/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiDisabledLookupTest.java
+++
b/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiDisabledLookupTest.java
@@ -16,47 +16,19 @@
*/
package org.apache.logging.log4j.jndi.lookup;
-import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import org.apache.logging.log4j.core.lookup.StrLookup;
-import org.apache.logging.log4j.jndi.test.junit.JndiRule;
-import org.junit.Rule;
import org.junit.Test;
/**
* JndiDisabledLookupTest
*
- * Verifies the Lookups are disabled without the log4j2.enableJndi property
set to true.
+ * Verifies the Lookups are disabled without the log4j2.enableJndiLookup
property set to true.
*/
public class JndiDisabledLookupTest {
- private static final String TEST_CONTEXT_RESOURCE_NAME =
"logging/context-name";
- private static final String TEST_CONTEXT_NAME = "app-1";
- private static final String TEST_INTEGRAL_NAME = "int-value";
- private static final int TEST_INTEGRAL_VALUE = 42;
- private static final String TEST_STRINGS_NAME = "string-collection";
- private static final Collection<String> TEST_STRINGS_COLLECTION =
Arrays.asList("one", "two", "three");
-
- @Rule
- public JndiRule jndiRule = new JndiRule(createBindings());
-
- private Map<String, Object> createBindings() {
- final Map<String, Object> map = new HashMap<>();
- map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX +
TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME);
- map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX +
TEST_INTEGRAL_NAME, TEST_INTEGRAL_VALUE);
- map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX +
TEST_STRINGS_NAME, TEST_STRINGS_COLLECTION);
- return map;
- }
-
- @Test(expected = IllegalStateException.class)
+ @Test
public void testLookup() {
- final StrLookup lookup = new JndiLookup();
-
- final String contextName = lookup.lookup(TEST_CONTEXT_RESOURCE_NAME);
- assertNull(contextName);
+ assertThrows(IllegalStateException.class, JndiLookup::new);
}
}
diff --git
a/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiLookupTest.java
b/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiLookupTest.java
index edee963e11..5a0f1cf998 100644
---
a/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiLookupTest.java
+++
b/log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiLookupTest.java
@@ -16,7 +16,8 @@
*/
package org.apache.logging.log4j.jndi.lookup;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import java.util.Arrays;
import java.util.Collection;