diff options
Diffstat (limited to 'src/test/java/com/google/devtools')
-rw-r--r-- | src/test/java/com/google/devtools/build/android/ziputils/SplitZipFiltersTest.java | 63 | ||||
-rw-r--r-- | src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java | 107 |
2 files changed, 170 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/android/ziputils/SplitZipFiltersTest.java b/src/test/java/com/google/devtools/build/android/ziputils/SplitZipFiltersTest.java new file mode 100644 index 0000000000..7fab1a461c --- /dev/null +++ b/src/test/java/com/google/devtools/build/android/ziputils/SplitZipFiltersTest.java @@ -0,0 +1,63 @@ +// 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.ziputils; + +import static com.google.common.truth.Truth.assertThat; +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.google.common.base.Predicate; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * Unit tests for {@link SplitZipFilters}. + */ +@RunWith(JUnit4.class) +public class SplitZipFiltersTest { + @Rule public final TemporaryFolder tmp = new TemporaryFolder(); + + @Test + public void testEntriesIn() throws Exception { + File filterZip = createZip("pkg1/test1.class", "pkg2/test2.class"); + Predicate<String> filter = SplitZipFilters.entriesIn(filterZip.getAbsolutePath()); + assertThat(filter.apply("pkg1/test1.class")).isTrue(); + assertThat(filter.apply("pkg2/test1.class")).isFalse(); + assertThat(filter.apply("pkg2/test2.class")).isTrue(); + assertThat(filter.apply("pkg1/test1$inner.class")).isFalse(); + } + + private File createZip(String... entries) throws FileNotFoundException, IOException { + File zip = tmp.newFile(); + int count = 0; + try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zip))) { + for (String entry : entries) { + zout.putNextEntry(new ZipEntry(entry)); + zout.write(("contents" + count++).getBytes(UTF_8)); // unique content + zout.closeEntry(); + } + } + return zip; + } +} diff --git a/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java b/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java index bba889271e..5d6f861159 100644 --- a/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java +++ b/src/test/java/com/google/devtools/build/android/ziputils/SplitZipTest.java @@ -18,6 +18,9 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableSet; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -426,6 +429,110 @@ public class SplitZipTest { } @Test + public void testInputFilter() throws Exception { + new ZipFileBuilder() + .add("pkg/test.txt", "hello world") + .add("pkg/test2.txt", "how are you") + .add("pkg/test.class", "hello world") + .add("pkg/test2.class", "how are you") + .add("pkg/R$attr.class", "bye bye") + .create("input.zip"); + + new ZipFileBuilder() + .add("pkg/test.txt", "hello world") + .add("pkg/test2.class", "how are you") + .create("expected.zip"); + byte[] expectedBytes = fileSystem.toByteArray("expected.zip"); + + new SplitZip() + .addOutput(new ZipOut(fileSystem.getOutputChannel("out/shard1.jar", false), + "out/shard1.jar")) + .setVerbose(true) + .addInput(new ZipIn(fileSystem.getInputChannel("input.zip"), "input.zip")) + .setInputFilter( + Predicates.in(ImmutableSet.of("pkg/test.txt", "pkg/test2.class", "pkg2/test.class"))) + .run() + .close(); + + byte[] outputBytes = fileSystem.toByteArray("out/shard1.jar"); + assertThat(outputBytes).isEqualTo(expectedBytes); + } + + @Test + public void testInputFilter_splitDexedClasses() throws Exception { + new ZipFileBuilder() + .add("pkg/test.class.dex", "hello world") + .add("pkg/test2.class", "how are you") + .add("pkg/R$attr.class", "bye bye") + .create("input.zip"); + + new ZipFileBuilder() + .add("pkg/test.class.dex", "hello world") + .add("pkg/R$attr.class", "bye bye") + .create("expected.zip"); + byte[] expectedBytes = fileSystem.toByteArray("expected.zip"); + + new SplitZip() + .addOutput(new ZipOut(fileSystem.getOutputChannel("out/shard1.jar", false), + "out/shard1.jar")) + .setVerbose(true) + .addInput(new ZipIn(fileSystem.getInputChannel("input.zip"), "input.zip")) + .setInputFilter( + Predicates.in(ImmutableSet.of("pkg/test.class", "pkg/R$attr.class"))) + .setSplitDexedClasses(true) + .run() + .close(); + + byte[] outputBytes = fileSystem.toByteArray("out/shard1.jar"); + assertThat(outputBytes).isEqualTo(expectedBytes); + } + + @Test + public void testInputFilter_mainDexFilter() throws Exception { + new ZipFileBuilder() + .add("pkg1/test1.class", "hello world") + .add("pkg2/test1.class", "how are you") + .add("pkg1/test2.class", "hello world") + .add("pkg2/test2.class", "how are you") + .add("pkg1/test3.class", "bye bye") + .add("pkg2/test3.class", "bye bye") + .create("input.jar"); + + String classFileList = "pkg1/test1.class\npkg2/test2.class\n"; + fileSystem.addFile("main_dex_list.txt", classFileList); + + new SplitZip() + .addOutput(new ZipOut(fileSystem.getOutputChannel("out/shard1.jar", false), + "out/shard1.jar")) + .addOutput(new ZipOut(fileSystem.getOutputChannel("out/shard2.jar", false), + "out/shard2.jar")) + .setVerbose(true) + .setMainClassListFile(fileSystem.getInputStream("main_dex_list.txt")) + .addInput(new ZipIn(fileSystem.getInputChannel("input.jar"), "input.jar")) + .setInputFilter( + Predicates.in( + ImmutableSet.of("pkg1/test1.class", "pkg2/test1.class", "pkg3/test1.class"))) + .setSplitDexedClasses(true) + .run() + .close(); + + // 1st shard contains only main dex list classes also in the filter + new ZipFileBuilder() + .add("pkg1/test1.class", "hello world") + .create("expected/shard1.jar"); + + new ZipFileBuilder() + .add("pkg2/test1.class", "how are you") + .create("expected/shard2.jar"); + + assertThat(fileSystem.toByteArray("out/shard1.jar")) + .isEqualTo(fileSystem.toByteArray("expected/shard1.jar")); + + assertThat(fileSystem.toByteArray("out/shard2.jar")) + .isEqualTo(fileSystem.toByteArray("expected/shard2.jar")); + } + + @Test public void testVerbose() { SplitZip instance = new SplitZip(); instance.setVerbose(true); |