1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework for Java.
3    * 
4    * Copyright (C) 2000-2006, 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  
11  package ch.qos.logback.core.joran;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.HashMap;
17  
18  import org.junit.Test;
19  
20  import ch.qos.logback.core.Context;
21  import ch.qos.logback.core.ContextBase;
22  import ch.qos.logback.core.joran.action.Action;
23  import ch.qos.logback.core.joran.action.ext.IncAction;
24  import ch.qos.logback.core.joran.spi.Pattern;
25  import ch.qos.logback.core.status.Status;
26  import ch.qos.logback.core.status.TrivialStatusListener;
27  import ch.qos.logback.core.util.Constants;
28  
29  public class TrivialcConfiguratorTest {
30  
31    Context context = new ContextBase();
32  
33    public void doTest(String filename) throws Exception {
34  
35      HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
36      rulesMap.put(new Pattern("x/inc"), new IncAction());
37  
38      TrivialConfigurator gc = new TrivialConfigurator(rulesMap);
39  
40      gc.setContext(context);
41      gc.doConfigure(Constants.TEST_DIR_PREFIX + "input/joran/" + filename);
42    }
43  
44    @Test
45    public void smokeTezt() throws Exception {
46      int oldBeginCount = IncAction.beginCount;
47      int oldEndCount = IncAction.endCount;
48      int oldErrorCount = IncAction.errorCount;
49      doTest("inc.xml");
50      assertEquals(oldErrorCount, IncAction.errorCount);
51      assertEquals(oldBeginCount + 1, IncAction.beginCount);
52      assertEquals(oldEndCount + 1, IncAction.endCount);
53    }
54  
55    @Test
56    public void teztInexistentFile() {
57      TrivialStatusListener tsl = new TrivialStatusListener();
58      String filename = "nothereBLAH.xml";
59      context.getStatusManager().add(tsl);
60      try {
61        doTest(filename);
62      } catch (Exception e) {
63      }
64      assertTrue(tsl.list.size() + " should be greater than or equal to 1",
65          tsl.list.size() >= 1);
66      Status s0 = tsl.list.get(0);
67      assertTrue(s0.getMessage().startsWith("Could not open [" + filename + "]"));
68    }
69  
70    @Test
71    public void teztIllFormedXML() {
72      TrivialStatusListener tsl = new TrivialStatusListener();
73      String filename = "illformed.xml";
74      context.getStatusManager().add(tsl);
75      try {
76        doTest(filename);
77      } catch (Exception e) {
78      }
79      assertEquals(2, tsl.list.size());
80      Status s0 = tsl.list.get(0);
81      assertTrue(s0.getMessage().startsWith(
82          "Parsing fatal error on line 5 and column 3"));
83      Status s1 = tsl.list.get(1);
84      assertTrue(s1
85          .getMessage()
86          .startsWith(
87              "Problem parsing XML document. See previously reported errors. Abandoning all further processing."));
88    }
89  }