From: <bo...@us...> - 2013-02-02 06:52:31
|
Revision: 501 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=501&view=rev Author: bodewig Date: 2013-02-02 06:52:23 +0000 (Sat, 02 Feb 2013) Log Message: ----------- compare NS URI for xsi:type attributes. Modified Paths: -------------- branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/DifferenceEngine.java branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/XMLConstants.java branches/xmlunit-1.x/tests/java/org/custommonkey/xmlunit/test_Diff.java Modified: branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/DifferenceEngine.java =================================================================== --- branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2013-02-01 16:39:16 UTC (rev 500) +++ branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2013-02-02 06:52:23 UTC (rev 501) @@ -752,16 +752,19 @@ String testLocal = testValue; String testPrefix = ""; int controlColon = controlValue.indexOf(":"); - if (controlColon > 0) { - controlLocal = controlValue.substring(controlColon); + if (controlColon >= 0 && controlColon < controlValue.length() - 1) { + controlLocal = controlValue.substring(controlColon + 1); controlPrefix = controlValue.substring(0, controlColon); } int testColon = testValue.indexOf(":"); - if (testColon > 0) { - testLocal = testValue.substring(testColon); + if (testColon >= 0 && testColon < testValue.length() - 1) { + testLocal = testValue.substring(testColon + 1); testPrefix = testValue.substring(0, testColon); } compare(controlLocal, testLocal, control, test, listener, ATTR_VALUE); + compare(findNamespaceURIForPrefix(control, controlPrefix), + findNamespaceURIForPrefix(test, testPrefix), control, test, + listener, ATTR_VALUE); } /** @@ -979,6 +982,34 @@ } /** + * Try to find the namespace URI that is mapped to the given + * prefix at the given node. + */ + private String findNamespaceURIForPrefix(Node onNode, String prefix) { + if (onNode != null && onNode instanceof Attr) { + onNode = ((Attr) onNode).getOwnerElement(); + } + while (onNode != null && onNode.getNodeType() != Node.ELEMENT_NODE) { + onNode = onNode.getParentNode(); + } + if (onNode == null) { + return null; + } + + NamedNodeMap attrs = onNode.getAttributes(); + Attr attr = null; + if (prefix == null || "".equals(prefix)) { + attr = (Attr) attrs.getNamedItem(XMLConstants.XMLNS_PREFIX); + } else { + attr = (Attr) attrs.getNamedItemNS(XMLConstants.XMLNS_ATTRIBUTE_URI, prefix); + } + if (attr != null) { + return attr.getValue(); + } + return findNamespaceURIForPrefix(onNode.getParentNode(), prefix); + } + + /** * Marker exception thrown by the protected compare() method and passed * upwards through the call stack to the public compare() method. */ Modified: branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/XMLConstants.java =================================================================== --- branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/XMLConstants.java 2013-02-01 16:39:16 UTC (rev 500) +++ branches/xmlunit-1.x/src/java/org/custommonkey/xmlunit/XMLConstants.java 2013-02-02 06:52:23 UTC (rev 501) @@ -53,6 +53,11 @@ public static final String XMLNS_PREFIX = "xmlns"; /** + * xmlns URI + */ + public static final String XMLNS_ATTRIBUTE_URI = "http://www.w3.org/2000/xmlns/"; + + /** * "<" */ public static final String OPEN_START_NODE = "<"; Modified: branches/xmlunit-1.x/tests/java/org/custommonkey/xmlunit/test_Diff.java =================================================================== --- branches/xmlunit-1.x/tests/java/org/custommonkey/xmlunit/test_Diff.java 2013-02-01 16:39:16 UTC (rev 500) +++ branches/xmlunit-1.x/tests/java/org/custommonkey/xmlunit/test_Diff.java 2013-02-02 06:52:23 UTC (rev 501) @@ -924,7 +924,45 @@ assertTrue(diff.toString(), diff.similar()); } - public void XtestXsiTypeSpecialCaseDoesntIgnorePrefix() throws Exception { + public void testXsiTypeSpecialCaseShortLocalName() throws Exception { + String test = "<ns1:Square xsi:type=\"ns1:a\" " + + "xmlns:ns1=\"http://example.com/\" " + + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"; + + String control = "<ns2:Square xsi:type=\"ns2:a\" " + + "xmlns:ns2=\"http://example.com/\" " + + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"; + Diff diff = new Diff(control, test); + assertTrue(diff.toString(), diff.similar()); + } + + public void testXsiTypeSpecialCaseWorksWithDefaultNs() throws Exception { + String test = "<Square xsi:type=\"Shape\" " + + "xmlns=\"http://example.com/\" " + + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"; + + String control = "<ns2:Square xsi:type=\"ns2:Shape\" " + + "xmlns:ns2=\"http://example.com/\" " + + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"; + Diff diff = new Diff(control, test); + assertTrue(diff.toString(), diff.similar()); + } + + public void testXsiTypeSpecialCaseInheritsParentNs() throws Exception { + String test = "<ns1:Shapes xmlns:ns1=\"http://example.com/\">" + + "<ns1:Square xsi:type=\"ns1:Shape\" " + + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>" + + "</ns1:Shapes>"; + + String control = "<ns2:Shapes xmlns:ns2=\"http://example.com/\">" + + "<ns2:Square xsi:type=\"ns2:Shape\" " + + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>" + + "</ns2:Shapes>"; + Diff diff = new Diff(control, test); + assertTrue(diff.toString(), diff.similar()); + } + + public void testXsiTypeSpecialCaseDoesntIgnorePrefix() throws Exception { String test = "<ns1:Square xsi:type=\"ns1:Shape\" " + "xmlns:ns1=\"http://example.com/\" " + "xmlns:ns2=\"http://example.com/another-uri/\" " This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |