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 nestedComplexWithPassword() throws Exception {
96 try {
97 fruitContext.addSubstitutionProperty("A_PASSWORD", "blue");
98
99 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexWithPassword.xml");
100 StatusPrinter.print(fruitContext);
101 StatusChecker checker = new StatusChecker(fruitContext);
102
103 checker.assertContainsMatch("value \"\\*{6}\" substituted for \"\\$\\{A_PASSWORD\\}\"");
104 verifyFruit();
105
106 } catch (Exception je) {
107 StatusPrinter.print(fruitContext);
108 throw je;
109 }
110 }
111
112 @Test
113 public void nestedComplexWithoutClassAtrribute() throws Exception {
114 try {
115 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexWithoutClassAtrribute.xml");
116
117 verifyFruit();
118
119 } catch (Exception je) {
120 StatusPrinter.print(fruitContext);
121 throw je;
122 }
123 }
124
125 void verifyFruitList() {
126 List<Fruit> fList = fruitContext.getFruitList();
127 assertNotNull(fList);
128 assertEquals(1, fList.size());
129
130 Fruit f0 = fList.get(0);
131 assertEquals(2, f0.cakeList.size());
132
133 Cake cakeA = f0.cakeList.get(0);
134 assertEquals("A", cakeA.getType());
135
136 Cake cakeB = f0.cakeList.get(1);
137 assertEquals("B", cakeB.getType());
138 }
139
140 @Test
141 public void nestedComplexCollection() throws Exception {
142 try {
143 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollection.xml");
144 verifyFruitList();
145 } catch (Exception je) {
146 StatusPrinter.print(fruitContext);
147 throw je;
148 }
149 }
150
151 @Test
152 public void nestedComplexCollectionWithoutClassAtrribute() throws Exception {
153 try {
154 simpleConfigurator.doConfigure(IMPLCIT_DIR + "nestedComplexCollectionWithoutClassAtrribute.xml");
155 verifyFruitList();
156 } catch (Exception je) {
157 StatusPrinter.print(fruitContext);
158 throw je;
159 }
160 }
161
162 @Test
163 public void statusListenerWithPrefix() throws Exception {
164 try {
165 simpleConfigurator.doConfigure(IMPLCIT_DIR + "statusListenerWithPrefix.xml");
166 StatusPrinter.print(fruitContext);
167 checker.assertIsErrorFree();
168 } catch (Exception je) {
169 StatusPrinter.print(fruitContext);
170 throw je;
171 }
172 }
173
174 }