1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// 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;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import com.android.dex.Dex;
import com.android.dx.command.dexer.DxContext;
import com.android.dx.dex.DexOptions;
import com.android.dx.dex.cf.CfOptions;
import com.android.dx.dex.file.DexFile;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/** Tests for {@link DexFileAggregator}. */
@RunWith(JUnit4.class)
public class DexFileAggregatorTest {
/** Standard .dex file limit on methods and fields. */
private static final int DEX_LIMIT = 265 * 265;
private static final int WASTE = 1;
@Mock private DexFileArchive dest;
@Captor private ArgumentCaptor<Dex> written;
private Dex dex;
@Before
public void setUp() throws IOException {
dex = DexFiles.toDex(convertClass(DexFileAggregatorTest.class));
MockitoAnnotations.initMocks(this);
}
@Test
public void testClose_emptyWritesNothing() throws Exception {
DexFileAggregator dexer =
new DexFileAggregator(new DxContext(), dest, MultidexStrategy.MINIMAL, DEX_LIMIT, WASTE);
dexer.close();
verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class));
}
@Test
public void testAddAndClose_singleInputWritesThatInput() throws Exception {
DexFileAggregator dexer =
new DexFileAggregator(new DxContext(), dest, MultidexStrategy.MINIMAL, 0, WASTE);
dexer.add(dex);
dexer.close();
verify(dest).addFile(any(ZipEntry.class), eq(dex));
}
@Test
public void testMultidex_underLimitWritesOneShard() throws Exception {
DexFileAggregator dexer = new DexFileAggregator(
new DxContext(), dest, MultidexStrategy.BEST_EFFORT, DEX_LIMIT, WASTE);
Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class));
dexer.add(dex);
dexer.add(dex2);
verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class));
dexer.close();
verify(dest).addFile(any(ZipEntry.class), written.capture());
assertThat(Iterables.size(written.getValue().classDefs())).isEqualTo(2);
}
@Test
public void testMultidex_overLimitWritesSecondShard() throws Exception {
DexFileAggregator dexer = new DexFileAggregator(new DxContext(), dest,
MultidexStrategy.BEST_EFFORT, 2 /* dex has more than 2 methods and fields */, WASTE);
Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class));
dexer.add(dex); // classFile is already over limit but we take anything in empty shard
dexer.add(dex2); // this should start a new shard
// Make sure there was one file written and that file is dex
verify(dest).addFile(any(ZipEntry.class), written.capture());
assertThat(written.getValue()).isSameAs(dex);
dexer.close();
verify(dest).addFile(any(ZipEntry.class), eq(dex2));
}
@Test
public void testMonodex_alwaysWritesSingleShard() throws Exception {
DexFileAggregator dexer = new DexFileAggregator(new DxContext(), dest, MultidexStrategy.OFF,
2 /* dex has more than 2 methods and fields */, WASTE);
Dex dex2 = DexFiles.toDex(convertClass(ByteStreams.class));
dexer.add(dex);
dexer.add(dex2);
verify(dest, times(0)).addFile(any(ZipEntry.class), any(Dex.class));
dexer.close();
verify(dest).addFile(any(ZipEntry.class), written.capture());
assertThat(Iterables.size(written.getValue().classDefs())).isEqualTo(2);
}
private static DexFile convertClass(Class<?> clazz) throws IOException {
String path = clazz.getName().replace('.', '/') + ".class";
try (InputStream in =
Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) {
return new DexConverter(new Dexing(new DxContext(), new DexOptions(), new CfOptions()))
.toDexFile(ByteStreams.toByteArray(in), path);
}
}
}
|