jfree / jfreechart

A 2D chart library for Java applications (JavaFX, Swing or server-side).
http://www.jfree.org/jfreechart/
GNU Lesser General Public License v2.1
1.22k stars 462 forks source link

Fix flaky test PolarPlotTest.testGetLegendItems2 #387

Open Sujishark opened 1 year ago

Sujishark commented 1 year ago

This PR aims to fix a test:

org.jfree.chart.plot.PolarPlotTest.testGetLegendItems2

This was found by using the NonDex tool.

Encountered the following error after running NonDex:

in org.jfree.chart.plot.PolarPlotTest
[ERROR] org.jfree.chart.plot.PolarPlotTest.testGetLegendItems2  Time elapsed: 0.053 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <A> but was: <C>
at org.jfree.chart@2.0.0-SNAPSHOT/org.jfree.chart.plot.PolarPlotTest.testGetLegendItems2(PolarPlotTest.java:108)

REASON AND FIX

The tests fail when the function values of 'items' are compared using assert statements intestGetLegendItems2. This is because the 'renderers' in Plot are set using HashMap which maintains a non-deterministic order.

https://github.com/jfree/jfreechart/blob/e2d6788d594c51941ddae337ae666fda5c52ad9f/src/test/java/org/jfree/chart/plot/PolarPlotTest.java#L103

https://github.com/jfree/jfreechart/blob/e2d6788d594c51941ddae337ae666fda5c52ad9f/src/main/java/org/jfree/chart/plot/PolarPlot.java#L261

According to HashMap documentation, "This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time."

This can be solved by changing from HashMap to LinkedHashMap to maintain a permanent deterministic order.

Reproduce:

The following command can be used to replicate the failures and validate the fix:

mvn edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.jfree.chart.plot.PolarPlotTest#testGetLegendItems2

Environment:

Java: openjdk version "11.0.20.1" Maven: Apache Maven 3.6.3

No user-facing change. No dependency upgrade.