1   package ch.qos.logback.core.pattern;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.function.Supplier;
6   
7   import org.junit.jupiter.api.Assertions;
8   import org.junit.jupiter.api.BeforeEach;
9   import org.junit.jupiter.api.Test;
10  
11  import ch.qos.logback.core.Context;
12  import ch.qos.logback.core.ContextBase;
13  import ch.qos.logback.core.pattern.parser.Node;
14  import ch.qos.logback.core.pattern.parser.Parser;
15  import ch.qos.logback.core.spi.ContextAware;
16  import ch.qos.logback.core.spi.LifeCycle;
17  import ch.qos.logback.core.spi.ScanException;
18  
19  // inspired by ch.qos.logback.core.pattern.parser.CompilerTest
20  public class ConverterUtilTest {
21  
22      Map<String, Supplier<DynamicConverter>> converterMap = new HashMap<>();
23      Context context = new ContextBase();
24  
25      @BeforeEach
26      public void setUp() {
27          converterMap.put("OTT", Converter123::new);
28          converterMap.put("hello", ConverterHello::new);
29          converterMap.putAll(Parser.DEFAULT_COMPOSITE_CONVERTER_MAP);
30      }
31  
32      @Test
33      public void contextAndStartTest() throws ScanException {
34          testContextAndStart("hi %hello");
35          testContextAndStart("hi %(%hello)");
36          testContextAndStart("hi %(abc %(%hello))");
37  
38      }
39  
40      private void testContextAndStart(String pattern) throws ScanException {
41          Parser<Object> p = new Parser<Object>(pattern);
42          p.setContext(context);
43          Node t = p.parse();
44          Converter<Object> head = p.compile(t, converterMap);
45          ConverterUtil.setContextForConverters(context, head);
46          checkContext(head);
47  
48          ConverterUtil.startConverters(head);
49          checkStart(head);
50      }
51  
52      private void checkStart(Converter<Object> head) {
53          Converter<Object> c = head;
54          while (c != null) {
55              if (c instanceof LifeCycle) {
56                  LifeCycle ca = (LifeCycle) c;
57                  Assertions.assertTrue(ca.isStarted());
58              }
59              if (c instanceof CompositeConverter) {
60                  CompositeConverter<Object> cc = (CompositeConverter<Object>) c;
61                  Converter<Object> childConverter = cc.childConverter;
62                  checkStart(childConverter);
63              }
64              c = c.getNext();
65          }
66  
67      }
68  
69      void checkContext(Converter<Object> head) {
70          Converter<Object> c = head;
71          while (c != null) {
72              if (c instanceof ContextAware) {
73                  ContextAware ca = (ContextAware) c;
74                  Assertions.assertNotNull(ca.getContext());
75              }
76              if (c instanceof CompositeConverter) {
77                  CompositeConverter<Object> cc = (CompositeConverter<Object>) c;
78                  Converter<Object> childConverter = cc.childConverter;
79                  checkContext(childConverter);
80              }
81              c = c.getNext();
82          }
83      }
84  
85  }