Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/ToolsOpts.java
38833 views
1
/*
2
* Copyright (c) 2012, 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 8002091
27
* @summary Test options patterns for javac,javah,javap and javadoc using
28
* javac as a test launcher. Create a dummy javac and intercept options to check
29
* reception of options as passed through the launcher without having to launch
30
* javac. Only -J and -cp ./* options should be consumed by the launcher.
31
* @run main ToolsOpts
32
* @author ssides
33
*/
34
35
import java.io.File;
36
import java.io.IOException;
37
import java.util.ArrayList;
38
import java.util.List;
39
40
public class ToolsOpts extends TestHelper {
41
static final String JBCP_PREPEND = "-J-Xbootclasspath/p:";
42
private static File testJar = null;
43
static String[][] optionPatterns = {
44
{"-J-Xmx128m"},
45
{"-J-version"},
46
{"-J-XshowSettings:vm"},
47
{"-J-Xdiag"},
48
{"-J-showversion"},
49
{"-J-version", "-option"},
50
{"-option"},
51
{"-option:sub"},
52
{"-option:sub-"},
53
{"-option:sub1,sub2"}, // -option:list
54
{"-option:{sub1,sub2,sub3}"}, // -option:{list}
55
{"-option:{{sub1,sub2,sub3}}"},// -option:{{list}}
56
{"-option/c:/export/date/tmp"},
57
{"-option=value"},
58
{"-Dpk1.pk2.pk3"}, // dot in option
59
{"-Dpk1.pk2=value"}, // dot in option followed by =value
60
{"@<filename>"},
61
{"-option", "http://site.com", "http://site.org"},
62
{"-option", "name", "p1:p2.."},
63
{"-All these non-options show launchers pass options as is to tool."},
64
{"-option"},
65
{"-option:sub"},
66
{"-option:sub-"},
67
{"-option", "<path>"},
68
{"-option", "<file>"},
69
{"-option", "<dir>"},
70
{"-option", "http://a/b/c/g;x?y#s"},
71
{"-option", "<html code>"},
72
{"-option", "name1:name2"},
73
{"-option", "3"},
74
{"option1", "-J-version", "option2"},
75
{"option1", "-J-version", "-J-XshowSettings:vm", "option2"},};
76
77
static void init() throws IOException {
78
if (testJar != null) {
79
return;
80
}
81
82
// A tool which simulates com.sun.tools.javac.Main argument processing,
83
// intercepts options passed via the javac launcher.
84
final String mainJava = "Main" + JAVA_FILE_EXT;
85
testJar = new File("test" + JAR_FILE_EXT);
86
List<String> contents = new ArrayList<>();
87
contents.add("package com.sun.tools.javac;");
88
contents.add("public class Main {");
89
contents.add(" public static void main(String... args) {\n");
90
contents.add(" for (String x : args) {\n");
91
contents.add(" if(x.compareTo(\" \")!=0)\n");
92
contents.add(" System.out.println(x);\n");
93
contents.add(" }\n");
94
contents.add(" }\n");
95
contents.add("}\n");
96
createFile(new File(mainJava), contents);
97
98
// compile and jar Main.java into test.jar
99
compile("-d", ".", mainJava);
100
createJar("cvf", testJar.getAbsolutePath(), "com");
101
}
102
103
static void pass(String msg) {
104
System.out.println("pass: " + msg);
105
}
106
107
static void errout(String msg) {
108
System.err.println(msg);
109
}
110
111
// Return position of -J option or -1 is does not contain a -J option.
112
static int indexOfJoption(String[] opts) {
113
for (int i = 0; i < opts.length; i++) {
114
if (opts[i].startsWith("-J")) {
115
return i;
116
}
117
}
118
return -1;
119
}
120
121
/*
122
* Check that J options a) are not passed to tool, and b) do the right thing,
123
* that is, they should be passed to java launcher and work as expected.
124
*/
125
static void checkJoptionOutput(TestResult tr, String[] opts) throws IOException {
126
// Check -J-version options are not passed but do what they should.
127
String jopts = "";
128
for (String pat : opts) {
129
jopts = jopts.concat(pat + " ");
130
if (tr.contains("-J")) {
131
throw new RuntimeException(
132
"failed: output should not contain option " + pat);
133
}
134
if (pat.compareTo("-J-version") == 0 ||
135
pat.compareTo("-J-showversion") == 0) {
136
if (!tr.contains("java version") &&
137
!tr.contains("openjdk version")) {
138
throw new RuntimeException("failed: " + pat +
139
" should display a version string.");
140
}
141
} else if (pat.compareTo("-J-XshowSettings:VM") == 0) {
142
if (!tr.contains("VM settings")) {
143
throw new RuntimeException("failed: " + pat +
144
" should have display VM settings.");
145
}
146
}
147
}
148
pass("Joption check: " + jopts);
149
}
150
151
/*
152
* Feed each option pattern in optionPatterns array to javac launcher with
153
* checking program preempting javac. Check that option received by 'dummy'
154
* javac is the one passed on the command line.
155
*/
156
static void runTestOptions() throws IOException {
157
init();
158
TestResult tr = null;
159
String sTestJar = testJar.getAbsolutePath();
160
int jpos = -1;
161
for (String arg[] : optionPatterns) {
162
jpos = indexOfJoption(arg);
163
//Build a cmd string for output in results reporting.
164
String cmdString = javacCmd + " " + JBCP_PREPEND + sTestJar;
165
for (String opt : arg) {
166
cmdString = cmdString.concat(" " + opt);
167
}
168
switch (arg.length) {
169
case 1:
170
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
171
arg[0]);
172
break;
173
case 2:
174
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
175
arg[0], arg[1]);
176
break;
177
case 3:
178
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
179
arg[0], arg[1], arg[2]);
180
break;
181
case 4:
182
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
183
arg[0], arg[1], arg[2], arg[3]);
184
break;
185
default:
186
tr = null;
187
break;
188
}
189
190
String[] output = tr.testOutput.toArray(new String[tr.testOutput.size()]);
191
//-Joptions should not be passed to tool
192
if (jpos > -1) {
193
checkJoptionOutput(tr, arg);
194
if (tr.contains(arg[jpos])) {
195
throw new RuntimeException(
196
"failed! Should not have passed -J option to tool.\n"
197
+ "CMD: " + cmdString);
198
}
199
} else {
200
//check that each non -J option was passed to tool.
201
for (int i = 0; i < arg.length; i++) {
202
if (output[i].compareTo(arg[i]) != 0) {
203
throw new RuntimeException(
204
"failed! CMD: " + cmdString + "\n case:" +
205
output[i] + " != " + arg[i]);
206
} else {
207
pass("check " + output[i] + " == " + arg[i]);
208
}
209
}
210
}
211
pass(cmdString);
212
}
213
}
214
215
public static void main(String... args) throws IOException {
216
runTestOptions();
217
}
218
}
219
220