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

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.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.apache.logging.log4j.test.appender.ListAppender;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.*;

public class PluginManagerPackagesTest {
    private static Configuration config;
    private static ListAppender listAppender;
    private static LoggerContext ctx;

    @BeforeClass
    public static void setupClass() {
    }

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

    @Test
    public void test() throws Exception {

        // To ensure our custom plugin is NOT included in the log4j plugin metadata file,
        // we make sure the class does not exist until after the build is finished.
        // So we don't create the custom plugin class until this test is run.
        final File orig = new File("target/test-classes/customplugin/FixedStringLayout.java.source");
        final File f = new File(orig.getParentFile(), "FixedStringLayout.java");
        assertTrue("renamed source file OK", orig.renameTo(f));
        compile(f);
        assertTrue("reverted source file OK", f.renameTo(orig));

        // load the compiled class
        Class.forName("customplugin.FixedStringLayout");

        // now that the custom plugin class exists, we load the config
        // with the packages element pointing to our custom plugin
        ctx = Configurator.initialize("Test1", "customplugin/log4j2-741.xml");
        config = ctx.getConfiguration();
        listAppender = (ListAppender) config.getAppender("List");

        final Logger logger = LogManager.getLogger(PluginManagerPackagesTest.class);
        logger.info("this message is ignored");

        final List<String> messages = listAppender.getMessages();
        assertEquals(messages.toString(), 1, messages.size());
        assertEquals("abc123XYZ", messages.get(0));
    }

    private void compile(final File f) throws IOException {
        // set up compiler
        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
        final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
        final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(f));

        // compile generated source
        // (switch off annotation processing: no need to create Log4j2Plugins.dat)
        final List<String> options = Arrays.asList("-proc:none");
        compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits).call();

        // check we don't have any compilation errors
        final List<String> errors = new ArrayList<>();
        for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
                errors.add(String.format("Compile error: %s%n", diagnostic.getMessage(Locale.getDefault())));
            }
        }
        fileManager.close();
        assertTrue(errors.toString(), errors.isEmpty());
    }
}

@@ -3861,16 +3861,76 @@
   final 
+List%3CString%3E errors = new ArrayList%3C%3E();%0A        try (final 
 Standard
@@ -4015,18 +4015,24 @@
 l, null)
-;%0A
+) %7B%0A    
         
@@ -4133,24 +4133,45 @@
 Files(Arrays
+%0A                    
 .asList(f));
@@ -4168,24 +4168,28 @@
 sList(f));%0A%0A
+    
         // c
@@ -4212,16 +4212,20 @@
  source%0A
+    
         
@@ -4299,32 +4299,36 @@
 ns.dat)%0A        
+  
+  
 final List%3CStrin
@@ -4365,24 +4365,28 @@
 roc:none%22);%0A
+    
         comp
@@ -4476,24 +4476,28 @@
 );%0A%0A        
+ 
+   
 // check we 
@@ -4538,59 +4538,8 @@
     
-    final List%3CString%3E errors = new ArrayList%3C%3E();%0A
     
@@ -4647,16 +4647,20 @@
         
+ 
+   
 if (diag
@@ -4700,24 +4700,28 @@
 nd.ERROR) %7B%0A
+    
             
@@ -4822,32 +4822,36 @@
 ));%0A            
+    
 %7D%0A        %7D%0A    
@@ -4836,32 +4836,36 @@
       %7D%0A        
+ 
+   
 %7D%0A        fileMa
@@ -4862,28 +4862,9 @@
     
-fileManager.close();
+%7D
 %0A   

Update Java platform from Java 6 to 7. Use try-with-resources.