Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/VM_Test/src/j9vm/runner/Menu.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2020 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package j9vm.runner;
23
import java.util.*;
24
import java.io.*;
25
import com.oti.j9.exclude.*;
26
27
public class Menu {
28
Vector menuItems;
29
Vector allClassNames;
30
String jarFileName;
31
String exeName;
32
String bootClassPath;
33
String userClassPath;
34
String javaVersion;
35
36
public Menu(String jarFileName, String exeName, String bootClassPath, String userClassPath, String javaVersion) {
37
this.menuItems = new Vector();
38
this.allClassNames = new Vector();
39
this.jarFileName = jarFileName;
40
this.exeName = exeName;
41
this.bootClassPath = bootClassPath;
42
this.userClassPath = userClassPath;
43
this.javaVersion = javaVersion;
44
}
45
public void addElement(MenuItem item) {
46
menuItems.addElement(item);
47
}
48
49
public void printMenu() {
50
System.out.println("---- VMTest Menu ----");
51
for (int i = 0; i<menuItems.size(); i++) {
52
MenuItem item = (MenuItem)menuItems.get(i);
53
String front = Integer.toString(i+1) + ") ";
54
while (front.length() < 4) front = front + " ";
55
System.out.print(front);
56
System.out.println(item.getDisplayString());
57
}
58
System.out.println("A) Run ALL tests");
59
System.out.println("Q) Quit");
60
}
61
62
public void doCommand(String command) {
63
if (command.equalsIgnoreCase("A")) {
64
runTestSet(allClassNames);
65
return;
66
}
67
try {
68
int n = Integer.parseInt(command);
69
if (command.equals(String.valueOf(n))) {
70
n--;
71
if ((n >= 0) && (n < menuItems.size())) {
72
runTestSet(((MenuItem)menuItems.get(n)).getClassNames());
73
}
74
}
75
} catch (NumberFormatException e) {
76
/* Nothing */
77
}
78
}
79
80
public void doMenu() {
81
byte[] buf = new byte[256];
82
boolean fPrintMenu = true;
83
for (;;) {
84
if (fPrintMenu) {
85
printMenu(); fPrintMenu = false;
86
}
87
System.out.print("Enter command(s): ");
88
System.out.flush();
89
try {
90
int result = System.in.read(buf, 0, buf.length);
91
if (result == 2 && buf[0] == 13) {
92
/* User just pressed return */
93
fPrintMenu = true;
94
continue;
95
}
96
} catch (IOException e) {
97
return;
98
}
99
StringTokenizer tokenizer = new StringTokenizer(new String(buf));
100
while (tokenizer.hasMoreTokens()) {
101
String command = tokenizer.nextToken();
102
if (command.equalsIgnoreCase("Q")) {
103
System.exit(0);
104
}
105
doCommand(command);
106
}
107
}
108
}
109
110
public static void usage() {
111
System.out.println("Usage: j9 [vmOptions] j9vm.runner.Menu [testOptions]");
112
System.out.println("testOptions:");
113
System.out.println(" -jar=<jarFileName> Read tests out of jar file <jarFileName>");
114
System.out.println(" (make sure the tests are in the classpath too, of course!)");
115
System.out.println(" -exe=<exeFileName> Override default VM executable name");
116
System.out.println(" -version=<javaVersion> Override default java option");
117
System.out.println(" -bp=<bootClassPath> Override default VM bp with -bp:<bootClassPath>");
118
System.out.println(" -cp=<userClassPath> Override default VM bp with -cp:<userClassPath>");
119
System.out.println(" -xlist=<excludeList> Specify an XML exclude list");
120
System.out.println(" -xids=<excludeIDS> Specify tags to choose excluded tests");
121
System.out.println(" -test=<test package> Run all tests in the given package");
122
System.out.println(" <n> Run menu item <n>");
123
System.out.println(" A or a Run all tests");
124
System.out.println("If no tests are specified (i.e. no <n> or A options) an interactive");
125
System.out.println("menu will be displayed.");
126
System.exit(1);
127
}
128
129
public static void main(String[] args) {
130
Vector commands = new Vector();
131
String exeName = null;
132
String javaVersion = null;
133
String jarName = null;
134
String bootClassPath = null;
135
String userClassPath = null;
136
String excludeFN = null;
137
String excludeIDs = null;
138
String packageToTest = null;
139
140
/* Parse all arguments beginning with '-' */
141
if (args != null) {
142
for (int i = 0; i<args.length; i++) {
143
if (!args[i].startsWith("-")) {
144
commands.addElement(args[i]);
145
continue;
146
}
147
/* TODO: find a cleaner way to do this */
148
if (args[i].startsWith("-exe=")) {
149
if (exeName != null) usage();
150
exeName = args[i].substring(5);
151
} else if (args[i].startsWith("-version=")) {
152
if (javaVersion != null) usage();
153
javaVersion = args[i].substring(9);
154
} else if (args[i].startsWith("-jar=")) {
155
if (jarName != null) usage();
156
jarName = args[i].substring(5);
157
} else if (args[i].startsWith("-bp=")) {
158
if (bootClassPath != null) usage();
159
bootClassPath = args[i].substring(4);
160
} else if (args[i].startsWith("-cp=")) {
161
if (userClassPath != null) usage();
162
userClassPath = args[i].substring(4);
163
} else if (args[i].startsWith("-xlist=")) {
164
if (excludeFN != null) usage();
165
excludeFN = args[i].substring(7);
166
} else if (args[i].startsWith("-xids=")) {
167
if (excludeIDs != null) usage();
168
excludeIDs = args[i].substring(6);
169
} else if (args[i].startsWith("-test=")) {
170
packageToTest = args[i].substring(6);
171
} else {
172
System.out.println("Unrecognized option: \"" + args[i] + "\"");
173
usage();
174
}
175
}
176
}
177
178
ExcludeList excludeList = null;
179
if ( excludeFN!=null && excludeIDs!=null) {
180
excludeList= ExcludeList.readFrom(excludeFN, excludeIDs);
181
if (excludeList == null)
182
System.exit(-1);
183
excludeList.explain(System.out);
184
}
185
186
if (exeName == null) {
187
exeName = System.getProperty("user.dir");
188
if (!exeName.endsWith(System.getProperty("file.separator"))) {
189
exeName = exeName + System.getProperty("file.separator");
190
}
191
exeName = exeName + "j9"; /* TODO: can we reach for the .exe name? */
192
}
193
194
if (javaVersion == null) {
195
/* set default java version to 9 */
196
javaVersion = "9";
197
}
198
199
if (jarName == null) {
200
/* Need to revisit this */
201
jarName = System.getProperty("java.class.path");
202
}
203
if ((bootClassPath == null) && (Integer.parseInt(javaVersion) < 9)) {
204
bootClassPath = System.getProperty("sun.boot.class.path");
205
}
206
if (userClassPath == null) {
207
userClassPath = System.getProperty("java.class.path");
208
}
209
210
if ((args == null) || (args.length < 1)) {
211
}
212
HashMap testPackageMap = new HashMap();
213
Enumeration enumeration = null;
214
try {
215
enumeration = new AllTestsInJar(jarName);
216
} catch (IOException e) {
217
System.out.println("test suite: error reading tests from Jar file \"" + jarName + "\"!");
218
System.exit(1);
219
}
220
System.out.println("test suite: read tests from \"" + jarName + "\"");
221
while (enumeration.hasMoreElements()) {
222
String testClassName = (String)enumeration.nextElement();
223
/* Put tests in bins according to their package. */
224
String testPackage = testClassName.substring(0, testClassName.lastIndexOf('.'));
225
Vector v = (Vector)testPackageMap.get(testPackage);
226
if (v == null) {
227
/* First test class from this package. */
228
v = new Vector();
229
testPackageMap.put(testPackage, v);
230
}
231
v.addElement(testClassName);
232
}
233
234
Iterator iter = testPackageMap.keySet().iterator();
235
Menu menu = new Menu(jarName, exeName, bootClassPath, userClassPath, javaVersion);
236
while (iter.hasNext()) {
237
String testPackage = (String)iter.next();
238
Vector v = (Vector)testPackageMap.get(testPackage);
239
if (v.size() == 1) {
240
/* Package contains only one test. */
241
String testName = (String)v.get(0);
242
243
if (excludeList==null || excludeList.shouldRun(testName)) {
244
menu.addElement(new MenuItem(testName, v));
245
menu.allClassNames.addElement(testName);
246
}
247
} else {
248
/* Package contains multiple tests. */
249
String testName = testPackage + ".AllTests";
250
if (excludeList==null || excludeList.shouldRun(testName)) {
251
menu.addElement(new MenuItem(testName, v));
252
253
for (int i = 0; i<v.size(); i++) {
254
testName = (String)v.get(i);
255
if (excludeList==null || excludeList.shouldRun(testName)) {
256
testName = " " + testName;
257
menu.addElement(new MenuItem(testName, (String)v.get(i)));
258
menu.allClassNames.addElement((String)v.get(i));
259
}
260
}
261
}
262
}
263
}
264
265
/* Get the index of 'packageToTest' in menu.menuItems vector
266
* and add it to commands vector.
267
*/
268
if (packageToTest != null) {
269
int index = 0;
270
for (index = 0; index < menu.menuItems.size(); index++)
271
{
272
MenuItem item = (MenuItem)menu.menuItems.get(index);
273
String packageName = item.getDisplayString();
274
packageName = packageName.substring(0, packageName.lastIndexOf('.'));
275
if (packageName.equals(packageToTest)) {
276
commands.addElement(String.valueOf(index+1));
277
break;
278
}
279
}
280
}
281
282
if (commands.size() > 0) {
283
/* Non-interactive mode. */
284
for (int i = 0; i<commands.size(); i++) {
285
menu.doCommand((String)commands.get(i));
286
}
287
} else {
288
/* Interactive mode */
289
menu.doMenu();
290
}
291
}
292
293
public Runner newRunnerForTest(String className, String jarFileName) {
294
try {
295
Class runnerClass = Class.forName(className + "Runner");
296
Class[] paramTypes = new Class[5];
297
Object[] paramValues = new Object[5];
298
paramTypes[0] = className.getClass(); paramValues[0] = className;
299
paramTypes[1] = exeName.getClass(); paramValues[1] = exeName;
300
if (bootClassPath == null) {
301
paramTypes[2] = String.class;
302
} else {
303
paramTypes[2] = bootClassPath.getClass();
304
}
305
paramValues[2] = bootClassPath;
306
paramTypes[3] = userClassPath.getClass(); paramValues[3] = userClassPath;
307
paramTypes[4] = javaVersion.getClass(); paramValues[4] = javaVersion;
308
return (Runner)runnerClass.getConstructor(paramTypes).newInstance(paramValues);
309
} catch (Exception e) {
310
return new Runner(className, exeName, bootClassPath, userClassPath, javaVersion);
311
}
312
}
313
314
public void runTestSet(Vector classNames) {
315
int numPassed = 0;
316
int numFailed = 0;
317
boolean passed;
318
for (int i = 0; i<classNames.size(); i++) {
319
String className = (String)classNames.get(i);
320
System.out.println("+++ " + className + ": +++");
321
Runner runner = newRunnerForTest(className, jarFileName);
322
passed = false;
323
try {
324
if (runner.run()) {
325
passed = true;
326
}
327
} catch (Throwable e) {
328
System.out.println("runTestSet() caught exception:");
329
e.printStackTrace();
330
}
331
if (passed) {
332
numPassed++;
333
System.out.println("--- Test PASSED ---");
334
} else {
335
numFailed++;
336
System.out.println("*** Test FAILED *** (" + className + ")");
337
}
338
System.out.println("");
339
}
340
System.out.println(numPassed + " passed / " + numFailed + " failed");
341
if (numFailed == 0) {
342
System.out.println("ALL J9VM TESTS PASSED");
343
}else{
344
System.exit(-2);
345
}
346
System.out.println();
347
}
348
349
350
}
351
352