1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.pattern;
16
17 import ch.qos.logback.classic.Level;
18 import ch.qos.logback.classic.LoggerContext;
19 import ch.qos.logback.classic.PatternLayout;
20 import ch.qos.logback.classic.spi.LoggingEvent;
21 import ch.qos.logback.core.CoreConstants;
22 import org.junit.jupiter.api.Test;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29
30 public class LegacyPatternLayoutTest {
31
32 LoggerContext context = new LoggerContext();
33
34 @Test public void subPattern() {
35 SubPatternLayout layout = new SubPatternLayout();
36 layout.setPattern("%"+SubPatternLayout.DOOO);
37 layout.setContext(context);
38 layout.start();
39 LoggingEvent event = new LoggingEvent();
40 event.setTimeStamp(0);
41
42 String result = layout.doLayout(event);
43 assertEquals("1970-01-01 01:00:00,000", result);
44 }
45
46 @Test
47 public void fromContext() {
48 Map<String, String> registry = (Map<String, String>) this.context
49 .getObject(CoreConstants.PATTERN_RULE_REGISTRY);
50
51 assertNull(registry);
52 if(registry == null) {
53 registry = new HashMap<String, String>();
54 this.context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, registry);
55 }
56
57 registry.put("legacy", LevelConverter.class.getName());
58
59 PatternLayout patternLayout = new PatternLayout();
60 patternLayout.setPattern("%legacy");
61 patternLayout.setContext(context);
62 patternLayout.start();
63 LoggingEvent event = new LoggingEvent();
64 event.setLevel(Level.WARN);
65 String result = patternLayout.doLayout(event);
66 assertEquals("WARN", result);
67 }
68
69 }