Skip to content

Commit

Permalink
Port log4j-jndi-test changes from 2.x (#2163)
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed Jan 10, 2024
1 parent 986a716 commit c5680fe
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
Expand All @@ -66,6 +74,28 @@ protected void after() {
.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<>();
Expand Down Expand Up @@ -117,4 +147,50 @@ public void testLookupWithDefaultInterpolator() {
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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c5680fe

Please sign in to comment.