<commit_before>package picocli.codegen.aot.graalvm.processor;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.junit.Test;

import javax.tools.Diagnostic;
import javax.tools.JavaFileManager;
import javax.tools.StandardLocation;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
import static org.junit.Assert.*;
import static picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor.OPTION_PROJECT;
import static picocli.codegen.aot.graalvm.processor.ProxyConfigGen.OPTION_INTERFACE_CLASSES;
import static picocli.codegen.aot.graalvm.processor.ResourceConfigGen.OPTION_BUNDLES;
import static picocli.codegen.aot.graalvm.processor.ResourceConfigGen.OPTION_RESOURCE_REGEX;
import static picocli.annotation.processing.tests.Resources.slurp;

public class NativeImageConfigGeneratorProcessorTest {

    private void assertNoGeneratedFile(Compilation compilation, JavaFileManager.Location location, String path) {
        boolean success = true;
        try {
            assertThat(compilation).generatedFile(location, path);
            success = false;
        } catch (AssertionError expected) {
        }
        if (!success) {
            fail("Found unexpected generated file: " + location + path);
        }
    }

    private void expectGeneratedWithNotes(Compilation compilation, String[][] allParams) {
        int noteCount = 0;
        for (String[] params : allParams) {
            String noteText = String.format(
                    "%s writing to: CLASS_OUTPUT/META-INF/native-image/picocli-generated/%s", params[0], params[1]);
            boolean generated = Boolean.parseBoolean(params[2]);
            if (generated) {
                assertThat(compilation)
                        .generatedFile(StandardLocation.CLASS_OUTPUT,
                                "META-INF/native-image/picocli-generated/" + params[1]);
                assertThat(compilation).hadNoteContaining(noteText);
                noteCount++;
            } else {
                assertThat(compilation).hadNoteContaining(params[0] + " is not enabled");
                assertNoGeneratedFile(compilation, StandardLocation.CLASS_OUTPUT,
                        "META-INF/native-image/picocli-generated/" + params[1]);
                List<String> notes = compilation.diagnostics().stream()
                        .filter(d -> d.getKind() == Diagnostic.Kind.NOTE)
                        .map(d -> d.getMessage(Locale.ENGLISH))
                        .collect(Collectors.toList());
                assertFalse(notes.stream().anyMatch(note -> note.contains(noteText)));
                assertThat(compilation).hadNoteContainingMatch(params[0] + " is not enabled");
                noteCount++;
            }
        }
        assertThat(compilation).hadNoteCount(noteCount);
    }

