Skip to content

Commit d6cd407

Browse files
authored
Merge pull request #24112 from keithc-ca/test_versions
Improve TestConfigParser
2 parents cd9fa94 + 9c9f9f2 commit d6cd407

4 files changed

Lines changed: 214 additions & 112 deletions

File tree

test/functional/cmdline_options_tester/src/Output.java

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@
1919
*
2020
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
2121
*/
22+
import com.ibm.oti.util.regex.Regex;
2223

23-
import java.util.regex.Matcher;
24-
import java.util.regex.Pattern;
25-
import java.util.regex.PatternSyntaxException;
24+
class Output extends TestCondition {
2625

27-
import com.ibm.oti.util.regex.*;
26+
private static boolean evalBoolean(String expr) {
27+
return !"no".equalsIgnoreCase(TestSuite.evaluateVariables(expr));
28+
}
2829

29-
class Output extends TestCondition {
30-
private String _type;
31-
private String _matchRegex;
32-
private String _matchJavaUtilPattern;
33-
private String _matchCase;
34-
private String _output; // the output string taken from the file
35-
private String _showRegexMatch;
36-
37-
public Output( String matchRegex, String matchJavaUtilPattern, String showRegexMatch, String matchCase, String type ) {
30+
private final String _type;
31+
private final String _matchRegex;
32+
private final String _matchJavaUtilPattern;
33+
private final String _matchCase;
34+
private String _output; // the output string taken from the file
35+
private final String _showRegexMatch;
36+
37+
public Output(String matchRegex, String matchJavaUtilPattern, String showRegexMatch, String matchCase, String type) {
3838
_matchRegex = matchRegex;
3939
_matchJavaUtilPattern = matchJavaUtilPattern;
4040
_showRegexMatch = showRegexMatch;
@@ -43,32 +43,32 @@ public Output( String matchRegex, String matchJavaUtilPattern, String showRegexM
4343
_output = "";
4444
}
4545

46-
void setOutput( String s ) {
46+
void setOutput(String s) {
4747
_output = s;
4848
}
49-
49+
5050
int getType() {
51-
return parseType( TestSuite.evaluateVariables( _type ) );
51+
return parseType(TestSuite.evaluateVariables(_type));
5252
}
53-
53+
5454
public boolean isJavaUtilPattern() {
55-
return !("no".equalsIgnoreCase( TestSuite.evaluateVariables( _matchJavaUtilPattern ) ));
55+
return evalBoolean(_matchJavaUtilPattern);
5656
}
57-
58-
boolean match( Object o ) {
57+
58+
boolean match(Object o) {
5959
String candidate = (String) o;
60-
boolean matchRegex = !("no".equalsIgnoreCase(TestSuite.evaluateVariables(_matchRegex)));
61-
boolean matchCase = !("no".equalsIgnoreCase(TestSuite.evaluateVariables(_matchCase)));
62-
boolean matchJavaUtilPattern = !("no".equalsIgnoreCase(TestSuite.evaluateVariables(_matchJavaUtilPattern)));
63-
boolean showRegexMatch = !("no".equalsIgnoreCase(TestSuite.evaluateVariables(_showRegexMatch)));
60+
boolean matchRegex = evalBoolean(_matchRegex);
61+
boolean matchCase = evalBoolean(_matchCase);
62+
boolean matchJavaUtilPattern = evalBoolean(_matchJavaUtilPattern);
63+
boolean showRegexMatch = evalBoolean(_showRegexMatch);
6464
String string = TestSuite.evaluateVariables(_output);
6565

66-
if ((matchRegex) && (!matchJavaUtilPattern)) {
66+
if (matchRegex && !matchJavaUtilPattern) {
6767
try {
6868
Regex regex = new Regex(string, matchCase);
6969
boolean retval = regex.matches(candidate);
70-
if( retval && showRegexMatch) {
71-
System.out.println("\tMatch ("+_type+"): "+candidate);
70+
if (retval && showRegexMatch) {
71+
System.out.println("\tMatch (" + _type + "): " + candidate);
7272
}
7373
return retval;
7474
} catch (Exception e) {
@@ -77,25 +77,27 @@ boolean match( Object o ) {
7777
e.printStackTrace();
7878
return false;
7979
}
80-
} else if ((matchRegex) && (matchJavaUtilPattern)) {
80+
} else if (matchRegex && matchJavaUtilPattern) {
8181
/* CMVC 163891: Use of java.util.regex.Pattern/Matcher must be in a separate class, to avoid
8282
* problems with the verifier when using the embedded class library.
8383
*/
84-
return OutputRegexHelper.ContainsMatches(candidate,string,matchCase,showRegexMatch,_type);
84+
return OutputRegexHelper.ContainsMatches(candidate, string, matchCase, showRegexMatch, _type);
8585
} else {
8686
if (!matchCase) {
8787
string = string.toLowerCase();
8888
candidate = candidate.toLowerCase();
8989
}
90-
boolean retval = (candidate.indexOf(string) >= 0);
91-
if( retval && showRegexMatch) {
92-
System.out.println("\tMatch ("+_type+"): "+candidate);
90+
boolean retval = candidate.contains(string);
91+
if (retval && showRegexMatch) {
92+
System.out.println("\tMatch (" + _type + "): " + candidate);
9393
}
9494
return retval;
9595
}
9696
}
97-
97+
98+
@Override
9899
public String toString() {
99-
return "Output match: " + TestSuite.evaluateVariables( _output );
100+
return "Output match: " + TestSuite.evaluateVariables(_output);
100101
}
102+
101103
}

test/functional/cmdline_options_tester/src/Test.java

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,26 @@
3434
import java.util.Collections;
3535
import java.util.List;
3636
import java.util.concurrent.TimeUnit;
37+
import java.util.regex.Matcher;
38+
import java.util.regex.Pattern;
3739

3840
@SuppressWarnings("nls")
3941
class Test {
42+
43+
static final class Range {
44+
final int min;
45+
final int max;
46+
47+
Range(int min, int max) {
48+
this.min = min;
49+
this.max = max;
50+
}
51+
52+
boolean includes(int value) {
53+
return (min <= value) && (value <= max);
54+
}
55+
}
56+
4057
private final String _id;
4158
private String _command;
4259
private final String _timeoutString;
@@ -54,6 +71,12 @@ class Test {
5471
private String _commandExecutable;
5572
private List<String> _commandArgs;
5673
private List<String> _commandInputLines;
74+
private final List<Range> _versionRanges;
75+
76+
private static final Pattern PLUS_PATTERN = Pattern.compile("\\s*(\\d+)\\s*\\+\\s*");
77+
private static final Pattern RANGE_PATTERN = Pattern.compile("\\s*\\[\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\]\\s*");
78+
private static final Pattern SINGLE_PATTERN = Pattern.compile("\\s*(\\d+)\\s*");
79+
5780
private static final long _JAVACORE_TIMEOUT_MILLIS = 5 * 60 * 1000; // 5 minute timeout
5881
private static final long SHORT_TIMEOUT_MILLIS = 30 * 1000; // 30 second timeout
5982

@@ -64,18 +87,28 @@ class Test {
6487
*/
6588
static final String CORE_COUNT_PROPERTY = "cmdline.corecount";
6689
static final int CORE_COUNT = Integer.getInteger(CORE_COUNT_PROPERTY, 2).intValue();
67-
90+
6891
/**
6992
* The time in milliseconds between capturing core files (if CORE_COUNT > 1)
7093
* may be specified via
7194
* -Dcmdline.coreintervalms=N
7295
* The default is one minute.
7396
*/
7497
static final long CORE_SPACING_MILLIS = Long.getLong("cmdline.coreintervalms", 60 * 1000).longValue();
75-
76-
static String archName = System.getProperty("os.arch");
77-
static boolean isRiscv = archName.toLowerCase().contains("riscv");
78-
static boolean isJava8 = System.getProperty("java.specification.version").equals("1.8");
98+
99+
static final String archName = System.getProperty("os.arch");
100+
static final boolean isRiscv = archName.toLowerCase().contains("riscv");
101+
static final int javaVersion;
102+
103+
static {
104+
String versionString = System.getProperty("java.specification.version");
105+
106+
if (versionString.startsWith("1.")) {
107+
versionString = versionString.substring(2);
108+
}
109+
110+
javaVersion = Integer.parseInt(versionString);
111+
}
79112

80113
/**
81114
* Create a new test case with the given id.
@@ -92,6 +125,7 @@ class Test {
92125
_standardOutput = new StringBuilder();
93126
_errorOutput = new StringBuilder();
94127
_timedOut = false;
128+
_versionRanges = new ArrayList<>();
95129
}
96130

97131
void setOutputLimit(int outputLimit) {
@@ -125,6 +159,51 @@ void addTestCondition(TestCondition tc) {
125159
_testConditions.add(tc);
126160
}
127161

162+
void constrainVersion(String constraint) {
163+
Matcher matcher;
164+
165+
matcher = PLUS_PATTERN.matcher(constraint);
166+
if (matcher.matches()) {
167+
int min = Integer.parseInt(matcher.group(1).trim());
168+
169+
_versionRanges.add(new Range(min, Integer.MAX_VALUE));
170+
return;
171+
}
172+
173+
matcher = RANGE_PATTERN.matcher(constraint);
174+
if (matcher.matches()) {
175+
int min = Integer.parseInt(matcher.group(1).trim());
176+
int max = Integer.parseInt(matcher.group(2).trim());
177+
178+
_versionRanges.add(new Range(min, max));
179+
return;
180+
}
181+
182+
matcher = SINGLE_PATTERN.matcher(constraint);
183+
if (matcher.matches()) {
184+
int version = Integer.parseInt(matcher.group(1).trim());
185+
186+
_versionRanges.add(new Range(version, version));
187+
return;
188+
}
189+
190+
throw new IllegalArgumentException(constraint);
191+
}
192+
193+
private boolean shouldRun() {
194+
if (_versionRanges.isEmpty()) {
195+
return true;
196+
}
197+
198+
for (Range range : _versionRanges) {
199+
if (range.includes(javaVersion)) {
200+
return true;
201+
}
202+
}
203+
204+
return false;
205+
}
206+
128207
/**
129208
* Does the required variable substitutions and runs the test case.
130209
*
@@ -138,6 +217,11 @@ public boolean run(long defaultTimeout) {
138217
// clear any previously captured output
139218
destroy();
140219

220+
if (!shouldRun()) {
221+
System.out.format("Skipping test which does not apply to Java %d%n.", javaVersion);
222+
return true;
223+
}
224+
141225
_matched = new boolean[_testConditions.size()];
142226
long timer;
143227
Process proc = null;
@@ -430,14 +514,14 @@ public void dumpVerboseOutput(boolean result) {
430514
public static boolean isLinux() {
431515
String osName = System.getProperty("os.name", "<unknown>");
432516

433-
return osName.toLowerCase().indexOf("linux") >= 0;
517+
return osName.toLowerCase().contains("linux");
434518
}
435519

436520
public static int getPID(Process process) throws Exception {
437521
if (isRiscv) {
438522
return 0;
439523
}
440-
if (isJava8) {
524+
if (javaVersion == 8) {
441525
Class<?> cl = process.getClass();
442526
if (!cl.getName().equals("java.lang.UNIXProcess")) {
443527
return 0;

test/functional/cmdline_options_tester/src/TestConfigParser.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
*
2020
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
2121
*/
22-
23-
import java.util.*;
22+
import java.util.ArrayList;
23+
import java.util.Hashtable;
24+
import java.util.List;
25+
import java.util.StringTokenizer;
2426
import java.util.regex.Pattern;
2527

26-
import com.oti.j9.exclude.*;
28+
import com.oti.j9.exclude.ExcludeList;
29+
import com.oti.j9.exclude.IXMLDocumentHandler;
30+
import com.oti.j9.exclude.XMLParser;
2731

2832
class TestConfigParser {
2933
private ExcludeList _excludes;
@@ -119,9 +123,9 @@ class TestConfigDocumentHandler implements IXMLDocumentHandler {
119123
// this flag allows us to continue to use this parser implementation for the more complicated use of the command element without having to re-write it as a sort of delegating PDA
120124
private boolean _isInNewCommandStanza;
121125
// used to collect the contents of the in-order "arg" tags under the new-style command stanza (which are then passed as arguments to the underlying process)
122-
private Vector<String> _commandArgs;
126+
private List<String> _commandArgs;
123127
// used to collect the contents of the in-order "input" tags under the new-style command stanza (which are then fed into the stdin of the underlying process as new-line terminated strings)
124-
private Vector<String> _commandInputLines;
128+
private List<String> _commandInputLines;
125129

126130
/**
127131
* Empty implementation
@@ -260,8 +264,8 @@ public void xmlStartElement(String elementName, Hashtable<String, String> attrib
260264
_commandExecutable = commandExecutable;
261265
//also set the flag to know that we are in a command context and create the arrays for the arguments and inputs which may appear in that stanza
262266
_isInNewCommandStanza = true;
263-
_commandArgs = new Vector<>();
264-
_commandInputLines = new Vector<>();
267+
_commandArgs = new ArrayList<>();
268+
_commandInputLines = new ArrayList<>();
265269
}
266270
} else if (elementName.equalsIgnoreCase("if")) {
267271
_iterator.addCommand(new IfTest(attributes.get("testVariable"), attributes.get("testValue"),
@@ -326,6 +330,10 @@ public void xmlEndElement(String elementName) {
326330
if (_isInNewCommandStanza) {
327331
_commandInputLines.add(readData);
328332
}
333+
} else if (elementName.equalsIgnoreCase("version")) {
334+
if (_currentTest != null) {
335+
_currentTest.constrainVersion(readData);
336+
}
329337
}
330338
_data.setLength(0); // any data in here has outlived it's usefulness
331339
}

0 commit comments

Comments
 (0)