1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/jdeps/MRJarWarning.java Thu Apr 27 16:18:18 2017 -0700 1.3 @@ -0,0 +1,169 @@ 1.4 +/* 1.5 + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8176329 1.30 + * @summary Test for jdeps warning when it encounters a multi-release jar 1.31 + * @run testng MRJarWarning 1.32 + */ 1.33 + 1.34 +import java.io.IOException; 1.35 +import java.io.OutputStream; 1.36 +import java.io.PrintWriter; 1.37 +import java.io.StringWriter; 1.38 +import java.nio.file.Files; 1.39 +import java.nio.file.Path; 1.40 +import java.nio.file.Paths; 1.41 +import java.util.Arrays; 1.42 +import java.util.Collections; 1.43 +import java.util.List; 1.44 +import java.util.Locale; 1.45 +import java.util.jar.Attributes; 1.46 +import java.util.jar.JarEntry; 1.47 +import java.util.jar.JarOutputStream; 1.48 +import java.util.jar.Manifest; 1.49 +import org.testng.Assert; 1.50 +import org.testng.annotations.BeforeSuite; 1.51 +import org.testng.annotations.DataProvider; 1.52 +import org.testng.annotations.Test; 1.53 + 1.54 +public class MRJarWarning { 1.55 + private static final String WARNING = " is a multi-release jar file"; 1.56 + private static final String MRJAR_ATTR = "Multi-Release"; 1.57 + 1.58 + Path mrjar1; 1.59 + Path mrjar2; 1.60 + Path nonMRjar; 1.61 + Path mrjarAllCaps; 1.62 + 1.63 + private Attributes defaultAttributes; 1.64 + 1.65 + @BeforeSuite 1.66 + public void setup() throws IOException { 1.67 + defaultAttributes = new Attributes(); 1.68 + defaultAttributes.putValue("Manifest-Version", "1.0"); 1.69 + defaultAttributes.putValue("Created-By", "1.8.0-internal"); 1.70 + 1.71 + mrjar1 = Paths.get("mrjar1.jar"); 1.72 + mrjar2 = Paths.get("mrjar2.jar"); 1.73 + nonMRjar = Paths.get("nonMRjar.jar"); 1.74 + mrjarAllCaps = Paths.get("mrjarAllCaps.jar"); 1.75 + 1.76 + Attributes mrJarAttrs = new Attributes(defaultAttributes); 1.77 + mrJarAttrs.putValue(MRJAR_ATTR, "true"); 1.78 + 1.79 + build(mrjar1, mrJarAttrs); 1.80 + build(mrjar2, mrJarAttrs); 1.81 + build(nonMRjar, defaultAttributes); 1.82 + 1.83 + // JEP 238 - "Multi-Release JAR Files" states that the attribute name 1.84 + // and value are case insensitive. Try with all caps to ensure that 1.85 + // jdeps still recognizes a multi-release jar. 1.86 + Attributes allCapsAttrs = new Attributes(defaultAttributes); 1.87 + allCapsAttrs.putValue(MRJAR_ATTR.toUpperCase(), "TRUE"); 1.88 + build(mrjarAllCaps, allCapsAttrs); 1.89 + } 1.90 + 1.91 + @DataProvider(name="provider") 1.92 + private Object[][] args() { 1.93 + // jdeps warning messages may be localized. 1.94 + // This test only checks for the English version. Return an empty 1.95 + // array (skip testing) if the default language is not English. 1.96 + String language = Locale.getDefault().getLanguage(); 1.97 + System.out.println("Language: " + language); 1.98 + 1.99 + if ("en".equals(language)) { 1.100 + return new Object[][] { 1.101 + // one mrjar arg 1.102 + { Arrays.asList(mrjar1.toString()), 1.103 + Arrays.asList(mrjar1)}, 1.104 + // two mrjar args 1.105 + { Arrays.asList(mrjar1.toString(), mrjar2.toString()), 1.106 + Arrays.asList(mrjar1, mrjar2)}, 1.107 + // one mrjar arg, with mrjar on classpath 1.108 + { Arrays.asList("-cp", mrjar2.toString(), mrjar1.toString()), 1.109 + Arrays.asList(mrjar1, mrjar2)}, 1.110 + // non-mrjar arg, with mrjar on classpath 1.111 + { Arrays.asList("-cp", mrjar1.toString(), nonMRjar.toString()), 1.112 + Arrays.asList(mrjar1)}, 1.113 + // mrjar arg with jar attribute name/value in ALL CAPS 1.114 + { Arrays.asList(mrjarAllCaps.toString()), 1.115 + Arrays.asList(mrjarAllCaps)}, 1.116 + // non-mrjar arg 1.117 + { Arrays.asList(nonMRjar.toString()), 1.118 + Collections.emptyList()} 1.119 + }; 1.120 + } else { 1.121 + System.out.println("Non-English language \""+ language + 1.122 + "\"; test passes superficially"); 1.123 + return new Object[][]{}; 1.124 + } 1.125 + } 1.126 + 1.127 + /* Run jdeps with the arguments given in 'args', and confirm that a warning 1.128 + * is issued for each Multi-Release jar in 'expectedMRpaths'. 1.129 + */ 1.130 + @Test(dataProvider="provider") 1.131 + public void checkWarning(List<String> args, List<Path> expectedMRpaths) { 1.132 + StringWriter sw = new StringWriter(); 1.133 + PrintWriter pw = new PrintWriter(sw); 1.134 + 1.135 + int rc = com.sun.tools.jdeps.Main.run(args.toArray(new String[args.size()]), pw); 1.136 + pw.close(); 1.137 + 1.138 + expectedMRJars(sw.toString(), expectedMRpaths); 1.139 + Assert.assertEquals(rc, 0, "non-zero exit code from jdeps"); 1.140 + } 1.141 + 1.142 + /* Confirm that warnings for the specified paths are in the output (or that 1.143 + * warnings are absent if 'paths' is empty). 1.144 + * Doesn't check for extra, unexpected warnings. 1.145 + */ 1.146 + private static void expectedMRJars(String output, List<Path> paths) { 1.147 + if (paths.isEmpty()) { 1.148 + Assert.assertFalse(output.contains(WARNING), 1.149 + "Expected no mrjars, but found:\n" + output); 1.150 + } else { 1.151 + for (Path path : paths) { 1.152 + String expect = "Warning: " + path.toString() + WARNING; 1.153 + Assert.assertTrue(output.contains(expect), 1.154 + "Did not find:\n" + expect + "\nin:\n" + output + "\n"); 1.155 + } 1.156 + } 1.157 + } 1.158 + 1.159 + /* Build a jar at the expected path, containing the given attributes */ 1.160 + private static void build(Path path, Attributes attributes) throws IOException { 1.161 + try (OutputStream os = Files.newOutputStream(path); 1.162 + JarOutputStream jos = new JarOutputStream(os)) { 1.163 + 1.164 + JarEntry me = new JarEntry("META-INF/MANIFEST.MF"); 1.165 + jos.putNextEntry(me); 1.166 + Manifest manifest = new Manifest(); 1.167 + manifest.getMainAttributes().putAll(attributes); 1.168 + manifest.write(jos); 1.169 + jos.closeEntry(); 1.170 + } 1.171 + } 1.172 +}