Skip to content

Commit

Permalink
Examples: Add error-prone example
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanpadhye committed Apr 17, 2018
1 parent ff7a732 commit 08f040d
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@
<version>1.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_check_api</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_test_helpers</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- Forced override -->
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.1-jre</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2018, The Regents of the University of California
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package edu.berkeley.cs.jqf.examples.errorprone;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;

import com.google.errorprone.ErrorProneCompiler;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.EqualsHashCode;
import com.pholser.junit.quickcheck.From;
import com.sun.tools.javac.main.Main;
import edu.berkeley.cs.jqf.examples.common.AsciiStringGenerator;
import edu.berkeley.cs.jqf.fuzz.Fuzz;
import edu.berkeley.cs.jqf.fuzz.JQF;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.sun.tools.javac.main.Main.Result.*;

/**
* @author Rohan Padhye
*/
@RunWith(JQF.class)
public class CompilerTest {

private Class<? extends BugChecker> bugCheckerClass = EqualsHashCode.class;
private ErrorProneCompiler compiler;
private final String[] args = {
"-encoding", "UTF-8",
"-XDdev",
"-parameters",
"-XDcompilePolicy=simple",
"-proc:none"
};


private File writeToFile(String data) throws IOException {
Path tmpDir = Files.createTempDirectory("errorprone-fuzz");
Path javaFile = tmpDir.resolve("Test.java");

try (BufferedWriter out = Files.newBufferedWriter(javaFile)) {
out.write(data);
}

return javaFile.toFile();
}

@Fuzz
public void testWithString(@From(AsciiStringGenerator.class) String classBody) {
// Construct test class with provided body
String code = String.format("class Test { %s }", classBody);
File javaFile;
try {
javaFile = writeToFile(code);
} catch (IOException e) {
throw new RuntimeException(e);
}

// Compile using javac + error-prone
Main.Result result = ErrorProneCompiler.compile(new String[]{ javaFile.getAbsolutePath()},
new PrintWriter(new ByteArrayOutputStream( )));

// Ensure that there was no issue with test command or environment
Assert.assertFalse(result == CMDERR);
Assert.assertFalse(result == SYSERR);

// Ignore compilation errors (only semantically valid inputs will pass)
Assume.assumeFalse(result == ERROR);

// This is the main test criteria -- abnormal exits are bugs
Assert.assertFalse( result == ABNORMAL);

}

@Test
public void test() {
testWithString("{ System.out.println(0); }");
}
}

0 comments on commit 08f040d

Please sign in to comment.