summaryrefslogtreecommitdiffstats
path: root/jack-tests/tests/com/android/jack/fileconflict/FileConflictTests.java
blob: 8b1809404698e5c2a428660295712a524aa771eb (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.android.jack.fileconflict;

import com.android.jack.JackAbortException;
import com.android.jack.backend.jayce.JayceFileImporter;
import com.android.jack.backend.jayce.TypeImportConflictException;
import com.android.jack.library.FileType;
import com.android.jack.library.FileTypeDoesNotExistException;
import com.android.jack.library.InputJackLibrary;
import com.android.jack.library.LibraryReadingException;
import com.android.jack.resource.ResourceImportConflictException;
import com.android.jack.resource.ResourceImporter;
import com.android.jack.shrob.obfuscation.NameProviderFactory;
import com.android.jack.test.category.KnownBugs;
import com.android.jack.test.toolchain.AbstractTestTools;
import com.android.jack.test.toolchain.IToolchain;
import com.android.jack.test.toolchain.JackApiToolchainBase;
import com.android.jack.test.toolchain.JackBasedToolchain;
import com.android.jack.test.toolchain.JackCliToolchain;
import com.android.jack.test.toolchain.LegacyJillToolchain;
import com.android.sched.util.location.FileLocation;
import com.android.sched.vfs.InputVFile;
import com.android.sched.vfs.VPath;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * JUnit tests for conflicts between Jayce files and resources.
 */
public class FileConflictTests {

  @Nonnull
  private static final VPath TYPE1_PATH =
      new VPath("com/android/jack/fileconflict/test001/jack/MyClass", '/');
  @Nonnull
  private static final VPath TYPE2_PATH =
      new VPath("com/android/jack/fileconflict/test001/jack/MyClass2", '/');
  @Nonnull
  private static final VPath TYPE3_PATH =
      new VPath("com/android/jack/fileconflict/test001/jack/MyClass3", '/');
  @Nonnull
  private static final VPath RESOURCE1_PATH = new VPath("Resource1", '/');
  @Nonnull
  private static final VPath RESOURCE2_PATH = new VPath("Resource2", '/');
  @Nonnull
  private static final VPath RESOURCE3_PATH = new VPath("Resource3", '/');

  @Nonnull
  private static final File TEST001_DIR =
      AbstractTestTools.getTestRootDir("com.android.jack.fileconflict.test001.jack");

  @Nonnull
  private static final File TEST002_DIR =
      AbstractTestTools.getTestRootDir("com.android.jack.fileconflict.test002.jack");

  @BeforeClass
  public static void setUpClass() {
    FileConflictTests.class.getClassLoader().setDefaultAssertionStatus(true);
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting Jack files, and
   * outputting to a Jack folder, with no collision policy specified.
   * @throws Exception
   */
  @Test
  public void test001a() throws Exception {
    File jackOutput = AbstractTestTools.createTempDir();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    try {
      runTest001(jackOutput, null, errOut, /* isApiTest = */ true, /* verbose = */ false);
      Assert.fail();
    } catch (JackAbortException e) {
      Assert.assertTrue(e.getCause() instanceof LibraryReadingException);
      Assert.assertTrue(e.getCause().getCause() instanceof TypeImportConflictException);
      String errString = errOut.toString();
      Assert.assertTrue(
          errString.contains("Type com.android.jack.fileconflict.test001.jack.MyClass"));
      Assert.assertTrue(errString.contains("has already been imported"));
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting Jack files, and
   * outputting to a Jack folder, with the collision policy set to "fail".
   * @throws Exception
   */
  @Test
  public void test001b() throws Exception {
    File jackOutput = AbstractTestTools.createTempDir();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    try {
      runTest001(jackOutput, "fail", errOut, /* isApiTest = */ true, /* verbose = */ false);
      Assert.fail();
    } catch (JackAbortException e) {
      Assert.assertTrue(e.getCause() instanceof LibraryReadingException);
      Assert.assertTrue(e.getCause().getCause() instanceof TypeImportConflictException);
      String errString = errOut.toString();
      Assert.assertTrue(
          errString.contains("Type com.android.jack.fileconflict.test001.jack.MyClass"));
      Assert.assertTrue(errString.contains("has already been imported"));
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting Jack files, and
   * outputting to a Jack folder, with the collision policy set to "keep-first", and the default
   * verbosity.
   * @throws Exception
   */
  @Test
  public void test001c() throws Exception {
    File jackOutput = AbstractTestTools.createTempDir();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    runTest001(jackOutput, "keep-first", errOut /* errorStream */, /* isApiTest = */ false,
        /* verbose = */ false);
    InputJackLibrary outputLib = null;
    try {
      outputLib = AbstractTestTools.getInputJackLibrary(jackOutput);
      InputVFile myClass1 = outputLib.getFile(FileType.JAYCE, TYPE1_PATH);
      InputVFile myClass2 = outputLib.getFile(FileType.JAYCE, TYPE2_PATH);
      InputVFile myClass3 = outputLib.getFile(FileType.JAYCE, TYPE3_PATH);
      checkJayceNotEmpty(myClass1);
      checkJayceNotEmpty(myClass2);
      checkJayceNotEmpty(myClass3);
    } catch (FileTypeDoesNotExistException e) {
      Assert.fail();
    } finally {
      if (outputLib != null) {
        outputLib.close();
      }
      Assert.assertTrue(errOut.toString().isEmpty());
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting Jack files, and
   * outputting to a Jack folder, with the collision policy set to "keep-first", and the verbosity
   * set to "DEBUG".
   * @throws Exception
   */
  @Test
  @Category(KnownBugs.class)
  public void test001d() throws Exception {
    File jackOutput = AbstractTestTools.createTempDir();
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    runTest001(jackOutput, "keep-first", errOut /* errorStream */, /* isApiTest = */ false,
        /* verbose = */ true);
    InputJackLibrary outputLib = null;
    try {
      outputLib = AbstractTestTools.getInputJackLibrary(jackOutput);
      InputVFile myClass1 = outputLib.getFile(FileType.JAYCE, TYPE1_PATH);
      InputVFile myClass2 = outputLib.getFile(FileType.JAYCE, TYPE2_PATH);
      InputVFile myClass3 = outputLib.getFile(FileType.JAYCE, TYPE3_PATH);
      checkJayceNotEmpty(myClass1);
      checkJayceNotEmpty(myClass2);
      checkJayceNotEmpty(myClass3);
    } catch (FileTypeDoesNotExistException e) {
      Assert.fail();
    } finally {
      if (outputLib != null) {
        outputLib.close();
      }
      String errString = errOut.toString();
      Assert.assertTrue(
          errString.contains("Type com.android.jack.fileconflict.test001.jack.MyClass"));
      Assert.assertTrue(
          errString.contains("Type com.android.jack.fileconflict.test001.jack.MyClass2"));
      Assert.assertTrue(
          errString.contains("Type com.android.jack.fileconflict.test001.jack.MyClass3"));
      Assert.assertTrue(errString.contains("has already been imported"));
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting resources, and
   * outputting to a Jack folder, with no collision policy specified.
   * @throws Exception
   */
  @Test
  public void test002a() throws Exception {
    File jackOutput;
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    try {
      jackOutput = runTest002(false /* non-zipped */, null /* collisionPolicy */, errOut,
          /* isApiTest = */ true, /* verbose = */ false);
      Assert.fail();
    } catch (JackAbortException e) {
      Assert.assertTrue(e.getCause() instanceof LibraryReadingException);
      Assert.assertTrue(e.getCause().getCause() instanceof ResourceImportConflictException);
      String errString = errOut.toString();
      Assert.assertTrue(errString.contains("Resource in"));
      Assert.assertTrue(errString.contains("rsc/Resource1"));
      Assert.assertTrue(errString.contains("has already been imported"));
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting resources, and
   * outputting to a Jack folder, with the collision policy set to "fail".
   * @throws Exception
   */
  @Test
  public void test002b() throws Exception {
    File jackOutput;
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    try {
      jackOutput = runTest002(false /* non-zipped */, "fail", errOut, /* isApiTest = */ true,
          /* verbose = */ false);
      Assert.fail();
    } catch (JackAbortException e) {
      Assert.assertTrue(e.getCause() instanceof LibraryReadingException);
      Assert.assertTrue(e.getCause().getCause() instanceof ResourceImportConflictException);
      String errString = errOut.toString();
      Assert.assertTrue(errString.contains("Resource in"));
      Assert.assertTrue(errString.contains("rsc/Resource1"));
      Assert.assertTrue(errString.contains("has already been imported"));
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting resources, and
   * outputting to a Jack folder, with the collision policy set to "keep-first", with default
   * verbosity.
   * @throws Exception
   */
  @Test
  public void test002c() throws Exception {
    File jackOutput;
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    jackOutput = runTest002(false /* non-zipped */, "keep-first", errOut, /* isApiTest = */ false,
        /* verbose = */ false);
    InputJackLibrary lib = null;
    try {
      lib = AbstractTestTools.getInputJackLibrary(jackOutput);
      checkResourceContent(lib, RESOURCE1_PATH, "Res1");
      checkResourceContent(lib, RESOURCE2_PATH, "Res2");
      checkResourceContent(lib, RESOURCE3_PATH, "Res3");
    } finally {
      if (lib != null) {
        lib.close();
      }
    }
    Assert.assertTrue(errOut.toString().isEmpty());
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting resources, and
   * outputting to a Jack zip, with no collision policy specified.
   * @throws Exception
   */
  @Test
  public void test002d() throws Exception {
    File jackOutput;
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    try {
      jackOutput = runTest002(true /* zipped */, null, errOut, /* isApiTest = */ true,
          /* verbose = */ false);
      Assert.fail();
    } catch (JackAbortException e) {
      Assert.assertTrue(e.getCause() instanceof LibraryReadingException);
      Assert.assertTrue(e.getCause().getCause() instanceof ResourceImportConflictException);
      String errString = errOut.toString();
      Assert.assertTrue(errString.contains("Resource in"));
      Assert.assertTrue(errString.contains("rsc/Resource1"));
      Assert.assertTrue(errString.contains("has already been imported"));
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting resources, and
   * outputting to a Jack zip, with the collision policy set to "fail".
   * @throws Exception
   */
  @Test
  public void test002e() throws Exception {
    File jackOutput;
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    try {
      jackOutput = runTest002(true /* zipped */, "fail", errOut, /* isApiTest = */ true,
          /* verbose = */ false);
      Assert.fail();
    } catch (JackAbortException e) {
      Assert.assertTrue(e.getCause() instanceof LibraryReadingException);
      Assert.assertTrue(e.getCause().getCause() instanceof ResourceImportConflictException);
      String errString = errOut.toString();
      Assert.assertTrue(errString.contains("Resource in"));
      Assert.assertTrue(errString.contains("rsc/Resource1"));
      Assert.assertTrue(errString.contains("has already been imported"));
    }
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting resources, and
   * outputting to a Jack zip, with the collision policy set to "keep-first", with default
   * verbosity.
   * @throws Exception
   */
  @Test
  public void test002f() throws Exception {
    File jackOutput;
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    jackOutput = runTest002(true /* zipped */, "keep-first", errOut, /* isApiTest = */ false,
        /* verbose = */ false);
    InputJackLibrary lib = null;
    try {
      lib = AbstractTestTools.getInputJackLibrary(jackOutput);
      checkResourceContent(lib, RESOURCE1_PATH, "Res1");
      checkResourceContent(lib, RESOURCE2_PATH, "Res2");
      checkResourceContent(lib, RESOURCE3_PATH, "Res3");
    } finally {
      if (lib != null) {
        lib.close();
      }
    }
    Assert.assertTrue(errOut.toString().isEmpty());
  }

  /**
   * Test the behavior of Jack when importing 2 Jack folders containing conflicting resources, and
   * outputting to a Jack zip, with the collision policy set to "keep-first", with verbosity set to
   * "DEBUG".
   * @throws Exception
   */
  @Test
  @Category(KnownBugs.class)
  public void test002g() throws Exception {
    File jackOutput;
    ByteArrayOutputStream errOut = new ByteArrayOutputStream();
    jackOutput = runTest002(true /* zipped */, "keep-first", errOut, /* isApiTest = */ false,
        /* verbose = */ true);
    InputJackLibrary lib = null;
    try {
      lib = AbstractTestTools.getInputJackLibrary(jackOutput);
      checkResourceContent(lib, RESOURCE1_PATH, "Res1");
      checkResourceContent(lib, RESOURCE2_PATH, "Res2");
      checkResourceContent(lib, RESOURCE3_PATH, "Res3");
    } finally {
      if (lib != null) {
        lib.close();
      }
    }
    String errString = errOut.toString();
    Assert.assertTrue(errString.contains("Resource in"));
    Assert.assertTrue(errString.contains("rsc/Resource1"));
    Assert.assertTrue(errString.contains("has already been imported"));
  }

  /**
   * Test the behavior of Jack when outputting a Jack file to a Jack folder where a Jack file of the
   * same name already exists. We expect the previous file to be overridden.
   * @throws Exception
   */
  @Test
  public void test003() throws Exception {

    File jackOutput = AbstractTestTools.createTempDir();
    File testSrcDir = AbstractTestTools.getTestRootDir("com.android.jack.fileconflict.test003");
    File tempJackFolder = AbstractTestTools.createTempDir();

    List<Class<? extends IToolchain>> exclude = new ArrayList<Class<? extends IToolchain>>();
    exclude.add(LegacyJillToolchain.class);
    JackBasedToolchain toolchain =
        AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class, exclude);
    toolchain.addResourceDir(new File(testSrcDir, "jack/rsc1"))
    .addToClasspath(toolchain.getDefaultBootClasspath())
    .srcToLib(
        tempJackFolder,
        /* zipFiles = */ false,
        testSrcDir);

    toolchain =
        AbstractTestTools.getCandidateToolchain(JackBasedToolchain.class, exclude);
    toolchain.addResourceDir(new File(testSrcDir, "jack/rsc2"))
    .addToClasspath(toolchain.getDefaultBootClasspath())
    .srcToLib(
        tempJackFolder,
        /* zipFiles = */ false,
        testSrcDir);

    VPath typePath = new VPath("com/android/jack/fileconflict/test003/jack/MyClass", '/');
    InputJackLibrary lib = null;
    try {
      lib = AbstractTestTools.getInputJackLibrary(tempJackFolder);

      // check Jayce file is not empty
      InputVFile jayceFile = lib.getFile(FileType.JAYCE, typePath);
      checkJayceNotEmpty(jayceFile);

      // check Resource file content
      checkResourceContent(lib, new VPath("Resource", '/'), "Res2");
    } finally {
      if (lib != null) {
        lib.close();
      }
    }
  }

  /**
   * Test the behavior of Jack when renaming a Jayce file along with the resource with a matching
   * name, and when a resource with the same name (after renaming) already exists.
   * @throws Exception
   */
  @Test
  public void test004() throws Exception {
    File jackOutput = AbstractTestTools.createTempDir();
    VPath pathToType = new VPath("com/android/jack/fileconflict/test004/jack/MyClass", '/');
    VPath pathToRenamedType = new VPath("pcz/nbqfcvq/wnpx/svyrpcbsyvph/hrgh004/wnpx/ZmPyngg", '/');
    VPath pathToResource2 = pathToRenamedType.clone().addSuffix(".txt");

    // compile source files to a lib dir while importing ZmPyngg.txt
    File testSrcDir =
        AbstractTestTools.getTestRootDir("com.android.jack.fileconflict.test004.jack");
    File jackImport1 = AbstractTestTools.createTempDir();

    JackApiToolchainBase toolchain =
        AbstractTestTools.getCandidateToolchain(JackApiToolchainBase.class);
    toolchain.addResourceDir(new File(testSrcDir, "rsc2"))
    .addToClasspath(toolchain.getDefaultBootClasspath())
    .srcToLib(
        jackImport1,
        /* zipFiles = */ false,
        testSrcDir);

    InputJackLibrary lib = null;
    try {
      lib = AbstractTestTools.getInputJackLibrary(jackImport1);
      checkResourceContent(lib, pathToResource2, "a");
      checkJayceNotEmpty(lib.getFile(FileType.JAYCE, pathToType));
    } finally {
      if (lib != null) {
        lib.close();
      }
    }

    // compile source files to the same lib dir while importing MyClass.txt which should be
    // obfuscated as ZmPyngg.txt
    toolchain = AbstractTestTools.getCandidateToolchain(JackApiToolchainBase.class);
    toolchain.addResourceDir(new File(testSrcDir, "rsc1"));
    toolchain.addProguardFlags(new File(testSrcDir, "proguard.flags"));
    toolchain.addProperty(NameProviderFactory.NAMEPROVIDER.getName(), "rot13");
    toolchain.addToClasspath(toolchain.getDefaultBootClasspath())
    .srcToLib(
        jackImport1,
        /* zipFiles = */ false,
        testSrcDir);

    try {
      lib = AbstractTestTools.getInputJackLibrary(jackImport1);
      checkResourceContent(lib, pathToResource2, "MyClass");
      checkJayceNotEmpty(lib.getFile(FileType.JAYCE, pathToRenamedType));
      checkJayceNotEmpty(lib.getFile(FileType.JAYCE, pathToType)); // still here from 1st run
    } finally {
      if (lib != null) {
        lib.close();
      }
    }
  }

  @Nonnull
  private JackBasedToolchain getToolchain(boolean isApiTest) {
    List<Class<? extends IToolchain>> excludeList = new ArrayList<Class<? extends IToolchain>>(1);
    excludeList.add(LegacyJillToolchain.class);
    if (isApiTest) {
      excludeList.add(JackCliToolchain.class);
    }
    return AbstractTestTools.getCandidateToolchain(JackApiToolchainBase.class, excludeList);
  }

  private void runTest001(@Nonnull File jackOutput, @CheckForNull String collisionPolicy,
      @CheckForNull OutputStream errorStream, boolean isApiTest, boolean verbose) throws Exception {

    // create Jack dirs to import
    File jackLib1 = AbstractTestTools.createTempDir();
    File jackLib2 = AbstractTestTools.createTempDir();

    File srcFile1 = new File(TEST001_DIR, "MyClass.java");
    File srcFile2 = new File(TEST001_DIR, "MyClass2.java");
    File srcFile3 = new File(TEST001_DIR, "MyClass3.java");

    JackBasedToolchain toolchain = getToolchain(isApiTest);
    if (errorStream != null) {
      toolchain.setErrorStream(errorStream);
    }
    toolchain.setVerbose(verbose);
    toolchain.addToClasspath(toolchain.getDefaultBootClasspath()).srcToLib(
        jackLib1,
        /* zipFile = */ false,
        srcFile1, srcFile2);

    toolchain = getToolchain(isApiTest);
    if (errorStream != null) {
      toolchain.setErrorStream(errorStream);
    }
    toolchain.setVerbose(verbose);
    toolchain.addToClasspath(toolchain.getDefaultBootClasspath()).srcToLib(
        jackLib2,
        /* zipFile = */ false,
        srcFile1, srcFile3);

    // run Jack on Jack dirs
    toolchain = getToolchain(isApiTest);
    toolchain.addProguardFlags(new File(TEST001_DIR, "proguard.flags"));
    toolchain.addStaticLibs(jackLib1, jackLib2);
    toolchain.addToClasspath(toolchain.getDefaultBootClasspath());
    if (collisionPolicy != null) {
      toolchain.addProperty(JayceFileImporter.COLLISION_POLICY.getName(), collisionPolicy);
    }
    if (errorStream != null) {
      toolchain.setErrorStream(errorStream);
    }
    toolchain.libToLib(new File [] {jackLib1, jackLib2}, jackOutput, /* zipFiles = */ false);
  }

  @Nonnull
  private File runTest002(boolean zip, @CheckForNull String collisionPolicy,
      @CheckForNull OutputStream errorStream, boolean isApiTest, boolean verbose) throws Exception {
    // compile source files to a Jack dir
    File jackImport1 = AbstractTestTools.createTempDir();
    File lib1 = new File(TEST002_DIR, "lib1");

    JackBasedToolchain toolchain = getToolchain(isApiTest);
    toolchain.addResourceDir(new File(lib1, "rsc"));
    if (errorStream != null) {
      toolchain.setErrorStream(errorStream);
    }
    toolchain.setVerbose(verbose);
    toolchain.addToClasspath(toolchain.getDefaultBootClasspath()).srcToLib(
        jackImport1,
        /* zipFiles = */ false,
        lib1);

    toolchain = getToolchain(isApiTest);
    File jackImport2 = AbstractTestTools.createTempDir();
    File lib2 = new File(TEST002_DIR, "lib2");
    toolchain = getToolchain(isApiTest);
    toolchain.addResourceDir(new File(lib2, "rsc"));
    if (errorStream != null) {
      toolchain.setErrorStream(errorStream);
    }
    toolchain.addToClasspath(toolchain.getDefaultBootClasspath()).srcToLib(
        jackImport2,
        /* zipFiles = */ false,
        lib2);

    // run Jack on Jack dirs
    toolchain = getToolchain(isApiTest);
    toolchain.addProguardFlags(new File(TEST002_DIR, "proguard.flags"));
    if (collisionPolicy != null) {
      toolchain.addProperty(ResourceImporter.RESOURCE_COLLISION_POLICY.getName(), collisionPolicy);
    }
    File jackOutput;
    if (zip) {
      jackOutput = AbstractTestTools.createTempFile("jackOutput", toolchain.getLibraryExtension());
    } else {
      jackOutput = AbstractTestTools.createTempDir();
    }
    if (errorStream != null) {
      toolchain.setErrorStream(errorStream);
    }
    toolchain.libToLib(new File[] {jackImport1,jackImport2}, jackOutput, /* zipFiles = */ zip);

    return jackOutput;
  }

  private void checkResourceContent(@Nonnull InputJackLibrary lib, @Nonnull VPath path,
      @Nonnull String expectedContent) throws IOException, FileTypeDoesNotExistException {
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(
          new InputStreamReader(lib.getFile(FileType.RSC, path).getInputStream()));
      String line = reader.readLine();
      Assert.assertEquals(expectedContent, line);
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
  }

  private void checkJayceNotEmpty(@Nonnull InputVFile jayceFile) {
    File file = new File(((FileLocation) jayceFile.getLocation()).getPath());
    Assert.assertTrue(file.length() > 0);
  }
}