Skip to content

Commit

Permalink
fix IncludeActionTest
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Apr 10, 2024
1 parent af92be4 commit 5a41c89
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand
}

InputStream in = getInputStream(mic, includeModel);
if(in == null) {
inError = true;
return;
}

SaxEventRecorder recorder = null;

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x>
<!DOCTYPE included>

<included>
<stack name="a"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x>
<!DOCTYPE configuration>

<x>
<configuration>
<include file="${includeKey}" />
<include file="${secondFileKey}" />
</x>
</configuration>
6 changes: 3 additions & 3 deletions logback-core/src/test/input/joran/inclusion/topByEntity.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x [
<!DOCTYPE configuration [
<!ENTITY includedEntity SYSTEM "includedEntity.xml">
]>

<x>
<configuration>

&includedEntity;

</x>
</configuration>
6 changes: 3 additions & 3 deletions logback-core/src/test/input/joran/inclusion/topByFile.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x>
<!DOCTYPE configuration>

<x>
<configuration>
<include file="${includeKey}" />
</x>
</configuration>
6 changes: 3 additions & 3 deletions logback-core/src/test/input/joran/inclusion/topByResource.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x>
<!DOCTYPE configuration>

<x>
<configuration>

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

</x>
</configuration>
6 changes: 3 additions & 3 deletions logback-core/src/test/input/joran/inclusion/topByUrl.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x>
<!DOCTYPE configuration>

<x>
<configuration>
<include url="${includeKey}" />
</x>
</configuration>
6 changes: 3 additions & 3 deletions logback-core/src/test/input/joran/inclusion/topOptional.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x>
<!DOCTYPE configuration>

<x>
<configuration>
<include optional="true" file="nonExistentFile.xml" />

<stack name="IA"/>
<stack name="IB"/>
</x>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE x>
<!DOCTYPE configuration>

<x>
<configuration>
<include optional="true" resource="nonExistentResource.xml" />

<stack name="IA"/>
<stack name="IB"/>
</x>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.function.Supplier;

import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.action.ImplicitModelAction;
import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.SaxEventInterpreter;
import ch.qos.logback.core.joran.spi.RuleStore;
Expand All @@ -29,15 +30,23 @@ public TrivialConfigurator(HashMap<ElementSelector, Supplier<Action>> rules) {
this.rulesMap = rules;
}


public TrivialConfigurator makeAnotherInstance() {
TrivialConfigurator tc = new TrivialConfigurator(rulesMap);
tc.setContext(context);
return tc;
}

@Override
protected void setImplicitRuleSupplier(SaxEventInterpreter interpreter) {
interpreter.setImplicitActionSupplier(ImplicitModelAction::new);
}

@Override
protected void addElementSelectorAndActionAssociations(RuleStore rs) {
protected void addElementSelectorAndActionAssociations(RuleStore aRuleStore) {
for (ElementSelector elementSelector : rulesMap.keySet()) {
Supplier<Action> actionSupplier = rulesMap.get(elementSelector);
rs.addRule(elementSelector, actionSupplier);
aRuleStore.addRule(elementSelector, actionSupplier);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Stack;
import java.util.function.Supplier;

import ch.qos.logback.core.model.processor.IncludeModelHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -49,7 +50,10 @@
import ch.qos.logback.core.status.testUtil.StatusChecker;
import ch.qos.logback.core.util.StatusPrinter;

@Disabled
import static ch.qos.logback.core.joran.JoranConstants.CONFIGURATION_TAG;
import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG;


public class IncludeActionTest {

final static String INCLUDE_KEY = "includeKey";
Expand Down Expand Up @@ -96,27 +100,32 @@ public class IncludeActionTest {
public void setUp() throws Exception {
FileTestUtil.makeTestOutputDir();
HashMap<ElementSelector, Supplier<Action>> rulesMap = new HashMap<>();
rulesMap.put(new ElementSelector("x"), () -> new TopElementAction());
rulesMap.put(new ElementSelector("x/include"), () -> new IncludeAction());
rulesMap.put(new ElementSelector("x/stack"), () -> new StackAction());
rulesMap.put(new ElementSelector(CONFIGURATION_TAG), () -> new TopElementAction());
rulesMap.put(new ElementSelector(CONFIGURATION_TAG+"/include"), () -> new IncludeAction());
rulesMap.put(new ElementSelector(CONFIGURATION_TAG+"/stack"), () -> new StackAction());

tc = new TrivialConfigurator(rulesMap) {



@Override
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
defaultProcessor.addHandler(TopModel.class, NOPModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, NOPModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);
defaultProcessor.addHandler(StackModel.class, StackModelHandler::makeInstance);
}

public void buildModelInterpretationContext() {
super.buildModelInterpretationContext();
this.modelInterpretationContext.setConfiguratorSupplier( () -> this.makeAnotherInstance() );
}
};

tc.setContext(context);
tc.getRuleStore().addPathPathMapping(INCLUDED_TAG, CONFIGURATION_TAG);
}

@AfterEach
public void tearDown() throws Exception {
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
//StatusPrinter.printInCaseOfErrorsOrWarnings(context);
context = null;
System.clearProperty(INCLUDE_KEY);
System.clearProperty(SECOND_FILE_KEY);
Expand All @@ -128,22 +137,22 @@ public void tearDown() throws Exception {
public void basicFile() throws JoranException {
System.setProperty(INCLUDE_KEY, INCLUDED_FILE);
tc.doConfigure(TOP_BY_FILE);
StatusPrinter.print(context);
//StatusPrinter.print(context);
verifyConfig(new String[] { "IA", "IB" });
}

@Test
public void optionalFile() throws JoranException {
tc.doConfigure(TOP_OPTIONAL);
verifyConfig(new String[] { "IA", "IB" });
StatusPrinter.print(context);
//StatusPrinter.print(context);
}

@Test
public void optionalResource() throws JoranException {
tc.doConfigure(TOP_OPTIONAL_RESOURCE);
verifyConfig(new String[] { "IA", "IB" });
StatusPrinter.print(context);
//StatusPrinter.print(context);
Assertions.assertEquals(Status.INFO, statusChecker.getHighestLevel(0));
}

Expand All @@ -158,7 +167,7 @@ public void basicResource() throws JoranException {
public void basicURL() throws JoranException {
System.setProperty(INCLUDE_KEY, URL_TO_INCLUDE);
tc.doConfigure(TOP_BY_URL);
StatusPrinter.print(context);
//StatusPrinter.print(context);
verifyConfig(new String[] { "IA", "IB" });
}

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

// we like to erase the temp file in order to see
Expand Down

0 comments on commit 5a41c89

Please sign in to comment.