1 4 package com.tc.config.schema.utils; 5 6 import com.tc.test.TCTestCase; 7 import com.terracottatech.configTest.TestRootDocument; 8 import com.terracottatech.configTest.TestRootDocument.TestRoot; 9 10 13 public class StandardXmlObjectComparatorTest extends TCTestCase { 14 15 private static final String ORIGINAL = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 16 "\n" + 17 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 18 " <element attr4=\"funk\">\n" + 19 " <inner-1>foobar</inner-1>\n" + 20 " <inner-2>147</inner-2>\n" + 21 " <inner-4>\n" + 22 " <complex-1>baz</complex-1>\n" + 23 " </inner-4>\n" + 24 " </element>\n" + 25 "</t:test-root>\n"; 26 27 private static final String EQUIVALENT_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 28 "\n" + 29 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 30 " <element attr4=\"funk\">\n" + 31 " <inner-1>foobar</inner-1>\n" + 32 " <inner-2> 0147 </inner-2>\n" + 33 " <inner-4>\n" + 34 " <complex-1>baz</complex-1>\n" + 35 " </inner-4>\n" + 36 " </element>\n" + 37 "</t:test-root>\n"; 38 39 private static final String DIFFERENT_1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 40 "\n" + 41 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 42 " <element attr4=\"Funk\">\n" + 43 " <inner-1>foobar</inner-1>\n" + 44 " <inner-2>147</inner-2>\n" + 45 " <inner-4>\n" + 46 " <complex-1>baz</complex-1>\n" + 47 " </inner-4>\n" + 48 " </element>\n" + 49 "</t:test-root>\n"; 50 51 private static final String DIFFERENT_10 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 52 "\n" + 53 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 54 " <element attr4=\" funk \">\n" + 55 " <inner-1>foobar</inner-1>\n" + 56 " <inner-2>147</inner-2>\n" + 57 " <inner-4>\n" + 58 " <complex-1>baz</complex-1>\n" + 59 " </inner-4>\n" + 60 " </element>\n" + 61 "</t:test-root>\n"; 62 63 private static final String DIFFERENT_2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 64 "\n" + 65 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 66 " <element attr4=\"funk\">\n" + 67 " <inner-1>foobar </inner-1>\n" + 68 " <inner-2>147</inner-2>\n" + 69 " <inner-4>\n" + 70 " <complex-1>baz</complex-1>\n" + 71 " </inner-4>\n" + 72 " </element>\n" + 73 "</t:test-root>\n"; 74 75 private static final String DIFFERENT_3 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 76 "\n" + 77 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 78 " <element attr4=\"funk\">\n" + 79 " <inner-1>foobar</inner-1>\n" + 80 " <inner-2>148</inner-2>\n" + 81 " <inner-4>\n" + 82 " <complex-1>baz</complex-1>\n" + 83 " </inner-4>\n" + 84 " </element>\n" + 85 "</t:test-root>\n"; 86 87 private static final String DIFFERENT_4 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 88 "\n" + 89 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 90 " <element attr4=\"funk\">\n" + 91 " <inner-1>foobar</inner-1>\n" + 92 " <inner-2>147</inner-2>\n" + 93 " </element>\n" + 94 "</t:test-root>\n"; 95 96 private static final String DIFFERENT_5 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 97 "\n" + 98 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 99 " <element attr4=\"funk\">\n" + 100 " <inner-1>foobar</inner-1>\n" + 101 " <inner-2>147</inner-2>\n" + 102 " <inner-4>\n" + 103 " <complex-1>bar</complex-1>\n" + 104 " </inner-4>\n" + 105 " </element>\n" + 106 "</t:test-root>\n"; 107 108 private static final String DIFFERENT_6 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 109 "\n" + 110 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 111 " <element attr4=\"funk\">\n" + 112 " <inner-1>foobar</inner-1>\n" + 113 " <inner-2>147</inner-2>\n" + 114 " <inner-4>\n" + 115 " <complex-1>Baz</complex-1>\n" + 116 " </inner-4>\n" + 117 " </element>\n" + 118 "</t:test-root>\n"; 119 120 private static final String DIFFERENT_7 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 121 "\n" + 122 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 123 " <element attr4=\"funk\">\n" + 124 " <inner-1>foobar</inner-1>\n" + 125 " <inner-2>147</inner-2>\n" + 126 " <inner-4>\n" + 127 " <complex-1> baz </complex-1>\n" + 128 " </inner-4>\n" + 129 " </element>\n" + 130 "</t:test-root>\n"; 131 132 private static final String DIFFERENT_8 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 133 "\n" + 134 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 135 " <element attr4=\"funk\">\n" + 136 " <inner-1>foobar</inner-1>\n" + 137 " <inner-2>147</inner-2>\n" + 138 " <inner-4>\n" + 139 " <complex-1>baz</complex-1>\n" + 140 " <complex-2 />\n" + 141 " </inner-4>\n" + 142 " </element>\n" + 143 "</t:test-root>\n"; 144 145 private static final String DIFFERENT_9 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + 146 "\n" + 147 "<t:test-root xmlns:t=\"http://www.terracottatech.com/config-test\">\n" + 148 " <element attr4=\"funk\">\n" + 149 " <inner-1>foobar</inner-1>\n" + 150 " <inner-2>147</inner-2>\n" + 151 " <inner-4>\n" + 152 " <complex-1>baz</complex-1>\n" + 153 " <complex-2>4</complex-2>\n" + 154 " </inner-4>\n" + 155 " </element>\n" + 156 "</t:test-root>\n"; 157 158 private StandardXmlObjectComparator comparator; 159 160 public void setUp() throws Exception { 161 comparator = new StandardXmlObjectComparator(); 162 } 163 164 public void testEquivalent() throws Exception { 165 checkEquals(ORIGINAL, ORIGINAL); 166 checkEquals(ORIGINAL, EQUIVALENT_1); 167 } 168 169 public void testNotEquivalent() throws Exception { 170 checkNotEquals(ORIGINAL, DIFFERENT_1, "/element/attr4"); 171 checkNotEquals(ORIGINAL, DIFFERENT_2, "/element/inner1"); 172 checkNotEquals(ORIGINAL, DIFFERENT_3, "/element/inner2"); 173 checkNotEquals(ORIGINAL, DIFFERENT_4, "/element/inner4"); 174 checkNotEquals(ORIGINAL, DIFFERENT_5, "/element/inner4/complex1"); 175 checkNotEquals(ORIGINAL, DIFFERENT_6, "/element/inner4/complex1"); 176 checkNotEquals(ORIGINAL, DIFFERENT_7, "/element/inner4/complex1"); 177 checkNotEquals(ORIGINAL, DIFFERENT_8, "/element/inner4/complex2"); 178 checkNotEquals(ORIGINAL, DIFFERENT_9, "/element/inner4/complex2"); 179 checkNotEquals(ORIGINAL, DIFFERENT_10, "/element/attr4"); 180 } 181 182 private void checkNotEquals(String one, String two, String where) throws Exception { 183 check(one, two, where); 184 } 185 186 private void checkEquals(String one, String two) throws Exception { 187 check(one, two, null); 188 } 189 190 private void check(String one, String two, String where) throws Exception { 191 TestRootDocument docOne = TestRootDocument.Factory.parse(one); 192 TestRootDocument docTwo = TestRootDocument.Factory.parse(two); 193 194 TestRoot rootOne = docOne.getTestRoot(); 195 TestRoot rootTwo = docTwo.getTestRoot(); 196 197 try { 198 this.comparator.checkEquals(rootOne, rootTwo); 199 if (where != null) fail("Should've gotten exception, but didn't."); 200 } catch (NotEqualException nee) { 201 if (where == null) throw nee; 202 assertContains(where, nee.getMessage()); 203 } 204 205 assertEquals(where == null, this.comparator.equals(rootOne, rootTwo)); 206 } 207 208 } 209 | Popular Tags |