Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdline_options_tester/src/TestSuite.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2004, 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
23
import java.io.*;
24
import java.text.*;
25
import java.util.*;
26
import com.oti.j9.exclude.*;
27
28
/**
29
* This is the test suite object that keeps track of all the tests and runs them.
30
*/
31
public class TestSuite {
32
33
/**
34
* The user-defined variables in the configuration file. Substitution of these is done
35
* later, just before the commands are actually executed
36
*/
37
private static Hashtable _variables = new Hashtable();
38
private static final String failedPropertyName = "cmdlinetester.failed_tests";
39
private ExcludeList _excludes;
40
private long _defaultTimeout;
41
private boolean _verbose;
42
private int _outputLimit;
43
private static SortedSet _processedVariables;
44
45
private ArrayList _passed;
46
private ArrayList _failed;
47
48
/* ArrayList to hold environment variables */
49
private static Hashtable _envVarList = new Hashtable();
50
/**
51
* Initializes variables.
52
*/
53
TestSuite( ExcludeList excludes, long defaultTimeout, boolean verbose, int outputLimit ) {
54
_variables = new Hashtable();
55
_excludes = excludes;
56
_defaultTimeout = defaultTimeout;
57
_verbose = verbose;
58
_passed = new ArrayList();
59
_failed = new ArrayList();
60
_outputLimit = outputLimit;
61
System.setProperty(failedPropertyName, "0");
62
}
63
64
void setVerbose( boolean verbose ) {
65
_verbose = verbose;
66
}
67
68
void setOutputLimit( int limit ) {
69
_outputLimit = limit;
70
}
71
72
/**
73
* Add a new test
74
*/
75
void runTest( Test test ) {
76
try {
77
if (_excludes != null && !_excludes.shouldRun( test.getId() )) {
78
System.out.println( "Test excluded: " + test.getId() + '\n' );
79
return;
80
}
81
String testName = test.getId();
82
System.out.println( "Testing: " + testName );
83
test.setOutputLimit(_outputLimit);
84
boolean result = test.run( _defaultTimeout ); // <-- actually run the test
85
if (result) {
86
_passed.add( testName );
87
System.out.println( "Test result: PASSED" );
88
if (_verbose) {
89
test.dumpVerboseOutput(result);
90
}
91
System.out.print( '\n' );
92
} else {
93
_failed.add( testName );
94
System.out.println( "Test result: FAILED" );
95
test.dumpVerboseOutput(result); // print verbose output in case of failure
96
System.out.print( '\n' );
97
98
int failedTests = Integer.parseInt(System.getProperty(failedPropertyName));
99
failedTests++;
100
System.setProperty(failedPropertyName, Integer.toString(failedTests));
101
}
102
test.destroy();
103
} catch (Exception e) {
104
TestSuite.printStackTrace(e);
105
}
106
}
107
108
void printResults() {
109
// print aggregate statistics
110
System.out.println("\n---TEST RESULTS---");
111
System.out.println("Number of PASSED tests: " + _passed.size() + " out of " + (_passed.size() + _failed.size()));
112
System.out.println("Number of FAILED tests: " + _failed.size() + " out of " + (_passed.size() + _failed.size()));
113
114
if (_failed.size() > 0) {
115
System.out.println("\n---SUMMARY OF FAILED TESTS---");
116
for (int i = 0; i < _failed.size(); i++) {
117
System.out.println( (String)_failed.get(i) );
118
}
119
System.out.println("-----------------------------\n");
120
}
121
}
122
123
int getFailureCount() {
124
return _failed.size();
125
}
126
127
public static void putVariable( String key, String value ) {
128
_variables.put( key, value );
129
}
130
131
public static String getVariable( String key ) {
132
//return (String)_variables.get( key );
133
/* MODIFIED by Oliver Deakin - 20050211 - treat system properties the same as variables - do this under the covers
134
so that there is no distinction between system properties and variables */
135
String returnString = (String) (_variables.get(key));
136
if (returnString == null)
137
{
138
// If key is not in the cmdlinetester variables, check system properties
139
returnString = (String) (TestSuite.getSystemProperty(key));
140
}
141
142
return returnString;
143
}
144
145
public static String evaluateVariables( String s ) {
146
return evaluateVariables(s, null);
147
}
148
149
/* A new way to implement use of environment variables */
150
public static void putEnvironmentVariable( String key, String value ) {
151
_envVarList.put( key, value );
152
}
153
154
/* This will return a list of the environment variables in a format that Runtime.exec can understand:
155
"name=value"
156
*/
157
public static String[] getEnvironmentVariableList() {
158
//Create an array string from the current environment variables
159
String[] envVarArray;
160
if (_envVarList.size() == 0)
161
{
162
return null;
163
}
164
165
envVarArray = new String[_envVarList.size()];
166
167
// Iterate through the Hashtable turning key/value pairs into the correct format
168
Object[] keyNames = _envVarList.keySet().toArray();
169
for (int keyNum = 0; keyNum<_envVarList.size(); keyNum++)
170
{
171
String key = (String)keyNames[keyNum];
172
String value = (String)_envVarList.get(key);
173
envVarArray[keyNum] = key + "=" + value;
174
}
175
176
return envVarArray;
177
}
178
179
public static String getSystemProperty( String key ) {
180
return (String) (System.getProperties().get( key ));
181
}
182
183
/**
184
* Does a variable replacement on s using the variables in <code>_variables</code> and
185
* System.properties. Also does a replacement of nested variables within variable
186
* names. For example, if $HI$ = "WORD", $WORD$ = "ING", and $TESTING$ = "SOMETHING",
187
* then $TEST{$HI$}$ would evaluate as follows:
188
*
189
* $TEST{$HI$}$ -> $TEST{WORD}$ -> $TESTING$ -> SOMETHING
190
*
191
* Curly brackets have no special meaning if outside of a set of dollar signs. When
192
* inside a set dollar signs, curly brackets always have special meaning. For
193
* example, "{}TEST$TEST{{HI}}${HI}" would evaluate as follows (using the same
194
* variable values as in the previous example):
195
*
196
* {}TEST$TEST{{HI}}${HI} -> {}TEST$TEST{WORD}${HI} -> {}TEST$TESTING${HI} ->
197
* {}TESTSOMETHING{HI}
198
*
199
* For each (<code>key</code>, <code>value</code>) pair in <code>_variables</code>
200
* and System.properties, this method replaces all instances of $<code>key</code>$
201
* with <code>value</code> (taking into account the nested variable replacement
202
* described above). All the substitutions from <code>_variables</code> are
203
* done before any of the substitutions from System.properties.
204
*
205
* Note: this method will "expand" the String "$$" to "$" after all other expansions
206
* have been performed. This is done so that the user can specify a literal "$" if
207
* desired.
208
*
209
* @param s - The source string
210
* @param variableName - The name of the variable whose value is being evaluated
211
* if applicable; null if it is not a variable's value that is being evaluated
212
* @return A copy of s with all the substitutions performed.
213
*/
214
public static String evaluateVariables( String s, String variableName ) {
215
if (s == null || s.equals("$$")) {
216
return s;
217
}
218
219
String value = s;
220
221
if (null == variableName) {
222
_processedVariables = new TreeSet();
223
} else {
224
if (_processedVariables.contains(variableName)) {
225
System.err.println(
226
"The variable \"" + variableName + "\" with value \"" + value +
227
"\" cannot be resolved because it refers to itself somewhere " +
228
"along the variable substitution chain.");
229
System.exit(1);
230
} else {
231
_processedVariables.add(variableName);
232
}
233
}
234
235
Stack tokens = new Stack();
236
int varStartIndex = -1;
237
238
for (int i = 0; i < s.length(); i++)
239
{
240
char currChar = s.charAt(i);
241
switch (currChar)
242
{
243
case '$':
244
if (!tokens.isEmpty() && '$' == ((Character)tokens.peek()).charValue()) {
245
tokens.pop();
246
if (tokens.isEmpty())
247
{
248
String newString = s.substring(0, varStartIndex - 1) +
249
expandVariable(s.substring(varStartIndex, i));
250
int newIndex = newString.length() - 1; // adjust for i++ at end of loop
251
252
if ( (i + 1) < s.length()) {
253
newString += s.substring(i + 1);
254
}
255
256
s = newString;
257
i = newIndex;
258
varStartIndex = -1;
259
}
260
} else {
261
if (tokens.isEmpty()) {
262
varStartIndex = i + 1;
263
}
264
tokens.push(Character.valueOf('$'));
265
}
266
break;
267
268
case '{':
269
if (!tokens.isEmpty()) {
270
tokens.push(Character.valueOf('{'));
271
}
272
break;
273
274
case '}':
275
if (!tokens.isEmpty()) {
276
if ('{' == ((Character)tokens.peek()).charValue()) {
277
tokens.pop();
278
} else {
279
System.err.println(
280
"The variable \"" + variableName + "\" with value \"" + value +
281
"\" cannot be resolved because it contains a '}' after " +
282
"an opening '$'.");
283
System.exit(1);
284
}
285
}
286
}
287
}
288
289
if (!tokens.isEmpty())
290
{
291
System.err.println(
292
"The variable \"" + variableName + "\" with value \"" + value +
293
"\" cannot be resolved because it is missing at least one token " +
294
"signifying the end of a variable ('$' or '}'); the first " +
295
"unbalanced start token is a '" +
296
((Character)tokens.pop()).charValue() + "'");
297
System.exit(1);
298
}
299
300
if (null != variableName) {
301
_processedVariables.remove(variableName);
302
}
303
304
if (null == variableName)
305
{
306
boolean lastWasDollar = false;
307
for (int i = 0; i < s.length(); i++)
308
{
309
char c = s.charAt(i);
310
if ('$' == c)
311
{
312
if (lastWasDollar)
313
{
314
s = s.substring(0, i - 1) + s.substring(i);
315
i--; // we removed a char from the String so adjust i accordingly
316
317
lastWasDollar = false;
318
} else {
319
lastWasDollar = true;
320
}
321
}
322
}
323
}
324
325
return s;
326
}
327
328
private static String expandVariable(String s)
329
{
330
String unexpandedName = s;
331
Stack tokens = new Stack();
332
int varStartIndex = -1;
333
334
for (int i = 0; i < s.length(); i++)
335
{
336
char currChar = s.charAt(i);
337
switch (currChar)
338
{
339
case '$':
340
if (!tokens.isEmpty() && '$' == ((Character)tokens.peek()).charValue()) {
341
tokens.pop();
342
if (tokens.isEmpty())
343
{
344
String newString = s.substring(0, varStartIndex - 1) +
345
expandVariable(s.substring(varStartIndex, i));
346
int newIndex = newString.length() - 1; // adjust for i++ at end of loop
347
348
if ( (i + 1) < s.length()) {
349
newString += s.substring(i + 1);
350
}
351
352
s = newString;
353
i = newIndex;
354
varStartIndex = -1;
355
}
356
} else {
357
if (tokens.isEmpty()) {
358
varStartIndex = i + 1;
359
}
360
tokens.push(Character.valueOf('$'));
361
}
362
break;
363
364
case '{':
365
if (tokens.isEmpty()) {
366
varStartIndex = i + 1;
367
}
368
tokens.push(Character.valueOf('{'));
369
break;
370
371
case '}':
372
if (tokens.isEmpty()) {
373
System.err.println(
374
"The variable name \"" + unexpandedName + "\" cannot be " +
375
"resolved because it contains a '}' that does not refer " +
376
"to a corresponding '{'.");
377
System.exit(1);
378
} else {
379
if ('{' == ((Character)tokens.peek()).charValue()) {
380
tokens.pop();
381
if (tokens.isEmpty())
382
{
383
String newString = s.substring(0, varStartIndex - 1) +
384
expandVariable(s.substring(varStartIndex, i));
385
int newIndex = newString.length() - 1; // adjust for i++ at end of loop
386
387
if ( (i + 1) < s.length()) {
388
newString += s.substring(i + 1);
389
}
390
391
s = newString;
392
i = newIndex;
393
varStartIndex = -1;
394
}
395
} else {
396
System.err.println(
397
"The variable name \"" + unexpandedName + "\" cannot " +
398
"be resolved because it contains a '}' after " +
399
"an opening '$'.");
400
System.exit(1);
401
}
402
}
403
}
404
}
405
406
if (!tokens.isEmpty())
407
{
408
System.err.println(
409
"The variable name \"" + unexpandedName + "\" cannot " +
410
"be resolved because it is missing at least one token " +
411
"signifying the end of a variable ('$' or '}'); the first " +
412
"unbalanced start token is a '" +
413
((Character)tokens.pop()).charValue() + "'");
414
System.exit(1);
415
}
416
417
String varValue = getVariableValue(s);
418
419
if (null == varValue)
420
{
421
System.err.println(
422
"The variable \"" + s + "\" does not exist; original string " +
423
"used to construct the variable name was \"" + unexpandedName + "\"");
424
System.exit(1);
425
}
426
427
return evaluateVariables(varValue, s);
428
}
429
430
private static String getVariableValue(String variableName)
431
{
432
if (variableName.equals("")) {
433
return "$$";
434
}
435
436
String value;
437
438
value = getVariable(variableName);
439
if (null != value)
440
{
441
return value;
442
}
443
444
value = (String)System.getProperties().get(variableName);
445
return value;
446
}
447
448
/**
449
* Basic implementation of the String.replaceAll that's in Java 1.4+
450
*
451
* @param s - The source string
452
* @param find - Substring to find
453
* @param replace - Substring to replace all instances of <code>find</code> with
454
* @return A copy of <code>s<code>, with all instances of <code>find</code> replaced with
455
* <code>replace</code>
456
*/
457
public static String doReplaceAll( String s, String find, String replace ) {
458
int lastIndex = 0;
459
int index = s.indexOf( find, lastIndex );
460
while (index >= 0) {
461
s = s.substring( 0, index ) + replace + s.substring( index + find.length() );
462
lastIndex = index + replace.length();
463
index = s.indexOf( find, lastIndex );
464
}
465
return s;
466
}
467
468
public static void printErrorMessage(String message) {
469
Calendar calendar = Calendar.getInstance();
470
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
471
System.out.println("***[TEST INFO " + df.format(calendar.getTime()) + "] " + message + "***");
472
}
473
474
public static void printStackTrace(Throwable e) {
475
StringWriter sw = new StringWriter();
476
PrintWriter pw = new PrintWriter(sw);
477
e.printStackTrace(pw);
478
479
String[] stack = sw.toString().split("\n");
480
for (int i = 0; i < stack.length; i++) {
481
String element = stack[i];
482
printErrorMessage(element);
483
}
484
485
pw.close();
486
}
487
}
488
489