1 /* 2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8163798 27 * @summary basic tests for multi-release jar versioned streams 28 * @library /test/lib 29 * @modules jdk.jartool/sun.tools.jar java.base/jdk.internal.util.jar 30 * @run testng TestVersionedStream 31 */ 32 33 import org.testng.Assert; 34 import org.testng.annotations.AfterClass; 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 38 import java.io.File; 39 import java.io.IOException; 40 import java.io.InputStream; 41 import java.io.UncheckedIOException; 42 import java.nio.file.Files; 43 import java.nio.file.Path; 44 import java.nio.file.Paths; 45 import java.util.ArrayList; 46 import java.util.Arrays; 47 import java.util.HashMap; 48 import java.util.Iterator; 49 import java.util.LinkedHashSet; 50 import java.util.List; 51 import java.util.Map; 52 import java.util.Set; 53 import java.util.jar.JarEntry; 54 import java.util.jar.JarFile; 55 import java.util.stream.Collectors; 56 import java.util.stream.Stream; 57 import java.util.zip.ZipFile; 58 59 import jdk.test.lib.util.FileUtils; 60 61 public class TestVersionedStream { 62 private final Path userdir; 63 private final Set<String> unversionedEntryNames; 64 65 public TestVersionedStream() throws IOException { 66 userdir = Paths.get(System.getProperty("user.dir", ".")); 67 68 // These are not real class files even though they end with .class. 69 // They are resource files so jar tool validation won't reject them. 70 // But they are what we want to test, especially q/Bar.class that 71 // could be in a concealed package if this was a modular multi-release 72 // jar. 73 createFiles( 74 "base/p/Bar.class", 75 "base/p/Foo.class", 76 "base/p/Main.class", 77 "v9/p/Foo.class", 78 "v10/p/Foo.class", 79 "v10/q/Bar.class", 80 "v11/p/Bar.class", 81 "v11/p/Foo.class" 82 ); 83 84 jar("cf mmr.jar -C base . --release 9 -C v9 . " + 85 "--release 10 -C v10 . --release 11 -C v11 ."); 86 87 System.out.println("Contents of mmr.jar\n======="); 88 89 try(JarFile jf = new JarFile("mmr.jar")) { 90 unversionedEntryNames = jf.stream() 91 .map(je -> je.getName()) 92 .peek(System.out::println) 93 .map(nm -> nm.startsWith("META-INF/versions/") 94 ? nm.replaceFirst("META-INF/versions/\\d+/", "") 95 : nm) 96 .collect(Collectors.toCollection(LinkedHashSet::new)); 97 } 98 99 System.out.println("======="); 100 } 101 102 @AfterClass 103 public void close() throws IOException { 104 Files.walk(userdir, 1) 105 .filter(p -> !p.equals(userdir)) 106 .forEach(p -> { 107 try { 108 if (Files.isDirectory(p)) { 109 FileUtils.deleteFileTreeWithRetry(p); 110 } else { 111 FileUtils.deleteFileIfExistsWithRetry(p); 112 } 113 } catch (IOException x) { 114 throw new UncheckedIOException(x); 115 } 116 }); 117 } 118 119 @DataProvider 120 public Object[][] data() { 121 return new Object[][] { 122 {Runtime.Version.parse("8")}, 123 {Runtime.Version.parse("9")}, 124 {Runtime.Version.parse("10")}, 125 {Runtime.Version.parse("11")}, 126 {JarFile.baseVersion()}, 127 {JarFile.runtimeVersion()} 128 }; 129 } 130 131 @Test(dataProvider="data") 132 public void test(Runtime.Version version) throws Exception { 133 try (JarFile jf = new JarFile(new File("mmr.jar"), false, ZipFile.OPEN_READ, version); 134 Stream<JarEntry> jes = jdk.internal.util.jar.VersionedStream.stream(jf)) 135 { 136 Assert.assertNotNull(jes); 137 138 // put versioned entries in list so we can reuse them 139 List<JarEntry> versionedEntries = jes.collect(Collectors.toList()); 140 141 Assert.assertTrue(versionedEntries.size() > 0); 142 143 // also keep the names 144 List<String> versionedNames = new ArrayList<>(versionedEntries.size()); 145 146 // verify the correct order while building enames 147 Iterator<String> allIt = unversionedEntryNames.iterator(); 148 Iterator<JarEntry> verIt = versionedEntries.iterator(); 149 boolean match = false; 150 151 while (verIt.hasNext()) { 152 match = false; 153 if (!allIt.hasNext()) break; 154 String name = verIt.next().getName(); 155 versionedNames.add(name); 156 while (allIt.hasNext()) { 157 if (name.equals(allIt.next())) { 158 match = true; 159 break; 160 } 161 } 162 } 163 if (!match) { 164 Assert.fail("versioned entries not in same order as unversioned entries"); 165 } 166 167 // verify the contents 168 Map<String,String> contents = new HashMap<>(); 169 contents.put("p/Bar.class", "base/p/Bar.class"); 170 contents.put("p/Main.class", "base/p/Main.class"); 171 switch (version.major()) { 172 case 8: 173 contents.put("p/Foo.class", "base/p/Foo.class"); 174 break; 175 case 9: 176 contents.put("p/Foo.class", "v9/p/Foo.class"); 177 break; 178 case 10: 179 contents.put("p/Foo.class", "v10/p/Foo.class"); 180 contents.put("q/Bar.class", "v10/q/Bar.class"); 181 break; 182 case 11: 183 contents.put("p/Bar.class", "v11/p/Bar.class"); 184 contents.put("p/Foo.class", "v11/p/Foo.class"); 185 contents.put("q/Bar.class", "v10/q/Bar.class"); 186 break; 187 default: 188 Assert.fail("Test out of date, please add more cases"); 189 } 190 191 contents.entrySet().stream().forEach(e -> { 192 String name = e.getKey(); 193 int i = versionedNames.indexOf(name); 194 Assert.assertTrue(i != -1, name + " not in enames"); 195 JarEntry je = versionedEntries.get(i); 196 try (InputStream is = jf.getInputStream(je)) { 197 String s = new String(is.readAllBytes()).replaceAll(System.lineSeparator(), ""); 198 Assert.assertTrue(s.endsWith(e.getValue()), s); 199 } catch (IOException x) { 200 throw new UncheckedIOException(x); 201 } 202 }); 203 } 204 } 205 206 private void createFiles(String... files) { 207 ArrayList<String> list = new ArrayList(); 208 Arrays.stream(files) 209 .map(f -> Paths.get(userdir.toAbsolutePath().toString(), f)) 210 .forEach(p -> { 211 try { 212 Files.createDirectories(p.getParent()); 213 Files.createFile(p); 214 list.clear(); 215 list.add(p.toString().replace(File.separatorChar, '/')); 216 Files.write(p, list); 217 } catch (IOException x) { 218 throw new UncheckedIOException(x); 219 }}); 220 } 221 222 private void jar(String args) { 223 new sun.tools.jar.Main(System.out, System.err, "jar") 224 .run(args.split(" +")); 225 } 226 }