View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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  package ch.qos.logback.access.joran;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import ch.qos.logback.access.spi.IAccessEvent;
21  import org.junit.After;
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import ch.qos.logback.access.AccessTestConstants;
26  import ch.qos.logback.access.dummy.DummyAccessEventBuilder;
27  import ch.qos.logback.access.spi.AccessContext;
28  import ch.qos.logback.core.joran.spi.JoranException;
29  import ch.qos.logback.core.read.ListAppender;
30  import ch.qos.logback.core.testUtil.StringListAppender;
31  
32  public class JoranConfiguratorTest {
33  
34      AccessContext context = new AccessContext();
35  
36      @Before
37      public void setUp() throws Exception {
38      }
39  
40      @After
41      public void tearDown() throws Exception {
42      }
43  
44      void configure(String file) throws JoranException {
45          JoranConfigurator jc = new JoranConfigurator();
46          jc.setContext(context);
47          jc.doConfigure(file);
48      }
49  
50      @Test
51      public void smoke() throws Exception {
52          configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/smoke.xml");
53  
54          ListAppender<IAccessEvent> listAppender = (ListAppender<IAccessEvent>) context.getAppender("LIST");
55          IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent();
56          listAppender.doAppend(event);
57  
58          assertEquals(1, listAppender.list.size());
59  
60          assertEquals(1, listAppender.list.size());
61          IAccessEvent ae = listAppender.list.get(0);
62          assertNotNull(ae);
63      }
64  
65      @Test
66      public void defaultLayout() throws Exception {
67          configure(AccessTestConstants.TEST_DIR_PREFIX + "input/joran/defaultLayout.xml");
68          StringListAppender<IAccessEvent> listAppender = (StringListAppender<IAccessEvent>) context.getAppender("STR_LIST");
69          IAccessEvent event = DummyAccessEventBuilder.buildNewAccessEvent();
70          listAppender.doAppend(event);
71          assertEquals(1, listAppender.strList.size());
72          // the result contains a line separator at the end
73          assertTrue(listAppender.strList.get(0).startsWith("testMethod"));
74      }
75  }