[LOG4J2-1500] Merging configurations fail with an NPE when comparing Nodes with different attributes. Closes #37.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d1c02ee5 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d1c02ee5 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d1c02ee5 Branch: refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure Commit: d1c02ee5e3a863528dfad66a9bbd17c45d475a25 Parents: 5b7b75d Author: Gary Gregory <ggreg...@apache.org> Authored: Fri Aug 5 17:52:25 2016 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Fri Aug 5 17:52:25 2016 -0700 ---------------------------------------------------------------------- .../config/composite/DefaultMergeStrategy.java | 6 ++- .../core/config/CompositeConfigurationTest.java | 19 ++++++++ .../test/resources/log4j-comp-root-loggers.xml | 46 ++++++++++++++++++++ src/changes/changes.xml | 3 ++ 4 files changed, 72 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1c02ee5/log4j-core/src/main/java/org/apache/logging/log4j/core/config/composite/DefaultMergeStrategy.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/composite/DefaultMergeStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/composite/DefaultMergeStrategy.java index 0263ec2..c799e1d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/composite/DefaultMergeStrategy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/composite/DefaultMergeStrategy.java @@ -260,10 +260,12 @@ public class DefaultMergeStrategy implements MergeStrategy { } private boolean isSameName(final Node node1, final Node node2) { - return node1.getAttributes().get(NAME).toLowerCase().equals(node2.getAttributes().get(NAME).toLowerCase()); + final String value = node1.getAttributes().get(NAME); + return value != null && value.toLowerCase().equals(node2.getAttributes().get(NAME).toLowerCase()); } private boolean isSameReference(final Node node1, final Node node2) { - return node1.getAttributes().get(REF).toLowerCase().equals(node2.getAttributes().get(REF).toLowerCase()); + final String value = node1.getAttributes().get(REF); + return value != null && value.toLowerCase().equals(node2.getAttributes().get(REF).toLowerCase()); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1c02ee5/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationTest.java index a4ffa73..8482cf4 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CompositeConfigurationTest.java @@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.config; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.util.Map; @@ -26,6 +27,7 @@ import org.apache.logging.log4j.core.appender.ConsoleAppender; import org.apache.logging.log4j.core.appender.FileAppender; import org.apache.logging.log4j.core.config.composite.CompositeConfiguration; import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -110,6 +112,23 @@ public class CompositeConfigurationTest { }; runTest(lcr, test); } + + @Test + public void testAttributeCheckWhenMergingConfigurations() { + final LoggerContextRule lcr = new LoggerContextRule("classpath:log4j-comp-root-loggers.xml,log4j-comp-logger.json"); + final Statement test = new Statement() { + @Override + public void evaluate() throws Throwable { + try { + final CompositeConfiguration config = (CompositeConfiguration) lcr.getConfiguration(); + Assert.assertNotNull(config); + } catch (NullPointerException e) { + fail("Should not throw NullPointerException when there are different nodes."); + } + } + }; + runTest(lcr, test); + } /* @Test public void overrideFilter() { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1c02ee5/log4j-core/src/test/resources/log4j-comp-root-loggers.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j-comp-root-loggers.xml b/log4j-core/src/test/resources/log4j-comp-root-loggers.xml new file mode 100644 index 0000000..79ea7e5 --- /dev/null +++ b/log4j-core/src/test/resources/log4j-comp-root-loggers.xml @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration status="ERROR" name="LoggerConfigTest"> + <Appenders> + <Console name="STDOUT"> + <PatternLayout pattern="%m%n"/> + </Console> + <File name="File" fileName="${filename}" bufferedIO="false"> + <PatternLayout> + <Pattern>%d %p %C{1.} [%t] %m%n</Pattern> + </PatternLayout> + </File> + </Appenders> + + <Loggers> + <Logger name="cat1" level="debug" additivity="false"> + <AppenderRef ref="File"/> + </Logger> + + <Logger name="cat3" level="debug" additivity="false"> + <AppenderRef ref="File"/> + </Logger> + + <Root level="error"> + <AppenderRef ref="STDOUT"/> + <Property name="hostname">server1</Property> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d1c02ee5/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index cceb355..77bc02b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -24,6 +24,9 @@ </properties> <body> <release version="2.7" date="2016-MM-DD" description="GA Release 2.7"> + <action issue="LOG4J2-1500" dev="ggregory" type="fix" due-to="Jose Leon"> + Merging configurations fail with an NPE when comparing Nodes with different attributes. + </action> <action issue="LOG4J2-1482" dev="ggregory" type="fix" due-to="Gary Gregory, Sumit Singhal"> Improper header in CsvParameterLayout. </action>