Skip to content

Commit c5680fe

Browse files
committed
Port log4j-jndi-test changes from 2.x (#2163)
1 parent 986a716 commit c5680fe

File tree

3 files changed

+86
-37
lines changed

3 files changed

+86
-37
lines changed

log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/InterpolatorTest.java

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,35 @@
1616
*/
1717
package org.apache.logging.log4j.jndi.lookup;
1818

19-
import static org.junit.Assert.assertEquals;
2019
import static org.junit.Assert.assertFalse;
2120
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertSame;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
2324

25+
import java.lang.reflect.Field;
2426
import java.text.SimpleDateFormat;
27+
import java.util.Collections;
2528
import java.util.Date;
2629
import java.util.HashMap;
2730
import java.util.Map;
31+
import org.apache.logging.log4j.Level;
2832
import org.apache.logging.log4j.ThreadContext;
33+
import org.apache.logging.log4j.core.LogEvent;
34+
import org.apache.logging.log4j.core.Logger;
35+
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
2936
import org.apache.logging.log4j.core.lookup.Interpolator;
3037
import org.apache.logging.log4j.core.lookup.MapLookup;
3138
import org.apache.logging.log4j.core.lookup.StrLookup;
3239
import org.apache.logging.log4j.jndi.test.junit.JndiRule;
40+
import org.apache.logging.log4j.message.StringMapMessage;
3341
import org.junit.ClassRule;
3442
import org.junit.Test;
3543
import org.junit.rules.ExternalResource;
3644
import org.junit.rules.RuleChain;
3745

3846
/**
39-
*
47+
* Tests {@link Interpolator}.
4048
*/
4149
public class InterpolatorTest {
4250

@@ -48,7 +56,7 @@ public class InterpolatorTest {
4856
private static final String TEST_CONTEXT_NAME = "app-1";
4957

5058
@ClassRule
51-
public static RuleChain rules = RuleChain.outerRule(new ExternalResource() {
59+
public static final RuleChain RULES = RuleChain.outerRule(new ExternalResource() {
5260
@Override
5361
protected void before() throws Throwable {
5462
System.setProperty(TESTKEY, TESTVAL);
@@ -66,6 +74,28 @@ protected void after() {
6674
.around(new JndiRule(
6775
JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME));
6876

77+
@Test
78+
public void testGetDefaultLookup() {
79+
final Map<String, String> map = new HashMap<>();
80+
map.put(TESTKEY, TESTVAL);
81+
final MapLookup defaultLookup = new MapLookup(map);
82+
final Interpolator interpolator = new Interpolator(defaultLookup);
83+
assertEquals(getLookupMap(defaultLookup), getLookupMap((MapLookup) interpolator.getDefaultLookup()));
84+
assertSame(defaultLookup, interpolator.getDefaultLookup());
85+
}
86+
87+
private static Map<String, String> getLookupMap(final MapLookup lookup) {
88+
try {
89+
final Field mapField = lookup.getClass().getDeclaredField("map");
90+
mapField.setAccessible(true);
91+
@SuppressWarnings("unchecked")
92+
Map<String, String> map = (Map<String, String>) mapField.get(lookup);
93+
return map;
94+
} catch (final Exception error) {
95+
throw new RuntimeException(error);
96+
}
97+
}
98+
6999
@Test
70100
public void testLookup() {
71101
final Map<String, String> map = new HashMap<>();
@@ -117,4 +147,50 @@ public void testLookupWithDefaultInterpolator() {
117147
assertLookupNotEmpty(lookup, "java:locale");
118148
assertLookupNotEmpty(lookup, "java:hw");
119149
}
150+
151+
@Test
152+
public void testInterpolatorMapMessageWithNoPrefix() {
153+
final HashMap<String, String> configProperties = new HashMap<>();
154+
configProperties.put("key", "configProperties");
155+
final Interpolator interpolator = new Interpolator(configProperties);
156+
final HashMap<String, String> map = new HashMap<>();
157+
map.put("key", "mapMessage");
158+
final LogEvent event = Log4jLogEvent.newBuilder()
159+
.setLoggerName(getClass().getName())
160+
.setLoggerFqcn(Logger.class.getName())
161+
.setLevel(Level.INFO)
162+
.setMessage(new StringMapMessage(map))
163+
.build();
164+
assertEquals("configProperties", interpolator.lookup(event, "key"));
165+
}
166+
167+
@Test
168+
public void testInterpolatorMapMessageWithNoPrefixConfigDoesntMatch() {
169+
final Interpolator interpolator = new Interpolator(Collections.emptyMap());
170+
final HashMap<String, String> map = new HashMap<>();
171+
map.put("key", "mapMessage");
172+
final LogEvent event = Log4jLogEvent.newBuilder()
173+
.setLoggerName(getClass().getName())
174+
.setLoggerFqcn(Logger.class.getName())
175+
.setLevel(Level.INFO)
176+
.setMessage(new StringMapMessage(map))
177+
.build();
178+
assertNull(interpolator.lookup(event, "key"), "Values without a map prefix should not match MapMessages");
179+
}
180+
181+
@Test
182+
public void testInterpolatorMapMessageWithMapPrefix() {
183+
final HashMap<String, String> configProperties = new HashMap<>();
184+
configProperties.put("key", "configProperties");
185+
final Interpolator interpolator = new Interpolator(configProperties);
186+
final HashMap<String, String> map = new HashMap<>();
187+
map.put("key", "mapMessage");
188+
final LogEvent event = Log4jLogEvent.newBuilder()
189+
.setLoggerName(getClass().getName())
190+
.setLoggerFqcn(Logger.class.getName())
191+
.setLevel(Level.INFO)
192+
.setMessage(new StringMapMessage(map))
193+
.build();
194+
assertEquals("mapMessage", interpolator.lookup(event, "map:key"));
195+
}
120196
}

log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiDisabledLookupTest.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,19 @@
1616
*/
1717
package org.apache.logging.log4j.jndi.lookup;
1818

19-
import static org.junit.Assert.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

21-
import java.util.Arrays;
22-
import java.util.Collection;
23-
import java.util.HashMap;
24-
import java.util.Map;
25-
import org.apache.logging.log4j.core.lookup.StrLookup;
26-
import org.apache.logging.log4j.jndi.test.junit.JndiRule;
27-
import org.junit.Rule;
2821
import org.junit.Test;
2922

3023
/**
3124
* JndiDisabledLookupTest
3225
*
33-
* Verifies the Lookups are disabled without the log4j2.enableJndi property set to true.
26+
* Verifies the Lookups are disabled without the log4j2.enableJndiLookup property set to true.
3427
*/
3528
public class JndiDisabledLookupTest {
3629

37-
private static final String TEST_CONTEXT_RESOURCE_NAME = "logging/context-name";
38-
private static final String TEST_CONTEXT_NAME = "app-1";
39-
private static final String TEST_INTEGRAL_NAME = "int-value";
40-
private static final int TEST_INTEGRAL_VALUE = 42;
41-
private static final String TEST_STRINGS_NAME = "string-collection";
42-
private static final Collection<String> TEST_STRINGS_COLLECTION = Arrays.asList("one", "two", "three");
43-
44-
@Rule
45-
public JndiRule jndiRule = new JndiRule(createBindings());
46-
47-
private Map<String, Object> createBindings() {
48-
final Map<String, Object> map = new HashMap<>();
49-
map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME, TEST_CONTEXT_NAME);
50-
map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_INTEGRAL_NAME, TEST_INTEGRAL_VALUE);
51-
map.put(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_STRINGS_NAME, TEST_STRINGS_COLLECTION);
52-
return map;
53-
}
54-
55-
@Test(expected = IllegalStateException.class)
30+
@Test
5631
public void testLookup() {
57-
final StrLookup lookup = new JndiLookup();
58-
59-
final String contextName = lookup.lookup(TEST_CONTEXT_RESOURCE_NAME);
60-
assertNull(contextName);
32+
assertThrows(IllegalStateException.class, JndiLookup::new);
6133
}
6234
}

log4j-jndi-test/src/test/java/org/apache/logging/log4j/jndi/lookup/JndiLookupTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.jndi.lookup;
1818

19-
import static org.junit.Assert.*;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNull;
2021

2122
import java.util.Arrays;
2223
import java.util.Collection;

0 commit comments

Comments
 (0)