    @Test
    public void testNothingDisabledByDefault() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));
        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "true" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableReflect() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ReflectConfigGen.OPTION_DISABLE) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));
        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "false" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "true" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableResource() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ResourceConfigGen.OPTION_DISABLE) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));
        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "false" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "true" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableProxy() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ProxyConfigGen.OPTION_DISABLE) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));

        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "false" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableProxyQuiet() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ProxyConfigGen.OPTION_DISABLE, "-A" + AbstractGenerator.OPTION_QUIET) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));

        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "false" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testGenerateReflectJava8ApiOptionProject() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + OPTION_PROJECT + "=example/full")
                        .compile(JavaFileObjects.forSourceLines(
                                "picocli.codegen.graalvm.example.ExampleFull",
                                slurp("/picocli/codegen/graalvm/example/ExampleFull.java")));
        assertThat(compilation).succeeded();
        assertThat(compilation)
                .generatedFile(StandardLocation.CLASS_OUTPUT, "META-INF/native-image/picocli-generated/example/full/reflect-config.json")<commit_msg>[#952] (Work in progress) fix broken annotationproc test<commit_after>package picocli.codegen.aot.graalvm.processor;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.junit.Test;

import javax.tools.Diagnostic;
import javax.tools.JavaFileManager;
import javax.tools.StandardLocation;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
import static org.junit.Assert.*;
import static picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor.OPTION_PROJECT;
import static picocli.codegen.aot.graalvm.processor.ProxyConfigGen.OPTION_INTERFACE_CLASSES;
import static picocli.codegen.aot.graalvm.processor.ResourceConfigGen.OPTION_BUNDLES;
import static picocli.codegen.aot.graalvm.processor.ResourceConfigGen.OPTION_RESOURCE_REGEX;
import static picocli.annotation.processing.tests.Resources.slurp;

public class NativeImageConfigGeneratorProcessorTest {

    private void assertNoGeneratedFile(Compilation compilation, JavaFileManager.Location location, String path) {
        boolean success = true;
        try {
            assertThat(compilation).generatedFile(location, path);
            success = false;
        } catch (AssertionError expected) {
        }
        if (!success) {
            fail("Found unexpected generated file: " + location + path);
        }
    }

    private void expectGeneratedWithNotes(Compilation compilation, String[][] allParams) {
        int noteCount = 0;
        for (String[] params : allParams) {
            String noteText = String.format(
                    "%s writing to: CLASS_OUTPUT/META-INF/native-image/picocli-generated/%s", params[0], params[1]);
            boolean generated = Boolean.parseBoolean(params[2]);
            boolean verbose = Boolean.parseBoolean(params[3]);
            if (generated) {
                assertThat(compilation)
                        .generatedFile(StandardLocation.CLASS_OUTPUT,
                                "META-INF/native-image/picocli-generated/" + params[1]);
                if (verbose) {
                    assertThat(compilation).hadNoteContaining(noteText);
                    noteCount++;
                }
            } else {
                assertNoGeneratedFile(compilation, StandardLocation.CLASS_OUTPUT,
                        "META-INF/native-image/picocli-generated/" + params[1]);
                if (verbose) {
                    assertThat(compilation).hadNoteContaining(params[0] + " is not enabled");
                    List<String> notes = compilation.diagnostics().stream()
                            .filter(d -> d.getKind() == Diagnostic.Kind.NOTE)
                            .map(d -> d.getMessage(Locale.ENGLISH))
                            .collect(Collectors.toList());
                    assertFalse(notes.stream().anyMatch(note -> note.contains(noteText)));
                    assertThat(compilation).hadNoteContainingMatch(params[0] + " is not enabled");
                    noteCount++;
                }
            }
        }
        assertThat(compilation).hadNoteCount(noteCount);
    }

    @Test
    public void testNothingDisabledByDefault() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));
        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true", "true" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true", "true" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "true", "true" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableReflect() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ReflectConfigGen.OPTION_DISABLE) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));
        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "false", "true" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" , "true"},
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "true" , "true"},
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableResource() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ResourceConfigGen.OPTION_DISABLE) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));
        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" , "true"},
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "false", "true" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "true" , "true"},
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableProxy() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ProxyConfigGen.OPTION_DISABLE) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));

        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" , "true"},
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" , "true"},
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "false", "true" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testOptionDisableProxyQuiet() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + ProxyConfigGen.OPTION_DISABLE, "-A" + AbstractGenerator.OPTION_QUIET) // no value
                        .compile(JavaFileObjects.forResource(
                                "picocli/examples/subcommands/ParentCommandDemo.java"));

        assertThat(compilation).succeeded();
        String[][] allParams = {
                { ReflectConfigGen.class.getSimpleName(),  "reflect-config.json",  "true" , "false" },
                { ResourceConfigGen.class.getSimpleName(), "resource-config.json", "true" , "false" },
                { ProxyConfigGen.class.getSimpleName(),    "proxy-config.json",    "false", "false" },
        };
        expectGeneratedWithNotes(compilation, allParams);
    }

    @Test
    public void testGenerateReflectJava8ApiOptionProject() {
        NativeImageConfigGeneratorProcessor processor = new NativeImageConfigGeneratorProcessor();
        Compilation compilation =
                javac()
                        .withProcessors(processor)
                        .withOptions("-A" + OPTION_PROJECT + "=example/full")
                        .compile(JavaFileObjects.forSourceLines(
                                "picocli.codegen.graalvm.example.ExampleFull",
                                slurp("/picocli/codegen/graalvm/example/ExampleFull.java")));
        assertThat(compilation).succeeded();
        assertThat(compilation)
                .generatedFile(StandardLocation.CLASS_OUTPUT, "META-INF/native-image/picocli-generated/example/full/reflect-config.json")
{'fraction_non_alphanumeric': 0.10606847970437092, 'fraction_numerical': 0.0012702811940643224, 'mean_word_length': 2.2568634825122227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4363292', 'n_tokens_mistral': 4435, 'n_tokens_neox': 4280, 'n_words': 654}