<commit_before>package org.smoothbuild.acceptance.builtin.java;

import static okio.Okio.buffer;
import static okio.Okio.source;
import static org.smoothbuild.util.Okios.readAndClose;
import static org.testory.Testory.thenEqual;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.junit.jupiter.api.Test;
import org.smoothbuild.acceptance.AcceptanceTestCase;

public class JavacTest extends AcceptanceTestCase {
  @Test
  public void error_is_logged_when_compilation_error_occurs() throws Exception {
    givenScript(
        "  result = [ file(toBlob('public private class MyClass {}'), 'MyClass.java') ]  ",
        "  | javac;                                                                      ");
    whenSmoothBuild("result");
    thenFinishedWithError();
    thenOutputContains("modifier private not allowed here");
  }

  @Test
  public void zero_files_can_be_compiled() throws Exception {
    givenScript(
        "  result = [] | javac;  ");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    thenOutputContains("Param 'srcs' is empty list.");
  }

  @Test
  public void one_file_can_be_compiled() throws Exception {
    String classSource = "public class MyClass { "
        + "public static String myMethod() {return \\\"test-string\\\";}}";
    givenScript(
        "  result = [file(toBlob('" + classSource+ "'), 'MyClass.java')] | javac;  ");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    thenEqual("test-string", invoke(new File(artifact("result"), "MyClass.class"), "myMethod"));
  }

  @Test
  public void one_file_with_library_dependency_can_be_compiled() throws Exception {
    StringBuilder classSource = new StringBuilder();
    classSource.append("import library.LibraryClass;");
    classSource.append("public class MyClass {");
    classSource.append("  public static String myMethod() {");
    classSource.append("    return Integer.toString(LibraryClass.add(2, 3));");
    classSource.append("  }");
    classSource.append("}");

    StringBuilder librarySource = new StringBuilder();
    librarySource.append("package library;");
    librarySource.append("public class LibraryClass {");
    librarySource.append("  public static int add(int a, int b) {");
    librarySource.append("    return a + b;");
    librarySource.append("  }");
    librarySource.append("}");

    givenFile("src/MyClass.java", classSource.toString());
    givenFile("srclib/library/LibraryClass.java", librarySource.toString());
    givenScript(
        "  libraryJar = files('//srclib') | javac | jar;           ",
        "  result = files('//src') | javac(libs = [ libraryJar ])  ",
        "  | concatenate(array2 = javac(files('//srclib')));       ");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();

    File libraryClassFile = new File(artifact("result"), "library/LibraryClass.class");
    File classFile = new File(artifact("result"), "MyClass.class");
    MyClassLoader classLoader = new MyClassLoader();
    loadClass(classLoader, byteCode(libraryClassFile));
    thenEqual("5", invoke(classLoader, classFile, "myMethod"));
  }

  @Test
  public void duplicate_java_files_cause_error() throws Exception {
    givenScript(
        "  classFile = file(toBlob('public class MyClass {}'), 'MyClass.java');  ",
        "  result = [ classFile, classFile ] | javac;                            ");
    whenSmoothBuild("result");
    thenFinishedWithError();
    thenOutputContains("duplicate class: MyClass");
  }

  @Test
  public void illegal_source_parameter_causes_error() throws Exception {<commit_msg>refactored JavacTest to use Truth framework<commit_after>package org.smoothbuild.acceptance.builtin.java;

import static com.google.common.truth.Truth.assertThat;
import static okio.Okio.buffer;
import static okio.Okio.source;
import static org.smoothbuild.util.Okios.readAndClose;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.junit.jupiter.api.Test;
import org.smoothbuild.acceptance.AcceptanceTestCase;

public class JavacTest extends AcceptanceTestCase {
  @Test
  public void error_is_logged_when_compilation_error_occurs() throws Exception {
    givenScript(
        "  result = [ file(toBlob('public private class MyClass {}'), 'MyClass.java') ]  ",
        "  | javac;                                                                      ");
    whenSmoothBuild("result");
    thenFinishedWithError();
    thenOutputContains("modifier private not allowed here");
  }

  @Test
  public void zero_files_can_be_compiled() throws Exception {
    givenScript(
        "  result = [] | javac;  ");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    thenOutputContains("Param 'srcs' is empty list.");
  }

  @Test
  public void one_file_can_be_compiled() throws Exception {
    String classSource = "public class MyClass { "
        + "public static String myMethod() {return \\\"test-string\\\";}}";
    givenScript(
        "  result = [file(toBlob('" + classSource+ "'), 'MyClass.java')] | javac;  ");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    assertThat(invoke(new File(artifact("result"), "MyClass.class"), "myMethod"))
        .isEqualTo("test-string");
  }

  @Test
  public void one_file_with_library_dependency_can_be_compiled() throws Exception {
    StringBuilder classSource = new StringBuilder();
    classSource.append("import library.LibraryClass;");
    classSource.append("public class MyClass {");
    classSource.append("  public static String myMethod() {");
    classSource.append("    return Integer.toString(LibraryClass.add(2, 3));");
    classSource.append("  }");
    classSource.append("}");

    StringBuilder librarySource = new StringBuilder();
    librarySource.append("package library;");
    librarySource.append("public class LibraryClass {");
    librarySource.append("  public static int add(int a, int b) {");
    librarySource.append("    return a + b;");
    librarySource.append("  }");
    librarySource.append("}");

    givenFile("src/MyClass.java", classSource.toString());
    givenFile("srclib/library/LibraryClass.java", librarySource.toString());
    givenScript(
        "  libraryJar = files('//srclib') | javac | jar;           ",
        "  result = files('//src') | javac(libs = [ libraryJar ])  ",
        "  | concatenate(array2 = javac(files('//srclib')));       ");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();

    File libraryClassFile = new File(artifact("result"), "library/LibraryClass.class");
    File classFile = new File(artifact("result"), "MyClass.class");
    MyClassLoader classLoader = new MyClassLoader();
    loadClass(classLoader, byteCode(libraryClassFile));
    assertThat(invoke(classLoader, classFile, "myMethod"))
        .isEqualTo("5");
  }

  @Test
  public void duplicate_java_files_cause_error() throws Exception {
    givenScript(
        "  classFile = file(toBlob('public class MyClass {}'), 'MyClass.java');  ",
        "  result = [ classFile, classFile ] | javac;                            ");
    whenSmoothBuild("result");
    thenFinishedWithError();
    thenOutputContains("duplicate class: MyClass");
  }

  @Test
  public void illegal_source_parameter_causes_error() throws Exception {
{'fraction_non_alphanumeric': 0.13336969862266468, 'fraction_numerical': 0.0010909586799399973, 'mean_word_length': 3.618387909319899, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1919425', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2075, 'n_words': 423}