Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/res2/NodeUtilsTest.java

raw
1 /*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.ide.common.res2;
18
19 import com.android.SdkConstants;
20 import junit.framework.TestCase;
21
22 import org.w3c.dom.Document;
23 import org.w3c.dom.NamedNodeMap;
24 import org.w3c.dom.Node;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.ParserConfigurationException;
29
30 public class NodeUtilsTest extends TestCase {
31
32 public void testBasicAttributes() throws Exception {
33 Document document = createDocument();
34
35 // create two nodes
36 Node node1 = document.createElement("N1");
37 Node node2 = document.createElement("N2");
38
39 NodeUtils.addAttribute(document, node1, null, "foo", "bar");
40 NodeUtils.addAttribute(document, node2, null, "foo", "bar");
41 assertTrue(NodeUtils.compareAttributes(node1.getAttributes(), node2.getAttributes()));
42
43 NodeUtils.addAttribute(document, node1, null, "foo2", "bar2");
44 assertFalse(NodeUtils.compareAttributes(node1.getAttributes(), node2.getAttributes()));
45
46 NodeUtils.addAttribute(document, node2, null, "foo2", "bar");
47 assertFalse(NodeUtils.compareAttributes(node1.getAttributes(), node2.getAttributes()));
48 }
49
50 public void testNamespaceAttributes() throws Exception {
51 Document document = createDocument();
52
53 // create two nodes
54 Node node1 = document.createElement("N1");
55 Node node2 = document.createElement("N2");
56
57 NodeUtils.addAttribute(document, node1, "http://some.uri/", "foo", "bar");
58 NodeUtils.addAttribute(document, node2, "http://some.uri/", "foo", "bar");
59 assertTrue(NodeUtils.compareAttributes(node1.getAttributes(), node2.getAttributes()));
60
61 NodeUtils.addAttribute(document, node1, "http://some.uri/", "foo2", "bar2");
62 NodeUtils.addAttribute(document, node2, "http://some.other.uri/", "foo2", "bar2");
63 assertFalse(NodeUtils.compareAttributes(node1.getAttributes(), node2.getAttributes()));
64 }
65
66 public void testNodesWithChildrenNodes() throws Exception {
67 Document document = createDocument();
68
69 // create two nodes
70 Node node1 = document.createElement("some-node");
71 Node node2 = document.createElement("some-node");
72
73 Node child1a = document.createElement("child1");
74 Node child1b = document.createElement("child2");
75 node1.appendChild(child1a);
76 node1.appendChild(child1b);
77
78 Node child2a = document.createElement("child1");
79 Node child2b = document.createElement("child2");
80 node2.appendChild(child2a);
81 node2.appendChild(child2b);
82
83 assertTrue(NodeUtils.compareElementNode(node1, node2, true));
84 }
85
86 public void testNodesWithChildrenNodesInWrongOrder() throws Exception {
87 Document document = createDocument();
88
89 // create two nodes
90 Node node1 = document.createElement("some-node");
91 Node node2 = document.createElement("some-node");
92
93 Node child1a = document.createElement("child1");
94 Node child1b = document.createElement("child2");
95 node1.appendChild(child1a);
96 node1.appendChild(child1b);
97
98 Node child2a = document.createElement("child2");
99 Node child2b = document.createElement("child1");
100 node2.appendChild(child2a);
101 node2.appendChild(child2b);
102
103 assertFalse(NodeUtils.compareElementNode(node1, node2, true));
104 assertFalse(NodeUtils.compareElementNode(node2, node1, true));
105 }
106
107 public void testNodesWithChildrenNodesInOtherOrder() throws Exception {
108 Document document = createDocument();
109
110 // create two nodes
111 Node node1 = document.createElement("some-node");
112 Node node2 = document.createElement("some-node");
113
114 Node child1a = document.createElement("child1");
115 Node child1b = document.createElement("child2");
116 node1.appendChild(child1a);
117 node1.appendChild(child1b);
118
119 Node child2a = document.createElement("child2");
120 Node child2b = document.createElement("child1");
121 node2.appendChild(child2a);
122 node2.appendChild(child2b);
123
124 assertTrue(NodeUtils.compareElementNode(node1, node2, false));
125 assertTrue(NodeUtils.compareElementNode(node2, node1, false));
126 }
127
128 public void testAdoptNode() throws Exception {
129 Document document = createDocument();
130 Node rootNode = document.createElement("root");
131 document.appendChild(rootNode);
132
133 // create a single s
134 Node node = document.createElement("some-node");
135
136 // add some children
137 Node child1 = document.createElement("child1");
138 Node child2 = document.createElement("child2");
139 node.appendChild(child1).appendChild(child2);
140 Node cdata = document.createCDATASection("some <random> text");
141 child2.appendChild(cdata);
142 Node text = document.createCDATASection("more <random> text");
143 child2.appendChild(text);
144
145 // add some attributes
146 NodeUtils.addAttribute(document, node, null, "foo", "bar");
147 NodeUtils.addAttribute(document, node, "http://some.uri", "foo2", "bar2");
148 NodeUtils.addAttribute(document, child1, "http://some.other.uri", "blah", "test");
149 NodeUtils.addAttribute(document, child2, "http://another.uri", "blah", "test");
150
151 // create the other document to receive the adopted node. It must have a root node.
152 Document document2 = createDocument();
153 rootNode = document2.createElement("root");
154 document2.appendChild(rootNode);
155
156 assertTrue(NodeUtils.compareElementNode(node, NodeUtils.adoptNode(document2, node), true));
157 }
158
159 public void testDuplicateNode() throws Exception {
160 Document document = createDocument();
161 Node rootNode = document.createElement("root");
162 document.appendChild(rootNode);
163
164 // create a single s
165 Node node = document.createElement("some-node");
166
167 // add some children
168 Node child1 = document.createElement("child1");
169 Node child2 = document.createElement("child2");
170 node.appendChild(child1).appendChild(child2);
171 Node cdata = document.createCDATASection("some <random> text");
172 child2.appendChild(cdata);
173 Node text = document.createCDATASection("more <random> text");
174 child2.appendChild(text);
175
176 // add some attributes
177 NodeUtils.addAttribute(document, node, null, "foo", "bar");
178 NodeUtils.addAttribute(document, node, "http://some.uri", "foo2", "bar2");
179 NodeUtils.addAttribute(document, child1, "http://some.other.uri", "blah", "test");
180 NodeUtils.addAttribute(document, child2, "http://another.uri", "blah", "test");
181
182 // create the other document to receive the duplicated node. It must have a root node.
183 Document document2 = createDocument();
184 rootNode = document2.createElement("root");
185 document2.appendChild(rootNode);
186
187 assertTrue(NodeUtils.compareElementNode(
188 node,
189 NodeUtils.duplicateNode(document2, node),
190 true));
191 }
192
193 public void testDuplicateAdoptNodeUpdatesNS() throws Exception {
194 Document document = createDocument();
195 Node rootNode = document.createElement("root");
196 String nsURI1 = "http://some.uri";
197 String nsURI2 = "http://some.other.uri";
198 NodeUtils.addAttribute(document, rootNode, SdkConstants.XMLNS_URI, "xmlns:prefix1", nsURI1);
199 NodeUtils.addAttribute(document, rootNode, SdkConstants.XMLNS_URI, "xmlns:prefix2", nsURI2);
200 document.appendChild(rootNode);
201
202 Node node1 = document.createElement("N1");
203 Node node2 = document.createElement("N2");
204 rootNode.appendChild(node1).appendChild(node2);
205
206 NodeUtils.addAttribute(document, node1, nsURI1, "prefix1:foo", "bar");
207 NodeUtils.addAttribute(document, node2, nsURI2, "prefix2:baz", "zap");
208
209 // create the other document to receive the duplicated and adopted node. It must have a root node.
210 Document document2 = createDocument();
211 rootNode = document2.createElement("root");
212 document2.appendChild(rootNode);
213
214 Node adoptedNode = NodeUtils.duplicateAndAdoptNode(document2, node1);
215 assertTrue(NodeUtils.compareElementNode(node1, adoptedNode, true));
216
217 // The new document should have xmlns attributes binding the prefix to the
218 // appropriate ns, and the two should not be mixed up.
219 NamedNodeMap doc2NamespaceAttrs = NodeUtils.getDocumentNamespaceAttributes(document2);
220 String prefix1 = NodeUtils.getPrefixForNs(doc2NamespaceAttrs, nsURI1);
221 String prefix2 = NodeUtils.getPrefixForNs(doc2NamespaceAttrs, nsURI2);
222 assertNotNull(prefix1);
223 assertNotNull(prefix2);
224 assertTrue(prefix1.startsWith(SdkConstants.XMLNS_PREFIX));
225 assertTrue(prefix2.startsWith(SdkConstants.XMLNS_PREFIX));
226 assertNotSame(prefix1, prefix2);
227
228 // Also check that the new prefixes are assigned to the right attribute nodes.
229 int prefixLen = SdkConstants.XMLNS_PREFIX.length();
230 Node adoptedFoo = adoptedNode.getAttributes()
231 .getNamedItem(prefix1.substring(prefixLen) + ":" + "foo");
232 assertTrue(adoptedFoo.getNodeValue().equals("bar"));
233 Node adoptedBaz = adoptedNode.getFirstChild().getAttributes()
234 .getNamedItem(prefix2.substring(prefixLen) + ":" + "baz");
235 assertTrue(adoptedBaz.getNodeValue().equals("zap"));
236
237 // Check that the original nodes are not modified.
238 Node originalFoo = node1.getAttributes().getNamedItem("prefix1:foo");
239 assertTrue(originalFoo.getNodeValue().equals("bar"));
240 Node originalBaz = node2.getAttributes().getNamedItem("prefix2:baz");
241 assertTrue(originalBaz.getNodeValue().equals("zap"));
242 }
243
244 public void testElementNamespaceAliasCompareAfterAdopt() throws Exception {
245 Document document = createDocument();
246 Node rootNode = document.createElement("root");
247 String nsURI = "urn:oasis:names:tc:xliff:document:1.2";
248 NodeUtils.addAttribute(document, rootNode, SdkConstants.XMLNS_URI, "xmlns:xliff", nsURI);
249 document.appendChild(rootNode);
250
251 Node node1 = document.createElementNS(nsURI, "xliff:g");
252 NodeUtils.addAttribute(document, node1, null, "id", "foo_number");
253 Node node2 = document.createTextNode("%1$d");
254 rootNode.appendChild(node1).appendChild(node2);
255
256 // create the other document to receive the duplicated and adopted node. It must have a root node.
257 Document document2 = createDocument();
258 rootNode = document2.createElement("root");
259 document2.appendChild(rootNode);
260
261 Node adoptedNode = NodeUtils.duplicateAndAdoptNode(document2, node1);
262 // Adoption may rewrite the prefix in the second document so that the nodeNames are different,
263 // but the logical names (localName + namespaceURI) are the same.
264 assertNotSame(node1.getNodeName(), adoptedNode.getNodeName());
265 assertTrue(NodeUtils.compareElementNode(node1, adoptedNode, true));
266 assertTrue(NodeUtils.compareElementNode(adoptedNode, node1, true));
267 }
268
269 public void testElementDiffNamespaceCompare() throws Exception {
270 // Test that comparison fails if the nodeName is the same, but the logical names are different
271 // (different namespace).
272 Document document = createDocument();
273 Node rootNode = document.createElement("root");
274 String nsURI = "urn:oasis:names:tc:xliff:document:1.2";
275 // Set the default namespace.
276 NodeUtils.addAttribute(document, rootNode, null, "xmlns", nsURI);
277 document.appendChild(rootNode);
278
279 // Create a node with a namespace but no prefix (it is the default).
280 Node node1 = document.createElementNS(nsURI, "g");
281 rootNode.appendChild(node1);
282
283 Document documentB = createDocument();
284 Node rootNodeB = documentB.createElement("root");
285 documentB.appendChild(rootNodeB);
286
287 // Now create a node with the same local name, but in a document without namespaces.
288 Node node1B = documentB.createElement("g");
289 rootNodeB.appendChild(node1B);
290
291 assertFalse(NodeUtils.compareElementNode(node1, node1B, true));
292 assertFalse(NodeUtils.compareElementNode(node1B, node1, true));
293 }
294
295 public void testAdoptNodeNSFromChildNodes() throws Exception {
296 // Test what happens if an xmlns:prefix="some.uri" declaration is declared under
297 // a child node that we are duplicating and adopting, instead of at the root.
298 // The duplicate and adopt process will move the xmlns declaration to the root
299 // of the new document, and remove it from the child node.
300 Document document = createDocument();
301 Node rootNode = document.createElement("root");
302 document.appendChild(rootNode);
303
304 Node node1 = document.createElement("N1");
305 rootNode.appendChild(node1);
306 Node node2 = document.createElement("item");
307 node1.appendChild(node2);
308
309 String toolsUri = SdkConstants.TOOLS_URI;
310 NodeUtils.addAttribute(document, node1, null, "c", "d");
311 NodeUtils.addAttribute(document, node1, SdkConstants.XMLNS_URI, "xmlns:tools", toolsUri);
312 NodeUtils.addAttribute(document, node1, toolsUri, "tools:targetApi", "17");
313
314 // create the other document to receive the duplicated and adopted node. It must have a root node.
315 Document document2 = createDocument();
316 rootNode = document2.createElement("root");
317 document2.appendChild(rootNode);
318
319 Node adoptedNode1 = NodeUtils.duplicateAndAdoptNode(document2, node1);
320 // The nodes are not quite the same, so we compare them manually
321 assertEquals(node1.getNodeName(), adoptedNode1.getNodeName());
322 Node adoptedNode2 = adoptedNode1.getFirstChild();
323 assertEquals(node2.getNodeName(), adoptedNode2.getNodeName());
324
325 NamedNodeMap doc2NamespaceAttrs = NodeUtils.getDocumentNamespaceAttributes(document2);
326 String adoptedToolsPrefix = NodeUtils.getPrefixForNs(doc2NamespaceAttrs, toolsUri);
327 assertNotNull(adoptedToolsPrefix);
328 assertTrue(adoptedToolsPrefix.startsWith(SdkConstants.XMLNS_PREFIX));
329 adoptedToolsPrefix = adoptedToolsPrefix.substring(SdkConstants.XMLNS_PREFIX.length());
330
331 assertEquals(3, node1.getAttributes().getLength());
332 assertEquals(2, adoptedNode1.getAttributes().getLength());
333 assertEquals(node1.getAttributes().getNamedItem("tools:targetApi").getNodeValue(),
334 adoptedNode1.getAttributes().getNamedItem(adoptedToolsPrefix + ":targetApi").getNodeValue());
335 assertEquals(node1.getAttributes().getNamedItem("c").getNodeValue(),
336 adoptedNode1.getAttributes().getNamedItem("c").getNodeValue());
337 }
338
339 public void testAdoptNodeNSOverriding() throws Exception {
340 // Test what happens if a node provides an xmlns:prefix="some.uri" declaration,
341 // that is used in some nodes, and then a child node re-declares the same prefix
342 // to override it with another uri, and uses that.
343 // The duplicate and adopt process will move the xmlns declaration to the root of the
344 // new document, but the two namespace URIs should remain distinct.
345 Document document = createDocument();
346 Node rootNode = document.createElement("root");
347 document.appendChild(rootNode);
348
349 Node node1 = document.createElement("N1");
350 rootNode.appendChild(node1);
351 String someNs = "http://some.uri";
352 NodeUtils.addAttribute(document, node1, SdkConstants.XMLNS_URI, "xmlns:prefix", someNs);
353 NodeUtils.addAttribute(document, node1, someNs, "prefix:attr1", "value1");
354
355 Node node2 = document.createElement("N2");
356 node1.appendChild(node2);
357 Node node3 = document.createElement("item");
358 node2.appendChild(node3);
359
360 String otherNs = "http://other.uri";
361 NodeUtils.addAttribute(document, node2, null, "c", "d");
362 NodeUtils.addAttribute(document, node2, SdkConstants.XMLNS_URI, "xmlns:prefix", otherNs);
363 NodeUtils.addAttribute(document, node2, otherNs, "prefix:attr2", "value2");
364
365 // create the other document to receive the duplicated and adopted node. It must have a root node.
366 Document document2 = createDocument();
367 rootNode = document2.createElement("root");
368 document2.appendChild(rootNode);
369
370 Node adoptedNode1 = NodeUtils.duplicateAndAdoptNode(document2, node1);
371
372 // The nodes are not quite the same, so we compare things manually.
373 assertEquals(node1.getNodeName(), adoptedNode1.getNodeName());
374 Node adoptedNode2 = adoptedNode1.getFirstChild();
375 assertEquals(node2.getNodeName(), adoptedNode2.getNodeName());
376 Node adoptedNode3 = adoptedNode2.getFirstChild();
377 assertEquals(node3.getNodeName(), adoptedNode3.getNodeName());
378
379 NamedNodeMap doc2NamespaceAttrs = NodeUtils.getDocumentNamespaceAttributes(document2);
380 String adoptedSomeNsPrefix = NodeUtils.getPrefixForNs(doc2NamespaceAttrs, someNs);
381 assertNotNull(adoptedSomeNsPrefix);
382 assertTrue(adoptedSomeNsPrefix.startsWith(SdkConstants.XMLNS_PREFIX));
383 adoptedSomeNsPrefix = adoptedSomeNsPrefix.substring(SdkConstants.XMLNS_PREFIX.length());
384
385 String adoptedOtherNsPrefix = NodeUtils.getPrefixForNs(doc2NamespaceAttrs, otherNs);
386 assertNotNull(adoptedOtherNsPrefix);
387 assertTrue(adoptedOtherNsPrefix.startsWith(SdkConstants.XMLNS_PREFIX));
388 adoptedOtherNsPrefix = adoptedOtherNsPrefix.substring(SdkConstants.XMLNS_PREFIX.length());
389
390 assertNotSame(adoptedSomeNsPrefix, adoptedOtherNsPrefix);
391
392 assertEquals(2, node1.getAttributes().getLength());
393 assertEquals(1, adoptedNode1.getAttributes().getLength());
394 assertEquals(node1.getAttributes().getNamedItem("prefix:attr1").getNodeValue(),
395 adoptedNode1.getAttributes().getNamedItem(
396 adoptedSomeNsPrefix + ":attr1").getNodeValue());
397
398 assertEquals(3, node2.getAttributes().getLength());
399 assertEquals(2, adoptedNode2.getAttributes().getLength());
400 assertEquals(node2.getAttributes().getNamedItem("c").getNodeValue(),
401 adoptedNode2.getAttributes().getNamedItem("c").getNodeValue());
402 assertEquals(node2.getAttributes().getNamedItem("prefix:attr2").getNodeValue(),
403 adoptedNode2.getAttributes().getNamedItem(
404 adoptedOtherNsPrefix + ":attr2").getNodeValue());
405 }
406
407 private static Document createDocument() throws ParserConfigurationException {
408 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
409 factory.setNamespaceAware(true);
410 factory.setValidating(false);
411 factory.setIgnoringComments(true);
412 DocumentBuilder builder;
413
414 builder = factory.newDocumentBuilder();
415 return builder.newDocument();
416 }
417 }