test/tools/jdeps/MRJarWarning.java

Thu, 27 Apr 2017 16:18:18 -0700

author
bchristi
date
Thu, 27 Apr 2017 16:18:18 -0700
changeset 3389
e2abef6f10b9
parent 3368
f206126308bc
permissions
-rw-r--r--

8176329: jdeps to detect MR jar file and output a warning
Reviewed-by: mchung

bchristi@3368 1 /*
bchristi@3368 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
bchristi@3368 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bchristi@3368 4 *
bchristi@3368 5 * This code is free software; you can redistribute it and/or modify it
bchristi@3368 6 * under the terms of the GNU General Public License version 2 only, as
bchristi@3368 7 * published by the Free Software Foundation.
bchristi@3368 8 *
bchristi@3368 9 * This code is distributed in the hope that it will be useful, but WITHOUT
bchristi@3368 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bchristi@3368 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
bchristi@3368 12 * version 2 for more details (a copy is included in the LICENSE file that
bchristi@3368 13 * accompanied this code).
bchristi@3368 14 *
bchristi@3368 15 * You should have received a copy of the GNU General Public License version
bchristi@3368 16 * 2 along with this work; if not, write to the Free Software Foundation,
bchristi@3368 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bchristi@3368 18 *
bchristi@3368 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bchristi@3368 20 * or visit www.oracle.com if you need additional information or have any
bchristi@3368 21 * questions.
bchristi@3368 22 */
bchristi@3368 23
bchristi@3368 24 /*
bchristi@3368 25 * @test
bchristi@3368 26 * @bug 8176329
bchristi@3368 27 * @summary Test for jdeps warning when it encounters a multi-release jar
bchristi@3368 28 * @run testng MRJarWarning
bchristi@3368 29 */
bchristi@3368 30
bchristi@3368 31 import java.io.IOException;
bchristi@3368 32 import java.io.OutputStream;
bchristi@3368 33 import java.io.PrintWriter;
bchristi@3368 34 import java.io.StringWriter;
bchristi@3368 35 import java.nio.file.Files;
bchristi@3368 36 import java.nio.file.Path;
bchristi@3368 37 import java.nio.file.Paths;
bchristi@3368 38 import java.util.Arrays;
bchristi@3368 39 import java.util.Collections;
bchristi@3368 40 import java.util.List;
bchristi@3368 41 import java.util.Locale;
bchristi@3368 42 import java.util.jar.Attributes;
bchristi@3368 43 import java.util.jar.JarEntry;
bchristi@3368 44 import java.util.jar.JarOutputStream;
bchristi@3368 45 import java.util.jar.Manifest;
bchristi@3368 46 import org.testng.Assert;
bchristi@3368 47 import org.testng.annotations.BeforeSuite;
bchristi@3368 48 import org.testng.annotations.DataProvider;
bchristi@3368 49 import org.testng.annotations.Test;
bchristi@3368 50
bchristi@3368 51 public class MRJarWarning {
bchristi@3368 52 private static final String WARNING = " is a multi-release jar file";
bchristi@3368 53 private static final String MRJAR_ATTR = "Multi-Release";
bchristi@3368 54
bchristi@3368 55 Path mrjar1;
bchristi@3368 56 Path mrjar2;
bchristi@3368 57 Path nonMRjar;
bchristi@3368 58 Path mrjarAllCaps;
bchristi@3368 59
bchristi@3368 60 private Attributes defaultAttributes;
bchristi@3368 61
bchristi@3368 62 @BeforeSuite
bchristi@3368 63 public void setup() throws IOException {
bchristi@3368 64 defaultAttributes = new Attributes();
bchristi@3368 65 defaultAttributes.putValue("Manifest-Version", "1.0");
bchristi@3368 66 defaultAttributes.putValue("Created-By", "1.8.0-internal");
bchristi@3368 67
bchristi@3368 68 mrjar1 = Paths.get("mrjar1.jar");
bchristi@3368 69 mrjar2 = Paths.get("mrjar2.jar");
bchristi@3368 70 nonMRjar = Paths.get("nonMRjar.jar");
bchristi@3368 71 mrjarAllCaps = Paths.get("mrjarAllCaps.jar");
bchristi@3368 72
bchristi@3368 73 Attributes mrJarAttrs = new Attributes(defaultAttributes);
bchristi@3368 74 mrJarAttrs.putValue(MRJAR_ATTR, "true");
bchristi@3368 75
bchristi@3368 76 build(mrjar1, mrJarAttrs);
bchristi@3368 77 build(mrjar2, mrJarAttrs);
bchristi@3368 78 build(nonMRjar, defaultAttributes);
bchristi@3368 79
bchristi@3368 80 // JEP 238 - "Multi-Release JAR Files" states that the attribute name
bchristi@3368 81 // and value are case insensitive. Try with all caps to ensure that
bchristi@3368 82 // jdeps still recognizes a multi-release jar.
bchristi@3368 83 Attributes allCapsAttrs = new Attributes(defaultAttributes);
bchristi@3368 84 allCapsAttrs.putValue(MRJAR_ATTR.toUpperCase(), "TRUE");
bchristi@3368 85 build(mrjarAllCaps, allCapsAttrs);
bchristi@3368 86 }
bchristi@3368 87
bchristi@3368 88 @DataProvider(name="provider")
bchristi@3368 89 private Object[][] args() {
bchristi@3368 90 // jdeps warning messages may be localized.
bchristi@3368 91 // This test only checks for the English version. Return an empty
bchristi@3368 92 // array (skip testing) if the default language is not English.
bchristi@3368 93 String language = Locale.getDefault().getLanguage();
bchristi@3368 94 System.out.println("Language: " + language);
bchristi@3368 95
bchristi@3368 96 if ("en".equals(language)) {
bchristi@3368 97 return new Object[][] {
bchristi@3368 98 // one mrjar arg
bchristi@3368 99 { Arrays.asList(mrjar1.toString()),
bchristi@3368 100 Arrays.asList(mrjar1)},
bchristi@3368 101 // two mrjar args
bchristi@3368 102 { Arrays.asList(mrjar1.toString(), mrjar2.toString()),
bchristi@3368 103 Arrays.asList(mrjar1, mrjar2)},
bchristi@3368 104 // one mrjar arg, with mrjar on classpath
bchristi@3368 105 { Arrays.asList("-cp", mrjar2.toString(), mrjar1.toString()),
bchristi@3368 106 Arrays.asList(mrjar1, mrjar2)},
bchristi@3368 107 // non-mrjar arg, with mrjar on classpath
bchristi@3368 108 { Arrays.asList("-cp", mrjar1.toString(), nonMRjar.toString()),
bchristi@3368 109 Arrays.asList(mrjar1)},
bchristi@3368 110 // mrjar arg with jar attribute name/value in ALL CAPS
bchristi@3368 111 { Arrays.asList(mrjarAllCaps.toString()),
bchristi@3368 112 Arrays.asList(mrjarAllCaps)},
bchristi@3368 113 // non-mrjar arg
bchristi@3368 114 { Arrays.asList(nonMRjar.toString()),
bchristi@3368 115 Collections.emptyList()}
bchristi@3368 116 };
bchristi@3368 117 } else {
bchristi@3368 118 System.out.println("Non-English language \""+ language +
bchristi@3368 119 "\"; test passes superficially");
bchristi@3368 120 return new Object[][]{};
bchristi@3368 121 }
bchristi@3368 122 }
bchristi@3368 123
bchristi@3368 124 /* Run jdeps with the arguments given in 'args', and confirm that a warning
bchristi@3368 125 * is issued for each Multi-Release jar in 'expectedMRpaths'.
bchristi@3368 126 */
bchristi@3368 127 @Test(dataProvider="provider")
bchristi@3368 128 public void checkWarning(List<String> args, List<Path> expectedMRpaths) {
bchristi@3368 129 StringWriter sw = new StringWriter();
bchristi@3368 130 PrintWriter pw = new PrintWriter(sw);
bchristi@3368 131
bchristi@3368 132 int rc = com.sun.tools.jdeps.Main.run(args.toArray(new String[args.size()]), pw);
bchristi@3368 133 pw.close();
bchristi@3368 134
bchristi@3368 135 expectedMRJars(sw.toString(), expectedMRpaths);
bchristi@3368 136 Assert.assertEquals(rc, 0, "non-zero exit code from jdeps");
bchristi@3368 137 }
bchristi@3368 138
bchristi@3368 139 /* Confirm that warnings for the specified paths are in the output (or that
bchristi@3368 140 * warnings are absent if 'paths' is empty).
bchristi@3368 141 * Doesn't check for extra, unexpected warnings.
bchristi@3368 142 */
bchristi@3368 143 private static void expectedMRJars(String output, List<Path> paths) {
bchristi@3368 144 if (paths.isEmpty()) {
bchristi@3368 145 Assert.assertFalse(output.contains(WARNING),
bchristi@3368 146 "Expected no mrjars, but found:\n" + output);
bchristi@3368 147 } else {
bchristi@3368 148 for (Path path : paths) {
bchristi@3368 149 String expect = "Warning: " + path.toString() + WARNING;
bchristi@3368 150 Assert.assertTrue(output.contains(expect),
bchristi@3368 151 "Did not find:\n" + expect + "\nin:\n" + output + "\n");
bchristi@3368 152 }
bchristi@3368 153 }
bchristi@3368 154 }
bchristi@3368 155
bchristi@3368 156 /* Build a jar at the expected path, containing the given attributes */
bchristi@3368 157 private static void build(Path path, Attributes attributes) throws IOException {
bchristi@3368 158 try (OutputStream os = Files.newOutputStream(path);
bchristi@3368 159 JarOutputStream jos = new JarOutputStream(os)) {
bchristi@3368 160
bchristi@3368 161 JarEntry me = new JarEntry("META-INF/MANIFEST.MF");
bchristi@3368 162 jos.putNextEntry(me);
bchristi@3368 163 Manifest manifest = new Manifest();
bchristi@3368 164 manifest.getMainAttributes().putAll(attributes);
bchristi@3368 165 manifest.write(jos);
bchristi@3368 166 jos.closeEntry();
bchristi@3368 167 }
bchristi@3368 168 }
bchristi@3368 169 }

mercurial