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

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.Appender;
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.helpers.Constants;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 */
public class StyleConverterTest {
    private static final String CONFIG = "log4j-style.xml";
    private static Configuration config;
    private static ListAppender app;
    private static LoggerContext ctx;

    private static final String EXPECTED =
        "\u001B[1;31mERROR\u001B[m \u001B[1;36mLoggerTest\u001B[m o.a.l.l.c.p.StyleConverterTest org.apache.logging.log4j.core.pattern.StyleConverterTest"
        + Constants.LINE_SEP;

    @BeforeClass
    public static void setupClass() {
        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
        ctx = (LoggerContext) LogManager.getContext(false);
        config = ctx.getConfiguration();
        for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
            if (entry.getKey().equals("List")) {
                app = (ListAppender) entry.getValue();
                break;
            }
        }
    }

    @AfterClass
    public static void cleanupClass() {
        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
        ctx.reconfigure();
        StatusLogger.getLogger().reset();
        ThreadContext.clear();
    }

    org.apache.logging.log4j.Logger logger = LogManager.getLogger("LoggerTest");

    @Test
    public void testReplacement() {
        logger.error(this.getClass().getName());

        final List<String> msgs = app.getMessages();
        assertNotNull(msgs);
        assertTrue("Incorrect number of messages. Should be 1 is " + msgs.size(), msgs.size() == 1);
        assertTrue("Replacement failed - expected ending " + EXPECTED + ", actual " + msgs.get(0), msgs.get(0).endsWith(EXPECTED));
        app.clear();
    }
}

@@ -958,30 +958,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -991,20 +991,16 @@
 og4j.Log
-Mana
 ger;%0Aimp
@@ -1032,719 +1032,217 @@
 g4j.
-ThreadContext;%0Aimport org.apache.logging.log4j.core.Appender;%0Aimport org.apache.logging.log4j.core.LoggerContext;%0Aimport org.apache.logging.log4j.core.config.Configuration;%0Aimport org.apache.logging.log4j.core.config.ConfigurationFactory;%0Aimport org.apache.logging.log4j.core.helpers.Constants;%0Aimport org.apache.logging.log4j.status.StatusLogger;%0Aimport org.apache.logging.log4j.test.appender.ListAppender;%0Aimport org.junit.AfterClass;%0Aimport org.junit.BeforeClass;%0Aimport org.junit.Test;%0A%0A/**%0A *%0A */%0Apublic class StyleConverterTest %7B%0A    private static final String CONFIG = %22log4j-style.xml%22;%0A    private static Configuration config;%0A    private static ListAppender app;%0A    private static LoggerContext ctx;
+core.helpers.Constants;%0Aimport org.apache.logging.log4j.junit.InitialLoggerContext;%0Aimport org.apache.logging.log4j.test.appender.ListAppender;%0Aimport org.junit.*;%0A%0A/**%0A *%0A */%0Apublic class StyleConverterTest %7B
 %0A%0A  
@@ -1477,19 +1477,12 @@
    @
-BeforeClass
+Rule
 %0A   
@@ -1493,463 +1493,311 @@
 lic 
-static void setupClass() %7B%0A        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);%0A        ctx = (LoggerContext) LogManager.getContext(false);%0A 
+InitialLoggerContext init = new InitialLoggerContext(%22log4j-style.xml%22);%0A%0A
     
-   config = ctx.getConfiguration();%0A        for (final Map.Entry%3CString, Appender%3E entry : config.getAppenders().entrySet()) %7B%0A            if (entry.getKey().equals(%22List%22)) %7B%0A                app = (ListAppender) entry.getValue();%0A                break;%0A            %7D%0A        %7D
+private Logger logger;%0A    private ListAppender app;%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A        this.logger = this.init.getLogger(%22LoggerTest%22);%0A        app = (ListAppender) this.init.getAppender(%22List%22);
 %0A   
@@ -1810,21 +1810,16 @@
   @After
-Class
 %0A    pub
@@ -1826,304 +1826,69 @@
 lic 
-static 
 void 
-cleanupClass() %7B%0A        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);%0A        ctx.reconfigure();%0A        StatusLogger.getLogger().reset();%0A        ThreadContext.clear();%0A    %7D%0A%0A    org.apache.logging.log4j.Logger logger = LogManager.getLogger(%22LoggerTest%22);
+tearDown() throws Exception %7B%0A        app.clear();%0A    %7D
 %0A%0A  
@@ -2296,37 +2296,16 @@
 CTED));%0A
-        app.clear();%0A
     %7D%0A%7D%0A

Update to use InitialLoggerContext rule.