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.core.joran.implicitAction;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertNotNull;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import ch.qos.logback.core.joran.SimpleConfigurator;
26  import ch.qos.logback.core.joran.action.Action;
27  import ch.qos.logback.core.joran.action.StatusListenerAction;
28  import ch.qos.logback.core.joran.spi.ElementSelector;
29  import ch.qos.logback.core.testUtil.CoreTestConstants;
30  import ch.qos.logback.core.testUtil.StatusChecker;
31  import ch.qos.logback.core.util.StatusPrinter;
32  
33  public class ImplicitActionTest {
34  
35      static final String IMPLCIT_DIR = CoreTestConstants.TEST_SRC_PREFIX + "input/joran/implicitAction/";
36  
37      FruitContext fruitContext = new FruitContext();
38      SimpleConfigurator simpleConfigurator;
39      StatusChecker checker = new StatusChecker(fruitContext);
40      
41      @Before
42      public void setUp() throws Exception {
43          fruitContext.setName("fruits");
44          HashMap<ElementSelector, Action> rulesMap = new HashMap<ElementSelector, Action>();
45          rulesMap.put(new ElementSelector("/context/"), new FruitContextAction());
46          rulesMap.put(new ElementSelector("/context/statusListener"), new StatusListenerAction());
47          simpleConfigurator = new SimpleConfigurator(rulesMap);
48          simpleConfigurator.setContext(fruitContext);
49      }
50  
51      void verifyFruit() {
52          List<Fruit> fList = fruitContext.getFruitList();
53          assertNotNull(fList);
54          assertEquals(1, fList.size());
55  
56          Fruit f0 = fList.get(0);
57          assertEquals("blue", f0.getName());
58          assertEquals(2, f0.textList.size());
59          assertEquals("hello", f0.textList.get(0));
60          assertEquals("world", f0.textList.get(1));
61      }
62  
63      @Test
64      public void nestedComplex() throws Exception {
65          try {
66              simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplex.xml");
67              verifyFruit();
68  
69          } catch (Exception je) {
70              StatusPrinter.print(fruitContext);
71              throw je;
72          }
73      }
74  
75      @Test
76      public void nestedComplexWithoutClassAtrribute() throws Exception {
77          try {
78              simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexWithoutClassAtrribute.xml");
79  
80              verifyFruit();
81  
82          } catch (Exception je) {
83              StatusPrinter.print(fruitContext);
84              throw je;
85          }
86      }
87  
88      void verifyFruitList() {
89          List<Fruit> fList = fruitContext.getFruitList();
90          assertNotNull(fList);
91          assertEquals(1, fList.size());
92  
93          Fruit f0 = fList.get(0);
94          assertEquals(2, f0.cakeList.size());
95  
96          Cake cakeA = f0.cakeList.get(0);
97          assertEquals("A", cakeA.getType());
98  
99          Cake cakeB = f0.cakeList.get(1);
100         assertEquals("B", cakeB.getType());
101     }
102 
103     @Test
104     public void nestedComplexCollection() throws Exception {
105         try {
106             simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollection.xml");
107             verifyFruitList();
108         } catch (Exception je) {
109             StatusPrinter.print(fruitContext);
110             throw je;
111         }
112     }
113 
114     @Test
115     public void nestedComplexCollectionWithoutClassAtrribute() throws Exception {
116         try {
117             simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollectionWithoutClassAtrribute.xml");
118             verifyFruitList();
119         } catch (Exception je) {
120             StatusPrinter.print(fruitContext);
121             throw je;
122         }
123     }
124 
125     @Test
126     public void statusListenerWithPrefix() throws Exception {
127         try {                                             
128             simpleConfigurator.doConfigure(IMPLCIT_DIR + "statusListenerWithPrefix.xml");
129             StatusPrinter.print(fruitContext);
130             checker.assertIsErrorFree();
131         } catch (Exception je) {
132             StatusPrinter.print(fruitContext);
133             throw je;
134         }
135     }
136 
137 }