1   package ch.qos.logback.core.joran.action;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   
6   import java.io.FileNotFoundException;
7   import java.net.MalformedURLException;
8   import java.net.UnknownHostException;
9   import java.util.HashMap;
10  
11  import org.junit.After;
12  import org.junit.Before;
13  import org.junit.Test;
14  import org.xml.sax.SAXParseException;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.ContextBase;
18  import ch.qos.logback.core.joran.TrivialConfigurator;
19  import ch.qos.logback.core.joran.action.ext.IncAction;
20  import ch.qos.logback.core.joran.spi.JoranException;
21  import ch.qos.logback.core.joran.spi.Pattern;
22  import ch.qos.logback.core.status.Status;
23  import ch.qos.logback.core.status.StatusChecker;
24  import ch.qos.logback.core.util.StatusPrinter;
25  
26  public class IncludeActionTest {
27  
28    final static String INCLUDE_KEY = "includeKey";
29    final static String SUB_FILE_KEY = "subFileKey";
30    final static String SECOND_FILE_KEY = "secondFileKey";
31  
32    Context context = new ContextBase();
33    TrivialConfigurator tc;
34  
35    static final String INCLUSION_DIR_PREFIX = "src/test/input/joran/inclusion/";
36  
37    static final String TOP_BY_FILE = INCLUSION_DIR_PREFIX + "topByFile.xml";
38  
39    static final String SUB_FILE = INCLUSION_DIR_PREFIX + "subByFile.xml";
40  
41    static final String MULTI_INCLUDE_BY_FILE = INCLUSION_DIR_PREFIX
42        + "multiIncludeByFile.xml";
43  
44    static final String SECOND_FILE = INCLUSION_DIR_PREFIX + "second.xml";
45  
46    static final String TOP_BY_URL = INCLUSION_DIR_PREFIX + "topByUrl.xml";
47  
48    static final String INCLUDE_BY_RESOURCE = INCLUSION_DIR_PREFIX
49        + "topByResource.xml";
50  
51    static final String INCLUDED_FILE = INCLUSION_DIR_PREFIX + "included.xml";
52    static final String URL_TO_INCLUDE = "file:./" + INCLUDED_FILE;
53  
54    static final String INVALID = INCLUSION_DIR_PREFIX + "invalid.xml";
55  
56    static final String INCLUDED_AS_RESOURCE = "asResource/joran/inclusion/includedAsResource.xml";
57  
58    public IncludeActionTest() {
59      HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
60      rulesMap.put(new Pattern("x"), new NOPAction());
61      rulesMap.put(new Pattern("x/inc"), new IncAction());
62      rulesMap.put(new Pattern("x/include"), new IncludeAction());
63  
64      tc = new TrivialConfigurator(rulesMap);
65      tc.setContext(context);
66    }
67  
68    @Before
69    public void setUp() throws Exception {
70      IncAction.reset();
71    }
72  
73    @After
74    public void tearDown() throws Exception {
75      context = null;
76      System.clearProperty(INCLUDE_KEY);
77      System.clearProperty(SECOND_FILE_KEY);
78      System.clearProperty(SUB_FILE_KEY);
79    }
80  
81    @Test
82    public void basicFile() throws JoranException {
83      System.setProperty(INCLUDE_KEY, INCLUDED_FILE);
84      tc.doConfigure(TOP_BY_FILE);
85      verifyConfig(2);
86    }
87  
88    @Test
89    public void basicResource() throws JoranException {
90      System.setProperty(INCLUDE_KEY, INCLUDED_AS_RESOURCE);
91      tc.doConfigure(INCLUDE_BY_RESOURCE);
92      StatusPrinter.print(context);
93      verifyConfig(2);
94    }
95  
96    @Test 
97    public void basicURL() throws JoranException {
98      System.setProperty(INCLUDE_KEY, URL_TO_INCLUDE);
99      tc.doConfigure(TOP_BY_URL);
100     StatusPrinter.print(context);
101     verifyConfig(2);
102   }
103 
104   @Test
105   public void noFileFound() throws JoranException {
106     System.setProperty(INCLUDE_KEY, "toto");
107     tc.doConfigure(TOP_BY_FILE);
108     assertEquals(Status.ERROR, context.getStatusManager().getLevel());
109     StatusChecker sc = new StatusChecker(context.getStatusManager());
110     assertTrue(sc.containsException(FileNotFoundException.class));
111   }
112 
113   @Test
114   public void withCorruptFile() throws JoranException {
115     System.setProperty(INCLUDE_KEY, INVALID);
116     tc.doConfigure(TOP_BY_FILE);
117     assertEquals(Status.ERROR, context.getStatusManager().getLevel());
118     StatusChecker sc = new StatusChecker(context.getStatusManager());
119     assertTrue(sc.containsException(SAXParseException.class));
120   }
121 
122   @Test
123   public void malformedURL() throws JoranException {
124     System.setProperty(INCLUDE_KEY, "htp://logback.qos.ch");
125     tc.doConfigure(TOP_BY_URL);
126     assertEquals(Status.ERROR, context.getStatusManager().getLevel());
127     StatusChecker sc = new StatusChecker(context.getStatusManager());
128     assertTrue(sc.containsException(MalformedURLException.class));
129   }
130 
131   @Test
132   public void unknownURL() throws JoranException {
133     System.setProperty(INCLUDE_KEY, "http://logback2345.qos.ch");
134     tc.doConfigure(TOP_BY_URL);
135     assertEquals(Status.ERROR, context.getStatusManager().getLevel());
136     StatusChecker sc = new StatusChecker(context.getStatusManager());
137     assertTrue(sc.containsException(UnknownHostException.class));
138   }
139 
140   @Test
141   public void nestedInclude() throws JoranException {
142     System.setProperty(SUB_FILE_KEY, INCLUDED_FILE);
143     System.setProperty(INCLUDE_KEY, SECOND_FILE);
144     tc.doConfigure(TOP_BY_FILE);
145     StatusPrinter.print(context);
146     verifyConfig(1);
147 
148   }
149 
150   @Test
151   public void multiInclude() throws JoranException {
152     System.setProperty(INCLUDE_KEY, INCLUDED_FILE);
153     System.setProperty(SECOND_FILE_KEY, SECOND_FILE);
154     tc.doConfigure(MULTI_INCLUDE_BY_FILE);
155     verifyConfig(3);
156   }
157 
158   @Test
159   public void errorInDoBegin() {
160     
161   }
162   
163   
164   void verifyConfig(int expected) {
165     assertEquals(expected, IncAction.beginCount);
166     assertEquals(expected, IncAction.endCount);
167   }
168 }