Menu

https://stackoverflow.com/questions/61184818/xmlunit-2-0-xpath-doesnt-ignoring-the-xml-node-order

Help
2020-04-13
2020-04-13
  • Swapnil Kotwal

    Swapnil Kotwal - 2020-04-13

    I've a XML as below

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE ResourceObject PUBLIC "my_corp.dtd" "my_corp.dtd">
    <ResourceObject displayName="TESTNGAD\AggUserFSP test" identity="CN=AggUserFSP test,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local" objectType="account" uuid="{97182a65-61f2-443c-b0fa-477d0821d8c4}">
       <Attributes>
         <Map>
           <entry key="accountFlags">
             <value>
               <List>
                 <String>Normal User Account</String>
                 <String>Password Cannot Expire</String>
               </List>
             </value>
           </entry>
           <entry key="homePhone" value="6555"/>
           <entry key="l" value="Pune"/>
           <entry key="memberOf">
             <value>
               <List>
                 <String>CN=FSPGRP2,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
                 <String>CN=FSPGRP1,OU=ADAggF,OU=unittests2,DC=AUTODOMAIN,DC=LOCAL</String>
                 <String>CN=LocalAggFrame,OU=FSPAggeFrame,OU=unittests,DC=TestNGAD,DC=local</String>
               </List>
             </value>
           </entry>
           <entry key="objectClass">
             <value>
               <List>
                 <String>top</String>
                 <String>person</String>
                 <String>organizationalPerson</String>
                 <String>user</String>
               </List>
             </value>
           </entry>
           <entry key="sn" value="test"/>
           <entry key="st" value="MH"/>
           <entry key="streetAddress" value="SB ROAD"/>
           <entry key="title" value="QA"/>
           <entry key="userPrincipalName" value="AggUserFSP test@TestNGAD.local"/>
         </Map>
       </Attributes>
     </ResourceObject>
    

    And, following XPATH I tried to ignore the order of the elements but still it is not working, can someone help me out here? I referred the discussion here https://github.com/xmlunit/xmlunit/issues/123

    diff = DiffBuilder
                        .compare(control)
                        .withTest(test)
                        .checkForSimilar().checkForIdentical() //ignore the order of 'elements` but check they are identical
                        .normalizeWhitespace()
                        .ignoreComments()
                        .ignoreWhitespace()
                        //.ignoreElementContentWhitespace()
                        .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
                                .whenElementIsNamed("Attributes").thenUse(ElementSelectors.byXPath("./Map/entry[@key]", ElementSelectors.byNameAndText))
                                .elseUse(ElementSelectors.byName)
                                .build()))
                        .build();
    
     
  • Stefan Bodewig

    Stefan Bodewig - 2020-04-13

    checkForSimilar and checkForIdentical are mutually exclusive and the las one you set wins. Unless you you provide a DifferenceEvaluator that downgrades node order differences to IDENTICAL you will never get better than SIMILAR with swapped nodes.
    So if you are really aiming for an identical result you will need to override the DifferenceEvaluator in use as well.
    The ElementSelector's job is to make XMLUnit match the nodes with each other that you consider to be "the same nodes", it does not make node order differences go away.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.