Debian Codegraph @

tests/org.eclipse.emf.test.xml/src/org/eclipse/emf/test/xml/xmi/CompareXML.java

raw
1 /**
2 * Copyright (c) 2002-2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.test.xml.xmi;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15
16 import java.io.ByteArrayInputStream;
17
18 import javax.xml.parsers.DocumentBuilder;
19
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.NamedNodeMap;
23 import org.w3c.dom.Node;
24
25
26 /**
27 * A helper class that provides methods that allow comparing two XML instances.
28 */
29 public class CompareXML
30 {
31 public static void compareFiles(DocumentBuilder builder, String expectedOutput, ByteArrayInputStream currentOutput) throws Exception
32 {
33 Document doc = builder.parse(currentOutput);
34 Document doc2 = builder.parse(expectedOutput);
35 compareNodes(expectedOutput, doc2.getDocumentElement(), doc.getDocumentElement());
36 }
37
38 public static void compareDocuments(String uri, Document expected, Document document) throws Exception
39 {
40 compareNodes(uri, expected.getDocumentElement(), document.getDocumentElement());
41 }
42
43 protected static void compareNodes(String uri, Node node1, Node node2)
44 {
45 compareData(uri, node1, node2);
46
47 switch (node1.getNodeType())
48 {
49 case Node.ELEMENT_NODE: {
50 boolean hasAttrs = node1.hasAttributes();
51 assertEquals(uri+": HasAttributes: " + node1 + "!=" + node2, hasAttrs, ((Element)node2).hasAttributes());
52 if (hasAttrs)
53 {
54 NamedNodeMap map1 = node1.getAttributes();
55 NamedNodeMap map2 = ((Element)node2).getAttributes();
56 int len = map1.getLength();
57 assertEquals(uri+": MissingAttr: " + node1 + "!=" + node2, len, map2.getLength());
58 for (int i = 0; i < len; i++)
59 {
60 Node n1 = map1.item(i);
61 if (n1.getLocalName() == null)
62 { // DOM Level 1 Node
63 Node n2 = map2.getNamedItem(n1.getNodeName());
64 assertNotNull(uri+": AttrNull: " + n1, n2);
65 compareData(uri, n1, n2);
66 }
67 else
68 {
69 Node n2 = map2.getNamedItemNS(n1.getNamespaceURI(), n1.getLocalName());
70 assertNotNull(uri+": AttrNull: " + n1, n2);
71 compareData(uri, n1, n2);
72 }
73 }
74 }
75 }
76 }
77
78 Node child1 = node1.getFirstChild();
79 Node child2 = node2.getFirstChild();
80 while (child1 != null && child2 != null)
81 {
82 compareNodes(uri, child1, child2);
83 child1 = child1.getNextSibling();
84 child2 = child2.getNextSibling();
85 }
86 assertEquals(uri+": NotEquals: " + node1 + "!=" + node2, child1, child2);
87 }
88
89 protected static void compareData(String uri, Node node1, Node node2)
90 {
91 assertEquals(uri+": NodeType error: " + node1 + "!=" + node2, node1.getNodeType(), node2.getNodeType());
92 assertEquals(uri+": NodeName: " + node1 + "!=" + node2, node1.getNodeName(), node2.getNodeName());
93 assertEquals(uri+": NodeLocalName: " + node1 + "!=" + node2, node1.getLocalName(), node2.getLocalName());
94 assertEquals(uri+": NodeNamespaceURI: " + node1 + "!=" + node2, node1.getNamespaceURI(), node2.getNamespaceURI());
95 assertEquals(uri+": NodeValue: " + node1 + "!=" + node2, node1.getNodeValue(), node2.getNodeValue());
96 }
97 }