1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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  }