/*
 * Copyright 2015 Google Inc.
 *
 * Licensed 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 com.palantir.javaformat.java;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Joiner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class DiagnosticTest {
    @TempDir
    public Path testFolder;

    private Locale backupLocale;

    @BeforeEach
    public void setUpLocale() throws Exception {
        backupLocale = Locale.getDefault();
        Locale.setDefault(Locale.ROOT);
    }

    @AfterEach
    public void restoreLocale() throws Exception {
        Locale.setDefault(backupLocale);
    }

    @Test
    public void parseError() throws Exception {
        String input = Joiner.on('\n')
                .join(
                        "public class InvalidSyntax {",
                        "  private static NumPrinter {",
                        "    public static void print(int n) {",
                        "      System.out.printf(\"%d%n\", n);",
                        "    }",
                        "  }",
                        "",
                        "  public static void main(String[] args) {",
                        "    NumPrinter.print(args.length);",
                        "  }",
                        "}");

        StringWriter stdout = new StringWriter();
        StringWriter stderr = new StringWriter();
        Main main = new Main(new PrintWriter(stdout, true), new PrintWriter(stderr, true), System.in);

        Path path = testFolder.resolve("InvalidSyntax.java");
        Files.write(path, input.getBytes(UTF_8));

        int result = main.format(path.toString());
        assertThat(stdout.toString()).isEmpty();
        assertThat(stderr.toString()).contains("InvalidSyntax.java:2:29: error: <identifier> expected");
        assertThat(result).isEqualTo(1);
    }

    @Test
    public void lexError() throws Exception {
        String input = "\\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu00not-actually-a-unicode-escape-sequence";

        StringWriter stdout = new StringWriter();
        StringWriter stderr = new StringWriter();
        Main main = new Main(new PrintWriter(stdout, true), new PrintWriter(stderr, true), System.in);

        Path path = testFolder.resolve("InvalidSyntax.java");
        Files.write(path, input.getBytes(UTF_8));

        int result = main.format(path.toString());
        assertThat(stdout.toString()).isEmpty();
        assertThat(stderr.toString()).contains("error: illegal unicode escape");
        assertThat(result).isEqualTo(1);
    }

    @Test
    public void oneFileParseError() throws Exception {
        String one = "class One {\n";
        String two = "class Two {}\n";

        StringWriter stdout = new StringWriter();
        StringWriter stderr = new StringWriter();
        Main main = new Main(new PrintWriter(stdout, true), new PrintWriter(stderr, true), System.in);

        Path pathOne = testFolder.resolve("One.java");
        Files.write(pathOne, one.getBytes(UTF_8));

        Path pathTwo = testFolder.resolve("Two.java");
        Files.write(pathTwo, two.getBytes(UTF_8));

        int result = main.format(pathOne.toString(), pathTwo.toString());
        assertThat(stdout.toString()).isEqualTo(two);
        assertThat(stderr.toString()).contains("One.java:1:13: error: reached end of file");
        assertThat(result).isEqualTo(1);
    }

    @Test
    public void oneFileParseErrorReplace() throws Exception {
        String one = "class One {}}\n";
        String two = "class Two {\n}\n";

        StringWriter stdout = new StringWriter();
        StringWriter stderr = new StringWriter();
        Main main = new Main(new PrintWriter(stdout, true), new PrintWriter(stderr, true), System.in);

        Path pathOne = testFolder.resolve("One.java");
        Files.write(pathOne, one.getBytes(UTF_8));

        Path pathTwo = testFolder.resolve("Two.java");
        Files.write(pathTwo, two.getBytes(UTF_8));

        int result = main.format("-i", pathOne.toString(), pathTwo.toString());
        assertThat(stdout.toString()).isEmpty();
        assertThat(stderr.toString()).contains("One.java:1:14: error: class, interface");
        assertThat(result).isEqualTo(1);
        // don't edit files with parse errors
        assertThat(Files.readAllLines(pathOne, UTF_8)).containsExactly("class One {}}");
        assertThat(Files.readAllLines(pathTwo, UTF_8)).containsExactly("class Two {}");
    }

    @Test
    public void parseError2() throws FormatterException, IOException, UsageException {
        String input = "class Foo { void f() {\n g() } }";

        Path path = testFolder.resolve("A.java");
        Files.write(path, input.getBytes(StandardCharsets.UTF_8));

        StringWriter out = new StringWriter();
        StringWriter err = new StringWriter();

        Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
        String[] args = {path.toString()};
        int exitCode = main.format(args);

        assertThat(exitCode).isEqualTo(1);
        assertThat(err.toString()).contains("A.java:2:6: error: ';' expected");
    }

    @Test
    public void parseErrorStdin() throws FormatterException, IOException, UsageException {
        String input = "class Foo { void f() {\n g() } }";

        InputStream inStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
        StringWriter out = new StringWriter();
        StringWriter err = new StringWriter();
        Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), inStream);
        String[] args = {"-"};
        int exitCode = main.format(args);

        assertThat(exitCode).isEqualTo(1);
        assertThat(err.toString()).contains("<stdin>:2:6: error: ';' expected");
    }

    @Test
    public void lexError2() throws FormatterException, IOException, UsageException {
        String input = "class Foo { void f() {\n g('foo'); } }";

        Path path = testFolder.resolve("A.java");
        Files.write(path, input.getBytes(StandardCharsets.UTF_8));

        StringWriter out = new StringWriter();
        StringWriter err = new StringWriter();

        Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
        String[] args = {path.toString()};
        int exitCode = main.format(args);

        assertThat(exitCode).isEqualTo(1);
        assertThat(err.toString()).contains("A.java:2:5: error: unclosed character literal");
    }

    @Test
    public void lexErrorStdin() throws FormatterException, IOException, UsageException {
        String input = "class Foo { void f() {\n g('foo'); } }";
        InputStream inStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
        StringWriter out = new StringWriter();
        StringWriter err = new StringWriter();
        Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), inStream);
        String[] args = {"-"};
        int exitCode = main.format(args);

        assertThat(exitCode).isEqualTo(1);
        assertThat(err.toString()).contains("<stdin>:2:5: error: unclosed character literal");
    }
}
