1 // Copyright 2017 The Bazel Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package com.google.devtools.build.android.desugar; 15 16 import static com.google.common.truth.Truth.assertThat; 17 18 import com.google.common.collect.ImmutableList; 19 import java.io.File; 20 import java.io.FileOutputStream; 21 import java.util.zip.ZipEntry; 22 import java.util.zip.ZipOutputStream; 23 import org.junit.After; 24 import org.junit.AfterClass; 25 import org.junit.Before; 26 import org.junit.BeforeClass; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 /** 32 * Test that exercises the behavior of the IndexedInputs class. 33 */ 34 @RunWith(JUnit4.class) 35 public final class IndexedInputsTest { 36 37 private static File lib1; 38 39 private static String lib1Name; 40 41 private static File lib2; 42 43 private static String lib2Name; 44 45 private static InputFileProvider lib1InputFileProvider; 46 47 private static InputFileProvider lib2InputFileProvider; 48 49 @BeforeClass 50 public static void setUpClass() throws Exception { 51 lib1 = File.createTempFile("lib1", ".jar"); 52 lib1Name = lib1.getName(); 53 try (ZipOutputStream zos1 = new ZipOutputStream(new FileOutputStream(lib1))) { 54 zos1.putNextEntry(new ZipEntry("a/b/C.class")); 55 zos1.putNextEntry(new ZipEntry("a/b/D.class")); 56 zos1.closeEntry(); 57 } 58 59 lib2 = File.createTempFile("lib2", ".jar"); 60 lib2Name = lib2.getName(); 61 try (ZipOutputStream zos2 = new ZipOutputStream(new FileOutputStream(lib2))) { 62 zos2.putNextEntry(new ZipEntry("a/b/C.class")); 63 zos2.putNextEntry(new ZipEntry("a/b/E.class")); 64 zos2.closeEntry(); 65 } 66 } 67 68 @Before 69 public void createProviders() throws Exception { 70 lib1InputFileProvider = new ZipInputFileProvider(lib1.toPath()); 71 lib2InputFileProvider = new ZipInputFileProvider(lib2.toPath()); 72 } 73 74 @After 75 public void closeProviders() throws Exception { 76 lib1InputFileProvider.close(); 77 lib2InputFileProvider.close(); 78 } 79 80 @AfterClass 81 public static void tearDownClass() throws Exception { 82 lib1.delete(); 83 lib2.delete(); 84 } 85 86 @Test 87 public void testClassFoundWithParentLibrary() throws Exception { 88 IndexedInputs indexedLib2 = new IndexedInputs(ImmutableList.of(lib2InputFileProvider)); 89 IndexedInputs indexedLib1 = new IndexedInputs(ImmutableList.of(lib1InputFileProvider)); 90 IndexedInputs indexedLib2AndLib1 = indexedLib1.withParent(indexedLib2); 91 assertThat(indexedLib2AndLib1.getInputFileProvider("a/b/C.class").toString()) 92 .isEqualTo(lib2Name); 93 assertThat(indexedLib2AndLib1.getInputFileProvider("a/b/D.class").toString()) 94 .isEqualTo(lib1Name); 95 assertThat(indexedLib2AndLib1.getInputFileProvider("a/b/E.class").toString()) 96 .isEqualTo(lib2Name); 97 98 indexedLib2 = new IndexedInputs(ImmutableList.of(lib2InputFileProvider)); 99 indexedLib1 = new IndexedInputs(ImmutableList.of(lib1InputFileProvider)); 100 IndexedInputs indexedLib1AndLib2 = indexedLib2.withParent(indexedLib1); 101 assertThat(indexedLib1AndLib2.getInputFileProvider("a/b/C.class").toString()) 102 .isEqualTo(lib1Name); 103 assertThat(indexedLib1AndLib2.getInputFileProvider("a/b/D.class").toString()) 104 .isEqualTo(lib1Name); 105 assertThat(indexedLib1AndLib2.getInputFileProvider("a/b/E.class").toString()) 106 .isEqualTo(lib2Name); 107 } 108 109 @Test 110 public void testClassFoundWithoutParentLibrary() throws Exception { 111 IndexedInputs ijLib1Lib2 = 112 new IndexedInputs(ImmutableList.of(lib1InputFileProvider, lib2InputFileProvider)); 113 assertThat(ijLib1Lib2.getInputFileProvider("a/b/C.class").toString()) 114 .isEqualTo(lib1Name); 115 assertThat(ijLib1Lib2.getInputFileProvider("a/b/D.class").toString()) 116 .isEqualTo(lib1Name); 117 assertThat(ijLib1Lib2.getInputFileProvider("a/b/E.class").toString()) 118 .isEqualTo(lib2Name); 119 120 IndexedInputs ijLib2Lib1 = 121 new IndexedInputs(ImmutableList.of(lib2InputFileProvider, lib1InputFileProvider)); 122 assertThat(ijLib2Lib1.getInputFileProvider("a/b/C.class").toString()) 123 .isEqualTo(lib2Name); 124 assertThat(ijLib2Lib1.getInputFileProvider("a/b/D.class").toString()) 125 .isEqualTo(lib1Name); 126 assertThat(ijLib2Lib1.getInputFileProvider("a/b/E.class").toString()) 127 .isEqualTo(lib2Name); 128 } 129 130 @Test 131 public void testClassNotFound() throws Exception { 132 IndexedInputs ijLib1Lib2 = 133 new IndexedInputs(ImmutableList.of(lib1InputFileProvider, lib2InputFileProvider)); 134 assertThat(ijLib1Lib2.getInputFileProvider("a/b/F.class")).isNull(); 135 } 136 } 137