15
15
16
16
import static com .google .common .truth .Truth .assertThat ;
17
17
import static java .nio .charset .StandardCharsets .UTF_8 ;
18
+ import static org .junit .Assert .assertThrows ;
18
19
19
20
import com .google .common .base .Joiner ;
20
21
import com .google .common .base .Splitter ;
@@ -92,6 +93,42 @@ private static Path rlocation(String path) throws IOException {
92
93
working .toFile ().deleteOnExit ();
93
94
}
94
95
96
+ @ Test
97
+ public void testMergeManifestWithBrokenManifestSyntax () throws Exception {
98
+ String dataDir =
99
+ Paths .get (System .getenv ("TEST_WORKSPACE" ), System .getenv ("TEST_BINARY" ))
100
+ .resolveSibling ("testing/manifestmerge" )
101
+ .toString ()
102
+ .replace ('\\' , '/' );
103
+ Files .createDirectories (working .resolve ("output" ));
104
+ final Path mergedManifest = working .resolve ("output/mergedManifest.xml" );
105
+ final Path brokenMergerManifest = rlocation (dataDir + "/brokenManifest/AndroidManifest.xml" );
106
+ assertThat (brokenMergerManifest .toFile ().exists ()).isTrue ();
107
+
108
+ AndroidManifestProcessor .ManifestProcessingException e =
109
+ assertThrows (
110
+ AndroidManifestProcessor .ManifestProcessingException .class ,
111
+ () -> {
112
+ ManifestMergerAction .main (
113
+ generateArgs (
114
+ brokenMergerManifest ,
115
+ ImmutableMap .of (),
116
+ false , /* isLibrary */
117
+ ImmutableMap .of ("applicationId" , "com.google.android.apps.testapp" ),
118
+ "" , /* custom_package */
119
+ mergedManifest ,
120
+ false /* mergeManifestPermissions */ )
121
+ .toArray (new String [0 ]));
122
+ });
123
+ assertThat (e )
124
+ .hasMessageThat ()
125
+ .contains (
126
+ "com.android.manifmerger.ManifestMerger2$MergeFailureException: "
127
+ + "org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 6; "
128
+ + "The markup in the document following the root element must be well-formed." );
129
+ assertThat (mergedManifest .toFile ().exists ()).isFalse ();
130
+ }
131
+
95
132
@ Test
96
133
public void testMerge_GenerateDummyManifest () throws Exception {
97
134
Files .createDirectories (working .resolve ("output" ));
@@ -159,7 +196,8 @@ public void testMerge_GenerateDummyManifest() throws Exception {
159
196
false , /* isLibrary */
160
197
ImmutableMap .of ("applicationId" , "com.google.android.apps.testapp" ),
161
198
"" , /* custom_package */
162
- mergedManifest );
199
+ mergedManifest ,
200
+ /* mergeManifestPermissions */ false );
163
201
ManifestMergerAction .main (args .toArray (new String [0 ]));
164
202
165
203
assertThat (
@@ -174,6 +212,64 @@ public void testMerge_GenerateDummyManifest() throws Exception {
174
212
.trim ());
175
213
}
176
214
215
+ @ Test
216
+ public void testMergeWithMergePermissionsEnabled () throws Exception {
217
+ // Largely copied from testMerge() above. Perhaps worth combining the two test methods into one
218
+ // method in the future?
219
+ String dataDir =
220
+ Paths .get (System .getenv ("TEST_WORKSPACE" ), System .getenv ("TEST_BINARY" ))
221
+ .resolveSibling ("testing/manifestmerge" )
222
+ .toString ()
223
+ .replace ("\\ " , "/" );
224
+
225
+ final Path mergerManifest = rlocation (dataDir + "/merger/AndroidManifest.xml" );
226
+ final Path mergeeManifestOne = rlocation (dataDir + "/mergeeOne/AndroidManifest.xml" );
227
+ final Path mergeeManifestTwo = rlocation (dataDir + "/mergeeTwo/AndroidManifest.xml" );
228
+ assertThat (mergerManifest .toFile ().exists ()).isTrue ();
229
+ assertThat (mergeeManifestOne .toFile ().exists ()).isTrue ();
230
+ assertThat (mergeeManifestTwo .toFile ().exists ()).isTrue ();
231
+
232
+ // The following code retrieves the path of the only AndroidManifest.xml in the
233
+ // expected-merged-permission/manifests directory. Unfortunately, this test runs
234
+ // internally and externally and the files have different names.
235
+ final File expectedManifestDirectory =
236
+ mergerManifest .getParent ().resolveSibling ("expected-merged-permissions" ).toFile ();
237
+ assertThat (expectedManifestDirectory .exists ()).isTrue ();
238
+ final String [] debug =
239
+ expectedManifestDirectory .list (new PatternFilenameFilter (".*AndroidManifest\\ .xml$" ));
240
+ assertThat (debug ).isNotNull ();
241
+ final File [] expectedManifestDirectoryManifests =
242
+ expectedManifestDirectory .listFiles ((File dir , String name ) -> true );
243
+ assertThat (expectedManifestDirectoryManifests ).isNotNull ();
244
+ assertThat (expectedManifestDirectoryManifests ).hasLength (1 );
245
+ final Path expectedManifest = expectedManifestDirectoryManifests [0 ].toPath ();
246
+
247
+ Files .createDirectories (working .resolve ("output" ));
248
+ final Path mergedManifest = working .resolve ("output/mergedManifest.xml" );
249
+
250
+ List <String > args =
251
+ generateArgs (
252
+ mergerManifest ,
253
+ ImmutableMap .of (mergeeManifestOne , "mergeeOne" , mergeeManifestTwo , "mergeeTwo" ),
254
+ false , /* isLibrary */
255
+ ImmutableMap .of ("applicationId" , "com.google.android.apps.testapp" ),
256
+ "" , /* custom_package */
257
+ mergedManifest ,
258
+ /* mergeManifestPermissions */ true );
259
+ ManifestMergerAction .main (args .toArray (new String [0 ]));
260
+
261
+ assertThat (
262
+ Joiner .on (" " )
263
+ .join (Files .readAllLines (mergedManifest , UTF_8 ))
264
+ .replaceAll ("\\ s+" , " " )
265
+ .trim ())
266
+ .isEqualTo (
267
+ Joiner .on (" " )
268
+ .join (Files .readAllLines (expectedManifest , UTF_8 ))
269
+ .replaceAll ("\\ s+" , " " )
270
+ .trim ());
271
+ }
272
+
177
273
@ Test public void fullIntegration () throws Exception {
178
274
Files .createDirectories (working .resolve ("output" ));
179
275
final Path binaryOutput = working .resolve ("output/binaryManifest.xml" );
@@ -198,8 +294,15 @@ public void testMerge_GenerateDummyManifest() throws Exception {
198
294
.getManifest ();
199
295
200
296
// libFoo manifest merging
201
- List <String > args = generateArgs (libFooManifest , ImmutableMap .<Path , String >of (), true ,
202
- ImmutableMap .<String , String >of (), "" , libFooOutput );
297
+ List <String > args =
298
+ generateArgs (
299
+ libFooManifest ,
300
+ ImmutableMap .<Path , String >of (),
301
+ true ,
302
+ ImmutableMap .<String , String >of (),
303
+ "" ,
304
+ libFooOutput ,
305
+ false );
203
306
ManifestMergerAction .main (args .toArray (new String [0 ]));
204
307
assertThat (Joiner .on (" " )
205
308
.join (Files .readAllLines (libFooOutput , UTF_8 ))
@@ -211,8 +314,15 @@ public void testMerge_GenerateDummyManifest() throws Exception {
211
314
+ "</manifest>" );
212
315
213
316
// libBar manifest merging
214
- args = generateArgs (libBarManifest , ImmutableMap .<Path , String >of (), true ,
215
- ImmutableMap .<String , String >of (), "com.google.libbar" , libBarOutput );
317
+ args =
318
+ generateArgs (
319
+ libBarManifest ,
320
+ ImmutableMap .<Path , String >of (),
321
+ true ,
322
+ ImmutableMap .<String , String >of (),
323
+ "com.google.libbar" ,
324
+ libBarOutput ,
325
+ false );
216
326
ManifestMergerAction .main (args .toArray (new String [0 ]));
217
327
assertThat (Joiner .on (" " )
218
328
.join (Files .readAllLines (libBarOutput , UTF_8 ))
@@ -235,7 +345,8 @@ public void testMerge_GenerateDummyManifest() throws Exception {
235
345
"applicationId" , "com.google.android.app" ,
236
346
"foo" , "this \\ \\ : is \" a, \" bad string" ),
237
347
/* customPackage= */ "" ,
238
- binaryOutput );
348
+ binaryOutput ,
349
+ /* mergeManifestPermissions */ false );
239
350
ManifestMergerAction .main (args .toArray (new String [0 ]));
240
351
assertThat (Joiner .on (" " )
241
352
.join (Files .readAllLines (binaryOutput , UTF_8 ))
@@ -255,14 +366,26 @@ private List<String> generateArgs(
255
366
boolean library ,
256
367
Map <String , String > manifestValues ,
257
368
String customPackage ,
258
- Path manifestOutput ) {
259
- return ImmutableList .of (
369
+ Path manifestOutput ,
370
+ boolean mergeManifestPermissions ) {
371
+ ImmutableList .Builder <String > builder = ImmutableList .builder ();
372
+ builder .add (
260
373
"--manifest" , manifest .toString (),
261
- "--mergeeManifests" , mapToDictionaryString (mergeeManifests ),
262
- "--mergeType" , library ? "LIBRARY" : "APPLICATION" ,
263
- "--manifestValues" , mapToDictionaryString (manifestValues ),
264
- "--customPackage" , customPackage ,
265
- "--manifestOutput" , manifestOutput .toString ());
374
+ "--mergeeManifests" , mapToDictionaryString (mergeeManifests ));
375
+ if (mergeManifestPermissions ) {
376
+ builder .add ("--mergeManifestPermissions" );
377
+ }
378
+
379
+ builder .add (
380
+ "--mergeType" ,
381
+ library ? "LIBRARY" : "APPLICATION" ,
382
+ "--manifestValues" ,
383
+ mapToDictionaryString (manifestValues ),
384
+ "--customPackage" ,
385
+ customPackage ,
386
+ "--manifestOutput" ,
387
+ manifestOutput .toString ());
388
+ return builder .build ();
266
389
}
267
390
268
391
private <K , V > String mapToDictionaryString (Map <K , V > map ) {
0 commit comments