/*
 * 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.core.config;

import java.io.File;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.config.xml.XMLConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.status.StatusLogger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

/**
 *
 */
public class CustomConfigurationTest {

    private static final String CONFIG = "log4j-props.xml";

    @BeforeClass
    public static void setupClass() {
    }

    @AfterClass
    public static void cleanupClass() {
        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        ctx.reconfigure();
        StatusLogger.getLogger().reset();
    }

    @Test
    public void testConfig() {
        File file = new File("target/test.log");
        file.delete();
        System.setProperty("log4j.level", "error");
        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        ctx.reconfigure();
        final Configuration config = ctx.getConfiguration();
        assertTrue("Configuration is not an XMLConfiguration", config instanceof XMLConfiguration);
        Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null,
            null,null, null, null, null);
        Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true",
            "false", "false", "4000", layout, null, "false", null, config);
        appender.start();
        config.addAppender(appender);
        AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
        AppenderRef[] refs = new AppenderRef[] {ref};

        LoggerConfig loggerConfig = LoggerConfig.createLogger("false", "info", "org.apache.logging.log4j",
            "true", refs, null, config, null );
        loggerConfig.addAppender(appender, null, null);
        config.addLogger("org.apache.logging.log4j", loggerConfig);
        ctx.updateLoggers();
        Logger logger = LogManager.getLogger("org.apache.logging.log4j.core.config.CustomConfigurationTest");
        logger.info("This is a test");
        file = new File("target/test.log");
        assertTrue("log file not created", file.exists());
        assertTrue("No data logged", file.length() > 0);

    }
}

@@ -867,52 +867,8 @@
 e;%0A%0A
-import org.apache.logging.log4j.LogManager;%0A
 impo
@@ -1268,27 +1268,34 @@
 g4j.
-status.StatusLogger
+junit.InitialLoggerContext
 ;%0Aim
@@ -1309,26 +1309,22 @@
 g.junit.
-AfterClass
+Before
 ;%0Aimport
@@ -1334,27 +1334,20 @@
 g.junit.
-BeforeClass
+Rule
 ;%0Aimport
@@ -1468,14 +1468,13 @@
    p
-rivate
+ublic
  sta
@@ -1494,249 +1494,67 @@
 ing 
-CONFIG = %22log4j-props.xml%22;%0A%0A    @BeforeClass%0A    public static void setupClass() %7B%0A    %7D%0A%0A    @AfterClass%0A    public static void cleanupClass() %7B%0A        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);%0A        fin
+LOG_FILE = %22target/test.log%22;%0A%0A    @Rule%0A    public Initi
 al
- 
 Logg
@@ -1567,125 +1567,149 @@
 ext 
-ctx = (LoggerContext) LogManager.getContext(false
+init = new InitialLoggerContext(%22log4j-props.xml%22
 );%0A
+%0A
     
-    ctx.reconfigure();%0A        StatusLogger.getLogger().res
+@Before%0A    public void setUp() throws Exception %7B%0A        new File(LOG_FILE).del
 et
+e
 ();%0A
@@ -1768,270 +1768,95 @@
     
-File file = new File(%22target/test.log%22);%0A        file.delete();%0A        System.setProperty(%22log4j.level%22, %22error%22);%0A        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);%0A        final LoggerContext ctx = (LoggerContext) LogManager
+System.setProperty(%22log4j.level%22, %22error%22);%0A        final LoggerContext ctx = this.init
 .get
@@ -1863,21 +1863,16 @@
 Context(
-false
 );%0A     
@@ -2258,33 +2258,24 @@
 ppender(
-%22target/test.log%22
+LOG_FILE
 , %22false
@@ -2910,18 +2910,11 @@
 r = 
-LogManager
+ctx
 .get
@@ -3032,16 +3032,27 @@
         
+final File 
 file = n
@@ -3059,33 +3059,24 @@
 ew File(
-%22target/test.log%22
+LOG_FILE
 );%0A     

Update test to use InitialLoggerContext rule.