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