From: <bo...@us...> - 2007-04-12 04:43:36
|
Revision: 177 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=177&view=rev Author: bodewig Date: 2007-04-11 21:43:38 -0700 (Wed, 11 Apr 2007) Log Message: ----------- Add a configuration option to ignore differences in attribute order Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2007-04-12 04:41:45 UTC (rev 176) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2007-04-12 04:43:38 UTC (rev 177) @@ -445,7 +445,8 @@ if (compareTo != null) { compareAttribute(nextAttr, compareTo, listener); - + + if (!XMLUnit.getIgnoreAttributeOrder()) { Attr attributeItem = (Attr) testAttr.item(i); String testAttrName = "[attribute absent]"; if (attributeItem != null) { @@ -453,6 +454,7 @@ } compare(attrName, testAttrName, nextAttr, compareTo, listener, ATTR_SEQUENCE); + } } else { compare(attrName, null, control, test, listener, ATTR_NAME_NOT_FOUND); Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-04-12 04:41:45 UTC (rev 176) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-04-12 04:43:38 UTC (rev 177) @@ -71,6 +71,7 @@ private static boolean ignoreComments = false; private static boolean normalize = false; private static boolean normalizeWhitespace = false; + private static boolean ignoreAttributeOrder = false; private static final String STRIP_WHITESPACE_STYLESHEET = new StringBuffer(XMLConstants.XML_DECLARATION) @@ -724,5 +725,35 @@ public static boolean getNormalizeWhitespace() { return normalizeWhitespace; } + + /** + * Whether to ignore the order of attributes on an element. + * + * <p>The order of attributes has never been relevant for XML + * documents, still XMLUnit will consider two pieces of XML + * not-identical (but similar) if they differ in order of + * attributes. Set this option to false to ignore the order.</p> + * + * <p>The default value is false for backwards compatibility + * reasons.</p> + */ + public static void setIgnoreAttributeOrder(boolean b) { + ignoreAttributeOrder = b; + } + + /** + * Whether to ignore the order of attributes on an element. + * + * <p>The order of attributes has never been relevant for XML + * documents, still XMLUnit will consider two pieces of XML + * not-identical (but similar) if they differ in order of + * attributes. Set this option to false to ignore the order.</p> + * + * <p>The default value is false for backwards compatibility + * reasons.</p> + */ + public static boolean getIgnoreAttributeOrder() { + return ignoreAttributeOrder; + } } Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-04-12 04:41:45 UTC (rev 176) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-04-12 04:43:38 UTC (rev 177) @@ -664,6 +664,17 @@ } public void testAttributeSequence() throws Exception { + testAttributeSequence(ATTR_SEQUENCE_ID); + resetListener(); + XMLUnit.setIgnoreAttributeOrder(true); + try { + testAttributeSequence(-1); + } finally { + XMLUnit.setIgnoreAttributeOrder(false); + } + } + + private void testAttributeSequence(int expected) throws Exception { Element control = document.createElement("foo"); Element test = document.createElement("foo"); OrderPreservingNamedNodeMap controlMap = @@ -683,10 +694,21 @@ } engine.compareElementAttributes(control, test, controlMap, testMap, listener); - assertEquals(ATTR_SEQUENCE_ID, listener.comparingWhat); + assertEquals(expected, listener.comparingWhat); } public void testAttributeSequenceNS() throws Exception { + testAttributeSequenceNS(ATTR_SEQUENCE_ID); + resetListener(); + XMLUnit.setIgnoreAttributeOrder(true); + try { + testAttributeSequenceNS(-1); + } finally { + XMLUnit.setIgnoreAttributeOrder(false); + } + } + + private void testAttributeSequenceNS(int expected) throws Exception { Element control = document.createElementNS("ns", "foo"); Element test = document.createElementNS("ns", "foo"); OrderPreservingNamedNodeMap controlMap = @@ -706,7 +728,7 @@ } engine.compareElementAttributes(control, test, controlMap, testMap, listener); - assertEquals(ATTR_SEQUENCE_ID, listener.comparingWhat); + assertEquals(expected, listener.comparingWhat); } private void listenToDifferences(String control, String test) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |