/*
 * 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 org.apache.logging.log4j.Level;
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.Filter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.filter.ThreadContextMapFilter;
import org.apache.logging.log4j.status.StatusLogger;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Map;

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

/**
 *
 */
public class XMLConfigurationTest {

    private static final String CONFIG = "log4j-test1.xml";
    private static final String LOGFILE = "target/test.log";

    @BeforeClass
    public static void setupClass() {
        System.setProperty(XMLConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
        final LoggerContext ctx = (LoggerContext) LogManager.getContext();
        final Configuration config = ctx.getConfiguration();
        if (config instanceof XMLConfiguration) {
            final String name = ((XMLConfiguration) config).getName();
            if (name == null || !name.equals("XMLConfigTest")) {
                ctx.reconfigure();
            }
        } else {
            ctx.reconfigure();
        }
    }

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

    @Test
    public void testLogger() {
        final Logger logger = LogManager.getLogger("org.apache.logging.log4j.test1.Test");
        assertTrue(logger instanceof org.apache.logging.log4j.core.Logger);
        final org.apache.logging.log4j.core.Logger l = (org.apache.logging.log4j.core.Logger) logger;
        assertEquals(Level.DEBUG, l.getLevel());
        final int filterCount = l.filterCount();
        assertTrue("number of filters - " + filterCount, filterCount == 1);
        final Iterator<Filter> iter = l.getFilters();
        final Filter filter = iter.next();
        assertTrue(filter instanceof ThreadContextMapFilter);
        final Map<String, Appender> appenders = l.getAppenders();
        assertNotNull(appenders);
        assertTrue("number of appenders = " + appenders.size(), appenders.size() == 1);
        final Appender a = appenders.get("STDOUT");
        assertNotNull(a);
        assertEquals(a.getName(), "STDOUT");
    }

    public void testConfiguredAppenders() {
        final LoggerContext ctx = (LoggerContext) LogManager.getContext();
        final Configuration c = ctx.getConfiguration();
        final Map<String, Appender> apps = c.getAppenders();
        assertNotNull(apps);
        assertEquals(apps.size(), 3);
    }

    @Test
    public void logToFile() throws Exception {
        final FileOutputStream fos = new FileOutputStream(LOGFILE, false);
        fos.flush();
        fos.close();
        final Logger logger = LogManager.getLogger("org.apache.logging.log4j.test2.Test");
        logger.debug("This is a test");
        final DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(LOGFILE)));
        int count = 0;
        String str = "";
        while (is.available() != 0) {
            str = is.readLine();
            ++count;
        }
        assertTrue("Incorrect count " + count, count == 1);
        assertTrue("Bad data", str.endsWith("This is a test"));
    }

}

@@ -4353,24 +4353,42 @@
 LOGFILE)));%0A
+        try %7B%0A    
         int 
@@ -4406,16 +4406,20 @@
         
+ 
+   
 String s
@@ -4435,16 +4435,20 @@
         
+   
+ 
 while (i
@@ -4481,16 +4481,20 @@
         
+    
 str = is
@@ -4518,16 +4518,20 @@
         
+  
+  
 ++count;
@@ -4531,16 +4531,20 @@
 +count;%0A
+    
         
@@ -4545,32 +4545,36 @@
       %7D%0A        
+    
 assertTrue(%22Inco
@@ -4609,32 +4609,36 @@
  == 1);%0A        
+  
+  
 assertTrue(%22Bad 
@@ -4673,21 +4673,75 @@
  a test%22));%0A
+        %7D finally %7B%0A            is.close();%0A        %7D%0A
     %7D%0A%0A%7D%0A

Fix stream resource leak.