Skip to content

Commit 5a41c89

Browse files
committed
fix IncludeActionTest
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent af92be4 commit 5a41c89

File tree

11 files changed

+60
-37
lines changed

11 files changed

+60
-37
lines changed

logback-core/src/main/java/ch/qos/logback/core/model/processor/IncludeModelHandler.java

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
7171
}
7272

7373
InputStream in = getInputStream(mic, includeModel);
74+
if(in == null) {
75+
inError = true;
76+
return;
77+
}
78+
7479
SaxEventRecorder recorder = null;
7580

7681
try {

logback-core/src/test/input/joran/inclusion/intermediaryByFile.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x>
2+
<!DOCTYPE included>
33

44
<included>
55
<stack name="a"/>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x>
2+
<!DOCTYPE configuration>
33

4-
<x>
4+
<configuration>
55
<include file="${includeKey}" />
66
<include file="${secondFileKey}" />
7-
</x>
7+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x [
2+
<!DOCTYPE configuration [
33
<!ENTITY includedEntity SYSTEM "includedEntity.xml">
44
]>
55

6-
<x>
6+
<configuration>
77

88
&includedEntity;
99

10-
</x>
10+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x>
2+
<!DOCTYPE configuration>
33

4-
<x>
4+
<configuration>
55
<include file="${includeKey}" />
6-
</x>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x>
2+
<!DOCTYPE configuration>
33

4-
<x>
4+
<configuration>
55

66
<include resource="${includeKey}" />
77

8-
</x>
8+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x>
2+
<!DOCTYPE configuration>
33

4-
<x>
4+
<configuration>
55
<include url="${includeKey}" />
6-
</x>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x>
2+
<!DOCTYPE configuration>
33

4-
<x>
4+
<configuration>
55
<include optional="true" file="nonExistentFile.xml" />
66

77
<stack name="IA"/>
88
<stack name="IB"/>
9-
</x>
9+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!DOCTYPE x>
2+
<!DOCTYPE configuration>
33

4-
<x>
4+
<configuration>
55
<include optional="true" resource="nonExistentResource.xml" />
66

77
<stack name="IA"/>
88
<stack name="IB"/>
9-
</x>
9+
</configuration>

logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.function.Supplier;
1818

1919
import ch.qos.logback.core.joran.action.Action;
20+
import ch.qos.logback.core.joran.action.ImplicitModelAction;
2021
import ch.qos.logback.core.joran.spi.ElementSelector;
2122
import ch.qos.logback.core.joran.spi.SaxEventInterpreter;
2223
import ch.qos.logback.core.joran.spi.RuleStore;
@@ -29,15 +30,23 @@ public TrivialConfigurator(HashMap<ElementSelector, Supplier<Action>> rules) {
2930
this.rulesMap = rules;
3031
}
3132

33+
34+
public TrivialConfigurator makeAnotherInstance() {
35+
TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
36+
tc.setContext(context);
37+
return tc;
38+
}
39+
3240
@Override
3341
protected void setImplicitRuleSupplier(SaxEventInterpreter interpreter) {
42+
interpreter.setImplicitActionSupplier(ImplicitModelAction::new);
3443
}
3544

3645
@Override
37-
protected void addElementSelectorAndActionAssociations(RuleStore rs) {
46+
protected void addElementSelectorAndActionAssociations(RuleStore aRuleStore) {
3847
for (ElementSelector elementSelector : rulesMap.keySet()) {
3948
Supplier<Action> actionSupplier = rulesMap.get(elementSelector);
40-
rs.addRule(elementSelector, actionSupplier);
49+
aRuleStore.addRule(elementSelector, actionSupplier);
4150
}
4251
}
4352

logback-core/src/test/java/ch/qos/logback/core/joran/action/IncludeActionTest.java

+22-13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Stack;
2424
import java.util.function.Supplier;
2525

26+
import ch.qos.logback.core.model.processor.IncludeModelHandler;
2627
import org.junit.jupiter.api.AfterEach;
2728
import org.junit.jupiter.api.Assertions;
2829
import org.junit.jupiter.api.BeforeEach;
@@ -49,7 +50,10 @@
4950
import ch.qos.logback.core.status.testUtil.StatusChecker;
5051
import ch.qos.logback.core.util.StatusPrinter;
5152

52-
@Disabled
53+
import static ch.qos.logback.core.joran.JoranConstants.CONFIGURATION_TAG;
54+
import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG;
55+
56+
5357
public class IncludeActionTest {
5458

5559
final static String INCLUDE_KEY = "includeKey";
@@ -96,27 +100,32 @@ public class IncludeActionTest {
96100
public void setUp() throws Exception {
97101
FileTestUtil.makeTestOutputDir();
98102
HashMap<ElementSelector, Supplier<Action>> rulesMap = new HashMap<>();
99-
rulesMap.put(new ElementSelector("x"), () -> new TopElementAction());
100-
rulesMap.put(new ElementSelector("x/include"), () -> new IncludeAction());
101-
rulesMap.put(new ElementSelector("x/stack"), () -> new StackAction());
103+
rulesMap.put(new ElementSelector(CONFIGURATION_TAG), () -> new TopElementAction());
104+
rulesMap.put(new ElementSelector(CONFIGURATION_TAG+"/include"), () -> new IncludeAction());
105+
rulesMap.put(new ElementSelector(CONFIGURATION_TAG+"/stack"), () -> new StackAction());
102106

103107
tc = new TrivialConfigurator(rulesMap) {
104-
105-
108+
106109
@Override
107110
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
108111
defaultProcessor.addHandler(TopModel.class, NOPModelHandler::makeInstance);
109-
defaultProcessor.addHandler(IncludeModel.class, NOPModelHandler::makeInstance);
112+
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);
110113
defaultProcessor.addHandler(StackModel.class, StackModelHandler::makeInstance);
111114
}
115+
116+
public void buildModelInterpretationContext() {
117+
super.buildModelInterpretationContext();
118+
this.modelInterpretationContext.setConfiguratorSupplier( () -> this.makeAnotherInstance() );
119+
}
112120
};
113121

114122
tc.setContext(context);
123+
tc.getRuleStore().addPathPathMapping(INCLUDED_TAG, CONFIGURATION_TAG);
115124
}
116125

117126
@AfterEach
118127
public void tearDown() throws Exception {
119-
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
128+
//StatusPrinter.printInCaseOfErrorsOrWarnings(context);
120129
context = null;
121130
System.clearProperty(INCLUDE_KEY);
122131
System.clearProperty(SECOND_FILE_KEY);
@@ -128,22 +137,22 @@ public void tearDown() throws Exception {
128137
public void basicFile() throws JoranException {
129138
System.setProperty(INCLUDE_KEY, INCLUDED_FILE);
130139
tc.doConfigure(TOP_BY_FILE);
131-
StatusPrinter.print(context);
140+
//StatusPrinter.print(context);
132141
verifyConfig(new String[] { "IA", "IB" });
133142
}
134143

135144
@Test
136145
public void optionalFile() throws JoranException {
137146
tc.doConfigure(TOP_OPTIONAL);
138147
verifyConfig(new String[] { "IA", "IB" });
139-
StatusPrinter.print(context);
148+
//StatusPrinter.print(context);
140149
}
141150

142151
@Test
143152
public void optionalResource() throws JoranException {
144153
tc.doConfigure(TOP_OPTIONAL_RESOURCE);
145154
verifyConfig(new String[] { "IA", "IB" });
146-
StatusPrinter.print(context);
155+
//StatusPrinter.print(context);
147156
Assertions.assertEquals(Status.INFO, statusChecker.getHighestLevel(0));
148157
}
149158

@@ -158,7 +167,7 @@ public void basicResource() throws JoranException {
158167
public void basicURL() throws JoranException {
159168
System.setProperty(INCLUDE_KEY, URL_TO_INCLUDE);
160169
tc.doConfigure(TOP_BY_URL);
161-
StatusPrinter.print(context);
170+
//StatusPrinter.print(context);
162171
verifyConfig(new String[] { "IA", "IB" });
163172
}
164173

@@ -175,7 +184,7 @@ public void withCorruptFile() throws JoranException, IOException {
175184
System.setProperty(INCLUDE_KEY, tmpOut);
176185
tc.doConfigure(TOP_BY_FILE);
177186
Assertions.assertEquals(Status.ERROR, statusChecker.getHighestLevel(0));
178-
StatusPrinter.print(context);
187+
//StatusPrinter.print(context);
179188
Assertions.assertTrue(statusChecker.containsException(SAXParseException.class));
180189

181190
// we like to erase the temp file in order to see

0 commit comments

Comments
 (0)