Skip to content

Commit 2886a39

Browse files
committedSep 10, 2024
8293877: Rewrite MineField test
Reviewed-by: mbaesken Backport-of: e137f9f2f0e4244307900cd0eadceb9b773e9858
1 parent fb79714 commit 2886a39

15 files changed

+1932
-1277
lines changed
 

‎test/langtools/tools/javac/Paths/Class-Path.sh

-198
This file was deleted.

‎test/langtools/tools/javac/Paths/Class-Path2.sh

-111
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* Copyright (c) 2003, 2022, 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+
/*
26+
* @test
27+
* @bug 4212732 8293877
28+
* @summary Test handling of the Class-Path attribute in jar file manifests
29+
* @library /tools/lib
30+
* @build toolbox.ToolBox Util ClassPath
31+
* @run main ClassPath
32+
*/
33+
34+
35+
/*
36+
* Converted from Class-Path.sh, originally written by Martin Buchholz.
37+
*
38+
* For the last version of the original, Class-Path.sh, see
39+
* https://github.com/openjdk/jdk/blob/jdk-19%2B36/test/langtools/tools/javac/Paths/Class-Path.sh
40+
*
41+
* This class primarily tests that the Class-Path attribute in jar files
42+
* is handled the same way by javac and java. It also has various tests
43+
* of the jar tool itself.
44+
*/
45+
46+
import java.io.IOException;
47+
import java.nio.file.Files;
48+
import java.nio.file.Path;
49+
import java.nio.file.StandardOpenOption;
50+
import java.util.List;
51+
52+
public class ClassPath extends Util {
53+
public static void main(String... args) throws Exception {
54+
new ClassPath().run(args);
55+
}
56+
57+
void run(String... args) throws Exception {
58+
setup();
59+
tests();
60+
cleanup();
61+
bottomLine();
62+
}
63+
64+
void setup() throws Exception {
65+
cleanup();
66+
tb.createDirectories("pkg");
67+
68+
/*----------------------------------------------------------------
69+
* Create mutually referential jar files
70+
*----------------------------------------------------------------*/
71+
72+
Files.writeString(Path.of("pkg/A.java"), """
73+
package pkg;
74+
import pkg.B;
75+
public class A {
76+
public static int f() { return B.g(); }
77+
public static int g() { return 0; }
78+
}
79+
""");
80+
Files.writeString(Path.of("pkg/B.java"), """
81+
package pkg;
82+
import pkg.A;
83+
public class B {
84+
public static int f() { return A.g(); }
85+
public static int g() { return 0; }
86+
}
87+
""");
88+
89+
javac("pkg/A.java", "pkg/B.java");
90+
91+
makeManifestWithClassPath("B.zip");
92+
jar("cmf", "MANIFEST.MF", "A.jar", "pkg/A.class");
93+
94+
makeManifestWithClassPath("A.jar");
95+
jar("cmf", "MANIFEST.MF", "B.zip", "pkg/B.class");
96+
97+
Files.writeString(Path.of("Main.java"), """
98+
import pkg.*;
99+
public class Main {
100+
public static void main(String[] a) { System.exit(A.f() + B.f()); }
101+
}
102+
""");
103+
}
104+
105+
void cleanup() throws IOException {
106+
deleteFiles("pkg", "Main.java", "Main.class", "Main.jar", "jars");
107+
deleteFiles("MANIFEST.MF", "A.jar", "B.zip");
108+
}
109+
110+
void tests() throws Exception {
111+
expectPass(JAVAC, "-cp A.jar Main.java");
112+
expectPass(JAVAC, "-cp B.zip Main.java");
113+
expectPass(JAVA, "-cp A.jar${PS}. Main");
114+
expectPass(JAVA, "-cp B.zip${PS}. Main");
115+
116+
/*----------------------------------------------------------------
117+
* Jar file Class-Path expanded only for jars found on user class path
118+
*----------------------------------------------------------------*/
119+
120+
tb.createDirectories("jars");
121+
moveFiles(List.of("A.jar", "B.zip"), "jars/.");
122+
123+
expectPass(JAVAC, "-cp jars/A.jar Main.java");
124+
expectPass(JAVA, "-cp jars/A.jar${PS}. Main");
125+
126+
expectPass(JAVAC, "-cp jars/B.zip Main.java");
127+
expectPass(JAVA, "-cp jars/B.zip${PS}. Main");
128+
129+
expectFail(JAVA, "-Xbootclasspath/p:jars/A.jar -cp . Main");
130+
expectFail(JAVA, "-Xbootclasspath/a:jars/B.zip -cp . Main");
131+
expectFail(JAVAC, "-Xbootclasspath/p:jars/A.jar -cp None Main.java");
132+
expectFail(JAVAC, "-Xbootclasspath/a:jars/B.zip -cp None Main.java");
133+
moveFiles(List.of("jars/A.jar", "jars/B.zip"), ".");
134+
135+
makeManifestWithClassPath("A.jar");
136+
Files.writeString(Path.of("MANIFEST.MF"), "Main-Class: Main\n", StandardOpenOption.APPEND);
137+
jar("cmf", "MANIFEST.MF", "Main.jar", "Main.class");
138+
139+
expectPass(JAVA, "-jar Main.jar");
140+
141+
makeManifestWithClassPath(".");
142+
jar("cmf", "MANIFEST.MF", "A.jar", "pkg/A.class");
143+
144+
expectPass(JAVAC, "-cp A.jar Main.java");
145+
expectPass(JAVA, "-jar Main.jar");
146+
147+
makeManifestWithClassPath("");
148+
jar("cmf", "MANIFEST.MF", "A.jar", "pkg/A.class");
149+
150+
expectFail(JAVAC, "-cp A.jar Main.java");
151+
expectFail(JAVA, "-jar Main.jar");
152+
153+
/*----------------------------------------------------------------
154+
* Test new flag -e (application entry point)
155+
*----------------------------------------------------------------*/
156+
157+
Files.writeString(Path.of("Hello.java"), """
158+
import pkg.*;
159+
public class Hello {
160+
public static void main(String[] a) { System.out.println("Hello World!"); }
161+
}
162+
""");
163+
164+
Files.writeString(Path.of("Bye.java"), """
165+
import pkg.*;
166+
public class Bye {
167+
public static void main(String[] a) { System.out.println("Good Bye!"); }
168+
}
169+
""");
170+
171+
// Set an empty classpath to override any inherited setting of CLASSPATH
172+
expectPass(classpath(""), JAVAC, "Hello.java Bye.java");
173+
174+
// test jar creation without manifest
175+
//
176+
expectPass(JAR, "cfe Hello.jar Hello Hello.class");
177+
expectPass(JAVA, "-jar Hello.jar");
178+
179+
// test for overriding the manifest during jar creation
180+
//
181+
Files.writeString(Path.of("MANIFEST.MF"), "Main-Class: Hello\n", StandardOpenOption.APPEND);
182+
183+
// test for error: " 'e' flag and manifest with the 'Main-Class'
184+
// attribute cannot be specified together, during creation
185+
expectFail(JAR, "cmfe MANIFEST.MF Bye.jar Bye Bye.class");
186+
187+
// test for overriding the manifest when updating the jar
188+
//
189+
expectPass(JAR, "cfe greetings.jar Hello Hello.class");
190+
expectPass(JAR, "ufe greetings.jar Bye Bye.class");
191+
expectPass(JAVA, "-jar greetings.jar");
192+
193+
// test for error: " 'e' flag and manifest with the 'Main-Class'
194+
// attribute cannot be specified together, during update
195+
expectFail(JAR, "umfe MANIFEST.MF greetings.jar Hello");
196+
197+
// test jar update when there are no input files
198+
expectPass(JAR, "ufe Hello.jar Bye");
199+
expectFail(JAVA, "-jar Hello.jar");
200+
expectPass(JAR, "umf MANIFEST.MF Hello.jar");
201+
202+
// test creating jar when the to-be-archived files
203+
// do not contain the specified main class, there is no check done
204+
// for the presence of the main class, so the test will pass
205+
//
206+
expectPass(JAR, "cfe Hello.jar Hello Bye.class");
207+
208+
// Jar creation and update when there is no manifest and inputfiles
209+
// specified
210+
expectFail(JAR, "cvf A.jar");
211+
expectFail(JAR, "uvf A.jar");
212+
213+
// error: no such file or directory
214+
expectFail(JAR, "cvf A.jar non-existing.file");
215+
expectFail(JAR, "uvf A.jar non-existing.file");
216+
217+
}
218+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2003, 2022, 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 4212732 6485027 8293877
27+
* @summary Test handling of the Class-Path attribute in jar file manifests
28+
* @library /tools/lib
29+
* @build toolbox.ToolBox Util ClassPath
30+
* @run main ClassPath2
31+
*/
32+
33+
/*
34+
* Converted from Class-Path2.sh, originally written by Martin Buchholz.
35+
*
36+
* For the last version of the original, Class-Path2.sh, see
37+
* https://github.com/openjdk/jdk/blob/jdk-19%2B36/test/langtools/tools/javac/Paths/Class-Path2.sh
38+
*
39+
* This class provides additional tests for the Class-Path attribute in jar
40+
* files, when the entries are not in the same directory.
41+
*/
42+
43+
44+
import java.io.IOException;
45+
import java.nio.file.Files;
46+
import java.nio.file.Path;
47+
48+
public class ClassPath2 extends Util {
49+
public static void main(String... args) throws Exception {
50+
new ClassPath2().run(args);
51+
}
52+
53+
void run(String... args) throws Exception {
54+
setup();
55+
tests();
56+
cleanup();
57+
bottomLine();
58+
}
59+
60+
void setup() throws Exception {
61+
cleanup();
62+
63+
tb.createDirectories("pkg");
64+
65+
/*----------------------------------------------------------------
66+
* Create mutually referential jar files
67+
*----------------------------------------------------------------*/
68+
69+
Files.writeString(Path.of("pkg/A.java"), """
70+
package pkg;
71+
import pkg.B;
72+
public class A {
73+
public static int f() { return B.g(); }
74+
public static int g() { return 0; }
75+
}
76+
""");
77+
Files.writeString(Path.of("pkg/B.java"), """
78+
package pkg;
79+
import pkg.A;
80+
public class B {
81+
public static int f() { return A.g(); }
82+
public static int g() { return 0; }
83+
}
84+
""");
85+
86+
javac("pkg/A.java", "pkg/B.java");
87+
88+
makeManifestWithClassPath("./sub/B.zip");
89+
jar("cmf", "MANIFEST.MF", "A.jar", "pkg/A.class");
90+
91+
makeManifestWithClassPath("../A.jar");
92+
jar("cmf", "MANIFEST.MF", "B.zip", "pkg/B.class");
93+
94+
Files.writeString(Path.of("Main.java"), """
95+
import pkg.*;
96+
public class Main {
97+
public static void main(String[] a) { System.exit(A.f() + B.f()); }
98+
}
99+
""");
100+
101+
deleteFiles("pkg");
102+
tb.createDirectories("jars");
103+
tb.createDirectories("jars/sub");
104+
tb.moveFile("A.jar", "jars/.");
105+
tb.moveFile("B.zip", "jars/sub/.");
106+
}
107+
108+
void cleanup() throws IOException {
109+
deleteFiles("pkg", "Main.java", "Main.class", "Main.jar", "jars");
110+
deleteFiles("MANIFEST.MF", "A.jar", "B.zip");
111+
}
112+
113+
void tests() throws Exception {
114+
115+
/*
116+
* Test 1: Compiling
117+
*/
118+
119+
expectPass(JAVAC, "-cp jars/A.jar Main.java");
120+
expectPass(JAVA, "-cp jars/A.jar${PS}. Main");
121+
122+
expectPass(JAVAC, "-cp jars/sub/B.zip Main.java");
123+
expectPass(JAVA, "-cp jars/sub/B.zip${PS}. Main");
124+
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
/*
2+
* Copyright (c) 2003, 2022, 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 4884487 6295519 6236704 6429613 8293877
27+
* @summary Test for proper diagnostics during path manipulation operations
28+
* @library /tools/lib
29+
* @build toolbox.ToolBox Util Diagnostics
30+
* @run main Diagnostics
31+
*/
32+
33+
34+
/*
35+
* Converted from Diagnostics.sh, originally written by Martin Buchholz.
36+
*
37+
* For the last version of the original, Diagnostics.sh, see
38+
* https://github.com/openjdk/jdk/blob/jdk-19%2B36/test/langtools/tools/javac/Paths/Diagnostics.sh
39+
*
40+
* This class primarily tests that javac generates warnings or errors
41+
* as appropriate for various input conditions.
42+
*
43+
* Note: only the {@code warning:} or {@code error:} prefixes are checked,
44+
* and not the subsequent text of the diagnostic.
45+
*/
46+
47+
import java.io.IOException;
48+
import java.nio.file.Files;
49+
import java.nio.file.Path;
50+
import java.util.Locale;
51+
52+
public class Diagnostics extends Util {
53+
public static void main(String... args) throws Exception {
54+
new Diagnostics().run(args);
55+
}
56+
57+
void run(String... args) throws Exception{
58+
setup();
59+
60+
Locale prev = Locale.getDefault();
61+
Locale.setDefault(Locale.US); // diagnostics in English, please!
62+
try {
63+
tests();
64+
} finally {
65+
Locale.setDefault(prev);
66+
}
67+
68+
cleanup();
69+
bottomLine();
70+
}
71+
72+
void setup() throws IOException {
73+
cleanup();
74+
Files.writeString(Path.of("Main.java"), "public class Main{public static void main(String[]a){}}");
75+
}
76+
77+
void cleanup() throws IOException {
78+
deleteFiles("Main.java", "Main.class");
79+
deleteFiles("classes", "classes.foo", "classes.jar", "classes.war", "classes.zip");
80+
deleteFiles("MANIFEST.MF", "classesRef.jar", "classesRefRef.jar", "jars");
81+
}
82+
83+
void tests() throws Exception {
84+
/*----------------------------------------------------------------
85+
* No warnings unless -Xlint:path is used
86+
*----------------------------------------------------------------*/
87+
checkWarning(false, "Main.java");
88+
checkWarning(false, "-cp .${PS}classes Main.java");
89+
90+
/*----------------------------------------------------------------
91+
* Warn for missing elts in user-specified paths
92+
*----------------------------------------------------------------*/
93+
94+
// use --source 8 -target 8 with bootclasspath-related options
95+
String JDK8 = "-source 8 -target 8 -Xlint:-options ";
96+
checkWarning(true, "-Xlint:path -cp .${PS}classes Main.java");
97+
checkWarning(true, JDK8 + "-Xlint:path -Xbootclasspath/p:classes Main.java");
98+
checkWarning(true, JDK8 + "-Xlint -Xbootclasspath/a:classes Main.java");
99+
100+
checkWarning(true, JDK8 + "-Xlint:-options -Xlint:path -endorseddirs classes Main.java");
101+
checkWarning(true, JDK8 + "-Xlint:-options -Xlint -extdirs classes Main.java");
102+
103+
/*----------------------------------------------------------------
104+
* No warning for missing elts in "system" paths
105+
*----------------------------------------------------------------*/
106+
// TODO? there are system paths we could check, such as --module-path
107+
108+
/*----------------------------------------------------------------
109+
* No warning if class path element exists
110+
*----------------------------------------------------------------*/
111+
tb.createDirectories("classes");
112+
113+
checkWarning(false, "-Xlint:path -cp .${PS}classes Main.java");
114+
checkWarning(false, JDK8 + "-Xlint:path -endorseddirs classes Main.java");
115+
checkWarning(false, JDK8 + "-Xlint:path -extdirs classes Main.java");
116+
checkWarning(false, JDK8 + "-Xlint:path -Xbootclasspath/p:classes Main.java");
117+
checkWarning(false, JDK8 + "-Xlint:path -Xbootclasspath/a:classes Main.java");
118+
119+
jar("cf", "classes.jar", "Main.class");
120+
tb.copyFile("classes.jar", "classes.war");
121+
tb.copyFile("classes.war", "classes.zip");
122+
checkWarning(false, "-Xlint:path -cp .${PS}classes.jar Main.java");
123+
checkWarning(true, "-Xlint:path -cp .${PS}classes.war Main.java");
124+
checkWarning(false, "-Xlint:path -cp .${PS}classes.zip Main.java");
125+
126+
/*----------------------------------------------------------------
127+
* Warn if -Xlint is used and if class path element refers to
128+
* regular file which doesn't look like a zip file, but is
129+
*----------------------------------------------------------------*/
130+
tb.copyFile("classes.war", "classes.foo");
131+
checkWarning(true, "-Xlint:path -cp .${PS}classes.foo Main.java");
132+
133+
/*----------------------------------------------------------------
134+
* No error if class path element refers to regular file which is
135+
* not a zip file
136+
*----------------------------------------------------------------*/
137+
checkError(false, "-cp Main.java Main.java"); // Main.java is NOT a jar file
138+
checkError(false, "Main.java");
139+
140+
/*----------------------------------------------------------------
141+
* Warn if -Xlint is used and if class path element refers to
142+
* regular file which is not a zip file
143+
*----------------------------------------------------------------*/
144+
checkWarning(true, "-Xlint -cp Main.java Main.java"); // Main.java is NOT a jar file
145+
146+
/*----------------------------------------------------------------
147+
* Test jar file class path reference recursion
148+
*----------------------------------------------------------------*/
149+
makeManifestWithClassPath("classesRef.jar");
150+
jar("cmf", "MANIFEST.MF", "classesRefRef.jar", "Main.class");
151+
152+
/*----------------------------------------------------------------
153+
* Non-existent recursive Class-Path reference gives warning
154+
*----------------------------------------------------------------*/
155+
checkWarning(false, " -classpath classesRefRef.jar Main.java");
156+
checkWarning(true, " -Xlint -classpath classesRefRef.jar Main.java");
157+
checkWarning(false, JDK8 + "-Xlint -Xbootclasspath/p:classesRefRef.jar Main.java");
158+
159+
createBadJarFiles("classesRef.jar");
160+
161+
/*----------------------------------------------------------------
162+
* Non-jar file recursive Class-Path reference gives error
163+
*----------------------------------------------------------------*/
164+
165+
checkError(true, " -classpath classesRefRef.jar Main.java");
166+
checkError(false, JDK8 + "-Xbootclasspath/a:classesRefRef.jar Main.java");
167+
168+
makeManifestWithClassPath("classes");
169+
jar("cmf", "MANIFEST.MF", "classesRef.jar", "Main.class");
170+
171+
/*----------------------------------------------------------------
172+
* Jar file recursive Class-Path reference is OK
173+
*----------------------------------------------------------------*/
174+
checkWarning(false, " -Xlint -classpath classesRefRef.jar Main.java");
175+
checkWarning(false, JDK8 + "-Xlint -Xbootclasspath/p:classesRefRef.jar Main.java");
176+
177+
178+
/*----------------------------------------------------------------
179+
* Class-Path attribute followed in extdirs or endorseddirs
180+
*----------------------------------------------------------------*/
181+
tb.createDirectories("jars");
182+
tb.copyFile("classesRefRef.jar", "jars/.");
183+
checkWarning(true, JDK8 + "-Xlint -extdirs jars Main.java");
184+
checkWarning(true, JDK8 + "-Xlint -endorseddirs jars Main.java");
185+
186+
/*----------------------------------------------------------------
187+
* Bad Jar file in extdirs and endorseddirs should not be ignored
188+
*----------------------------------------------------------------*/
189+
createBadJarFiles("jars/classesRef.jar");
190+
checkError(true, JDK8 + "-Xlint -extdirs jars Main.java");
191+
checkError(true, JDK8 + "-Xlint -endorseddirs jars Main.java");
192+
}
193+
194+
void checkWarning(boolean expect, String args) throws Exception {
195+
Result result = javac(splitArgs(args));
196+
int exitCode = result.exitCode();
197+
if (exitCode != 0) {
198+
throw new Exception("javac failed: exit code " + exitCode);
199+
}
200+
String output = result.out();
201+
if (output.contains("warning:")) {
202+
if (!expect) {
203+
out.println("FAIL: Command 'javac " + args + "' printed an unexpected warning");
204+
failCount++;
205+
} else {
206+
passCount++;
207+
}
208+
} else {
209+
if (expect) {
210+
out.println("FAIL: Command 'javac " + args + "' did not generate the expected warning");
211+
failCount++;
212+
} else {
213+
passCount++;
214+
}
215+
}
216+
}
217+
218+
void checkError(boolean expect, String args) throws Exception {
219+
Result result = javac(splitArgs(args));
220+
int exitCode = result.exitCode();
221+
boolean ok = true;
222+
if (expect) {
223+
if (exitCode == 0) {
224+
out.println("FAIL: Command 'javac " + args + " was supposed to exit with non-zero return code");
225+
ok = false;
226+
}
227+
if (!result.out().contains("error:")) {
228+
out.println("FAIL: Command 'javac " + args + " did not generate any error message");
229+
ok = false;
230+
}
231+
} else {
232+
if (exitCode != 0) {
233+
out.println("FAIL: Command 'javac " + args + " failed with a non-zero return code");
234+
ok = false;
235+
}
236+
if (result.out().contains("error:")) {
237+
out.println("FAIL: Command 'javac " + args + " printed an unexpected error message");
238+
ok = false;
239+
}
240+
}
241+
if (ok) {
242+
passCount++;
243+
} else {
244+
failCount++;
245+
}
246+
}
247+
248+
void createBadJarFiles(String... paths) throws IOException {
249+
for (String p : paths) {
250+
Files.writeString(Path.of(p), "not a jar file\n");
251+
}
252+
}
253+
}

‎test/langtools/tools/javac/Paths/Diagnostics.sh

-210
This file was deleted.
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2003, 2022, 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+
/*
26+
* @test
27+
* @bug 4940642 8293877
28+
* @summary Check for -help and -X flags
29+
*/
30+
31+
/*
32+
* Converted from Help.sh, originally written by Martin Buchholz
33+
*
34+
* For the last version of the original, Help.sh, see
35+
* https://github.com/openjdk/jdk/blob/jdk-19%2B36/test/langtools/tools/javac/Paths/Help.sh
36+
*
37+
* This class provides rudimentary tests of the javac command-line help.
38+
*/
39+
40+
import java.io.PrintWriter;
41+
import java.io.StringWriter;
42+
import java.util.spi.ToolProvider;
43+
44+
public class Help {
45+
public static void main(String... args) throws Exception {
46+
new Help().run(args);
47+
}
48+
49+
void run(String... args) throws Exception {
50+
String helpText = javac("-help");
51+
check(helpText,
52+
"-X ", "-J", "-classpath ", "-cp ", "-bootclasspath ", "-sourcepath ");
53+
54+
String xText = javac("-X");
55+
check(xText, "-Xbootclasspath/p:");
56+
}
57+
58+
void check(String text, String... options) throws Exception {
59+
for (String opt : options) {
60+
System.err.println("Checking '" + opt + "'");
61+
if (!text.contains(opt)) {
62+
text.lines().forEach(System.err::println);
63+
throw new Exception("Bad help output");
64+
}
65+
}
66+
}
67+
68+
String javac(String... args) throws Exception {
69+
var javac = ToolProvider.findFirst("javac")
70+
.orElseThrow(() -> new Exception("cannot find javac"));
71+
try (StringWriter sw = new StringWriter();
72+
PrintWriter pw = new PrintWriter(sw)) {
73+
int rc = javac.run(pw, pw, args);
74+
if (rc != 0) {
75+
throw new Error("unexpected exit from javac: " + rc);
76+
}
77+
pw.flush();
78+
return sw.toString();
79+
}
80+
}
81+
}
82+

‎test/langtools/tools/javac/Paths/Help.sh

-69
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
/*
2+
* Copyright (c) 2003, 2022, 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+
/*
26+
* @test
27+
* @bug 4758537 4809833 8149599 8293877
28+
* @summary Test that javac and java find files in similar ways
29+
* @library /tools/lib
30+
* @build toolbox.ToolBox Util MineField
31+
* @run main MineField
32+
*/
33+
34+
/*
35+
* Converted from MineField.sh, originally written by Martin Buchholz.
36+
*
37+
* For the last version of the original, MineField.sh, see
38+
* https://github.com/openjdk/jdk/blob/jdk-19%2B36/test/langtools/tools/javac/Paths/MineField.sh
39+
*
40+
* This class primarily tests that javac and the java launcher provide
41+
* equivalent handling of all path-related options, like {@code -classpath}.
42+
*/
43+
44+
/*
45+
#----------------------------------------------------------------
46+
# The search order for classes used by both java and javac is:
47+
#
48+
# -Xbootclasspath/p:<path>
49+
# -endorseddirs <dirs> or -Djava.endorsed.dirs=<dirs> (search for jar/zip only)
50+
# -bootclasspath <path> or -Xbootclasspath:<path>
51+
# -Xbootclasspath/a:<path>
52+
# -extdirs <dirs> or -Djava.ext.dirs=<dirs> (search for jar/zip only)
53+
# -classpath <path>, -cp <path>, env CLASSPATH=<path>
54+
#
55+
# Peculiarities of the class file search:
56+
# - Empty elements of the (user) classpath default to ".",
57+
# while empty elements of other paths are ignored.
58+
# - Only for the user classpath is an empty string value equivalent to "."
59+
# - Specifying a bootclasspath on the command line obliterates any
60+
# previous -Xbootclasspath/p: or -Xbootclasspath/a: command line flags.
61+
#
62+
# JDK 9 update:
63+
# java: The java launcher does not support any of the following:
64+
# * -Xbootclasspath/p: -Xbootclasspath:
65+
# * -endorseddirs -Djava.endorsed.dirs
66+
# * -extdirs -Djava.ext.dirs
67+
# All test cases exercising these features have been removed.
68+
# javac: The following features are only supported when compiling
69+
# for older releases:
70+
# * -Xbootclasspath/p: -Xbootclasspath: -bootclasspath -Xbootclasspath/a:
71+
# * -endorseddirs -Djava.endorsed.dirs
72+
# * -extdirs -Djava.ext.dirs
73+
# All test cases exercising these features have been modified to
74+
# use -source 8 -target 8. In addition, javac test cases involving
75+
# use of the runtime properties java.endorsed.dirs and java.extdirs
76+
# (by means of -J-Dname=value) have been removed.
77+
# Although the primary purpose of the test cases in this file is to
78+
# compare javac and java behavior, some tests remain for javac for
79+
# which there is no java equivalent. However, the cases remain as useful
80+
# test cases for javac handling of the paths involved.
81+
*/
82+
83+
import java.io.IOException;
84+
import java.nio.file.Files;
85+
import java.nio.file.Path;
86+
87+
public class MineField extends Util {
88+
public static void main(String... args) throws Exception {
89+
new MineField().run(args);
90+
}
91+
92+
void run(String... args) throws Exception{
93+
setup();
94+
tests();
95+
cleanup();
96+
bottomLine();
97+
}
98+
99+
void cleanup() throws IOException {
100+
deleteFiles("GooSrc", "GooJar", "GooZip", "GooClass");
101+
deleteFiles("BadSrc", "BadJar", "BadZip", "BadClass");
102+
deleteFiles("OneDir", "Main.java", "MANIFEST.MF");
103+
deleteFiles(listFiles(Path.of("."), "*.class"));
104+
deleteFiles("java-lang.jar");
105+
}
106+
107+
/**
108+
* "Prepare the minefield".
109+
*/
110+
void setup() throws Exception {
111+
cleanup();
112+
113+
tb.createDirectories("GooSrc", "GooJar", "GooZip", "GooClass");
114+
tb.createDirectories("BadSrc", "BadJar", "BadZip", "BadClass");
115+
116+
Files.writeString(Path.of("Lib.java"),
117+
"public class Lib {public static void f(){}}");
118+
javac("Lib.java");
119+
jar("cf", "GooJar/Lib.jar", "Lib.class");
120+
jar("cf", "GooZip/Lib.zip", "Lib.class");
121+
tb.moveFile("Lib.class", "GooClass/.");
122+
tb.moveFile("Lib.java", "GooSrc/.");
123+
checkFiles("GooZip/Lib.zip", "GooJar/Lib.jar", "GooSrc/Lib.java");
124+
125+
Files.writeString(Path.of("Lib.java"),
126+
"public class Lib {/* Bad */}");
127+
javac("Lib.java");
128+
jar("cf", "BadJar/Lib.jar", "Lib.class");
129+
jar("cf", "BadZip/Lib.zip", "Lib.class");
130+
tb.moveFile("Lib.class", "BadClass/.");
131+
tb.moveFile("Lib.java", "BadSrc/.");
132+
checkFiles("BadZip/Lib.zip", "BadJar/Lib.jar", "BadSrc/Lib.java");
133+
134+
Files.writeString(Path.of("Main.java"),
135+
"public class Main {public static void main(String[] a) {Lib.f();}}");
136+
Path libModules = javaHome.resolve("lib").resolve("modules");
137+
if (Files.isReadable(libModules)) {
138+
jimage("extract", "--dir", "modules", libModules.toString());
139+
jar("cf", "java-lang.jar", "-C", "modules/java.base", "java/lang");
140+
deleteFiles("modules");
141+
} else {
142+
Path modules = javaHome.resolve("modules");
143+
if (Files.isDirectory(modules)) {
144+
jar("cf", "java-lang.jar", "-C", modules.resolve("java.base").toString(), "java/lang");
145+
} else {
146+
throw new Exception("Cannot create java-lang.jar");
147+
}
148+
}
149+
}
150+
151+
void tests() throws Exception {
152+
153+
//----------------------------------------------------------------
154+
// Verify that javac class search order is the same as java's
155+
//----------------------------------------------------------------
156+
157+
expectFail(JAVAC, """
158+
-source 8 -target 8
159+
-Xbootclasspath/p:GooClass
160+
-bootclasspath java-lang.jar${PS}BadZip/Lib.zip
161+
Main.java""");
162+
163+
expectPass(JAVAC, """
164+
-source 8 -target 8
165+
-Xbootclasspath/p:BadClass${PS}GooClass
166+
-bootclasspath java-lang.jar${PS}GooZip/Lib.zip${PS}BadClass
167+
Main.java""");
168+
169+
expectPass(JAVAC, """
170+
-source 8 -target 8
171+
-Xbootclasspath/p:BadJar/Lib.jar
172+
-Xbootclasspath:java-lang.jar${PS}GooClass
173+
Main.java""");
174+
175+
//----------------------------------------------------------------
176+
177+
expectFail(JAVAC, """
178+
-source 8 -target 8
179+
-bootclasspath java-lang.jar${PS}GooZip/Lib.zip
180+
-Xbootclasspath/p:BadClass
181+
Main.java""");
182+
183+
expectPass(JAVAC, """
184+
-source 8 -target 8
185+
-bootclasspath java-lang.jar${PS}BadZip/Lib.zip
186+
-Xbootclasspath/p:GooClass${PS}BadJar/Lib.jar
187+
Main.java""");
188+
189+
//----------------------------------------------------------------
190+
191+
expectFail(JAVAC, """
192+
-source 8 -target 8
193+
-Xbootclasspath/p:BadClass
194+
-Xbootclasspath/a:GooClass
195+
Main.java""");
196+
197+
expectPass(JAVAC, """
198+
-source 8 -target 8
199+
-Xbootclasspath/p:GooClass${PS}BadClass
200+
-Xbootclasspath/a:BadClass
201+
Main.java""");
202+
203+
expectPass(JAVA, """
204+
-Xbootclasspath/a:GooClass
205+
Main""");
206+
207+
//----------------------------------------------------------------
208+
209+
expectFail(JAVAC, """
210+
-source 8 -target 8
211+
-Xbootclasspath/p:GooClass
212+
-Xbootclasspath:BadClass${PS}java-lang.jar
213+
-Xbootclasspath/a:GooClass
214+
Main.java""");
215+
216+
expectPass(JAVAC, """
217+
-source 8 -target 8
218+
-Xbootclasspath/p:BadClass
219+
-Xbootclasspath:GooClass${PS}BadClass${PS}java-lang.jar
220+
-Xbootclasspath/a:BadClass
221+
Main.java""");
222+
223+
//----------------------------------------------------------------
224+
225+
expectPass(JAVAC, """
226+
-source 8 -target 8
227+
-endorseddirs BadClass${PS}GooZip${PS}BadJar
228+
-Xbootclasspath:"BadClass${PS}java-lang.jar
229+
Main.java""");
230+
231+
expectPass(JAVAC, """
232+
-source 8 -target 8
233+
-Djava.endorsed.dirs=BadClass${PS}GooZip${PS}BadJar
234+
-Xbootclasspath:BadClass${PS}java-lang.jar
235+
Main.java""");
236+
237+
//----------------------------------------------------------------
238+
239+
expectFail(JAVAC, """
240+
-source 8 -target 8
241+
-Xbootclasspath/a:BadClass
242+
-extdirs GooZip
243+
Main.java""");
244+
245+
expectPass(JAVAC, """
246+
-source 8 -target 8
247+
-Xbootclasspath/a:GooClass${PS}BadClass
248+
-extdirs BadZip
249+
Main.java""");
250+
251+
//----------------------------------------------------------------
252+
253+
expectFail(JAVAC, """
254+
-source 8 -target 8
255+
-extdirs GooClass${PS}BadZip
256+
-cp GooZip/Lib.zip
257+
Main.java""");
258+
259+
expectPass(JAVAC, """
260+
-source 8 -target 8
261+
-extdirs BadClass${PS}GooZip${PS}BadJar
262+
-cp BadZip/Lib.zip
263+
Main.java""");
264+
265+
expectPass(JAVAC, """
266+
-source 8 -target 8
267+
-Djava.ext.dirs=GooZip${PS}BadJar
268+
-classpath BadZip/Lib.zip
269+
Main.java""");
270+
271+
//----------------------------------------------------------------
272+
273+
expectFail(JAVAC, "-classpath BadClass${PS}GooClass Main.java");
274+
expectPass(JAVAC, "-classpath GooClass${PS}BadClass Main.java");
275+
expectFail(JAVA, "-classpath BadClass${PS}GooClass${PS}. Main");
276+
expectPass(JAVA, "-classpath GooClass${PS}BadClass${PS}. Main");
277+
278+
expectFail(JAVAC, "-cp BadJar/Lib.jar${PS}GooZip/Lib.zip Main.java");
279+
expectPass(JAVAC, "-cp GooJar/Lib.jar${PS}BadZip/Lib.zip Main.java");
280+
expectFail(JAVA, "-cp BadJar/Lib.jar${PS}${PS}GooZip/Lib.zip Main");
281+
expectPass(JAVA, "-cp GooJar/Lib.jar${PS}${PS}BadZip/Lib.zip Main");
282+
283+
//----------------------------------------------------------------
284+
285+
expectFail(classpath("BadZip/Lib.zip${PS}GooJar/Lib.jar"), JAVAC,"Main.java");
286+
expectPass(classpath("GooZip/Lib.zip${PS}BadJar/Lib.jar"), JAVAC, "Main.java");
287+
expectFail(classpath("${PS}BadZip/Lib.zip${PS}GooJar/Lib.jar"), JAVA, "Main");
288+
expectPass(classpath("${PS}GooZip/Lib.zip${PS}BadJar/Lib.jar"), JAVA, "Main");
289+
290+
//----------------------------------------------------------------
291+
// Check behavior of empty paths and empty path elements
292+
//----------------------------------------------------------------
293+
294+
Path GooClass = Path.of("GooClass");
295+
Path GooJar = Path.of("GooJar");
296+
297+
expectFail(GooClass, JAVAC, "-cp .. ../Main.java");
298+
expectFail(GooClass, JAVA, "-cp .. Main");
299+
300+
// Unspecified classpath defaults to "."
301+
Path OneDir = Path.of("OneDir");
302+
tb.createDirectories(OneDir);
303+
tb.copyFile(Path.of("Main.java"), OneDir);
304+
tb.copyFile(GooClass.resolve("Lib.class"), OneDir);
305+
expectPass(OneDir, JAVAC, "Main.java");
306+
expectPass(OneDir, JAVA, "Main");
307+
308+
// Empty classpath elements mean "."
309+
expectPass(GooClass, JAVAC, "-cp ${PS}.. ../Main.java");
310+
expectPass(GooClass, JAVA, "-cp ${PS}.. Main");
311+
312+
expectPass(GooClass, JAVAC, "-cp ..${PS} ../Main.java");
313+
expectPass(GooClass, JAVA, "-cp ..${PS} Main");
314+
315+
expectPass(GooClass, JAVAC, "-cp ..${PS}${PS}/xyzzy ../Main.java");
316+
expectPass(GooClass, JAVA, "-cp ..${PS}${PS}/xyzzy Main");
317+
318+
// All other empty path elements are ignored.
319+
320+
// note presence of empty arg in this invocation
321+
expectFail(GooJar, null, JAVAC, "-source", "8", "-target", "8", "-extdirs", "", "-cp", "..", "../Main.java");
322+
323+
expectFail(GooJar, JAVAC, "-source 8 -target 8 -extdirs ${PS} -cp .. ../Main.java");
324+
expectFail(GooJar, JAVAC, "-source 8 -target 8 -Djava.ext.dirs=${PS} -cp .. ../Main.java");
325+
326+
expectPass(GooJar, JAVAC, "-source 8 -target 8 -extdirs . -cp .. ../Main.java");
327+
expectPass(GooJar, JAVAC, "-source 8 -target 8 -Djava.ext.dirs=. -cp .. ../Main.java");
328+
329+
expectFail(GooJar, JAVAC, "-source 8 -target 8 -Djava.endorsed.dirs= -cp .. ../Main.java");
330+
331+
expectFail(GooJar, JAVAC, "-source 8 -target 8 -endorseddirs ${PS} -cp .. ../Main.java");
332+
333+
expectPass(GooJar, JAVAC, "-source 8 -target 8 -Djava.endorsed.dirs=. -cp .. ../Main.java");
334+
335+
expectFail(GooClass, JAVAC, "-source 8 -target 8 -Xbootclasspath/p: -cp .. ../Main.java");
336+
337+
expectPass(GooClass, JAVAC, "-source 8 -target 8 -Xbootclasspath/p:. -cp .. ../Main.java");
338+
339+
expectFail(GooClass, JAVAC, "-source 8 -target 8 -Xbootclasspath:../java-lang.jar -cp .. ../Main.java");
340+
341+
expectPass(GooClass, JAVAC, "-source 8 -target 8 -Xbootclasspath:../java-lang.jar${PS}. -cp .. ../Main.java");
342+
343+
expectFail(GooClass, JAVAC, "-source 8 -target 8 -Xbootclasspath/a: -cp .. ../Main.java");
344+
expectFail(GooClass, JAVA, "-Xbootclasspath/a: -cp .. Main");
345+
346+
expectPass(GooClass, JAVAC, "-source 8 -target 8 -Xbootclasspath/a:. -cp .. ../Main.java");
347+
expectPass(GooClass, JAVA, "-Xbootclasspath/a:. -cp .. Main");
348+
349+
}
350+
351+
352+
}

‎test/langtools/tools/javac/Paths/MineField.sh

-277
This file was deleted.

‎test/langtools/tools/javac/Paths/Util.java

+560
Large diffs are not rendered by default.

‎test/langtools/tools/javac/Paths/Util.sh

-115
This file was deleted.

‎test/langtools/tools/javac/Paths/WildcardMineField.java

+340
Large diffs are not rendered by default.

‎test/langtools/tools/javac/Paths/wcMineField.sh

-296
This file was deleted.

‎test/langtools/tools/lib/toolbox/ToolBox.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public void copyFile(String from, String to) throws IOException {
245245
public void copyFile(Path from, Path to) throws IOException {
246246
if (Files.isDirectory(to)) {
247247
to = to.resolve(from.getFileName());
248-
} else {
248+
} else if (to.getParent() != null) {
249249
Files.createDirectories(to.getParent());
250250
}
251251
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Sep 10, 2024

@openjdk-notifier[bot]
Please sign in to comment.