1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.joran.action;
15
16 import java.util.Iterator;
17
18 import ch.qos.logback.core.model.ModelConstants;
19 import org.junit.jupiter.api.AfterEach;
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23
24 import ch.qos.logback.core.Context;
25 import ch.qos.logback.core.ContextBase;
26 import ch.qos.logback.core.joran.spi.ActionException;
27 import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
28 import ch.qos.logback.core.joran.spi.SaxEventInterpreter;
29 import ch.qos.logback.core.model.ImplicitModel;
30 import ch.qos.logback.core.model.PropertyModel;
31 import ch.qos.logback.core.model.TopModel;
32 import ch.qos.logback.core.model.processor.DefaultProcessor;
33 import ch.qos.logback.core.model.processor.ImplicitModelHandler;
34 import ch.qos.logback.core.model.processor.ModelInterpretationContext;
35 import ch.qos.logback.core.model.processor.NOPModelHandler;
36 import ch.qos.logback.core.model.processor.PropertyModelHandler;
37 import ch.qos.logback.core.status.ErrorStatus;
38 import ch.qos.logback.core.status.Status;
39 import ch.qos.logback.core.testUtil.CoreTestConstants;
40 import ch.qos.logback.core.util.StatusPrinter;
41
42
43
44
45
46
47 public class PropertyActionTest {
48
49 Context context;
50 SaxEventInterpretationContext interpretationContext;
51 ModelInterpretationContext mic;
52 SaxEventInterpreter x;
53
54 PropertyAction propertyAction;
55 DummyAttributes atts = new DummyAttributes();
56 DefaultProcessor defaultProcessor;
57 TopModel topModel = new TopModel();
58 String tagName = "property";
59
60 @BeforeEach
61 public void setUp() throws Exception {
62 context = new ContextBase();
63 interpretationContext = new SaxEventInterpretationContext(context, null);
64 mic = new ModelInterpretationContext(context);
65 topModel.setTag("top");
66 interpretationContext.pushModel(topModel);
67 mic.pushModel(topModel);
68 propertyAction = new PropertyAction();
69 propertyAction.setContext(context);
70 defaultProcessor = new DefaultProcessor(context, mic);
71 defaultProcessor.addHandler(TopModel.class, NOPModelHandler::makeInstance);
72 defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
73 defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);
74 }
75
76 @AfterEach
77 public void tearDown() throws Exception {
78 StatusPrinter.print(context);
79 context = null;
80 propertyAction = null;
81 atts = null;
82 }
83
84 @Test
85 public void nameValuePair() throws ActionException {
86 atts.setValue("name", "v1");
87 atts.setValue("value", "work");
88 propertyAction.begin(interpretationContext, tagName, atts);
89 propertyAction.end(interpretationContext, tagName);
90 defaultProcessor.process(topModel);
91 Assertions.assertEquals("work", mic.getProperty("v1"));
92 }
93
94 @Test
95 public void nameValuePairWithPrerequisiteSubsitution() throws ActionException {
96 context.putProperty("w", "wor");
97 atts.setValue("name", "v1");
98 atts.setValue("value", "${w}k");
99 propertyAction.begin(interpretationContext, tagName, atts);
100 propertyAction.end(interpretationContext, tagName);
101 defaultProcessor.process(topModel);
102 Assertions.assertEquals("work", mic.getProperty("v1"));
103 }
104
105 @Test
106 public void noValue() throws ActionException {
107 atts.setValue("name", "v1");
108 propertyAction.begin(interpretationContext, tagName, atts);
109 propertyAction.end(interpretationContext, tagName);
110 defaultProcessor.process(topModel);
111 Assertions.assertEquals(2, context.getStatusManager().getCount());
112 Assertions.assertTrue(checkError());
113 }
114
115 @Test
116 public void noName() throws ActionException {
117 atts.setValue("value", "v1");
118 propertyAction.begin(interpretationContext, tagName, atts);
119 propertyAction.end(interpretationContext, tagName);
120 defaultProcessor.process(topModel);
121 Assertions.assertEquals(2, context.getStatusManager().getCount());
122 Assertions.assertTrue(checkError());
123 }
124
125 @Test
126 public void noAttributes() throws ActionException {
127 propertyAction.begin(interpretationContext, "noAttributes", atts);
128 propertyAction.end(interpretationContext, "noAttributes");
129 defaultProcessor.process(topModel);
130 Assertions.assertEquals(2, context.getStatusManager().getCount());
131 Assertions.assertTrue(checkError());
132 StatusPrinter.print(context);
133 }
134
135 @Test
136 public void testFileNotLoaded() throws ActionException {
137 atts.setValue("file", "toto");
138 atts.setValue("value", "work");
139 propertyAction.begin(interpretationContext, tagName, atts);
140 propertyAction.end(interpretationContext, tagName);
141 defaultProcessor.process(topModel);
142 Assertions.assertEquals(2, context.getStatusManager().getCount());
143 Assertions.assertTrue(checkError());
144 }
145
146 @Test
147 public void testLoadFileWithPrerequisiteSubsitution() throws ActionException {
148 context.putProperty("STEM", CoreTestConstants.TEST_SRC_PREFIX + "input/joran");
149 atts.setValue("file", "${STEM}/propertyActionTest.properties");
150 propertyAction.begin(interpretationContext, tagName, atts);
151 propertyAction.end(interpretationContext, tagName);
152 defaultProcessor.process(topModel);
153 Assertions.assertEquals("tata", mic.getProperty("v1"));
154 Assertions.assertEquals("toto", mic.getProperty("v2"));
155 }
156
157 @Test
158 public void testLoadFile() throws ActionException {
159 atts.setValue("file", CoreTestConstants.TEST_SRC_PREFIX + "input/joran/propertyActionTest.properties");
160 propertyAction.begin(interpretationContext, tagName, atts);
161 propertyAction.end(interpretationContext, tagName);
162 defaultProcessor.process(topModel);
163 Assertions.assertEquals("tata", mic.getProperty("v1"));
164 Assertions.assertEquals("toto", mic.getProperty("v2"));
165 }
166
167 @Test
168 public void testLoadResource() throws ActionException {
169 atts.setValue("resource", "asResource/joran/propertyActionTest.properties");
170 propertyAction.begin(interpretationContext, tagName, atts);
171 propertyAction.end(interpretationContext, tagName);
172 defaultProcessor.process(topModel);
173 Assertions.assertEquals("tata", mic.getProperty("r1"));
174 Assertions.assertEquals("toto", mic.getProperty("r2"));
175 }
176
177 @Test
178 public void testLoadResourceWithPrerequisiteSubsitution() throws ActionException {
179 context.putProperty("STEM", "asResource/joran");
180 atts.setValue("resource", "${STEM}/propertyActionTest.properties");
181 propertyAction.begin(interpretationContext, tagName, atts);
182 propertyAction.end(interpretationContext, tagName);
183 defaultProcessor.process(topModel);
184 Assertions.assertEquals("tata", mic.getProperty("r1"));
185 Assertions.assertEquals("toto", mic.getProperty("r2"));
186 }
187
188 @Test
189 public void testLoadNotPossible() throws ActionException {
190 atts.setValue("file", "toto");
191 propertyAction.begin(interpretationContext, tagName, atts);
192 propertyAction.end(interpretationContext, tagName);
193 defaultProcessor.process(topModel);
194 Assertions.assertEquals(2, context.getStatusManager().getCount());
195 Assertions.assertTrue(checkFileErrors());
196 }
197
198 private boolean checkError() {
199 Iterator<Status> it = context.getStatusManager().getCopyOfStatusList().iterator();
200 ErrorStatus es = (ErrorStatus) it.next();
201 return ModelConstants.INVALID_ATTRIBUTES.equals(es.getMessage());
202 }
203
204 private boolean checkFileErrors() {
205 Iterator<Status> it = context.getStatusManager().getCopyOfStatusList().iterator();
206 ErrorStatus es1 = (ErrorStatus) it.next();
207 return "Could not find properties file [toto].".equals(es1.getMessage());
208 }
209 }