Skip to content
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

Unable to ignore element order #44

Closed
ghost opened this issue Jan 5, 2016 · 8 comments
Closed

Unable to ignore element order #44

ghost opened this issue Jan 5, 2016 · 8 comments
Labels

Comments

@ghost
Copy link

ghost commented Jan 5, 2016

hello,

I am using the version 2.0.0-alpha-03.
I want to compare two XML that contain the same data in a different order.
Example:

Test:


value1
value2


value3
value4

Control:


value3
value4


value1
value2

I have the same objects in my list of objects, therefore I expect to obtain a similar comparison result, as the order does not matter in my case.
My problem is that I can not find a way to achieve this result.
I always get this error: "Expected text value 'value3' but was 'value1'"
Is it possible to provide a way to easily ignore the order in such a case?

Thank's

@bodewig
Copy link
Member

bodewig commented Jan 5, 2016

In order to get a similar result XMLUnit must at least consider comparing the first object element of control to the second one in test. I.e. the ElementSelector you use must return false when comparing the first one to the first one and true when comparing first to second.

Have you configured an ElementSelector that would achieve just that? See https://github.com/xmlunit/user-guide/wiki/SelectingNodes

From your example it is difficult to tell what would identify "matching" object elements. The textual content of the child with name field1? In case it is something like this, then this is identical to the table-example of the user guide page.

@ghost
Copy link
Author

ghost commented Jan 5, 2016

My example was too simple. In fact, in my case, I am comparing two XML generated by java objects serialization. As I can serialize Set of objects, I want to compare them in the same way that would be done by a java equals on a set of objects. If I have the same set of objects, without regarding the order, I need to return that the XML are similar.
I do not know the schema of my xml, I want a generic test class that works for all the schemas.

=> To identify the matching object element, we need to recursively check that all the fields under the tested element are in the control element, no matter the order. i.e. when we check if object1 from the tested xml is in the control xml, we will iterate on the objects of the control, and for each object check if all the subelements of the tested object are in the current control object.

So I was thinking that you could provide a modifier on the DiffBuilder object to set a boolean "ignoreOrder" to do this.

I did not find anything in the doc to easily do this.

@bodewig
Copy link
Member

bodewig commented Jan 5, 2016

That's because it isn't easy :-)

Not at all.

What you want would boil down to trying to compare all possible pairs of child elements and then pick the permutation that caused the least amount of differences. I've already thought about adding an ElementSelector like this but it would require a fair amount of caching and the ElementSelector would need access to a stripped down version of the DifferenceEngine it belonged to (at least it would need the same DifferenceEvaluator, but should not use the attached ComparisonListeners).

I'm sorry, there is no generic solution right now.

@ghost
Copy link
Author

ghost commented Jan 5, 2016

Yes, according to my understanding of the mechanism of XMLUnit, you first need to find which control element must be compared to the testing one, but in my case we don't want to compare a specific node with another that was previously filtered by the ElementSelector. So we should have an ElementSelector that return a set of nodes (by default all the nodes with the same tag name, on the same level), and check if the tested element is in the set.

Let me know if one day you find a way to achieve such a thing, and thank's for your help ! :)

@bodewig
Copy link
Member

bodewig commented Jan 5, 2016

I've opened #45 for this.

check if the tested element is in the set

means to compare the nodes recursively again, which may contain sets of nodes that need the same treatment somewhere down the tree, in the end. This is more complex in XML than one would hope for. In your case I'd probably rather deserialize the documents again and use Java to compare the sets.

@ghost
Copy link
Author

ghost commented Jan 5, 2016

Yes I was thinking about this solution, but this is a pretty huge project with hundreds of classes and most of them do not override equals/hashcode.
Implementing all of them can have some unpredictible side effects.
So I was betting on XMLUnit to simplify the task :P
But if I have no choice, I will implement the hashCode/equals and compare the java objects

@bodewig
Copy link
Member

bodewig commented Jan 22, 2016

I'd like to close this issue - even if the answer to your question isn't the one you'd like to hear. Is this OK with you?

@ghost
Copy link
Author

ghost commented Jan 22, 2016

No problem, thank's for your help !

@bodewig bodewig closed this as completed Jan 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant