1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic;
11  
12  import static ch.qos.logback.classic.TestConstants.ISO_REGEX;
13  import static ch.qos.logback.classic.TestConstants.MAIN_REGEX;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertTrue;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import ch.qos.logback.classic.joran.JoranConfigurator;
25  import ch.qos.logback.classic.pattern.ConverterTest;
26  import ch.qos.logback.classic.spi.LoggingEvent;
27  import ch.qos.logback.classic.testUtil.SampleConverter;
28  import ch.qos.logback.classic.util.TeztConstants;
29  import ch.qos.logback.core.Context;
30  import ch.qos.logback.core.joran.spi.JoranException;
31  import ch.qos.logback.core.pattern.PatternLayoutBase;
32  import ch.qos.logback.core.pattern.parser.AbstractPatternLayoutBaseTest;
33  import ch.qos.logback.core.testUtil.StringListAppender;
34  
35  public class PatternLayoutTest extends AbstractPatternLayoutBaseTest {
36  
37    private PatternLayout pl = new PatternLayout();
38    private LoggerContext lc = new LoggerContext();
39    Logger logger = lc.getLogger(ConverterTest.class);
40    Logger root = lc.getLogger(LoggerContext.ROOT_NAME);
41    
42    LoggingEvent le;
43    List optionList = new ArrayList();
44  
45    public PatternLayoutTest() {
46      super();
47      Exception ex = new Exception("Bogus exception");
48      le = makeLoggingEvent(ex);
49    }
50  
51    @Before
52    public void setUp() {
53      pl.setContext(lc);
54    }
55  
56    LoggingEvent makeLoggingEvent(Exception ex) {
57      return new LoggingEvent(
58          ch.qos.logback.core.pattern.FormattingConverter.class.getName(),
59          logger, Level.INFO, "Some message", ex, null);
60    }
61  
62    @Override
63    public LoggingEvent getEventObject() {
64      return makeLoggingEvent(null);
65    }
66  
67    public PatternLayoutBase getPatternLayoutBase() {
68      return new PatternLayout();
69    }
70  
71    @Test
72    public void testOK() {
73      pl.setPattern("%d %le [%t] %lo{30} - %m%n");
74      pl.start();
75      String val = pl.doLayout(getEventObject());
76      // 2006-02-01 22:38:06,212 INFO [main] c.q.l.pattern.ConverterTest - Some
77      // message
78      String regex = ISO_REGEX + " INFO " + MAIN_REGEX
79          + " c.q.l.c.pattern.ConverterTest - Some message\\s*";
80  
81      assertTrue(val.matches(regex));
82    }
83  
84    @Test
85    public void testNoExeptionHandler() {
86      pl.setPattern("%m%n");
87      pl.start();
88      String val = pl.doLayout(le);
89      assertTrue(val.contains("java.lang.Exception: Bogus exception"));
90    }
91  
92    @Test
93    public void testCompositePattern() {
94      pl.setPattern("%-56(%d %lo{20}) - %m%n");
95      pl.start();
96      String val = pl.doLayout(getEventObject());
97      // 2008-03-18 21:55:54,250 c.q.l.c.pattern.ConverterTest - Some message
98      String regex = ISO_REGEX
99          + " c.q.l.c.p.ConverterTest          - Some message\\s*";
100     assertTrue(val.matches(regex));
101 
102   }
103 
104   @Test
105   public void testNopExeptionHandler() {
106     pl.setPattern("%nopex %m%n");
107     pl.start();
108     String val = pl.doLayout(le);
109     assertTrue(!val.contains("java.lang.Exception: Bogus exception"));
110   }
111 
112   @Test
113   public void testWithParenthesis() {
114     pl.setPattern("\\(%msg:%msg\\) %msg");
115     pl.start();
116     le = makeLoggingEvent(null);
117     String val = pl.doLayout(le);
118     // System.out.println("VAL == " + val);
119     assertEquals("(Some message:Some message) Some message", val);
120   }
121 
122   @Test
123   public void testWithLettersComingFromLog4j() {
124     // Letters: p = level and c = logger
125     pl.setPattern("%d %p [%t] %c{30} - %m%n");
126     pl.start();
127     String val = pl.doLayout(getEventObject());
128     // 2006-02-01 22:38:06,212 INFO [main] c.q.l.pattern.ConverterTest - Some
129     // message
130     String regex = TestConstants.ISO_REGEX + " INFO " + MAIN_REGEX
131         + " c.q.l.c.pattern.ConverterTest - Some message\\s*";
132     assertTrue(val.matches(regex));
133   }
134 
135   @Test
136   public void contextNameTest() {
137     pl.setPattern("%contextName");
138     lc.setName("aValue");
139     pl.start();
140     String val = pl.doLayout(getEventObject());
141     assertEquals("aValue", val);
142   }
143 
144   @Override
145   public Context getContext() {
146     return lc;
147   }
148 
149   void configure(String file) throws JoranException {
150     JoranConfigurator jc = new JoranConfigurator();
151     jc.setContext(lc);
152     jc.doConfigure(file);
153   }
154 
155   @Test
156   public void testConversionRuleSupportInPatternLayout() throws JoranException {
157     configure(TeztConstants.TEST_DIR_PREFIX + "input/joran/conversionRule/patternLayout0.xml");
158     root.getAppender("LIST");
159     String msg  = "Simon says";
160     logger.debug(msg);
161     StringListAppender sla = (StringListAppender)    root.getAppender("LIST");
162     assertNotNull(sla);
163     assertEquals(1, sla.strList.size());
164     assertEquals(SampleConverter.SAMPLE_STR+" - "+msg, sla.strList.get(0)); 
165   }
166 }