diff options
author | Liam Miller-Cushon <cushon@google.com> | 2017-02-11 03:06:31 +0000 |
---|---|---|
committer | Dmitry Lomov <dslomov@google.com> | 2017-02-13 11:34:16 +0000 |
commit | 19cdec0dbd900c534df35d8da47843521c96f64d (patch) | |
tree | 9b0693d78f0cf5ee928043b11773abfbe313bd09 /src/java_tools/buildjar/javatests/com | |
parent | 3a6f6297917b9d97d83d8384e346e2c725819c3d (diff) |
Create a 'vanilla' JavaBuilder
that is portable across JDK versions, and doesn't depend on the javac
distributed with Bazel.
There is no support for Error Prone, strict Java deps, header compilation,
Android desugaring, or any other features that depend on the Bazel javac.
--
PiperOrigin-RevId: 147224490
MOS_MIGRATED_REVID=147224490
Diffstat (limited to 'src/java_tools/buildjar/javatests/com')
3 files changed, 154 insertions, 1 deletions
diff --git a/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/BUILD b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/BUILD new file mode 100644 index 0000000000..65ca6bd977 --- /dev/null +++ b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/BUILD @@ -0,0 +1,18 @@ +filegroup( + name = "srcs", + srcs = glob(["**"]) + [ + "//src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/resourcejar:srcs", + ], + visibility = ["//src/java_tools/buildjar:__pkg__"], +) + +java_test( + name = "VanillaJavaBuilderTest", + srcs = ["VanillaJavaBuilderTest.java"], + deps = [ + "//src/java_tools/buildjar/java/com/google/devtools/build/buildjar:vanilla_java_builder", + "//third_party:guava", + "//third_party:junit4", + "//third_party:truth", + ], +) diff --git a/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java new file mode 100644 index 0000000000..71a702ed05 --- /dev/null +++ b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/VanillaJavaBuilderTest.java @@ -0,0 +1,135 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.buildjar; + +import static com.google.common.truth.Truth.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.ByteStreams; +import com.google.devtools.build.buildjar.VanillaJavaBuilder.VanillaJavaBuilderResult; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Enumeration; +import java.util.List; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.jar.JarOutputStream; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** {@link VanillaJavaBuilder}Test */ +@RunWith(JUnit4.class) +public class VanillaJavaBuilderTest { + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + VanillaJavaBuilderResult run(List<String> args) throws Exception { + try (VanillaJavaBuilder builder = new VanillaJavaBuilder()) { + return builder.run(args); + } + } + + ImmutableMap<String, byte[]> readJar(File file) throws IOException { + ImmutableMap.Builder<String, byte[]> result = ImmutableMap.builder(); + try (JarFile jf = new JarFile(file)) { + Enumeration<JarEntry> entries = jf.entries(); + while (entries.hasMoreElements()) { + JarEntry je = entries.nextElement(); + result.put(je.getName(), ByteStreams.toByteArray(jf.getInputStream(je))); + } + } + return result.build(); + } + + @Test + public void hello() throws Exception { + Path source = temporaryFolder.newFile("Test.java").toPath(); + Path output = temporaryFolder.newFile("out.jar").toPath(); + Files.write( + source, + ImmutableList.of( + "class A {", // + "}"), + UTF_8); + Path sourceJar = temporaryFolder.newFile("src.srcjar").toPath(); + try (OutputStream os = Files.newOutputStream(sourceJar); + JarOutputStream jos = new JarOutputStream(os)) { + jos.putNextEntry(new JarEntry("B.java")); + jos.write("class B {}".getBytes(UTF_8)); + } + Path resource = temporaryFolder.newFile("resource.properties").toPath(); + Files.write(resource, "hello".getBytes(UTF_8)); + + VanillaJavaBuilderResult result = + run( + ImmutableList.of( + "--sources", + source.toString(), + "--source_jars", + sourceJar.toString(), + "--output", + output.toString(), + "--classpath_resources", + resource.toString(), + "--bootclasspath", + Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--classdir", + temporaryFolder.newFolder().toString())); + + assertThat(result.output()).isEmpty(); + assertThat(result.ok()).isTrue(); + + ImmutableMap<String, byte[]> outputEntries = readJar(output.toFile()); + assertThat(outputEntries.keySet()) + .containsExactly( + "META-INF/", "META-INF/MANIFEST.MF", "A.class", "B.class", "resource.properties"); + } + + @Test + public void error() throws Exception { + Path source = temporaryFolder.newFile("Test.java").toPath(); + Path output = temporaryFolder.newFolder().toPath().resolve("out.jar"); + Files.write( + source, + ImmutableList.of( + "class A {", // + "}}"), + UTF_8); + + VanillaJavaBuilderResult result = + run( + ImmutableList.of( + "--sources", + source.toString(), + "--output", + output.toString(), + "--bootclasspath", + Paths.get(System.getProperty("java.home")).resolve("lib/rt.jar").toString(), + "--classdir", + temporaryFolder.newFolder().toString())); + + assertThat(result.output()).contains("class, interface, or enum expected"); + assertThat(result.ok()).isFalse(); + assertThat(Files.exists(output)).isFalse(); + } +} diff --git a/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/resourcejar/BUILD b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/resourcejar/BUILD index a587c7cb73..743a4933bd 100644 --- a/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/resourcejar/BUILD +++ b/src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar/resourcejar/BUILD @@ -1,7 +1,7 @@ filegroup( name = "srcs", srcs = glob(["**"]), - visibility = ["//src/java_tools/buildjar:__pkg__"], + visibility = ["//src/java_tools/buildjar/javatests/com/google/devtools/build/buildjar:__pkg__"], ) java_test( |