test/tools/jdeps/MRJarWarning.java

changeset 3389
e2abef6f10b9
parent 3368
f206126308bc
equal deleted inserted replaced
3388:e4d2d5a018e3 3389:e2abef6f10b9
1 /*
2 * Copyright (c) 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 8176329
27 * @summary Test for jdeps warning when it encounters a multi-release jar
28 * @run testng MRJarWarning
29 */
30
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.io.PrintWriter;
34 import java.io.StringWriter;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.jar.Attributes;
43 import java.util.jar.JarEntry;
44 import java.util.jar.JarOutputStream;
45 import java.util.jar.Manifest;
46 import org.testng.Assert;
47 import org.testng.annotations.BeforeSuite;
48 import org.testng.annotations.DataProvider;
49 import org.testng.annotations.Test;
50
51 public class MRJarWarning {
52 private static final String WARNING = " is a multi-release jar file";
53 private static final String MRJAR_ATTR = "Multi-Release";
54
55 Path mrjar1;
56 Path mrjar2;
57 Path nonMRjar;
58 Path mrjarAllCaps;
59
60 private Attributes defaultAttributes;
61
62 @BeforeSuite
63 public void setup() throws IOException {
64 defaultAttributes = new Attributes();
65 defaultAttributes.putValue("Manifest-Version", "1.0");
66 defaultAttributes.putValue("Created-By", "1.8.0-internal");
67
68 mrjar1 = Paths.get("mrjar1.jar");
69 mrjar2 = Paths.get("mrjar2.jar");
70 nonMRjar = Paths.get("nonMRjar.jar");
71 mrjarAllCaps = Paths.get("mrjarAllCaps.jar");
72
73 Attributes mrJarAttrs = new Attributes(defaultAttributes);
74 mrJarAttrs.putValue(MRJAR_ATTR, "true");
75
76 build(mrjar1, mrJarAttrs);
77 build(mrjar2, mrJarAttrs);
78 build(nonMRjar, defaultAttributes);
79
80 // JEP 238 - "Multi-Release JAR Files" states that the attribute name
81 // and value are case insensitive. Try with all caps to ensure that
82 // jdeps still recognizes a multi-release jar.
83 Attributes allCapsAttrs = new Attributes(defaultAttributes);
84 allCapsAttrs.putValue(MRJAR_ATTR.toUpperCase(), "TRUE");
85 build(mrjarAllCaps, allCapsAttrs);
86 }
87
88 @DataProvider(name="provider")
89 private Object[][] args() {
90 // jdeps warning messages may be localized.
91 // This test only checks for the English version. Return an empty
92 // array (skip testing) if the default language is not English.
93 String language = Locale.getDefault().getLanguage();
94 System.out.println("Language: " + language);
95
96 if ("en".equals(language)) {
97 return new Object[][] {
98 // one mrjar arg
99 { Arrays.asList(mrjar1.toString()),
100 Arrays.asList(mrjar1)},
101 // two mrjar args
102 { Arrays.asList(mrjar1.toString(), mrjar2.toString()),
103 Arrays.asList(mrjar1, mrjar2)},
104 // one mrjar arg, with mrjar on classpath
105 { Arrays.asList("-cp", mrjar2.toString(), mrjar1.toString()),
106 Arrays.asList(mrjar1, mrjar2)},
107 // non-mrjar arg, with mrjar on classpath
108 { Arrays.asList("-cp", mrjar1.toString(), nonMRjar.toString()),
109 Arrays.asList(mrjar1)},
110 // mrjar arg with jar attribute name/value in ALL CAPS
111 { Arrays.asList(mrjarAllCaps.toString()),
112 Arrays.asList(mrjarAllCaps)},
113 // non-mrjar arg
114 { Arrays.asList(nonMRjar.toString()),
115 Collections.emptyList()}
116 };
117 } else {
118 System.out.println("Non-English language \""+ language +
119 "\"; test passes superficially");
120 return new Object[][]{};
121 }
122 }
123
124 /* Run jdeps with the arguments given in 'args', and confirm that a warning
125 * is issued for each Multi-Release jar in 'expectedMRpaths'.
126 */
127 @Test(dataProvider="provider")
128 public void checkWarning(List<String> args, List<Path> expectedMRpaths) {
129 StringWriter sw = new StringWriter();
130 PrintWriter pw = new PrintWriter(sw);
131
132 int rc = com.sun.tools.jdeps.Main.run(args.toArray(new String[args.size()]), pw);
133 pw.close();
134
135 expectedMRJars(sw.toString(), expectedMRpaths);
136 Assert.assertEquals(rc, 0, "non-zero exit code from jdeps");
137 }
138
139 /* Confirm that warnings for the specified paths are in the output (or that
140 * warnings are absent if 'paths' is empty).
141 * Doesn't check for extra, unexpected warnings.
142 */
143 private static void expectedMRJars(String output, List<Path> paths) {
144 if (paths.isEmpty()) {
145 Assert.assertFalse(output.contains(WARNING),
146 "Expected no mrjars, but found:\n" + output);
147 } else {
148 for (Path path : paths) {
149 String expect = "Warning: " + path.toString() + WARNING;
150 Assert.assertTrue(output.contains(expect),
151 "Did not find:\n" + expect + "\nin:\n" + output + "\n");
152 }
153 }
154 }
155
156 /* Build a jar at the expected path, containing the given attributes */
157 private static void build(Path path, Attributes attributes) throws IOException {
158 try (OutputStream os = Files.newOutputStream(path);
159 JarOutputStream jos = new JarOutputStream(os)) {
160
161 JarEntry me = new JarEntry("META-INF/MANIFEST.MF");
162 jos.putNextEntry(me);
163 Manifest manifest = new Manifest();
164 manifest.getMainAttributes().putAll(attributes);
165 manifest.write(jos);
166 jos.closeEntry();
167 }
168 }
169 }

mercurial