From 5a41c892002012550f207e45d3de57ccbfb6d259 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Wed, 10 Apr 2024 22:11:45 +0200 Subject: [PATCH] fix IncludeActionTest Signed-off-by: Ceki Gulcu --- .../model/processor/IncludeModelHandler.java | 5 +++ .../joran/inclusion/intermediaryByFile.xml | 2 +- .../joran/inclusion/multiIncludeByFile.xml | 6 ++-- .../input/joran/inclusion/topByEntity.xml | 6 ++-- .../test/input/joran/inclusion/topByFile.xml | 6 ++-- .../input/joran/inclusion/topByResource.xml | 6 ++-- .../test/input/joran/inclusion/topByUrl.xml | 6 ++-- .../input/joran/inclusion/topOptional.xml | 6 ++-- .../joran/inclusion/topOptionalResource.xml | 6 ++-- .../core/joran/TrivialConfigurator.java | 13 +++++-- .../core/joran/action/IncludeActionTest.java | 35 ++++++++++++------- 11 files changed, 60 insertions(+), 37 deletions(-) diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/processor/IncludeModelHandler.java b/logback-core/src/main/java/ch/qos/logback/core/model/processor/IncludeModelHandler.java index 2baedadb8c..7775ab009e 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/processor/IncludeModelHandler.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/processor/IncludeModelHandler.java @@ -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 { diff --git a/logback-core/src/test/input/joran/inclusion/intermediaryByFile.xml b/logback-core/src/test/input/joran/inclusion/intermediaryByFile.xml index dce2acb6c3..d5491fef89 100644 --- a/logback-core/src/test/input/joran/inclusion/intermediaryByFile.xml +++ b/logback-core/src/test/input/joran/inclusion/intermediaryByFile.xml @@ -1,5 +1,5 @@ - + diff --git a/logback-core/src/test/input/joran/inclusion/multiIncludeByFile.xml b/logback-core/src/test/input/joran/inclusion/multiIncludeByFile.xml index dd1d158db1..ab5c0bc747 100644 --- a/logback-core/src/test/input/joran/inclusion/multiIncludeByFile.xml +++ b/logback-core/src/test/input/joran/inclusion/multiIncludeByFile.xml @@ -1,7 +1,7 @@ - + - + - + diff --git a/logback-core/src/test/input/joran/inclusion/topByEntity.xml b/logback-core/src/test/input/joran/inclusion/topByEntity.xml index 519219086f..4c340d54d0 100755 --- a/logback-core/src/test/input/joran/inclusion/topByEntity.xml +++ b/logback-core/src/test/input/joran/inclusion/topByEntity.xml @@ -1,10 +1,10 @@ - ]> - + &includedEntity; - + diff --git a/logback-core/src/test/input/joran/inclusion/topByFile.xml b/logback-core/src/test/input/joran/inclusion/topByFile.xml index 4dc521c261..7041261dec 100644 --- a/logback-core/src/test/input/joran/inclusion/topByFile.xml +++ b/logback-core/src/test/input/joran/inclusion/topByFile.xml @@ -1,6 +1,6 @@ - + - + - + diff --git a/logback-core/src/test/input/joran/inclusion/topByResource.xml b/logback-core/src/test/input/joran/inclusion/topByResource.xml index 63e02db68f..03d750731d 100644 --- a/logback-core/src/test/input/joran/inclusion/topByResource.xml +++ b/logback-core/src/test/input/joran/inclusion/topByResource.xml @@ -1,8 +1,8 @@ - + - + - + diff --git a/logback-core/src/test/input/joran/inclusion/topByUrl.xml b/logback-core/src/test/input/joran/inclusion/topByUrl.xml index f49cd974de..93159d1705 100644 --- a/logback-core/src/test/input/joran/inclusion/topByUrl.xml +++ b/logback-core/src/test/input/joran/inclusion/topByUrl.xml @@ -1,6 +1,6 @@ - + - + - + diff --git a/logback-core/src/test/input/joran/inclusion/topOptional.xml b/logback-core/src/test/input/joran/inclusion/topOptional.xml index 3625575e20..f060c6f504 100644 --- a/logback-core/src/test/input/joran/inclusion/topOptional.xml +++ b/logback-core/src/test/input/joran/inclusion/topOptional.xml @@ -1,9 +1,9 @@ - + - + - + diff --git a/logback-core/src/test/input/joran/inclusion/topOptionalResource.xml b/logback-core/src/test/input/joran/inclusion/topOptionalResource.xml index 951b11d95c..439d7687ea 100644 --- a/logback-core/src/test/input/joran/inclusion/topOptionalResource.xml +++ b/logback-core/src/test/input/joran/inclusion/topOptionalResource.xml @@ -1,9 +1,9 @@ - + - + - + diff --git a/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java b/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java index 583ddd51c9..76fc694c68 100644 --- a/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java +++ b/logback-core/src/test/java/ch/qos/logback/core/joran/TrivialConfigurator.java @@ -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; @@ -29,15 +30,23 @@ public TrivialConfigurator(HashMap> 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 actionSupplier = rulesMap.get(elementSelector); - rs.addRule(elementSelector, actionSupplier); + aRuleStore.addRule(elementSelector, actionSupplier); } } diff --git a/logback-core/src/test/java/ch/qos/logback/core/joran/action/IncludeActionTest.java b/logback-core/src/test/java/ch/qos/logback/core/joran/action/IncludeActionTest.java index e84e6ee124..034920e805 100755 --- a/logback-core/src/test/java/ch/qos/logback/core/joran/action/IncludeActionTest.java +++ b/logback-core/src/test/java/ch/qos/logback/core/joran/action/IncludeActionTest.java @@ -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; @@ -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"; @@ -96,27 +100,32 @@ public class IncludeActionTest { public void setUp() throws Exception { FileTestUtil.makeTestOutputDir(); HashMap> 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); @@ -128,7 +137,7 @@ 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" }); } @@ -136,14 +145,14 @@ public void basicFile() throws JoranException { 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)); } @@ -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" }); } @@ -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