| 1 |
#!/bin/sh |
| 2 |
set -e |
| 3 |
|
| 4 |
WORKDIR=$(mktemp -d) |
| 5 |
trap 'rm -rf "$WORKDIR"' EXIT |
| 6 |
|
| 7 |
# Write test file |
| 8 |
cat <<EOF > "$WORKDIR/Main.java" |
| 9 |
import static com.google.common.truth.Truth.assertAbout; |
| 10 |
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; |
| 11 |
|
| 12 |
import com.google.testing.compile.JavaFileObjects; |
| 13 |
import javax.tools.JavaFileObject; |
| 14 |
import org.junit.Test; |
| 15 |
import org.junit.runner.JUnitCore; |
| 16 |
import org.junit.runner.Result; |
| 17 |
import org.junit.runner.notification.Failure; |
| 18 |
|
| 19 |
public class Main { |
| 20 |
@Test |
| 21 |
public void testCompilation() { |
| 22 |
JavaFileObject HelloWorld = JavaFileObjects.forSourceLines( |
| 23 |
"HelloWorld", |
| 24 |
"public class HelloWorld {", |
| 25 |
" public static void main(String[] args) {", |
| 26 |
" System.out.println(\"Hello World!\");", |
| 27 |
" }", |
| 28 |
"}" |
| 29 |
); |
| 30 |
assertAbout(javaSource()).that(HelloWorld).compilesWithoutError(); |
| 31 |
} |
| 32 |
|
| 33 |
public static void main(String[] args) { |
| 34 |
Result result = JUnitCore.runClasses(Main.class); |
| 35 |
if (result.wasSuccessful()) { |
| 36 |
System.out.println("Success!"); |
| 37 |
System.exit(0); |
| 38 |
} else { |
| 39 |
for (Failure failure : result.getFailures()) { |
| 40 |
System.err.println(failure.toString()); |
| 41 |
if (failure.getException() != null) { |
| 42 |
failure.getException().printStackTrace(); |
| 43 |
} |
| 44 |
} |
| 45 |
System.exit(1); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
EOF |
| 50 |
|
| 51 |
# Define classpath |
| 52 |
COMPILE_TESTING_JAR="${COMPILE_TESTING_JAR:-/usr/share/java/google-compile-testing.jar}" |
| 53 |
CP="$COMPILE_TESTING_JAR:/usr/share/java/junit4.jar:/usr/share/java/truth.jar:/usr/share/java/guava.jar:/usr/share/java/jspecify.jar:/usr/share/java/auto-common.jar:/usr/share/java/auto-value-annotations.jar" |
| 54 |
|
| 55 |
# Compile the test |
| 56 |
javac -cp "$CP" -d "$WORKDIR" "$WORKDIR/Main.java" |
| 57 |
|
| 58 |
# Run the test |
| 59 |
# We might need exports for JVM if compile-testing accesses internals. |
| 60 |
# Let's try without first, and add them if it fails with InaccessibleObjectException. |
| 61 |
java -cp "$CP:$WORKDIR" Main |