private static void assertAttributesEqual ( String message , XmlCursor expected , XmlCursor actual ) { Map < QName , String > map1 = new HashMap < QName , String > ( ) ; Map < QName , String > map2 = new HashMap < QName , String > ( ) ; boolean attr1 = expected . toFirstAttribute ( ) ; boolean attr2 = actual . toFirstAttribute ( ) ; if ( attr1 != attr2 ) { if ( expected . isAttr ( ) || actual . isAttr ( ) ) { expected . toParent ( ) ; } fail ( message , "Differing number of attributes for element: '" + QNameHelper . pretty ( expected . getName ( ) ) + "'" ) ; } else if ( ! attr1 ) { return ; } else { map1 . put ( expected . getName ( ) , expected . getTextValue ( ) ) ; map2 . put ( actual . getName ( ) , actual . getTextValue ( ) ) ; } while ( true ) { attr1 = expected . toNextAttribute ( ) ; attr2 = actual . toNextAttribute ( ) ; if ( attr1 != attr2 ) { if ( expected . isAttr ( ) || actual . isAttr ( ) ) { expected . toParent ( ) ; } fail ( message , "Differing number of attributes for element: '" + QNameHelper . pretty ( expected . getName ( ) ) + "'" ) ; } else if ( ! attr1 ) { break ; } else { map1 . put ( expected . getName ( ) , expected . getTextValue ( ) ) ; map2 . put ( actual . getName ( ) , actual . getTextValue ( ) ) ; } } expected . toParent ( ) ; actual . toParent ( ) ; // check that attribute maps match, neglecting order Iterator < QName > iter = map1 . keySet ( ) . iterator ( ) ; while ( iter . hasNext ( ) ) { QName name = iter . next ( ) ; String value1 = map1 . get ( name ) ; String value2 = map2 . get ( name ) ; if ( value2 == null ) { fail ( message , "Expected attribute value missing for element: " + QNameHelper . pretty ( expected . getName ( ) ) + "--> '" + name + "'" ) ; } else if ( ! value2 . equals ( value1 ) ) { fail ( message , "Attribute values for element '" + QNameHelper . pretty ( expected . getName ( ) ) + "': Expected '" + value1 + "', Actual '" + value2 + "'" ) ; } } }
Compares the attributes of the elements at the current position of two XmlCursors . The ordering of the attributes is ignored in the comparison . Fails the JUnit test case if the attributes or their values are different .