From ebe764c01b5606a9ce477acbb025912cb385a58e Mon Sep 17 00:00:00 2001 From: Matt Sicker Date: Sun, 12 Jan 2014 16:16:46 -0600 Subject: [PATCH] Add config file JUnit rules. --- .../log4j/test/rule/AbstractConfigFileRule.java | 70 +++++++++++++++++++ .../logging/log4j/test/rule/LegacyLog4jConfig.java | 35 ++++++++++ .../logging/log4j/test/rule/Log4jConfig.java | 81 ++++++++++++++++++++++ .../logging/log4j/test/rule/LogbackConfig.java | 35 ++++++++++ 4 files changed, 221 insertions(+) create mode 100644 log4j-core/src/test/java/org/apache/logging/log4j/test/rule/AbstractConfigFileRule.java create mode 100644 log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LegacyLog4jConfig.java create mode 100644 log4j-core/src/test/java/org/apache/logging/log4j/test/rule/Log4jConfig.java create mode 100644 log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LogbackConfig.java diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/AbstractConfigFileRule.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/AbstractConfigFileRule.java new file mode 100644 index 0000000..cf768cc --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/AbstractConfigFileRule.java @@ -0,0 +1,70 @@ +/* + * 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. + */ +package org.apache.logging.log4j.test.rule; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +public abstract class AbstractConfigFileRule implements TestRule { + private final String configFileProperty; + private final String configFileName; + + /** + * Construct a new external configuration rule using a specified file name. + * + * @param configFileProperty name of property to set. + * @param configFileName name or path to file to use for log4j2 config. + */ + protected AbstractConfigFileRule(final String configFileProperty, final String configFileName) { + this.configFileProperty = configFileProperty; + this.configFileName = configFileName; + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + init(description); + System.setProperty(configFileProperty, configFileName); + try { + base.evaluate(); + } finally { + System.clearProperty(configFileProperty); + reset(description); + } + } + }; + } + + /** + * Implements any additional steps to be taken to initialize logging system. + * + * @param description test description. + * @throws Throwable + */ + protected abstract void init(Description description) throws Throwable; + + /** + * Implements any additional steps to be taken to reset the logging system to its default state. + * + * @param description test description. + */ + protected abstract void reset(Description description); + +} diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LegacyLog4jConfig.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LegacyLog4jConfig.java new file mode 100644 index 0000000..6aade1b --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LegacyLog4jConfig.java @@ -0,0 +1,35 @@ +/* + * 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. + */ +package org.apache.logging.log4j.test.rule; + +import org.junit.runner.Description; + +public class LegacyLog4jConfig extends AbstractConfigFileRule { + public LegacyLog4jConfig(final String configFileName) { + super("log4j.configuration", configFileName); + } + + @Override + protected void init(Description description) throws Throwable { + // nothing special + } + + @Override + protected void reset(Description description) { + // nothing special + } +} diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/Log4jConfig.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/Log4jConfig.java new file mode 100644 index 0000000..5bdb407 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/Log4jConfig.java @@ -0,0 +1,81 @@ +/* + * 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. + */ +package org.apache.logging.log4j.test.rule; + +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.status.StatusLogger; +import org.junit.runner.Description; + +/** + * JUnit rule for using a specific log4j2 config file. Allows using both the LogManager and the rule object + * itself for getting the correct LoggerContext. Replaces the repetitive @BeforeClass/@AfterClass set up of + * the current config file. + *

+ * Usage: + *

+ *     {@code @Rule} public Log4jConfig config = new Log4jConfig("some-config.xml");
+ *
+ *     {@code @Test} public void test() {
+ *         Logger logger = config.getLogger("TestLog");
+ *     }
+ * 
+ */ +public class Log4jConfig extends AbstractConfigFileRule { + private final String configFileName; + private LoggerContext context; + + public Log4jConfig(final String configFileName) { + super(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, configFileName); + this.configFileName = configFileName; + } + + @Override + protected void init(final Description description) throws Throwable { + this.context = Configurator.initialize(description.getClassName(), this.configFileName); + } + + @Override + protected void reset(final Description description) { + if (this.context.isStarted()) { + this.context.stop(); + } + this.context.reconfigure(); + StatusLogger.getLogger().reset(); + } + + public LoggerContext getContext() { + return this.context; + } + + public Logger getLogger(String name) { + return this.context.getLogger(name); + } + + public Configuration getConfiguration() { + return this.context.getConfiguration(); + } + + @SuppressWarnings("unchecked") + public T getAppender(final String name) { + return (T) getConfiguration().getAppenders().get(name); + } +} diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LogbackConfig.java b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LogbackConfig.java new file mode 100644 index 0000000..b932e09 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/test/rule/LogbackConfig.java @@ -0,0 +1,35 @@ +/* + * 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. + */ +package org.apache.logging.log4j.test.rule; + +import org.junit.runner.Description; + +public class LogbackConfig extends AbstractConfigFileRule { + public LogbackConfig(final String configFileName) { + super("logback.configurationFile", configFileName); + } + + @Override + protected void init(Description description) throws Throwable { + // nothing special + } + + @Override + protected void reset(Description description) { + // nothing special + } +} -- 1.8.5.2