-
Notifications
You must be signed in to change notification settings - Fork 461
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
8265828: [TestBug] Save and restore the default Locale in javafx.base unit test LocalDateTimeStringConverterTest #954
Conversation
… unit test LocalDateTimeStringConverterTest
👋 Welcome back lukostyra! A progress list of the required criteria for merging this PR into |
Webrevs
|
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.
Looks good.
@@ -94,6 +93,17 @@ public LocalDateTimeStringConverterTest(LocalDateTimeStringConverter converter, | |||
this.parser = parser; | |||
} | |||
|
|||
@BeforeClass public static void setupBeforeAll() { |
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.
Minor: we usually put annotations on a separate line, although some files (like this one) put the @Test
annotation on the same line, splitting them is preferred. I'll approve it as-is, and reapprove if you decide to change (I'll leave it up to you).
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.
Since I have to make some changes to this PR, I will update this as well.
@lukostyra This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. 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 2 new commits pushed to the
Please see this link for an up-to-date comparison between the source branch of this pull request and the As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@kevinrushforth) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
@@ -56,11 +57,9 @@ public class LocalDateTimeStringConverterTest { | |||
|
|||
private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy HH mm ss"); | |||
private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd hh mm ss a"); | |||
private static Locale oldLocale; |
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.
Isn't the creation of the DateTimeFormatter using the default locale? If so, this should probably be done after the locale is set.
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.
This is a good point. Moving the initialization of those two fields to the setupBeforeAll
method seems safest.
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.
That is a fair point.
I'll have to change the code a bit, as implementations()
method is called before a @BeforeClass
-tagged method (which is probably why originally Locale.setDefault()
was called there) and aFormatter
/aParser
are already used there, expected to be initialized.
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 JUnit5 can help you here.
You can check out my recently merged PR for an example for the usage of the parameterized api introduced in JUnit5.
#936
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 guess they fixed this rather irritating "feature" in JUnit5. Another, possibly less-intrusive, approach would be to initialize all of the static fields in a static { ... }
block (with a comment as to why you can't use an @BeforeClass
method.
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.
Pending response to comment raised by Marius.
@@ -56,11 +57,9 @@ public class LocalDateTimeStringConverterTest { | |||
|
|||
private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy HH mm ss"); | |||
private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd hh mm ss a"); | |||
private static Locale oldLocale; |
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 wonder how many other tests we have that depend on specific Locale? Perhaps we need to apply the same treatment to:
- LocalDateStringConverterTest
- LocalTimeStringConverterTest
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 could also change those, as they use DateTimeFormatter
as well which uses Locale
underneath as discussed above. @kevinrushforth what do you think?
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.
Yes, it seems reasonable to include those tests as well, since those tests have the same problem.
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.
see inline comments
@@ -56,11 +57,9 @@ public class LocalDateTimeStringConverterTest { | |||
|
|||
private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy HH mm ss"); | |||
private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd hh mm ss a"); | |||
private static Locale oldLocale; |
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 guess they fixed this rather irritating "feature" in JUnit5. Another, possibly less-intrusive, approach would be to initialize all of the static fields in a static { ... }
block (with a comment as to why you can't use an @BeforeClass
method.
@@ -56,11 +57,9 @@ public class LocalDateTimeStringConverterTest { | |||
|
|||
private static final DateTimeFormatter aFormatter = DateTimeFormatter.ofPattern("dd MM yyyy HH mm ss"); | |||
private static final DateTimeFormatter aParser = DateTimeFormatter.ofPattern("yyyy MM dd hh mm ss a"); | |||
private static Locale oldLocale; |
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.
Yes, it seems reasonable to include those tests as well, since those tests have the same problem.
…ocale * Locale initialization was moved to @BeforeClass method. * DateTimeFormatter objects are allocated after Locale initialization * Since LocalDateTimeStringConverter depends on DateTimeFormatter and on VM's Locale, creation of it was moved to @before method.
Treatment done in this commit is similar to the previous change.
After implementing CR fixes it turned out that these tests started to fail at random. Upon more investigation it turned out that the order in which JUnit calls test methods matters a lot. In general:
Additionally, Test Class order is selected at random. That means that modifying a VM-wide setting like Locale cannot reside in I had to refactor initialization code for these tests in order to resolve all problems and make them more independent. In short:
The solution I made does not feel the most convenient but with above constraints it was difficult to come up with anything cleaner/less intrusive. This might also be a side-effect of me not knowing a mechanism in JUnit that could help us in such situation, so if there are any bits worth looking into I'd be happy to add them and simplify this change a bit. |
@lukostyra this pull request can not be integrated into git checkout JDK-8265828-locale
git fetch https://git.openjdk.org/jfx master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push |
Thank you for getting to the bottom of this.
This was the surprising find, and explains the odd problems you were seeing.
Right. This is documented behavior and is (or at least should be) well understood. It's why we are careful to avoid mutating static data or global state in a
Yes, this is the conclusion I would draw as well. |
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 refactoring looks good to me. I left a few comments on one of the tests. Those comments apply to all of them.
modules/javafx.base/src/test/java/test/javafx/util/converter/LocalDateStringConverterTest.java
Show resolved
Hide resolved
modules/javafx.base/src/test/java/test/javafx/util/converter/LocalDateStringConverterTest.java
Outdated
Show resolved
Hide resolved
modules/javafx.base/src/test/java/test/javafx/util/converter/LocalDateStringConverterTest.java
Outdated
Show resolved
Hide resolved
modules/javafx.base/src/test/java/test/javafx/util/converter/LocalDateStringConverterTest.java
Outdated
Show resolved
Hide resolved
modules/javafx.base/src/test/java/test/javafx/util/converter/LocalDateStringConverterTest.java
Show resolved
Hide resolved
...s/javafx.base/src/test/java/test/javafx/util/converter/LocalDateTimeStringConverterTest.java
Outdated
Show resolved
Hide resolved
modules/javafx.base/src/test/java/test/javafx/util/converter/LocalTimeStringConverterTest.java
Outdated
Show resolved
Hide resolved
modules/javafx.base/src/test/java/test/javafx/util/converter/LocalTimeStringConverterTest.java
Outdated
Show resolved
Hide resolved
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.
Looks good. You can remove the now-unused import of java.security.InvalidParameterException
(I'll reapprove when you do).
/integrate |
@lukostyra |
/sponsor |
Going to push as commit 4ad8582.
Your commit was automatically rebased without conflicts. |
@kevinrushforth @lukostyra Pushed as commit 4ad8582. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
The change moves Locale setting in the test to
@BeforeClass
and@AfterClass
calls.@BeforeClass
method call stores current default VM locale and applies Locale.US, while@AfterClass
method restores old VM locale after all tests are completed.I tested it both on Mac and Windows, in both cases Locale is changed, restored properly and tests pass.
Progress
Issue
Reviewers
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jfx pull/954/head:pull/954
$ git checkout pull/954
Update a local copy of the PR:
$ git checkout pull/954
$ git pull https://git.openjdk.org/jfx pull/954/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 954
View PR using the GUI difftool:
$ git pr show -t 954
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jfx/pull/954.diff