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