1
2
3
4
5
6
7
8
9
10
11
12
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 import java.util.function.Supplier;
22
23 import org.junit.Before;
24 import org.junit.Test;
25
26 import ch.qos.logback.core.Context;
27 import ch.qos.logback.core.joran.SimpleConfigurator;
28 import ch.qos.logback.core.joran.action.Action;
29 import ch.qos.logback.core.joran.action.StatusListenerAction;
30 import ch.qos.logback.core.joran.spi.ElementSelector;
31 import ch.qos.logback.core.model.ImplicitModel;
32 import ch.qos.logback.core.model.PropertyModel;
33 import ch.qos.logback.core.model.StatusListenerModel;
34 import ch.qos.logback.core.model.processor.DefaultProcessor;
35 import ch.qos.logback.core.model.processor.ImplicitModelHandler;
36 import ch.qos.logback.core.model.processor.ModelInterpretationContext;
37 import ch.qos.logback.core.model.processor.PropertyModelHandler;
38 import ch.qos.logback.core.model.processor.StatusListenerModelHandler;
39 import ch.qos.logback.core.testUtil.CoreTestConstants;
40 import ch.qos.logback.core.testUtil.StatusChecker;
41 import ch.qos.logback.core.util.StatusPrinter;
42
43 public class ImplicitActionTest {
44
45 static final String IMPLCIT_DIR = CoreTestConstants.TEST_SRC_PREFIX + "input/joran/implicitAction/";
46
47 FruitContext fruitContext = new FruitContext();
48 SimpleConfigurator simpleConfigurator;
49 StatusChecker checker = new StatusChecker(fruitContext);
50
51 @Before
52 public void setUp() throws Exception {
53 fruitContext.setName("fruits");
54 HashMap<ElementSelector, Supplier<Action>> rulesMap = new HashMap<>();
55 rulesMap.put(new ElementSelector("/context/"), () -> new FruitContextAction());
56 rulesMap.put(new ElementSelector("/context/statusListener"), () -> new StatusListenerAction());
57 simpleConfigurator = new SimpleConfigurator(rulesMap) {
58 @Override
59 protected DefaultProcessor buildDefaultProcessor(Context context, ModelInterpretationContext mic) {
60 DefaultProcessor defaultProcessor = super.buildDefaultProcessor(context, mic);
61 defaultProcessor.addHandler(FruitContextModel.class, FruitContextModelHandler::makeInstance);
62 defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
63 defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
64 defaultProcessor.addHandler(StatusListenerModel.class, StatusListenerModelHandler::makeInstance);
65
66 return defaultProcessor;
67 }
68
69 };
70 simpleConfigurator.setContext(fruitContext);
71 }
72
73 void verifyFruit() {
74 List<Fruit> fList = fruitContext.getFruitList();
75 assertNotNull(fList);
76 assertEquals(1, fList.size());
77
78 Fruit f0 = fList.get(0);
79 assertEquals("blue", f0.getName());
80 assertEquals(2, f0.textList.size());
81 assertEquals("hello", f0.textList.get(0));
82 assertEquals("world", f0.textList.get(1));
83 }
84
85 @Test
86 public void nestedComplex() throws Exception {
87 try {
88 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplex.xml");
89 StatusPrinter.print(fruitContext);
90 verifyFruit();
91
92 } catch (Exception je) {
93 StatusPrinter.print(fruitContext);
94 throw je;
95 }
96 }
97
98 @Test
99 public void nestedComplexWithoutClassAtrribute() throws Exception {
100 try {
101 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexWithoutClassAtrribute.xml");
102
103 verifyFruit();
104
105 } catch (Exception je) {
106 StatusPrinter.print(fruitContext);
107 throw je;
108 }
109 }
110
111 void verifyFruitList() {
112 List<Fruit> fList = fruitContext.getFruitList();
113 assertNotNull(fList);
114 assertEquals(1, fList.size());
115
116 Fruit f0 = fList.get(0);
117 assertEquals(2, f0.cakeList.size());
118
119 Cake cakeA = f0.cakeList.get(0);
120 assertEquals("A", cakeA.getType());
121
122 Cake cakeB = f0.cakeList.get(1);
123 assertEquals("B", cakeB.getType());
124 }
125
126 @Test
127 public void nestedComplexCollection() throws Exception {
128 try {
129 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollection.xml");
130 verifyFruitList();
131 } catch (Exception je) {
132 StatusPrinter.print(fruitContext);
133 throw je;
134 }
135 }
136
137 @Test
138 public void nestedComplexCollectionWithoutClassAtrribute() throws Exception {
139 try {
140 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollectionWithoutClassAtrribute.xml");
141 verifyFruitList();
142 } catch (Exception je) {
143 StatusPrinter.print(fruitContext);
144 throw je;
145 }
146 }
147
148 @Test
149 public void statusListenerWithPrefix() throws Exception {
150 try {
151 simpleConfigurator.doConfigure(IMPLCIT_DIR + "statusListenerWithPrefix.xml");
152 StatusPrinter.print(fruitContext);
153 checker.assertIsErrorFree();
154 } catch (Exception je) {
155 StatusPrinter.print(fruitContext);
156 throw je;
157 }
158 }
159
160 }