diff options
author | 2017-11-09 01:15:23 +0100 | |
---|---|---|
committer | 2017-11-09 18:29:25 +0100 | |
commit | 72129ad6baac7035d1a1469e9bc07ccf037f6440 (patch) | |
tree | 13a1aa3a77d0bd7cd992dc7b39504ebd8f86193f /src/test/java/com/google/devtools/build/android | |
parent | 1a55fc5e3924f77a42f0503eee85aa1d1365fd2f (diff) |
Support multiple (disjoint) inputs and add --multidex=given_shard flag to DexFileMerger tool
RELNOTES: None.
PiperOrigin-RevId: 175082253
Diffstat (limited to 'src/test/java/com/google/devtools/build/android')
4 files changed, 135 insertions, 26 deletions
diff --git a/src/test/java/com/google/devtools/build/android/dexer/BUILD b/src/test/java/com/google/devtools/build/android/dexer/BUILD index c60e7474d7..08941fedb3 100644 --- a/src/test/java/com/google/devtools/build/android/dexer/BUILD +++ b/src/test/java/com/google/devtools/build/android/dexer/BUILD @@ -22,8 +22,6 @@ java_library( exclude = [ "NoAndroidSdkStubTest.java", "AllTests.java", - # TODO(kmb,ajmichael): need 2nd Java 7 Jar for this test - "DexFileSplitterTest.java", ], ), "//conditions:default": ["NoAndroidSdkStubTest.java"], @@ -42,16 +40,23 @@ java_library( }), ) +java_library( + name = "testdata", + srcs = glob(["testdata/**/*.java"]), +) + java_test( name = "AllTests", size = "small", data = [ "test_main_dex_list.txt", + ":testdata", ":tests", ], jvm_flags = [ "-Dtestmaindexlist=$(location :test_main_dex_list.txt)", "-Dtestinputjar=$(location :tests)", + "-Dtestinputjar2=$(location :testdata)", ], runtime_deps = [ ":tests", diff --git a/src/test/java/com/google/devtools/build/android/dexer/DexFileMergerTest.java b/src/test/java/com/google/devtools/build/android/dexer/DexFileMergerTest.java index c772e427e8..b459b70a36 100644 --- a/src/test/java/com/google/devtools/build/android/dexer/DexFileMergerTest.java +++ b/src/test/java/com/google/devtools/build/android/dexer/DexFileMergerTest.java @@ -19,11 +19,13 @@ import static org.junit.Assert.fail; import com.android.dex.ClassDef; import com.android.dex.Dex; +import com.android.dex.DexException; import com.android.dx.command.dexer.DxContext; import com.android.dx.dex.code.PositionList; import com.google.common.base.Function; import com.google.common.base.Predicates; import com.google.common.collect.HashMultimap; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterators; import com.google.common.collect.Multimap; @@ -50,11 +52,12 @@ public class DexFileMergerTest { private static final Path WORKING_DIR = Paths.get(System.getProperty("user.dir")); private static final Path INPUT_JAR = WORKING_DIR.resolve(System.getProperty("testinputjar")); + private static final Path INPUT_JAR2 = WORKING_DIR.resolve(System.getProperty("testinputjar2")); private static final Path MAIN_DEX_LIST_FILE = WORKING_DIR.resolve(System.getProperty("testmaindexlist")); static final String DEX_PREFIX = "classes"; - /** Exercises DexFileMerger in monodex mode. */ + /** Exercises DexFileMerger to write a single .dex file. */ @Test public void testMergeDexArchive_singleOutputDex() throws Exception { Path dexArchive = buildDexArchive(); @@ -64,6 +67,43 @@ public class DexFileMergerTest { assertSingleDexOutput(expectedClassCount, outputArchive, "classes.dex"); } + @Test + public void testMergeDexArchive_duplicateInputFails() throws Exception { + Path dexArchive = buildDexArchive(); + try { + runDexFileMerger( + 256 * 256, + /*forceJumbo=*/ false, + "duplicate.dex.zip", + MultidexStrategy.MINIMAL, + /*mainDexList=*/ null, + /*minimalMainDex=*/ false, + DEX_PREFIX, + dexArchive, + dexArchive); // input Jar twice to induce failure + fail("DexException expected"); + } catch (DexException expected) {} + } + + /** Similar to {@link #testMergeDexArchive_singleOutputDex} but uses --multidex=given_shard. */ + @Test + public void testMergeDexArchive_givenShard() throws Exception { + Path dexArchive = buildDexArchive(INPUT_JAR, "3.classes.jar"); + Path outputArchive = + runDexFileMerger( + 256 * 256, + /*forceJumbo=*/ false, + "given_shard.dex.zip", + MultidexStrategy.GIVEN_SHARD, + /*mainDexList=*/ null, + /*minimalMainDex=*/ false, + DEX_PREFIX, + dexArchive); + + int expectedClassCount = matchingFileCount(dexArchive, ".*\\.class.dex$"); + assertSingleDexOutput(expectedClassCount, outputArchive, "classes3.dex"); + } + /** * Similar to {@link #testMergeDexArchive_singleOutputDex} but different name for output dex file. */ @@ -72,19 +112,41 @@ public class DexFileMergerTest { Path dexArchive = buildDexArchive(); Path outputArchive = runDexFileMerger( - dexArchive, 256 * 256, /*forceJumbo=*/ false, - "from_dex_archive.dex.zip", + "prefix.dex.zip", MultidexStrategy.MINIMAL, /*mainDexList=*/ null, /*minimalMainDex=*/ false, - "noname"); + "noname", + dexArchive); int expectedClassCount = matchingFileCount(dexArchive, ".*\\.class.dex$"); assertSingleDexOutput(expectedClassCount, outputArchive, "noname.dex"); } + /** Exercises DexFileMerger with two input archives. */ + @Test + public void testMergeDexArchive_multipleInputs() throws Exception { + Path dexArchive = buildDexArchive(); + Path dexArchive2 = buildDexArchive(INPUT_JAR2, "libtestdata.jar.dex.zip"); + Path outputArchive = + runDexFileMerger( + 256 * 256, + /*forceJumbo=*/ false, + "multiple_inputs.dex.zip", + MultidexStrategy.MINIMAL, + /*mainDexList=*/ null, + /*minimalMainDex=*/ false, + DEX_PREFIX, + dexArchive, + dexArchive2); + + int expectedClassCount = matchingFileCount(dexArchive, ".*\\.class.dex$"); + expectedClassCount += matchingFileCount(dexArchive2, ".*\\.class.dex$"); + assertSingleDexOutput(expectedClassCount, outputArchive, "classes.dex"); + } + /** * Similar to {@link #testMergeDexArchive_singleOutputDex} but forces multiple output dex files. */ @@ -102,14 +164,14 @@ public class DexFileMergerTest { Path dexArchive = buildDexArchive(); Path outputArchive = runDexFileMerger( - dexArchive, 200, /*forceJumbo=*/ false, "main_dex_list.dex.zip", MultidexStrategy.MINIMAL, MAIN_DEX_LIST_FILE, /*minimalMainDex=*/ false, - DEX_PREFIX); + DEX_PREFIX, + dexArchive); int expectedClassCount = matchingFileCount(dexArchive, ".*\\.class.dex$"); assertMainDexOutput(expectedClassCount, outputArchive, false); @@ -120,14 +182,14 @@ public class DexFileMergerTest { Path dexArchive = buildDexArchive(); Path outputArchive = runDexFileMerger( - dexArchive, 256 * 256, /*forceJumbo=*/ false, "minimal_main_dex.dex.zip", MultidexStrategy.MINIMAL, MAIN_DEX_LIST_FILE, /*minimalMainDex=*/ true, - DEX_PREFIX); + DEX_PREFIX, + dexArchive); int expectedClassCount = matchingFileCount(dexArchive, ".*\\.class.dex$"); assertMainDexOutput(expectedClassCount, outputArchive, true); @@ -138,14 +200,14 @@ public class DexFileMergerTest { Path dexArchive = buildDexArchive(); try { runDexFileMerger( - dexArchive, 200, /*forceJumbo=*/ false, "classes.dex.zip", MultidexStrategy.OFF, /*mainDexList=*/ null, /*minimalMainDex=*/ true, - DEX_PREFIX); + DEX_PREFIX, + dexArchive); fail("Expected DexFileMerger to fail"); } catch (IllegalArgumentException e) { assertThat(e) @@ -154,14 +216,14 @@ public class DexFileMergerTest { } try { runDexFileMerger( - dexArchive, 200, /*forceJumbo=*/ false, "classes.dex.zip", MultidexStrategy.OFF, MAIN_DEX_LIST_FILE, /*minimalMainDex=*/ false, - DEX_PREFIX); + DEX_PREFIX, + dexArchive); fail("Expected DexFileMerger to fail"); } catch (IllegalArgumentException e) { assertThat(e) @@ -175,9 +237,9 @@ public class DexFileMergerTest { Path dexArchive = buildDexArchive(); Path outputArchive; try { - outputArchive = runDexFileMerger(dexArchive, 256 * 256, /*forceJumbo=*/ true, - "from_dex_archive.dex.zip", MultidexStrategy.OFF, /*mainDexList=*/ null, - /*minimalMainDex=*/ false, DEX_PREFIX); + outputArchive = runDexFileMerger(256 * 256, /*forceJumbo=*/ true, "from_dex_archive.dex.zip", + MultidexStrategy.OFF, /*mainDexList=*/ null, /*minimalMainDex=*/ false, DEX_PREFIX, + dexArchive); } catch (IllegalStateException e) { assertThat(e).hasMessage("--forceJumbo flag not supported"); System.err.println("Skipping this test due to missing --forceJumbo support in Android SDK."); @@ -275,28 +337,28 @@ public class DexFileMergerTest { private Path runDexFileMerger(Path dexArchive, int maxNumberOfIdxPerDex, String outputBasename) throws IOException { return runDexFileMerger( - dexArchive, maxNumberOfIdxPerDex, /*forceJumbo=*/ false, outputBasename, MultidexStrategy.MINIMAL, /*mainDexList=*/ null, /*minimalMainDex=*/ false, - DEX_PREFIX); + DEX_PREFIX, + dexArchive); } private Path runDexFileMerger( - Path dexArchive, int maxNumberOfIdxPerDex, boolean forceJumbo, String outputBasename, MultidexStrategy multidexMode, @Nullable Path mainDexList, boolean minimalMainDex, - String dexPrefix) + String dexPrefix, + Path... dexArchives) throws IOException { DexFileMerger.Options options = new DexFileMerger.Options(); - options.inputArchive = dexArchive; + options.inputArchives = ImmutableList.copyOf(dexArchives); options.outputArchive = FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR"), outputBasename); options.multidexMode = multidexMode; @@ -311,11 +373,13 @@ public class DexFileMergerTest { } private Path buildDexArchive() throws Exception { + return buildDexArchive(INPUT_JAR, "libtests.dex.zip"); + } + + private Path buildDexArchive(Path inputJar, String outputZip) throws Exception { DexBuilder.Options options = new DexBuilder.Options(); - // Use Jar file that has this test in it as the input Jar - options.inputJar = INPUT_JAR; - options.outputZip = - FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR"), "libtests.dex.zip"); + options.inputJar = inputJar; + options.outputZip = FileSystems.getDefault().getPath(System.getenv("TEST_TMPDIR"), outputZip); options.maxThreads = 1; Dexing.DexingOptions dexingOptions = new Dexing.DexingOptions(); dexingOptions.optimize = true; diff --git a/src/test/java/com/google/devtools/build/android/dexer/testdata/AnotherClass.java b/src/test/java/com/google/devtools/build/android/dexer/testdata/AnotherClass.java new file mode 100644 index 0000000000..5aba303964 --- /dev/null +++ b/src/test/java/com/google/devtools/build/android/dexer/testdata/AnotherClass.java @@ -0,0 +1,20 @@ +// 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.android.dexer.testdata; + +public class AnotherClass { + public String bar() { + return ""; + } +} diff --git a/src/test/java/com/google/devtools/build/android/dexer/testdata/SomeClass.java b/src/test/java/com/google/devtools/build/android/dexer/testdata/SomeClass.java new file mode 100644 index 0000000000..f298ea98a6 --- /dev/null +++ b/src/test/java/com/google/devtools/build/android/dexer/testdata/SomeClass.java @@ -0,0 +1,20 @@ +// 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.android.dexer.testdata; + +public class SomeClass { + public int foo() { + return 0; + } +} |