001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config.schema.utils;
005:
006: import com.tc.test.TCTestCase;
007: import com.terracottatech.configTest.TestRootDocument;
008: import com.terracottatech.configTest.TestRootDocument.TestRoot;
009:
010: /**
011: * Unit test for {@link StandardXmlObjectComparator}.
012: */
013: public class StandardXmlObjectComparatorTest extends TCTestCase {
014:
015: private static final String ORIGINAL = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
016: + "\n"
017: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
018: + " <element attr4=\"funk\">\n"
019: + " <inner-1>foobar</inner-1>\n"
020: + " <inner-2>147</inner-2>\n"
021: + " <inner-4>\n"
022: + " <complex-1>baz</complex-1>\n"
023: + " </inner-4>\n"
024: + " </element>\n" + "</t:test-root>\n";
025:
026: private static final String EQUIVALENT_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
027: + "\n"
028: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
029: + " <element attr4=\"funk\">\n"
030: + " <inner-1>foobar</inner-1>\n"
031: + " <inner-2> 0147 </inner-2>\n"
032: + " <inner-4>\n"
033: + " <complex-1>baz</complex-1>\n"
034: + " </inner-4>\n"
035: + " </element>\n" + "</t:test-root>\n";
036:
037: private static final String DIFFERENT_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
038: + "\n"
039: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
040: + " <element attr4=\"Funk\">\n"
041: + " <inner-1>foobar</inner-1>\n"
042: + " <inner-2>147</inner-2>\n"
043: + " <inner-4>\n"
044: + " <complex-1>baz</complex-1>\n"
045: + " </inner-4>\n"
046: + " </element>\n" + "</t:test-root>\n";
047:
048: private static final String DIFFERENT_10 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
049: + "\n"
050: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
051: + " <element attr4=\" funk \">\n"
052: + " <inner-1>foobar</inner-1>\n"
053: + " <inner-2>147</inner-2>\n"
054: + " <inner-4>\n"
055: + " <complex-1>baz</complex-1>\n"
056: + " </inner-4>\n"
057: + " </element>\n" + "</t:test-root>\n";
058:
059: private static final String DIFFERENT_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
060: + "\n"
061: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
062: + " <element attr4=\"funk\">\n"
063: + " <inner-1>foobar </inner-1>\n"
064: + " <inner-2>147</inner-2>\n"
065: + " <inner-4>\n"
066: + " <complex-1>baz</complex-1>\n"
067: + " </inner-4>\n"
068: + " </element>\n" + "</t:test-root>\n";
069:
070: private static final String DIFFERENT_3 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
071: + "\n"
072: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
073: + " <element attr4=\"funk\">\n"
074: + " <inner-1>foobar</inner-1>\n"
075: + " <inner-2>148</inner-2>\n"
076: + " <inner-4>\n"
077: + " <complex-1>baz</complex-1>\n"
078: + " </inner-4>\n"
079: + " </element>\n" + "</t:test-root>\n";
080:
081: private static final String DIFFERENT_4 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
082: + "\n"
083: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
084: + " <element attr4=\"funk\">\n"
085: + " <inner-1>foobar</inner-1>\n"
086: + " <inner-2>147</inner-2>\n"
087: + " </element>\n"
088: + "</t:test-root>\n";
089:
090: private static final String DIFFERENT_5 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
091: + "\n"
092: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
093: + " <element attr4=\"funk\">\n"
094: + " <inner-1>foobar</inner-1>\n"
095: + " <inner-2>147</inner-2>\n"
096: + " <inner-4>\n"
097: + " <complex-1>bar</complex-1>\n"
098: + " </inner-4>\n"
099: + " </element>\n" + "</t:test-root>\n";
100:
101: private static final String DIFFERENT_6 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
102: + "\n"
103: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
104: + " <element attr4=\"funk\">\n"
105: + " <inner-1>foobar</inner-1>\n"
106: + " <inner-2>147</inner-2>\n"
107: + " <inner-4>\n"
108: + " <complex-1>Baz</complex-1>\n"
109: + " </inner-4>\n"
110: + " </element>\n" + "</t:test-root>\n";
111:
112: private static final String DIFFERENT_7 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
113: + "\n"
114: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
115: + " <element attr4=\"funk\">\n"
116: + " <inner-1>foobar</inner-1>\n"
117: + " <inner-2>147</inner-2>\n"
118: + " <inner-4>\n"
119: + " <complex-1> baz </complex-1>\n"
120: + " </inner-4>\n"
121: + " </element>\n"
122: + "</t:test-root>\n";
123:
124: private static final String DIFFERENT_8 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
125: + "\n"
126: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
127: + " <element attr4=\"funk\">\n"
128: + " <inner-1>foobar</inner-1>\n"
129: + " <inner-2>147</inner-2>\n"
130: + " <inner-4>\n"
131: + " <complex-1>baz</complex-1>\n"
132: + " <complex-2 />\n"
133: + " </inner-4>\n"
134: + " </element>\n" + "</t:test-root>\n";
135:
136: private static final String DIFFERENT_9 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
137: + "\n"
138: + "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n"
139: + " <element attr4=\"funk\">\n"
140: + " <inner-1>foobar</inner-1>\n"
141: + " <inner-2>147</inner-2>\n"
142: + " <inner-4>\n"
143: + " <complex-1>baz</complex-1>\n"
144: + " <complex-2>4</complex-2>\n"
145: + " </inner-4>\n"
146: + " </element>\n" + "</t:test-root>\n";
147:
148: private StandardXmlObjectComparator comparator;
149:
150: public void setUp() throws Exception {
151: comparator = new StandardXmlObjectComparator();
152: }
153:
154: public void testEquivalent() throws Exception {
155: checkEquals(ORIGINAL, ORIGINAL);
156: checkEquals(ORIGINAL, EQUIVALENT_1);
157: }
158:
159: public void testNotEquivalent() throws Exception {
160: checkNotEquals(ORIGINAL, DIFFERENT_1, "/element/attr4");
161: checkNotEquals(ORIGINAL, DIFFERENT_2, "/element/inner1");
162: checkNotEquals(ORIGINAL, DIFFERENT_3, "/element/inner2");
163: checkNotEquals(ORIGINAL, DIFFERENT_4, "/element/inner4");
164: checkNotEquals(ORIGINAL, DIFFERENT_5,
165: "/element/inner4/complex1");
166: checkNotEquals(ORIGINAL, DIFFERENT_6,
167: "/element/inner4/complex1");
168: checkNotEquals(ORIGINAL, DIFFERENT_7,
169: "/element/inner4/complex1");
170: checkNotEquals(ORIGINAL, DIFFERENT_8,
171: "/element/inner4/complex2");
172: checkNotEquals(ORIGINAL, DIFFERENT_9,
173: "/element/inner4/complex2");
174: checkNotEquals(ORIGINAL, DIFFERENT_10, "/element/attr4");
175: }
176:
177: private void checkNotEquals(String one, String two, String where)
178: throws Exception {
179: check(one, two, where);
180: }
181:
182: private void checkEquals(String one, String two) throws Exception {
183: check(one, two, null);
184: }
185:
186: private void check(String one, String two, String where)
187: throws Exception {
188: TestRootDocument docOne = TestRootDocument.Factory.parse(one);
189: TestRootDocument docTwo = TestRootDocument.Factory.parse(two);
190:
191: TestRoot rootOne = docOne.getTestRoot();
192: TestRoot rootTwo = docTwo.getTestRoot();
193:
194: try {
195: this .comparator.checkEquals(rootOne, rootTwo);
196: if (where != null)
197: fail("Should've gotten exception, but didn't.");
198: } catch (NotEqualException nee) {
199: if (where == null)
200: throw nee;
201: assertContains(where, nee.getMessage());
202: }
203:
204: assertEquals(where == null, this.comparator.equals(rootOne,
205: rootTwo));
206: }
207:
208: }
|