|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import unittest |
|
18 from testautomation import compare_xml |
|
19 |
|
20 class TestGetXmlEncoding(unittest.TestCase): |
|
21 def assert_enc_eq(self, encoding, xml_data): |
|
22 self.assertEquals(encoding, compare_xml._get_xml_encoding(xml_data)) |
|
23 |
|
24 def test_get_encoding(self): |
|
25 self.assert_enc_eq('ASCII', u"""<?xml version="1.0" encoding="ASCII"?><root x="\u0084"/>""".encode('ascii', 'xmlcharrefreplace')) |
|
26 self.assert_enc_eq('ISO-8859-1', u"""<?xml version="1.0" encoding = "ISO-8859-1"?><root x="\u0084"/>""".encode('latin1')) |
|
27 self.assert_enc_eq('utf-8', u"""<?xml version="1.0" encoding='utf-8'?><root x="\u0084"/>""".encode('utf-8')) |
|
28 self.assert_enc_eq('UTF-16', u"""<?xml encoding = 'UTF-16' version="1.0"?><root x="\u0084"/>""".encode('utf-16')) |
|
29 self.assert_enc_eq('', u"""<?xml version="1.0"?><root x="\u0084"/>""".encode('utf-8')) |
|
30 self.assert_enc_eq('', u"""<root x="\u0084"/>""".encode('utf-8')) |
|
31 |
|
32 class TestCompareXml(unittest.TestCase): |
|
33 REF_DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
34 <rootElem my_attr="jeje"> |
|
35 <subElem>a sub-element</subElem> |
|
36 <subElem>another sub-element</subElem> |
|
37 <subElem>yet another sub-element</subElem> |
|
38 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
39 </rootElem>""" |
|
40 |
|
41 def assert_comparison_result_equals(self, data1, data2, expected_result, msg=None, **kwargs): |
|
42 class DebugStream: |
|
43 def __init__(self): |
|
44 self.messages = [] |
|
45 def write(self, data): |
|
46 self.messages.append(data) |
|
47 |
|
48 ds = DebugStream() |
|
49 kwargs['debug_stream'] = ds |
|
50 result = compare_xml.compare_xml_documents(data1, data2, **kwargs) |
|
51 if result != expected_result: |
|
52 d = [] |
|
53 if msg != None: d.append(msg + '\n') |
|
54 d.append("Comparison results are not equal (expected %s, got %s)\n" % (expected_result, result)) |
|
55 d.append("Debug output:\n") |
|
56 d.extend(ds.messages) |
|
57 self.fail(''.join(d)) |
|
58 |
|
59 def test_identical(self): |
|
60 self.assert_comparison_result_equals(self.REF_DATA, self.REF_DATA, True) |
|
61 |
|
62 def test_different_whitespace(self): |
|
63 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
64 <rootElem |
|
65 my_attr="jeje"> |
|
66 <subElem>a sub-element</subElem><subElem>another sub-element</subElem> |
|
67 <subElem>yet another sub-element</subElem> |
|
68 <subElem |
|
69 attr1="attribute 1" |
|
70 attr2="attribute 2"/> |
|
71 </rootElem>""" |
|
72 self.assert_comparison_result_equals(self.REF_DATA, DATA, True) |
|
73 |
|
74 def test_attrs_in_different_order(self): |
|
75 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
76 <rootElem my_attr="jeje"> |
|
77 <subElem>a sub-element</subElem> |
|
78 <subElem>another sub-element</subElem> |
|
79 <subElem>yet another sub-element</subElem> |
|
80 <subElem attr2="attribute 2" attr1="attribute 1"/> |
|
81 </rootElem>""" |
|
82 self.assert_comparison_result_equals(self.REF_DATA, DATA, True) |
|
83 |
|
84 def test_elements_in_different_order(self): |
|
85 REF_DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
86 <rootElem my_attr="jeje"> |
|
87 <subElem><x>1</x><y/><z z_attr="1"></z></subElem> |
|
88 <subElem><x>2</x><y/><z z_attr="2"></z></subElem> |
|
89 <subElem><x>3</x><y/><z z_attr="3"></z></subElem> |
|
90 <subElem><x><y></y><z/></x></subElem> |
|
91 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
92 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
93 <test attr="yeah"/> |
|
94 </rootElem>""" |
|
95 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
96 <rootElem my_attr="jeje"> |
|
97 <subElem attr2="attribute 2" attr1="attribute 1"/> |
|
98 <subElem><z z_attr="3"></z><y/><x>3</x></subElem> |
|
99 <subElem><x>1</x><y/><z z_attr="1"></z></subElem> |
|
100 <test attr="yeah"/> |
|
101 <subElem><x><z/><y/></x></subElem> |
|
102 <subElem><y/><x>2</x><z z_attr="2"></z></subElem> |
|
103 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
104 </rootElem>""" |
|
105 self.assert_comparison_result_equals(REF_DATA, DATA, True) |
|
106 |
|
107 def test_different_root_contents(self): |
|
108 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
109 <rootElem my_attr="jeje"> |
|
110 some text content in root |
|
111 <subElem>a sub-element</subElem> |
|
112 <subElem>another sub-element</subElem> |
|
113 <subElem>yet another sub-element</subElem> |
|
114 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
115 </rootElem>""" |
|
116 self.assert_comparison_result_equals(self.REF_DATA, DATA, False) |
|
117 |
|
118 def test_different_subelem_contents(self): |
|
119 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
120 <rootElem my_attr="jeje"> |
|
121 <subElem>a sub-element (with different content)</subElem> |
|
122 <subElem>another sub-element</subElem> |
|
123 <subElem>yet another sub-element</subElem> |
|
124 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
125 </rootElem>""" |
|
126 self.assert_comparison_result_equals(self.REF_DATA, DATA, False) |
|
127 |
|
128 def test_different_attrs_in_root(self): |
|
129 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
130 <rootElem my_attr="jeh"> |
|
131 <subElem>a sub-element (with different content)</subElem> |
|
132 <subElem>another sub-element</subElem> |
|
133 <subElem>yet another sub-element</subElem> |
|
134 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
135 </rootElem>""" |
|
136 self.assert_comparison_result_equals(self.REF_DATA, DATA, False) |
|
137 |
|
138 def test_missing_attrs_in_root(self): |
|
139 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
140 <rootElem> |
|
141 <subElem>a sub-element (with different content)</subElem> |
|
142 <subElem>another sub-element</subElem> |
|
143 <subElem>yet another sub-element</subElem> |
|
144 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
145 </rootElem>""" |
|
146 self.assert_comparison_result_equals(self.REF_DATA, DATA, False) |
|
147 |
|
148 def test_different_attrs_in_subelem(self): |
|
149 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
150 <rootElem my_attr="jeje"> |
|
151 <subElem>a sub-element (with different content)</subElem> |
|
152 <subElem>another sub-element</subElem> |
|
153 <subElem>yet another sub-element</subElem> |
|
154 <subElem attr1="attribute I" attr2="attribute II"/> |
|
155 </rootElem>""" |
|
156 self.assert_comparison_result_equals(self.REF_DATA, DATA, False) |
|
157 |
|
158 def test_different_whitespace_in_subelem_content(self): |
|
159 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
160 <rootElem my_attr="jeje"> |
|
161 <subElem> a sub-element </subElem> |
|
162 <subElem>another sub-element</subElem> |
|
163 <subElem>yet another sub-element</subElem> |
|
164 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
165 </rootElem>""" |
|
166 self.assert_comparison_result_equals(self.REF_DATA, DATA, True) |
|
167 |
|
168 def test_ignore_root_namespace(self): |
|
169 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
170 <rootElem xmlns:myns="http://my.namespace.com/schema" my_attr="jeje"> |
|
171 <subElem>a sub-element</subElem> |
|
172 <subElem>another sub-element</subElem> |
|
173 <subElem>yet another sub-element</subElem> |
|
174 <subElem attr1="attribute 1" attr2="attribute 2"/> |
|
175 </rootElem>""" |
|
176 self.assert_comparison_result_equals(self.REF_DATA, DATA, True) |
|
177 |
|
178 def test_ignore_namespace(self): |
|
179 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
180 <myns:rootElem xmlns:myns="http://my.namespace.com/schema" my_attr="jeje"> |
|
181 <myns:subElem>a sub-element</myns:subElem> |
|
182 <myns:subElem>another sub-element</myns:subElem> |
|
183 <myns:subElem>yet another sub-element</myns:subElem> |
|
184 <myns:subElem attr1="attribute 1" attr2="attribute 2"/> |
|
185 </myns:rootElem>""" |
|
186 self.assert_comparison_result_equals(self.REF_DATA, DATA, True, ignore_namespaces=True) |
|
187 |
|
188 def test_ignore_empty_tags(self): |
|
189 REF_DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
190 <rootElem my_attr="jeje"> |
|
191 <subElem>a sub-element</subElem> |
|
192 <subElem> |
|
193 <subSubElem>1</subSubElem> |
|
194 <subSubElem>2</subSubElem> |
|
195 <emptySubElem/> |
|
196 </subElem> |
|
197 <emptySubElem/> |
|
198 </rootElem>""" |
|
199 DATA = """<?xml version="1.0" encoding="UTF-8"?> |
|
200 <rootElem my_attr="jeje"> |
|
201 <subElem>a sub-element</subElem> |
|
202 <subElem> |
|
203 <subSubElem>1</subSubElem> |
|
204 <subSubElem>2</subSubElem> |
|
205 </subElem> |
|
206 </rootElem>""" |
|
207 self.assert_comparison_result_equals(REF_DATA, DATA, False) |
|
208 self.assert_comparison_result_equals(DATA, REF_DATA, False) |
|
209 self.assert_comparison_result_equals(REF_DATA, DATA, False, ignored_empty_tags=['/rootElem/emptySubElem']) |
|
210 self.assert_comparison_result_equals(DATA, REF_DATA, False, ignored_empty_tags=['/rootElem/emptySubElem']) |
|
211 self.assert_comparison_result_equals(REF_DATA, DATA, False, ignored_empty_tags=['/rootElem/subElem/emptySubElem']) |
|
212 self.assert_comparison_result_equals(DATA, REF_DATA, False, ignored_empty_tags=['/rootElem/subElem/emptySubElem']) |
|
213 self.assert_comparison_result_equals(REF_DATA, DATA, True, ignored_empty_tags=['/rootElem/emptySubElem', '/rootElem/subElem/emptySubElem']) |
|
214 self.assert_comparison_result_equals(DATA, REF_DATA, True, ignored_empty_tags=['/rootElem/emptySubElem', '/rootElem/subElem/emptySubElem']) |
|
215 #self.assert_comparison_result_equals(DATA, REF_DATA, True, ignored_empty_tags=['emptySubElem']) |
|
216 #self.assert_comparison_result_equals(REF_DATA, DATA, False, ignored_empty_tags=[('emptySubElem', 1)]) |
|
217 #self.assert_comparison_result_equals(DATA, REF_DATA, False, ignored_empty_tags=[('emptySubElem', 1)]) |
|
218 #self.assert_comparison_result_equals(REF_DATA, DATA, False, ignored_empty_tags=[('emptySubElem', 2)]) |
|
219 #self.assert_comparison_result_equals(DATA, REF_DATA, False, ignored_empty_tags=[('emptySubElem', 2)]) |
|
220 #self.assert_comparison_result_equals(REF_DATA, DATA, True, ignored_empty_tags=[('emptySubElem', 1), ('emptySubElem', 2)]) |
|
221 #self.assert_comparison_result_equals(DATA, REF_DATA, True, ignored_empty_tags=[('emptySubElem', 1), ('emptySubElem', 2)]) |
|
222 |
|
223 def test_check_encoding(self): |
|
224 DATA_DICT = { |
|
225 'ASCII': u"""<?xml version="1.0" encoding="ASCII"?><root x="\u0084"/>""".encode('ascii', 'xmlcharrefreplace'), |
|
226 'ISO-8859-1': u"""<?xml version="1.0" encoding = "ISO-8859-1"?><root x="\u0084"/>""".encode('latin1'), |
|
227 'UTF8': u"""<?xml version="1.0" encoding='UTF-8'?><root x="\u0084"/>""".encode('utf-8'), |
|
228 'UTF-16': u"""<?xml version="1.0" encoding="UTF-16"?><root x="\u0084"/>""".encode('utf-16'), |
|
229 'None1': u"""<?xml version="1.0"?><root x="\u0084"/>""".encode('utf-8'), |
|
230 'None2': u"""<root x="\u0084"/>""".encode('utf-8'), |
|
231 } |
|
232 |
|
233 for key1 in DATA_DICT.keys(): |
|
234 for key2 in DATA_DICT.keys(): |
|
235 if key1 != key2 and not (key1.startswith('None') or key2.startswith('None')): |
|
236 self.assert_comparison_result_equals( |
|
237 DATA_DICT[key1], DATA_DICT[key2], |
|
238 False, "Encoding check failed for '%s' vs. '%s'" % (key1, key2), |
|
239 check_encoding=True) |
|
240 self.assert_comparison_result_equals( |
|
241 DATA_DICT[key1], DATA_DICT[key2], |
|
242 True, "Comparison without encoding check failed for '%s' vs. '%s'" % (key1, key2), |
|
243 check_encoding=False) |