diff options
author | 2017-07-14 11:22:56 +0200 | |
---|---|---|
committer | 2017-07-14 12:54:49 +0200 | |
commit | 0afe1195f0a8c1c2e0d5d15e0d7d0d816dafec28 (patch) | |
tree | d642bee5a53d3e017d2f90b9bf6aef64c5624b0f /src/test/java/com/google/devtools/build/android | |
parent | e0455653fb426a25d7242ed7d8fe244b19997396 (diff) |
docs: Document --spawn_strategy=standalone for test
Resolves #2571
Closes #3316.
PiperOrigin-RevId: 161930376
Diffstat (limited to 'src/test/java/com/google/devtools/build/android')
4 files changed, 691 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverterTest.java b/src/test/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverterTest.java new file mode 100644 index 0000000000..9fac92c48c --- /dev/null +++ b/src/test/java/com/google/devtools/build/android/ideinfo/ArtifactLocationConverterTest.java @@ -0,0 +1,106 @@ +// Copyright 2016 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.android.ideinfo; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +import com.google.common.base.Joiner; +import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; +import com.google.devtools.common.options.OptionsParsingException; +import java.nio.file.Paths; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests {@link ArtifactLocationConverter}. + */ +@RunWith(JUnit4.class) +public class ArtifactLocationConverterTest { + + private ArtifactLocationConverter converter; + + @Before + public final void init() throws Exception { + converter = new ArtifactLocationConverter(); + } + + @Test + public void testConverterSourceArtifact() throws Exception { + ArtifactLocation parsed = converter.convert( + Joiner.on(',').join("", "test.java") + ); + assertThat(parsed) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRelativePath(Paths.get("test.java").toString()) + .setIsSource(true) + .build()); + } + + @Test + public void testConverterDerivedArtifact() throws Exception { + ArtifactLocation parsed = converter.convert( + Joiner.on(',').join("bin", "java/com/test.java") + ); + assertThat(parsed) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRootExecutionPathFragment(Paths.get("bin").toString()) + .setRelativePath(Paths.get("java/com/test.java").toString()) + .setIsSource(false) + .build()); + } + + @Test + public void testConverterExternal() throws Exception { + ArtifactLocation externalArtifact = + converter.convert(Joiner.on(',').join("", "test.java", "1")); + assertThat(externalArtifact) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRelativePath(Paths.get("test.java").toString()) + .setIsSource(true) + .setIsExternal(true) + .build()); + ArtifactLocation nonExternalArtifact = + converter.convert(Joiner.on(',').join("", "test.java", "0")); + assertThat(nonExternalArtifact) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRelativePath(Paths.get("test.java").toString()) + .setIsSource(true) + .setIsExternal(false) + .build()); + } + + @Test + public void testInvalidFormatFails() throws Exception { + assertFails("/root", ArtifactLocationConverter.INVALID_FORMAT); + assertFails("/root,exec,rel,extra", ArtifactLocationConverter.INVALID_FORMAT); + } + + private void assertFails(String input, String expectedError) { + try { + new ArtifactLocationConverter().convert(input); + fail(); + } catch (OptionsParsingException e) { + assertThat(e).hasMessage(expectedError); + } + } +} + diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/BUILD b/src/test/java/com/google/devtools/build/android/ideinfo/BUILD new file mode 100644 index 0000000000..924ec5f28b --- /dev/null +++ b/src/test/java/com/google/devtools/build/android/ideinfo/BUILD @@ -0,0 +1,52 @@ +filegroup( + name = "srcs", + srcs = glob(["**"]), + visibility = ["//src/test/java/com/google/devtools/build/android:__pkg__"], +) + +java_test( + name = "JarFilterTest", + size = "small", + srcs = ["JarFilterTest.java"], + deps = [ + "//src/main/java/com/google/devtools/common/options", + "//src/main/protobuf:package_manifest_java_proto", + "//src/tools/android/java/com/google/devtools/build/android/ideinfo:jar_filter_lib", + "//third_party:guava", + "//third_party:jsr305", + "//third_party:junit4", + "//third_party:truth", + "//third_party/protobuf:protobuf_java", + ], +) + +java_test( + name = "PackageParserTest", + size = "small", + srcs = ["PackageParserTest.java"], + deps = [ + "//src/main/java/com/google/devtools/common/options", + "//src/main/protobuf:package_manifest_java_proto", + "//src/tools/android/java/com/google/devtools/build/android/ideinfo:package_parser_lib", + "//third_party:guava", + "//third_party:jsr305", + "//third_party:junit4", + "//third_party:truth", + "//third_party/protobuf:protobuf_java", + ], +) + +java_test( + name = "ArtifactLocationConverterTest", + size = "small", + srcs = ["ArtifactLocationConverterTest.java"], + deps = [ + "//src/main/java/com/google/devtools/common/options", + "//src/main/protobuf:package_manifest_java_proto", + "//src/tools/android/java/com/google/devtools/build/android/ideinfo:package_parser_lib", + "//third_party:guava", + "//third_party:junit4", + "//third_party:truth", + "//third_party/protobuf:protobuf_java", + ], +) diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/JarFilterTest.java b/src/test/java/com/google/devtools/build/android/ideinfo/JarFilterTest.java new file mode 100644 index 0000000000..a17048d9e0 --- /dev/null +++ b/src/test/java/com/google/devtools/build/android/ideinfo/JarFilterTest.java @@ -0,0 +1,252 @@ +// Copyright 2015 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.android.ideinfo; + +import static com.google.common.truth.Truth.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.io.Files; +import com.google.devtools.build.android.ideinfo.JarFilter.JarFilterOptions; +import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; +import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.JavaSourcePackage; +import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.PackageManifest; +import java.io.File; +import java.io.FileOutputStream; +import java.util.Enumeration; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link JarFilter} */ +@RunWith(JUnit4.class) +public class JarFilterTest { + + @Rule public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testFilterMethod() throws Exception { + List<String> prefixes = + ImmutableList.of("com/google/foo/Foo", "com/google/bar/Bar", "com/google/baz/Baz"); + assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/foo/Foo.class")).isTrue(); + assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/foo/Foo$Inner.class")).isTrue(); + assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/bar/Bar.class")).isTrue(); + assertThat(JarFilter.shouldKeepClass(prefixes, "com/google/foo/Foo/NotFoo.class")).isFalse(); + assertThat(JarFilter.shouldKeepClass(prefixes, "wrong/com/google/foo/Foo.class")).isFalse(); + } + + @Test + public void legacyIntegrationTest() throws Exception { + PackageManifest packageManifest = + PackageManifest.newBuilder() + .addSources( + JavaSourcePackage.newBuilder() + .setArtifactLocation( + ArtifactLocation.newBuilder() + .setIsSource(true) + .setRelativePath("com/google/foo/Foo.java")) + .setPackageString("com.google.foo")) + .addSources( + JavaSourcePackage.newBuilder() + .setArtifactLocation( + ArtifactLocation.newBuilder() + .setIsSource(true) + .setRelativePath("com/google/bar/Bar.java")) + .setPackageString("com.google.bar")) + .addSources( + JavaSourcePackage.newBuilder() + .setArtifactLocation( + ArtifactLocation.newBuilder() + .setIsSource(true) + .setRelativePath("some/path/Test.java")) + .setPackageString("com.google.test")) + .build(); + assertThat(JarFilter.parsePackageManifest(packageManifest)) + .containsExactly("com/google/foo/Foo", "com/google/bar/Bar", "com/google/test/Test"); + File manifest = folder.newFile("foo.manifest"); + try (FileOutputStream outputStream = new FileOutputStream(manifest)) { + packageManifest.writeTo(outputStream); + } + + File filterJar = folder.newFile("foo.jar"); + try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterJar))) { + zo.putNextEntry(new ZipEntry("com/google/foo/Foo.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/Foo$Inner.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/bar/Bar.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/test/Test.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.class")); + zo.closeEntry(); + } + + File outputJar = folder.newFile("foo-filtered-gen.jar"); + + String[] args = + new String[] { + "--jars", + filterJar.getPath(), + "--output", + outputJar.getPath(), + "--manifest", + manifest.getPath() + }; + JarFilter.JarFilterOptions options = JarFilter.parseArgs(args); + JarFilter.main(options); + + List<String> filteredJarNames = Lists.newArrayList(); + try (ZipFile zipFile = new ZipFile(outputJar)) { + Enumeration<? extends ZipEntry> entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry zipEntry = entries.nextElement(); + filteredJarNames.add(zipEntry.getName()); + } + } + + assertThat(filteredJarNames) + .containsExactly( + "com/google/foo/Foo.class", + "com/google/foo/Foo$Inner.class", + "com/google/bar/Bar.class", + "com/google/test/Test.class"); + } + + @Test + public void fullIntegrationTest() throws Exception { + File fooJava = folder.newFile("Foo.java"); + Files.write("package com.google.foo; class Foo { class Inner {} }".getBytes(UTF_8), fooJava); + + File barJava = folder.newFile("Bar.java"); + Files.write("package com.google.foo.bar; class Bar {}".getBytes(UTF_8), barJava); + + File srcJar = folder.newFile("gen.srcjar"); + try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(srcJar))) { + zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen.java")); + zo.write("package gen; class Gen {}".getBytes(UTF_8)); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen2.java")); + zo.write("package gen; class Gen2 {}".getBytes(UTF_8)); + zo.closeEntry(); + } + + File src3Jar = folder.newFile("gen3.srcjar"); + try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(src3Jar))) { + zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen3.java")); + zo.write("package gen; class Gen3 {}".getBytes(UTF_8)); + zo.closeEntry(); + } + + File filterJar = folder.newFile("foo.jar"); + try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterJar))) { + zo.putNextEntry(new ZipEntry("com/google/foo/Foo.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/Foo$Inner.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("gen/Gen.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("gen/Gen2.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("gen/Gen3.class")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.class")); + zo.closeEntry(); + } + File filterSrcJar = folder.newFile("foo-src.jar"); + try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterSrcJar))) { + zo.putNextEntry(new ZipEntry("com/google/foo/Foo.java")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.java")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("gen/Gen.java")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("gen/Gen2.java")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("gen/Gen3.java")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.java")); + zo.closeEntry(); + zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar2.java")); + zo.closeEntry(); + } + + File filteredJar = folder.newFile("foo-filtered-gen.jar"); + File filteredSourceJar = folder.newFile("foo-filtered-gen-src.jar"); + + String[] args = + new String[] { + "--keep_java_files", + fooJava.getPath() + File.pathSeparator + barJava.getPath(), + "--keep_source_jars", + Joiner.on(File.pathSeparator).join(srcJar.getPath(), src3Jar.getPath()), + "--filter_jars", + filterJar.getPath(), + "--filter_source_jars", + filterSrcJar.getPath(), + "--filtered_jar", + filteredJar.getPath(), + "--filtered_source_jar", + filteredSourceJar.getPath() + }; + JarFilterOptions options = JarFilter.parseArgs(args); + JarFilter.main(options); + + List<String> filteredJarNames = Lists.newArrayList(); + try (ZipFile zipFile = new ZipFile(filteredJar)) { + Enumeration<? extends ZipEntry> entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry zipEntry = entries.nextElement(); + filteredJarNames.add(zipEntry.getName()); + } + } + + List<String> filteredSourceJarNames = Lists.newArrayList(); + try (ZipFile zipFile = new ZipFile(filteredSourceJar)) { + Enumeration<? extends ZipEntry> entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry zipEntry = entries.nextElement(); + filteredSourceJarNames.add(zipEntry.getName()); + } + } + + assertThat(filteredJarNames) + .containsExactly( + "com/google/foo/Foo.class", + "com/google/foo/Foo$Inner.class", + "com/google/foo/bar/Bar.class", + "gen/Gen.class", + "gen/Gen2.class", + "gen/Gen3.class"); + + assertThat(filteredSourceJarNames) + .containsExactly( + "com/google/foo/Foo.java", + "com/google/foo/bar/Bar.java", + "gen/Gen.java", + "gen/Gen2.java", + "gen/Gen3.java"); + } +} diff --git a/src/test/java/com/google/devtools/build/android/ideinfo/PackageParserTest.java b/src/test/java/com/google/devtools/build/android/ideinfo/PackageParserTest.java new file mode 100644 index 0000000000..e775ca36c7 --- /dev/null +++ b/src/test/java/com/google/devtools/build/android/ideinfo/PackageParserTest.java @@ -0,0 +1,281 @@ +// Copyright 2015 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.android.ideinfo; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.ArtifactLocation; +import com.google.protobuf.MessageLite; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; +import javax.annotation.Nonnull; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Unit tests for {@link PackageParser} + */ +@RunWith(JUnit4.class) +public class PackageParserTest { + + private static class MockPackageParserIoProvider extends PackageParserIoProvider { + private final Map<Path, InputStream> sources = Maps.newHashMap(); + private final List<ArtifactLocation> sourceLocations = Lists.newArrayList(); + private StringWriter writer = new StringWriter(); + + public MockPackageParserIoProvider addSource(ArtifactLocation source, String javaSrc) { + try { + Path path = Paths.get(source.getRootExecutionPathFragment(), source.getRelativePath()); + sources.put(path, new ByteArrayInputStream(javaSrc.getBytes("UTF-8"))); + sourceLocations.add(source); + + } catch (UnsupportedEncodingException | InvalidPathException e) { + fail(e.getMessage()); + } + return this; + } + + public void reset() { + sources.clear(); + sourceLocations.clear(); + writer = new StringWriter(); + } + + public List<ArtifactLocation> getSourceLocations() { + return Lists.newArrayList(sourceLocations); + } + + @Nonnull + @Override + public BufferedReader getReader(Path file) throws IOException { + InputStream input = sources.get(file); + return new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); + } + + @Override + public void writeProto(@Nonnull MessageLite message, @Nonnull Path file) throws IOException { + writer.write(message.toString()); + } + } + + private static final ArtifactLocation DUMMY_SOURCE_ARTIFACT = + ArtifactLocation.newBuilder() + .setRelativePath("java/com/google/Foo.java") + .setIsSource(true) + .build(); + + private static final ArtifactLocation DUMMY_DERIVED_ARTIFACT = + ArtifactLocation.newBuilder() + .setRootExecutionPathFragment("bin") + .setRelativePath("java/com/google/Bla.java") + .setIsSource(false) + .build(); + + private static final ArtifactLocation DUMMY_SCALA_SOURCE_ARTIFACT = + ArtifactLocation.newBuilder() + .setRelativePath("scala/com/google/Foo.scala") + .setIsSource(true) + .build(); + + private static final ArtifactLocation DUMMY_SCALA_DERIVED_ARTIFACT = + ArtifactLocation.newBuilder() + .setRootExecutionPathFragment("bin") + .setRelativePath("scala/com/google/Bla.scala") + .setIsSource(false) + .build(); + + private MockPackageParserIoProvider mockIoProvider; + private PackageParser parser; + + @Before + public void setUp() { + mockIoProvider = new MockPackageParserIoProvider(); + parser = new PackageParser(mockIoProvider); + } + + private Map<ArtifactLocation, String> parsePackageStrings() throws Exception { + List<ArtifactLocation> sources = mockIoProvider.getSourceLocations(); + return parser.parsePackageStrings(sources); + } + + @Test + public void testParseCommandLineArguments() throws Exception { + String[] args = new String[] { + "--output_manifest", + "/tmp/out.manifest", + "--sources", + Joiner.on(':').join( + ",java/com/google/Foo.java", + "bin/out,java/com/google/Bla.java") + }; + PackageParser.PackageParserOptions options = PackageParser.parseArgs(args); + assertThat(options.outputManifest.toString()) + .isEqualTo(Paths.get("/tmp/out.manifest").toString()); + assertThat(options.sources).hasSize(2); + assertThat(options.sources.get(0)) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRelativePath(Paths.get("java/com/google/Foo.java").toString()) + .setIsSource(true) + .build()); + assertThat(options.sources.get(1)) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRootExecutionPathFragment(Paths.get("bin/out").toString()) + .setRelativePath(Paths.get("java/com/google/Bla.java").toString()) + .setIsSource(false) + .build()); + } + + @Test + public void testReadNoSources() throws Exception { + Map<ArtifactLocation, String> map = parsePackageStrings(); + assertThat(map).isEmpty(); + } + + @Test + public void testSingleRead() throws Exception { + mockIoProvider + .addSource( + DUMMY_SOURCE_ARTIFACT, + "package com.google;\n public class Bla {}\""); + Map<ArtifactLocation, String> map = parsePackageStrings(); + assertThat(map).hasSize(1); + assertThat(map).containsEntry( + DUMMY_SOURCE_ARTIFACT, + "com.google"); + } + + @Test + public void testMultiRead() throws Exception { + mockIoProvider + .addSource(DUMMY_SOURCE_ARTIFACT, "package com.test;\n public class Foo {}\"") + .addSource(DUMMY_DERIVED_ARTIFACT, "package com.other;\n public class Bla {}\""); + Map<ArtifactLocation, String> map = parsePackageStrings(); + assertThat(map).hasSize(2); + assertThat(map).containsEntry(DUMMY_SOURCE_ARTIFACT, "com.test"); + assertThat(map).containsEntry(DUMMY_DERIVED_ARTIFACT, "com.other"); + } + + @Test + public void testReadSomeInvalid() throws Exception { + mockIoProvider + .addSource(DUMMY_SOURCE_ARTIFACT, "package %com.test;\n public class Foo {}\"") + .addSource(DUMMY_DERIVED_ARTIFACT, "package com.other;\n public class Bla {}\""); + Map<ArtifactLocation, String> map = parsePackageStrings(); + assertThat(map).hasSize(1); + assertThat(map).containsEntry(DUMMY_DERIVED_ARTIFACT, "com.other"); + } + + @Test + public void testReadAllInvalid() throws Exception { + mockIoProvider + .addSource(DUMMY_SOURCE_ARTIFACT, "#package com.test;\n public class Foo {}\"") + .addSource(DUMMY_DERIVED_ARTIFACT, "package %com.other\n public class Bla {}\""); + Map<ArtifactLocation, String> map = parsePackageStrings(); + assertThat(map).isEmpty(); + } + + @Test + public void testReadScala() throws Exception { + mockIoProvider + .addSource(DUMMY_SCALA_SOURCE_ARTIFACT, "package com.test\n class Foo {}\"") + .addSource(DUMMY_SCALA_DERIVED_ARTIFACT, "package com.other {}\n object Bla {}\""); + Map<ArtifactLocation, String> map = parsePackageStrings(); + assertThat(map).containsEntry(DUMMY_SCALA_SOURCE_ARTIFACT, "com.test"); + assertThat(map).containsEntry(DUMMY_SCALA_DERIVED_ARTIFACT, "com.other"); + } + + @Test + public void testWriteEmptyMap() throws Exception { + parser.writeManifest( + Maps.<ArtifactLocation, String> newHashMap(), + Paths.get("/java/com/google/test.manifest")); + assertThat(mockIoProvider.writer.toString()).isEmpty(); + } + + @Test + public void testWriteMap() throws Exception { + Map<ArtifactLocation, String> map = ImmutableMap.of( + DUMMY_SOURCE_ARTIFACT, + "com.google", + DUMMY_DERIVED_ARTIFACT, + "com.other" + ); + parser.writeManifest(map, Paths.get("/java/com/google/test.manifest")); + + String writtenString = mockIoProvider.writer.toString(); + assertThat(writtenString).contains(String.format( + "relative_path: \"%s\"", + DUMMY_SOURCE_ARTIFACT.getRelativePath())); + assertThat(writtenString).contains("package_string: \"com.google\""); + + assertThat(writtenString).contains(String.format( + "root_execution_path_fragment: \"%s\"", + DUMMY_DERIVED_ARTIFACT.getRootExecutionPathFragment())); + assertThat(writtenString).contains(String.format( + "relative_path: \"%s\"", + DUMMY_DERIVED_ARTIFACT.getRelativePath())); + assertThat(writtenString).contains("package_string: \"com.other\""); + } + + @Test + public void testHandlesOldFormat() throws Exception { + String[] args = new String[] { + "--output_manifest", + "/tmp/out.manifest", + "--sources", + Joiner.on(':').join( + ",java/com/google/Foo.java,/usr/local/google/code", + "bin,java/com/google/Bla.java,/usr/local/_tmp/code/bin" + )}; + PackageParser.PackageParserOptions options = PackageParser.parseArgs(args); + assertThat(options.outputManifest.toString()) + .isEqualTo(Paths.get("/tmp/out.manifest").toString()); + assertThat(options.sources).hasSize(2); + assertThat(options.sources.get(0)) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRelativePath(Paths.get("java/com/google/Foo.java").toString()) + .setIsSource(true) + .build()); + assertThat(options.sources.get(1)) + .isEqualTo( + ArtifactLocation.newBuilder() + .setRootExecutionPathFragment(Paths.get("bin").toString()) + .setRelativePath(Paths.get("java/com/google/Bla.java").toString()) + .setIsSource(false) + .build()); + } + +} |