Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/jdeps/MRJarWarning.java
32285 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 817632926* @summary Test for jdeps warning when it encounters a multi-release jar27* @run testng MRJarWarning28*/2930import java.io.IOException;31import java.io.OutputStream;32import java.io.PrintWriter;33import java.io.StringWriter;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.util.Arrays;38import java.util.Collections;39import java.util.List;40import java.util.Locale;41import java.util.jar.Attributes;42import java.util.jar.JarEntry;43import java.util.jar.JarOutputStream;44import java.util.jar.Manifest;45import org.testng.Assert;46import org.testng.annotations.BeforeSuite;47import org.testng.annotations.DataProvider;48import org.testng.annotations.Test;4950public class MRJarWarning {51private static final String WARNING = " is a multi-release jar file";52private static final String MRJAR_ATTR = "Multi-Release";5354Path mrjar1;55Path mrjar2;56Path nonMRjar;57Path mrjarAllCaps;5859private Attributes defaultAttributes;6061@BeforeSuite62public void setup() throws IOException {63defaultAttributes = new Attributes();64defaultAttributes.putValue("Manifest-Version", "1.0");65defaultAttributes.putValue("Created-By", "1.8.0-internal");6667mrjar1 = Paths.get("mrjar1.jar");68mrjar2 = Paths.get("mrjar2.jar");69nonMRjar = Paths.get("nonMRjar.jar");70mrjarAllCaps = Paths.get("mrjarAllCaps.jar");7172Attributes mrJarAttrs = new Attributes(defaultAttributes);73mrJarAttrs.putValue(MRJAR_ATTR, "true");7475build(mrjar1, mrJarAttrs);76build(mrjar2, mrJarAttrs);77build(nonMRjar, defaultAttributes);7879// JEP 238 - "Multi-Release JAR Files" states that the attribute name80// and value are case insensitive. Try with all caps to ensure that81// jdeps still recognizes a multi-release jar.82Attributes allCapsAttrs = new Attributes(defaultAttributes);83allCapsAttrs.putValue(MRJAR_ATTR.toUpperCase(), "TRUE");84build(mrjarAllCaps, allCapsAttrs);85}8687@DataProvider(name="provider")88private Object[][] args() {89// jdeps warning messages may be localized.90// This test only checks for the English version. Return an empty91// array (skip testing) if the default language is not English.92String language = Locale.getDefault().getLanguage();93System.out.println("Language: " + language);9495if ("en".equals(language)) {96return new Object[][] {97// one mrjar arg98{ Arrays.asList(mrjar1.toString()),99Arrays.asList(mrjar1)},100// two mrjar args101{ Arrays.asList(mrjar1.toString(), mrjar2.toString()),102Arrays.asList(mrjar1, mrjar2)},103// one mrjar arg, with mrjar on classpath104{ Arrays.asList("-cp", mrjar2.toString(), mrjar1.toString()),105Arrays.asList(mrjar1, mrjar2)},106// non-mrjar arg, with mrjar on classpath107{ Arrays.asList("-cp", mrjar1.toString(), nonMRjar.toString()),108Arrays.asList(mrjar1)},109// mrjar arg with jar attribute name/value in ALL CAPS110{ Arrays.asList(mrjarAllCaps.toString()),111Arrays.asList(mrjarAllCaps)},112// non-mrjar arg113{ Arrays.asList(nonMRjar.toString()),114Collections.emptyList()}115};116} else {117System.out.println("Non-English language \""+ language +118"\"; test passes superficially");119return new Object[][]{};120}121}122123/* Run jdeps with the arguments given in 'args', and confirm that a warning124* is issued for each Multi-Release jar in 'expectedMRpaths'.125*/126@Test(dataProvider="provider")127public void checkWarning(List<String> args, List<Path> expectedMRpaths) {128StringWriter sw = new StringWriter();129PrintWriter pw = new PrintWriter(sw);130131int rc = com.sun.tools.jdeps.Main.run(args.toArray(new String[args.size()]), pw);132pw.close();133134expectedMRJars(sw.toString(), expectedMRpaths);135Assert.assertEquals(rc, 0, "non-zero exit code from jdeps");136}137138/* Confirm that warnings for the specified paths are in the output (or that139* warnings are absent if 'paths' is empty).140* Doesn't check for extra, unexpected warnings.141*/142private static void expectedMRJars(String output, List<Path> paths) {143if (paths.isEmpty()) {144Assert.assertFalse(output.contains(WARNING),145"Expected no mrjars, but found:\n" + output);146} else {147for (Path path : paths) {148String expect = "Warning: " + path.toString() + WARNING;149Assert.assertTrue(output.contains(expect),150"Did not find:\n" + expect + "\nin:\n" + output + "\n");151}152}153}154155/* Build a jar at the expected path, containing the given attributes */156private static void build(Path path, Attributes attributes) throws IOException {157try (OutputStream os = Files.newOutputStream(path);158JarOutputStream jos = new JarOutputStream(os)) {159160JarEntry me = new JarEntry("META-INF/MANIFEST.MF");161jos.putNextEntry(me);162Manifest manifest = new Manifest();163manifest.getMainAttributes().putAll(attributes);164manifest.write(jos);165jos.closeEntry();166}167}168}169170171