Skip to content

Commit

Permalink
Fix tests on JDK 9 and 10
Browse files Browse the repository at this point in the history
  • Loading branch information
cushon authored and Copybara-Service committed Sep 7, 2018
1 parent f03acca commit 4566a42
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ load(":build_defs.bzl", "default_javacopts")
# Exports the canonical javac bootclasspath artifact locations
genrule(
name = "javac-bootclasspath-locations",
srcs = ["@bazel_tools//tools/jdk:bootclasspath"],
srcs = ["//tools/jdk:platformclasspath8"],
outs = ["JavacBootclasspathLocations.java"],
cmd = "$(location javac-bootclasspath-locations.sh) $@ $(GENDIR) $(SRCS)",
tools = ["javac-bootclasspath-locations.sh"],
Expand All @@ -25,7 +25,7 @@ java_library(
"JavacBootclasspath.java",
":javac-bootclasspath-locations",
],
data = ["@bazel_tools//tools/jdk:bootclasspath"],
data = ["//tools/jdk:platformclasspath8"],
visibility = ["//visibility:public"],
deps = ["//third_party:guava"],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ BOOTCLASSPATH=$(echo "$*" | \
sed "s|^${GENDIR}/||" | \
sed "s|^.*local_jdk|local_jdk|" | \
sed "s|^third_party|${PWD##*/}/third_party|" | \
sed "s|^tools/jdk|${PWD##*/}/tools/jdk|" | \
tr "\n" ":" | \
sed "s/:$//"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ private <T> void onExecuteAsyncFailure(
try {
ListenableScheduledFuture<?> sf =
retryService.schedule(
() -> executeAsync(call, outerF, backoff), waitMillis, TimeUnit.MILLISECONDS);
() -> {
executeAsync(call, outerF, backoff);
},
waitMillis,
TimeUnit.MILLISECONDS);
Futures.addCallback(
sf,
new FutureCallback<Object>() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/google/devtools/build/skydoc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ filegroup(

java_binary(
name = "skydoc",
jvm_flags = [
# quiet warnings from com.google.protobuf.UnsafeUtil,
# see: https://github.com/google/protobuf/issues/3781
# and: https://github.com/bazelbuild/bazel/issues/5599
"--add-opens=java.base/java.nio=ALL-UNNAMED",
"--add-opens=java.base/java.lang=ALL-UNNAMED",
# ... but only on JDK >= 9
"-XX:+IgnoreUnrecognizedVMOptions",
],
main_class = "com.google.devtools.build.skydoc.SkydocMain",
visibility = ["//visibility:public"],
runtime_deps = [
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/google/devtools/skylark/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ package(default_visibility = ["//src:__subpackages__"])
java_binary(
name = "Skylark",
srcs = ["Skylark.java"],
jvm_flags = [
# quiet warnings from com.google.protobuf.UnsafeUtil,
# see: https://github.com/google/protobuf/issues/3781
# and: https://github.com/bazelbuild/bazel/issues/5599
"--add-opens=java.base/java.nio=ALL-UNNAMED",
"--add-opens=java.base/java.lang=ALL-UNNAMED",
# ... but only on JDK >= 9
"-XX:+IgnoreUnrecognizedVMOptions",
],
main_class = "com.google.devtools.skylark.Skylark",
deps = [
"//src/main/java/com/google/devtools/build/lib:events",
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/com/google/devtools/build/android/desugar/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,12 @@ java_test(
srcs = [
"DesugarCoreLibraryFunctionalTest.java",
],
# TODO (b/72181101): -Xbootclasspath/p is removed in JDK 9.
jvm_flags = ["-Xbootclasspath/p:$(location :testdata_desugared_core_library):$(location //third_party/java/jacoco:blaze-agent)"],
jvm_flags = [
# TODO (b/72181101): -Xbootclasspath/p is removed in JDK 9.
"-XX:+IgnoreUnrecognizedVMOptions",
"-Xbootclasspath/a:$(location :testdata_desugared_core_library):$(location //third_party/java/jacoco:blaze-agent)",
"--patch-module=java.base=$(location :testdata_desugared_core_library)",
],
tags = ["no_windows"],
deps = [
":testdata_desugared_core_library", # Make tests run against desugared library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ java_library(
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner:Runner_deploy.jar",
"//third_party/ijar",
"//third_party/java/jdk/langtools:test-srcs",
"@local_jdk//:bootclasspath",
"@local_jdk//:jdk",
"@bazel_tools//tools/jdk:current_java_runtime",
] + select({
# TODO(bazel-team): Once https://github.com/bazelbuild/bazel/issues/2241
# is resolved, use cc implementation of singlejar on windows
Expand Down
3 changes: 1 addition & 2 deletions src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ filegroup(
"//third_party/java/jdk/langtools:test-srcs",
"//tools:srcs",
"@bazel_skylib//:test_deps",
"@local_jdk//:bootclasspath",
"@local_jdk//:jdk",
"@bazel_tools//tools/jdk:current_java_runtime",
] + select({
# TODO(bazel-team): Once https://github.com/bazelbuild/bazel/issues/2241
# is resolved, use cc implementation of singlejar on windows
Expand Down
111 changes: 54 additions & 57 deletions third_party/ijar/test/IjarTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.ByteStreams;
import com.google.devtools.build.java.bazel.BazelJavaCompiler;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -52,16 +50,15 @@
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;

/**
* JUnit tests for ijar tool.
*/
/** JUnit tests for ijar tool. */
@RunWith(JUnit4.class)
public class IjarTests {

Expand All @@ -80,27 +77,26 @@ private static File getTmpDir() {
DiagnosticCollector<JavaFileObject> diagnostics;

private JavaCompiler.CompilationTask makeCompilationTask(String... files) throws IOException {
JavaCompiler compiler = BazelJavaCompiler.newInstance();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
fileManager.setLocation(StandardLocation.CLASS_PATH,
Arrays.asList(new File("third_party/ijar/test/interface_ijar_testlib.jar")));
fileManager.setLocation(StandardLocation.CLASS_OUTPUT,
Arrays.asList(getTmpDir()));
fileManager.setLocation(
StandardLocation.CLASS_PATH,
Arrays.asList(new File("third_party/ijar/test/interface_ijar_testlib.jar")));
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(getTmpDir()));
diagnostics = new DiagnosticCollector<JavaFileObject>();
return compiler.getTask(null,
fileManager,
diagnostics,
Arrays.asList("-Xlint:deprecation"), // used for deprecation tests
null,
fileManager.getJavaFileObjects(files));
return compiler.getTask(
null,
fileManager,
diagnostics,
Arrays.asList("-Xlint:deprecation"), // used for deprecation tests
null,
fileManager.getJavaFileObjects(files));
}

/**
* Test that the ijar tool preserves private nested classes as they
* may be exposed through public API. This test relies on an
* interface jar provided through the build rule
* :interface_ijar_testlib and the Java source file
* PrivateNestedClass.java.
* Test that the ijar tool preserves private nested classes as they may be exposed through public
* API. This test relies on an interface jar provided through the build rule
* :interface_ijar_testlib and the Java source file PrivateNestedClass.java.
*/
@Test
public void testPrivateNestedClass() throws IOException {
Expand All @@ -109,21 +105,16 @@ public void testPrivateNestedClass() throws IOException {
}
}

/**
* Test that the ijar tool preserves annotations, especially @Target
* meta-annotation.
*/
/** Test that the ijar tool preserves annotations, especially @Target meta-annotation. */
@Test
public void testRestrictedAnnotations() throws IOException {
assertFalse(makeCompilationTask("third_party/ijar/test/UseRestrictedAnnotation.java").call());
}

/**
* Test that the ijar tool preserves private nested classes as they
* may be exposed through public API. This test relies on an
* interface jar provided through the build rule
* :interface_ijar_testlib and the Java source file
* PrivateNestedClass.java.
* Test that the ijar tool preserves private nested classes as they may be exposed through public
* API. This test relies on an interface jar provided through the build rule
* :interface_ijar_testlib and the Java source file PrivateNestedClass.java.
*/
@Test
public void testDeprecatedParts() throws IOException {
Expand All @@ -132,36 +123,42 @@ public void testDeprecatedParts() throws IOException {
}
int deprecatedWarningCount = 0;
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
if ((diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING) &&
if ((diagnostic.getKind() == Diagnostic.Kind.MANDATORY_WARNING)
&&
// Java 6:
(diagnostic.getMessage(Locale.ENGLISH).startsWith("[deprecation]") ||
// Java 7:
diagnostic.getMessage(Locale.ENGLISH).contains("has been deprecated"))) {
(diagnostic.getMessage(Locale.ENGLISH).startsWith("[deprecation]")
||
// Java 7:
diagnostic.getMessage(Locale.ENGLISH).contains("has been deprecated"))) {
deprecatedWarningCount++;
}
}
assertEquals(16, deprecatedWarningCount);
assertThat(deprecatedWarningCount).isAtLeast(16);
}

/**
* Test that the ijar tool preserves EnclosingMethod attributes and doesn't
* prevent annotation processors from accessing all the elements in a package.
* Test that the ijar tool preserves EnclosingMethod attributes and doesn't prevent annotation
* processors from accessing all the elements in a package.
*/
@Test
public void testEnclosingMethod() throws IOException {
JavaCompiler.CompilationTask task = makeCompilationTask("third_party/ijar/test/package-info.java");
task.setProcessors(Arrays.asList(new AbstractProcessor() {
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton("*");
}
JavaCompiler.CompilationTask task =
makeCompilationTask("third_party/ijar/test/package-info.java");
task.setProcessors(
Arrays.asList(
new AbstractProcessor() {
@Override
public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton("*");
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
roundEnv.getElementsAnnotatedWith(java.lang.Override.class);
return true;
}
}));
@Override
public boolean process(
Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
roundEnv.getElementsAnnotatedWith(java.lang.Override.class);
return true;
}
}));
if (!task.call()) {
fail(getFailedCompilationMessage());
}
Expand Down Expand Up @@ -196,12 +193,13 @@ private String getFailedCompilationMessage() {
StringBuilder builder = new StringBuilder();
builder.append("Build failed unexpectedly");
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
builder.append(String.format(
"\t%s line %d column %d: %s",
diagnostic.getKind().toString(),
diagnostic.getLineNumber(),
diagnostic.getColumnNumber(),
diagnostic.getMessage(Locale.ENGLISH)));
builder.append(
String.format(
"\t%s line %d column %d: %s",
diagnostic.getKind().toString(),
diagnostic.getLineNumber(),
diagnostic.getColumnNumber(),
diagnostic.getMessage(Locale.ENGLISH)));
}
return builder.toString();
}
Expand Down Expand Up @@ -406,8 +404,7 @@ private static void assertNonManifestFilesBitIdentical(JarFile original, JarFile
throws IOException {
// Make sure that all other files came across bitwise equal
for (String classEntry :
original
.stream()
original.stream()
.map(JarEntry::getName)
.filter(name -> !name.equals("META-INF/MANIFEST.MF"))
.collect(toImmutableList())) {
Expand Down
4 changes: 2 additions & 2 deletions third_party/ijar/test/ijar_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ function test_type_annotation() {
$JAVAP -classpath $TYPEANN2_IJAR -v Util >& $TEST_log || fail "javap failed"
expect_log "RuntimeVisibleTypeAnnotations" "RuntimeVisibleTypeAnnotations not preserved!"
cp $TYPEANN2_JAVA $TEST_TMPDIR/TypeAnnotationTest2.java
$JAVAC -J-Xbootclasspath/p:$LANGTOOLS8 $TEST_TMPDIR/TypeAnnotationTest2.java -cp $TYPEANN2_IJAR ||
$JAVAC $TEST_TMPDIR/TypeAnnotationTest2.java -cp $TYPEANN2_IJAR ||
fail "javac failed"
}

Expand All @@ -367,7 +367,7 @@ function test_object_class() {
# Check that Object.class can be processed
mkdir -p $TEST_TMPDIR/java/lang
cp $OBJECT_JAVA $TEST_TMPDIR/java/lang/.
$JAVAC $TEST_TMPDIR/java/lang/Object.java || fail "javac failed"
$JAVAC -source 8 -target 8 $TEST_TMPDIR/java/lang/Object.java || fail "javac failed"
$JAR cf $OBJECT_JAR -C $TEST_TMPDIR java/lang/Object.class || fail "jar failed"

$IJAR $OBJECT_JAR $OBJECT_IJAR || fail "ijar failed"
Expand Down
Loading

0 comments on commit 4566a42

Please sign in to comment.