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