You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "harshith2000 (via GitHub)" <gi...@apache.org> on 2023/11/15 01:28:17 UTC
[PR] Fixed two non-deterministic tests in EnumUtilsTest.java [commons-lang]
harshith2000 opened a new pull request, #1131:
URL: https://github.com/apache/commons-lang/pull/1131
Found two non-deterministic tests failing in EnumUtilsTest.java
Test names: [test_getEnumMap](https://github.com/apache/commons-lang/blob/912ab7154d5280a2632f375b5efd97e1b70149a4/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java#L341), [test_getEnumMap_keyFunction](https://github.com/apache/commons-lang/blob/912ab7154d5280a2632f375b5efd97e1b70149a4/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java#L355)
### Reason for failure:
The issue we're encountering in the test arises from the nature of the Map collection in Java. The Map returned by the getEnumMap method does not guarantee the order of its elements.
https://github.com/apache/commons-lang/blob/912ab7154d5280a2632f375b5efd97e1b70149a4/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java#L342
This is especially true when you're using implementations like HashMap, which is what Collectors.toMap() typically uses under the hood. According to the [official documentation](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html), HashMap does not maintain the order of its elements.
https://github.com/apache/commons-lang/blob/912ab7154d5280a2632f375b5efd97e1b70149a4/src/main/java/org/apache/commons/lang3/EnumUtils.java#L301
### Fix:
Instead of converting the [test map](https://github.com/apache/commons-lang/blob/912ab7154d5280a2632f375b5efd97e1b70149a4/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java#L343) to a string, and then doing an assert check, we can create an expected Map and then compare the two maps. This way, we check whether both maps have the same key-value pairs, but don't consider the order in which these elements are stored.
### Steps to reproduce the behavior:
I used an open-source tool called [NonDex](https://github.com/TestingResearchIllinois/NonDex) to detect the assumption by shuffling the order of returned exception types.
Running the following commands will test the aforementioned operation
**Clone the Repo**
```
https://github.com/apache/commons-lang
```
**Compile the project**
```
mvn install -am -DskipTests
```
**(Optional) Run the unit test**
```
mvn test -Dtest=org.apache.commons.lang3.EnumUtilsTest#test_getEnumMap_keyFunction
```
**Run the unit test using NonDex**
```
mvn edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.apache.commons.lang3.EnumUtilsTest#test_getEnumMap_keyFunction
```
### Stack trace for additional information:
```
[ERROR] Failures:
[ERROR] EnumUtilsTest.test_getEnumMap:343 getEnumMap not created correctly ==> expected: <{RED=RED, AMBER=AMBER, GREEN=GREEN}> but was: <{GREEN=GREEN, RED=RED, AMBER=AMBER}>
[ERROR] EnumUtilsTest.test_getEnumMap_keyFunction:357 getEnumMap not created correctly ==> expected: <{1=JAN, 2=FEB, 3=MAR, 4=APR, 5=MAY, 6=JUN, 7=JUL, 8=AUG, 9=SEP, 10=OCT, 11=NOV, 12=DEC}> but was: <{8=AUG, 1=JAN, 10=OCT, 12=DEC, 2=FEB, 7=JUL, 6=JUN, 11=NOV, 3=MAR, 5=MAY, 9=SEP, 4=APR}>
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fixed two non-deterministic tests in EnumUtilsTest.java [commons-lang]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #1131:
URL: https://github.com/apache/commons-lang/pull/1131#issuecomment-1821978027
TY @harshith2000 !
Merged.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fixed two non-deterministic tests in EnumUtilsTest.java [commons-lang]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory merged PR #1131:
URL: https://github.com/apache/commons-lang/pull/1131
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fixed two non-deterministic tests in EnumUtilsTest.java [commons-lang]
Posted by "harshith2000 (via GitHub)" <gi...@apache.org>.
harshith2000 commented on code in PR #1131:
URL: https://github.com/apache/commons-lang/pull/1131#discussion_r1398053230
##########
src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:
##########
@@ -29,6 +29,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
+import java.util.HashMap;
Review Comment:
Hi,
Thank you for the feedback. I've made the changes. Please let me know if there's anything else.
##########
src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:
##########
@@ -354,8 +360,21 @@ public void test_getEnumMap() {
@Test
public void test_getEnumMap_keyFunction() {
final Map<Integer, Month> test = EnumUtils.getEnumMap(Month.class, Month::getId);
- assertEquals("{1=JAN, 2=FEB, 3=MAR, 4=APR, 5=MAY, 6=JUN, 7=JUL, 8=AUG, 9=SEP, 10=OCT, 11=NOV, 12=DEC}", test.toString(),
- "getEnumMap not created correctly");
+ final Map<Integer, Month> expected = new HashMap() {{
+ put(1, Month.JAN);
Review Comment:
Hi,
Thank you for the feedback. I've made the changes. Please let me know if there's anything else.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fixed two non-deterministic tests in EnumUtilsTest.java [commons-lang]
Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #1131:
URL: https://github.com/apache/commons-lang/pull/1131#discussion_r1398043845
##########
src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:
##########
@@ -354,8 +360,21 @@ public void test_getEnumMap() {
@Test
public void test_getEnumMap_keyFunction() {
final Map<Integer, Month> test = EnumUtils.getEnumMap(Month.class, Month::getId);
- assertEquals("{1=JAN, 2=FEB, 3=MAR, 4=APR, 5=MAY, 6=JUN, 7=JUL, 8=AUG, 9=SEP, 10=OCT, 11=NOV, 12=DEC}", test.toString(),
- "getEnumMap not created correctly");
+ final Map<Integer, Month> expected = new HashMap() {{
+ put(1, Month.JAN);
Review Comment:
Use normal code, please. This notation will generate compiler warnings since the diamond notation can't be used.
```
final Map<Integer, Month> expected = new HashMap<>();
expected.put(1, Month.JAN);
...
```
##########
src/test/java/org/apache/commons/lang3/EnumUtilsTest.java:
##########
@@ -29,6 +29,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
+import java.util.HashMap;
Review Comment:
Please run `mvn` locally before pushing to catch build errors.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fixed two non-deterministic tests in EnumUtilsTest.java [commons-lang]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1131:
URL: https://github.com/apache/commons-lang/pull/1131#issuecomment-1817333344
## [Codecov](https://app.codecov.io/gh/apache/commons-lang/pull/1131?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`912ab71`)](https://app.codecov.io/gh/apache/commons-lang/commit/912ab7154d5280a2632f375b5efd97e1b70149a4?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) 92.15% compared to head [(`0482768`)](https://app.codecov.io/gh/apache/commons-lang/pull/1131?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) 92.16%.
> Report is 3 commits behind head on master.
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #1131 +/- ##
============================================
+ Coverage 92.15% 92.16% +0.01%
- Complexity 7596 7597 +1
============================================
Files 200 200
Lines 15910 15910
Branches 2925 2925
============================================
+ Hits 14662 14664 +2
+ Misses 675 674 -1
+ Partials 573 572 -1
```
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/commons-lang/pull/1131?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org