New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8271302: Regex Test Refresh #5092
Conversation
|
This PR migrates all regular expression tests in the jdk/java/util/regex directory to use TestNG assertions and annotations. The assertions utilized for this refresh are shared in common with standard ones from JUnit5 should such a migration occur in the future. |
Webrevs
|
In the JBS issue, it looks like the Description was put in the Environment. :) |
@igraves This change now passes all automated pre-integration checks. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 321 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.
|
@@ -24,7 +24,6 @@ | |||
/** | |||
* @test | |||
* @summary tests RegExp framework (use -Dseed=X to set PRNG seed) | |||
* @author Mike McCloskey |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened to Mike here? :-)
@@ -1,5 +1,5 @@ | |||
/* | |||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. | |||
* Copyright (c) 2019, 2021 Oracle and/or its affiliates. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add comma after 2021, or the copyright headers check won't pass.
@@ -26,17 +26,17 @@ | |||
* @bug 8223174 | |||
* @summary Pattern.compile() can throw confusing NegativeArraySizeException | |||
* @requires os.maxMemory >= 5g | |||
* @run main/othervm NegativeArraySize -Xms5G -Xmx5G | |||
* @run testng/othervm -Xms5G -Xmx5G NegativeArraySize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I note that the order of the arguments has changed. Will that work as expected? Had it worked as expected before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new order is consistent with other tests. I had difficulty getting it to run in the original configuration. Perhaps jtreg is more sensitive on order with the testng runner.
} | ||
@Test | ||
public static void testNegativeArraySize() { | ||
assertThrows(OutOfMemoryError.class, () -> Pattern.compile("\\Q" + "a".repeat(42 + Integer.MAX_VALUE / 3))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One observation on this regex. Although the regex looks invalid because \\Q
misses the pairing \\E
, it can still be compiled (with a reasonable number of a's, of course). Moreover, the resulting pattern matches strings in a surprising way:
jshell> Pattern.compile("\\Qaaa").matcher("aaa").matches()
$1 ==> true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe that behavior is expected after all. From "Mastering Regular Expressions" by Jeffrey E.F. Friedl, 3rd Edition, p. 136:
Literal-text span:
\Q...\E
First introduced with Perl, the special sequence
\Q...\E
turns off all regex meta-characters between them, except for\E
itself. (If the\E
is omitted, they are turned off until the end of the regex.)
} | ||
|
||
private static void check(String p, String s, boolean expected) { | ||
Matcher matcher = Pattern.compile(p).matcher(s); | ||
if (matcher.find() != expected) | ||
failCount++; | ||
assertSame(matcher.find(), expected); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use assertSame(Object, Object)
? I would expect assertEquals(boolean, boolean)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Artifacting because of the use of ==
I'll make it more readable.
@@ -325,154 +203,138 @@ private static String toSupplementaries(String s) { | |||
return sb.toString(); | |||
} | |||
|
|||
// Regular expression tests | |||
// Regular expression test// Most of the tests are in a file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mistakenly joined comment lines?
if (r != Pattern.compile(p).matcher(s).matches()) { | ||
failCount++; | ||
} | ||
assertSame(r, Pattern.compile(p).matcher(s).matches()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertEquals(boolean, boolean)
might be better.
if (r != Pattern.compile(p).matcher(s).matches()) { | ||
failCount++; | ||
} | ||
assertSame(r, Pattern.compile(p).matcher(s).matches()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expectThrows(PatternSyntaxException.class, ...)
might suit better for this and similar cases in this file. Not only does expectThrows
test for expected exception, it also returns an exception which you can further inspect. Now, if we only had assertThat
or similar functionality for compound assertions, the complete test might've looked nicely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latter comment referred to hand-rolled try-fail constructs like this:
jdk/test/jdk/java/util/regex/RegExTest.java
Lines 4281 to 4289 in 3937bf8
try { | |
Pattern.compile(pat); | |
fail(); | |
} catch (PatternSyntaxException e) { | |
if (!e.getMessage().startsWith( | |
"capturing group name does not start with a" | |
+ " Latin letter")) { | |
fail(); | |
} |
Not sure why my IDE plugin messed up the comment position.
Whew! Changes to GraphemeTest.java and NegativeArraySize.java look fine. I finally figured out how to look at RegExTest.java effectively -- none of the diff views were helpful at all. I just brought up the old and new versions in windows side by side and just scrolled through them. Overall it looks good, mostly a replacement of the ad hoc internal framework (failCount++) with assertX from testng, and some minor reorganizations here and there, such as the splitting of One observation about this technique is that using the "failCount++" approach, if there is a failure, the tests in the same method will continue to run. With the assertX approach, the test will stop at that point, and any assertions later in the same method won't get run at all. Since we expect all the tests to pass all the time, this has no effect in the normal case. If there is a bug, though, the assertX approach might result in a single failure whereas the failCount++ approach might result in several failures. While this bothers some people, it doesn't bother me; it's likely only to occur at development time, and it's like to be only a minor impediment to development. Indeed, it's commonly viewed as a testing antipattern to have a single test method test multiple cases. The solution is to fix that, and not worry about failCount++ vs assertX. I think the long run solution here is to continue cleaning up these tests, possibly breaking down test methods further, eventually driving things into a purely table-driven or data generator/provider approach. |
import static org.testng.Assert.assertTrue; | ||
import static org.testng.Assert.fail; | ||
import static org.testng.Assert.expectThrows; //Replaced by assertThrows in JUnit5 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Slightly odd to mention JUnit 5 here, since this is being converted to a TestNG test. Are these notes for yourself for future work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes. I'll pull that.
/integrate |
Going to push as commit fecefb8.
Your commit was automatically rebased without conflicts. |
Looks like it was missed that the test fails oi a github actions: Run if ! grep --include=test-summary.txt -lqr build/*/test-results -e "TEST SUCCESS" ; then
|
https://bugs.openjdk.java.net/browse/JDK-8273169
|
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/5092/head:pull/5092
$ git checkout pull/5092
Update a local copy of the PR:
$ git checkout pull/5092
$ git pull https://git.openjdk.java.net/jdk pull/5092/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 5092
View PR using the GUI difftool:
$ git pr show -t 5092
Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/5092.diff