Skip to content

Conversation

@hofi1
Copy link
Owner

@hofi1 hofi1 commented Sep 26, 2023

Problem:

In this test, the contents of an XML file and a JSON file is getting parsed and compared. But XML does not specify the order of the elements in the String, what means that the actual and the expected string cannot be directly compared for equality.
The flaky test was found by using the NonDex tool.

assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator")));

Solution:

To fix this assertion without the import of a new XMLAssertion library, just like in other PRs discussed, the XML String gets converted to a JSON Object and afterward compared for similarity. This is needed due to the fact that XML Objects do not offer a similarity() method.

assertTrue(XML.toJSONObject(expected.toString()).similar(XML.toJSONObject(actualString)));

Result:

The test is deterministic and not flaky. This improves the quality of the test and reduces the time to search for the bug during future development, as well as the need for reruns of the pipeline.

Reproduce

mvn edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.json.junit.XMLTest#testIndentComplicatedJsonObjectWithArrayAndWithConfig

eamonnmcmanus and others added 4 commits September 19, 2023 07:38
One test method was missing `@Test` so it was never run.

One test method used another test class as the base for finding a test
resource. While this works in practice with Maven, it is not strictly
right.
@hofi1 hofi1 force-pushed the bugfix/fix-flakyness-org.json.junit.XMLTest#testIndentComplicatedJsonObjectWithArrayAndWithConfig branch 2 times, most recently from 157baee to 1397794 Compare September 26, 2023 16:07
eedijs and others added 4 commits September 27, 2023 19:30
For object keys, we can just skip the part of `nextValue()` that parses values
that are objects or arrays. Then the existing logic for unquoted values will
already stop at `{` or `[`, and that will produce a `Missing value` exception.
@KiruthikaJanakiraman
Copy link

The fix makes sense, but can you try to make a fix without adding a dependency to pom as adding a new dependency may cause security implications which might lead to rejection of the PR.

@hofi1
Copy link
Owner Author

hofi1 commented Oct 5, 2023

@KiruthikaJanakiraman thank you for your great feedback.
I totally see your point and I tried to do it without the XMLAssert library, but since the XML strings which I am comparing are so big, it is not really feasible to write my own JUnit assertions for that as well as they are only available as strings, what makes it really hard to convert them in any other data structure (like a map or similar thing) to compare them.

mureinik and others added 3 commits October 5, 2023 15:36
XMLTest.testIndentComplicatedJsonObjectWithArrayAndWithConfig fails
when run on Windows due to mismatching linebreaks (that aren't
important for the test's functionality) between the actual and
expected strings.

For the actual strings, linebreaks are canonized to the platform's
native linebreak using `replaceAll("\\n|\\r\\n",
System.getProperty("line.separator")`. However, the expected result is
read from a file, and is left with the linebreaks that were originally
used to create it.

The solution is to perform the same canonization on both strings.

Closes stleary#781
Use the built-in System.lineSeparator() instead of implementing it
ourselves with System.getProperty("line.separator") in order to clean
up the code and make it easier to maintain.
For exponential decimal conversion, number is not touched.
Leading zeros removed from numeric number strings before converting to number.
Madjosz and others added 8 commits October 7, 2023 09:38
* fixes stleary#713
* document JSONException in JavaDoc
* remove unused Comparable<T> boundary to reuse GenericBean in test
Fix XMLTest.testIndentComplicatedJsonObjectWithArrayAndWithConfig() for Windows - in the test
Fix XMLTest.testIndentComplicatedJsonObjectWithArrayAndWithConfig() for Windows - in the test
add validity check for JSONObject constructors
@hofi1 hofi1 force-pushed the bugfix/fix-flakyness-org.json.junit.XMLTest#testIndentComplicatedJsonObjectWithArrayAndWithConfig branch 4 times, most recently from f4f7fc4 to f346203 Compare October 14, 2023 22:06
@hofi1 hofi1 merged commit f346203 into master Oct 14, 2023
@MyEnthusiastic
Copy link

MyEnthusiastic commented Oct 23, 2023

Awesome work Simon! 🎉 congratulation for getting merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.