Closed yruicdl closed 8 years ago
That's an interesting issue. The reason is simple. If the library takes into account the array order, it knows which elements to compare. If it ignores the order, the task is much harder. Imagine the following example
[{"key":1}, {"key":2}, {"key":3}]
If I compare it with this array
[{"key":2}, {"key":3}, {"key":4}]
what the error message should say? That we have expected 1 but got 4? Or that we expected 3 and got 4? It's quite hard problem to decide if the order in the array has to be ignored. Moreover there can be more than one difference and the structures may be quite deep.
I have to think about it.
In general, it can not be solved. If there is only one difference, we can describe it. For [1, 2, 3, 4] vs [2, 3, 4, 5] we can say that 1!=5 (the question is, if it makes sense). But if there is more than one difference, I can't say which elements are different. In [1, 2, 3, 4] vs. [4, 5, 6, 7] I do not know if 2!=5 or 2!=6 or 2!=7.
While it can not be solved in general, in theory we can do something for your special case. We can compare the elements that are most alike. In your case compare elements with the same "iso" field. Unfortunately it has quadratic time complexity and I am not sure if it's worth it. But if you have any idea how to solve the problem, I'd like to hear it.
I agree with you, it's hard to solve this problem in general. We can do some preparation before doing compare. Anyway, thanks for explanation! I really like jsonunit, it's an awesome library :)
String exp = "{"dataStreamsCount":1,"dataStreams":[{"device":"testDevice_single_boolean_sensors","sensor":"sensor_boolean_1","dataPointsCount":20,"dataPoints":[{"iso":"2011-12-31T17:01:01.001Z","value":"true"},{"iso":"2011-12-31T17:01:01.002Z","value":"false"},{"iso":"2011-12-31T17:01:01.003Z","value":"true"},{"iso":"2011-12-31T17:01:01.004Z","value":"false"},{"iso":"2011-12-31T17:01:01.005Z","value":"true"},{"iso":"2011-12-31T17:01:01.007Z","value":"false"},{"iso":"2011-12-31T17:01:01.008Z","value":"true"},{"iso":"2011-12-31T17:01:01.009Z","value":"false"},{"iso":"2011-12-31T17:01:01.011Z","value":"true"},{"iso":"2011-12-31T17:01:01.012Z","value":"false"},{"iso":"2011-12-31T17:01:01.013Z","value":"true"},{"iso":"2011-12-31T17:01:01.014Z","value":"false"},{"iso":"2011-12-31T17:01:01.016Z","value":"true"},{"iso":"2011-12-31T17:01:01.017Z","value":"false"},{"iso":"2011-12-31T17:01:01.019Z","value":"true"},{"iso":"2011-12-31T17:01:01.020Z","value":"false"},{"iso":"2011-12-31T17:01:01.021Z","value":"true"},{"iso":"2011-12-31T17:01:01.022Z","value":"false"},{"iso":"2011-12-31T17:01:01.023Z","value":"true"},{"iso":"2011-12-31T17:01:01.024Z","value":"false"}]}]}";
String actual = "{"dataStreamsCount":1,"dataStreams":[{"device":"testDevice_single_boolean_sensors","sensor":"sensor_boolean_1","dataPointsCount":20,"dataPoints":[{"value":"true","iso":"2011-12-31T17:01:01.001Z"},{"value":"false","iso":"2011-12-31T17:01:01.002Z"},{"value":"true","iso":"2011-12-31T17:01:01.003Z"},{"value":"false","iso":"2011-12-31T17:01:01.004Z"},{"value":"false","iso":"2011-12-31T17:01:01.005Z"},{"value":"false","iso":"2011-12-31T17:01:01.007Z"},{"value":"true","iso":"2011-12-31T17:01:01.008Z"},{"value":"false","iso":"2011-12-31T17:01:01.009Z"},{"value":"false","iso":"2011-12-31T17:01:01.011Z"},{"value":"false","iso":"2011-12-31T17:01:01.012Z"},{"value":"false","iso":"2011-12-31T17:01:01.013Z"},{"value":"false","iso":"2011-12-31T17:01:01.014Z"},{"value":"true","iso":"2011-12-31T17:01:01.016Z"},{"value":"false","iso":"2011-12-31T17:01:01.017Z"},{"value":"false","iso":"2011-12-31T17:01:01.019Z"},{"value":"false","iso":"2011-12-31T17:01:01.020Z"},{"value":"true","iso":"2011-12-31T17:01:01.021Z"},{"value":"false","iso":"2011-12-31T17:01:01.022Z"},{"value":"false","iso":"2011-12-31T17:01:01.023Z"},{"value":"false","iso":"2011-12-31T17:01:01.024Z"}]}]}";
assertJsonEquals(exp, actual,JsonAssert.when(Option.IGNORING_ARRAY_ORDER)); assertJsonEquals(exp, actual);
compare above 2 output. Compare method 1 will not give difference detail, but compare method 2 will do.