Search in sources :

Example 16 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class DiagnosticKindTest method testWarning.

@Test
public void testWarning() throws Exception {
    compilerBuilder.report(ScannerSupplier.fromBugCheckerClasses(WarningChecker.class));
    ErrorProneTestCompiler compiler = compilerBuilder.build();
    Result result = compiler.compile(Arrays.asList(compiler.fileManager().forSourceLines("Test.java", TEST_CODE)));
    assertThat(diagnosticHelper.getDiagnostics()).hasSize(1);
    assertThat(diagnosticHelper.getDiagnostics().get(0).getKind()).isEqualTo(Diagnostic.Kind.WARNING);
    assertThat(diagnosticHelper.getDiagnostics().get(0).toString()).contains("warning:");
    assertThat(result).isEqualTo(Result.OK);
}
Also used : Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 17 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class DiagnosticKindTest method testError.

@Test
public void testError() throws Exception {
    compilerBuilder.report(ScannerSupplier.fromBugCheckerClasses(ErrorChecker.class));
    ErrorProneTestCompiler compiler = compilerBuilder.build();
    Result result = compiler.compile(Arrays.asList(compiler.fileManager().forSourceLines("Test.java", TEST_CODE)));
    assertThat(diagnosticHelper.getDiagnostics()).hasSize(1);
    assertThat(diagnosticHelper.getDiagnostics().get(0).getKind()).isEqualTo(Diagnostic.Kind.ERROR);
    assertThat(diagnosticHelper.getDiagnostics().get(0).toString()).contains("error:");
    assertThat(result).isEqualTo(Result.ERROR);
}
Also used : Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 18 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class ErrorProneCompilerIntegrationTest method fileWithMultipleTopLevelClassesExtends.

@Test
public void fileWithMultipleTopLevelClassesExtends() throws Exception {
    Result exitCode = compiler.compile(compiler.fileManager().forResources(getClass(), "testdata/MultipleTopLevelClassesWithNoErrors.java", "testdata/ExtendedMultipleTopLevelClassesWithNoErrors.java"));
    assertThat(outputStream.toString(), exitCode, is(Result.OK));
}
Also used : Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 19 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class ErrorProneCompilerIntegrationTest method reportReadyForAnalysisOnce.

/**
   * Test that if javac does dataflow on a class twice error-prone only analyses it once.
   */
@Test
public void reportReadyForAnalysisOnce() throws Exception {
    Result exitCode = compiler.compile(compiler.fileManager().forResources(getClass(), "testdata/FlowConstants.java", "testdata/FlowSub.java", // subclass is desugared, once normally).
    "testdata/FlowSuper.java"));
    assertThat(outputStream.toString(), exitCode, is(Result.OK));
}
Also used : Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Example 20 with Result

use of com.sun.tools.javac.main.Main.Result in project error-prone by google.

the class ErrorProneCompilerIntegrationTest method plugin.

@Test
public void plugin() throws Exception {
    Path base = tmpFolder.newFolder().toPath();
    Path source = base.resolve("test/Test.java");
    Files.createDirectories(source.getParent());
    Files.write(source, Arrays.asList(//
    "package test;", "public class Test {", "  int f() { return 42; }", "}"), UTF_8);
    Path jar = base.resolve("libproc.jar");
    try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(jar))) {
        jos.putNextEntry(new JarEntry("META-INF/services/" + BugChecker.class.getName()));
        jos.write((CPSChecker.class.getName() + "\n").getBytes(UTF_8));
        String classFile = CPSChecker.class.getName().replace('.', '/') + ".class";
        jos.putNextEntry(new JarEntry(classFile));
        ByteStreams.copy(getClass().getClassLoader().getResourceAsStream(classFile), jos);
    }
    // no plugins
    {
        List<String> args = ImmutableList.of(source.toAbsolutePath().toString(), "-processorpath", File.pathSeparator);
        StringWriter out = new StringWriter();
        Result result = ErrorProneCompiler.compile(args.toArray(new String[0]), new PrintWriter(out, true));
        assertThat(result).isEqualTo(Result.OK);
    }
    // with plugins
    {
        List<String> args = ImmutableList.of(source.toAbsolutePath().toString(), "-processorpath", jar.toAbsolutePath().toString());
        StringWriter out = new StringWriter();
        Result result = ErrorProneCompiler.compile(args.toArray(new String[0]), new PrintWriter(out, true));
        assertThat(out.toString()).contains("Using 'return' is considered harmful");
        assertThat(result).isEqualTo(Result.ERROR);
    }
}
Also used : Path(java.nio.file.Path) StringWriter(java.io.StringWriter) BugChecker(com.google.errorprone.bugpatterns.BugChecker) JarOutputStream(java.util.jar.JarOutputStream) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Matchers.containsString(org.hamcrest.Matchers.containsString) JarEntry(java.util.jar.JarEntry) PrintWriter(java.io.PrintWriter) Result(com.sun.tools.javac.main.Main.Result) Test(org.junit.Test)

Aggregations

Result (com.sun.tools.javac.main.Main.Result)36 Test (org.junit.Test)34 JavaFileObject (javax.tools.JavaFileObject)20 Diagnostic (javax.tools.Diagnostic)10 Matchers.containsString (org.hamcrest.Matchers.containsString)7 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)2 Path (java.nio.file.Path)2 ImmutableList (com.google.common.collect.ImmutableList)1 LookForCheckNameInDiagnostic (com.google.errorprone.DiagnosticTestHelper.LookForCheckNameInDiagnostic)1 InvalidCommandLineOptionException (com.google.errorprone.InvalidCommandLineOptionException)1 BadShiftAmount (com.google.errorprone.bugpatterns.BadShiftAmount)1 BugChecker (com.google.errorprone.bugpatterns.BugChecker)1 NonAtomicVolatileUpdate (com.google.errorprone.bugpatterns.NonAtomicVolatileUpdate)1 Main (com.sun.tools.javac.main.Main)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 List (java.util.List)1 JarEntry (java.util.jar.JarEntry)1