001: package org.uispec4j.xml;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005: import org.uispec4j.utils.UnitTestCase;
006:
007: public class XmlAssertTest extends UnitTestCase {
008: public void testNoDiff() throws Exception {
009: String xml = "<root>" + " <child attr='1'>"
010: + " <subChild attr='1.1'/>" + " </child>"
011: + "</root>";
012: checkXml(xml, xml, true, true);
013: }
014:
015: public void testDifferent() throws Exception {
016: String xml1 = "<root>" + " <child attr='1'>"
017: + " <subChild attr='1.1'/>" + " </child>"
018: + "</root>";
019: String xml2 = "<root>" + " <child attr='1'>"
020: + " <subChild attr='1.2'/>" + " </child>"
021: + "</root>";
022: checkXml(xml1, xml2, false, false);
023: }
024:
025: public void testChildrenInDifferentOrderAreEquivalentButNotEquals()
026: throws Exception {
027: String xml1 = "<hello><tag1/><tag2/><tag3/></hello>";
028: String xml2 = "<hello><tag3/><tag1/><tag2/></hello>";
029: checkXml(xml1, xml2, true, false);
030: }
031:
032: public void testAttributeDifference() throws Exception {
033: String xml1 = "<hello><tag1/><tag2 a=\"A\"/><tag3/></hello>";
034: String xml2 = "<hello><tag3/><tag1/><tag2/></hello>";
035: checkXml(xml1, xml2, false, false);
036: }
037:
038: public void testAttributeInDifferentOrder() throws Exception {
039: String xml1 = "<hello><tag1 a=\"A\" b=\"B\"/></hello>";
040: String xml2 = "<hello><tag1 b=\"B\" a=\"A\"/></hello>";
041: checkXml(xml1, xml2, true, true);
042: }
043:
044: public void testAgainDifferentOrders() throws Exception {
045: String xml1 = "<struct>" + " <val1>0</val1>"
046: + " <val2>Paris-Tokyo</val2>" + " <val1>1</val1>"
047: + "</struct>";
048: String xml2 = "<struct>" + " <val2>Paris-Tokyo</val2>"
049: + " <val1>0</val1>" + " <val1>1</val1>" + "</struct>";
050:
051: checkXml(xml1, xml2, true, false);
052: }
053:
054: public void testIdenticalTagCountAreTakenIntoAccount()
055: throws Exception {
056: String xml1 = "<root>" + " <child/>" + " <child/>"
057: + "</root>";
058: String xml2 = "<root>" + " <child/>" + " <child/>"
059: + " <child/>" + "</root>";
060: checkXml(xml1, xml2, false, false);
061: }
062:
063: public void testSameCharacters() throws Exception {
064: String xml1 = "<hello><tag1>XYZ</tag1></hello>";
065: String xml2 = "<hello><tag1>XYZ</tag1></hello>";
066: checkXml(xml1, xml2, true, true);
067: }
068:
069: public void testWhitespace() throws Exception {
070: String xml1 = "<hello>" + " <list>" + " <tag1>XYZ</tag1>"
071: + " <tag2>PQR</tag2>" + " <tag3>"
072: + " <tag4/>" + " </tag3>" + " </list>"
073: + "</hello>";
074: String xml2 = "<hello><list><tag1>XYZ</tag1><tag2>PQR</tag2><tag3><tag4/></tag3></list></hello>";
075: checkXml(xml1, xml2, true, true);
076: checkXml(xml2, xml1, true, true);
077: }
078:
079: public void testNotSameCharacters() throws Exception {
080: String xml1 = "<hello><tag1>ABC</tag1></hello>";
081: String xml2 = "<hello><tag1>XYZ</tag1></hello>";
082: checkXml(xml1, xml2, false, false);
083: }
084:
085: public void testSame() throws Exception {
086: String xml1 = "<hello></hello>";
087: String xml2 = "<hello/>";
088: checkXml(xml1, xml2, true, true);
089: }
090:
091: private void checkXml(String xmlA, String xmlB,
092: boolean isEquivalent, boolean isEqual) throws Exception {
093: if (isEqual) {
094: assertTrue(isEquivalent);
095: XmlAssert.assertEquivalent(xmlA, xmlB);
096: XmlAssert.assertEquals(xmlA, xmlB);
097: } else {
098: if (!isEquivalent) {
099: checkAssertEquivalentFailure(xmlA, xmlB);
100: } else {
101: XmlAssert.assertEquivalent(xmlA, xmlB);
102: }
103: checkAssertEqualsFailure(xmlA, xmlB);
104: }
105: }
106:
107: private void checkAssertEquivalentFailure(String xmlA, String xmlB)
108: throws Exception {
109: try {
110: XmlAssert.assertEquivalent(xmlA, xmlB);
111: throw new AssertionFailureNotDetectedError();
112: } catch (AssertionFailedError e) {
113: }
114: }
115:
116: private void checkAssertEqualsFailure(String xmlA, String xmlB)
117: throws Exception {
118: try {
119: XmlAssert.assertEquals(xmlA, xmlB);
120: throw new AssertionFailureNotDetectedError();
121: } catch (AssertionFailedError e) {
122: }
123: }
124: }
|