2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xml;
18 import java.io.Serializable;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.List;
23 import java.util.TreeMap;
25 import junit.framework.TestCase;
27 import org.dom4j.Attribute;
28 import org.dom4j.Document;
29 import org.dom4j.DocumentException;
30 import org.dom4j.Element;
31 import org.dom4j.io.DOMReader;
32 import org.dom4j.io.DOMWriter;
35 * XML test support utilities.
37 * @author Erik Brakkee
39 public final class XmlUtils {
41 * Disabled constructor.
49 * Checks equality of two XML documents excluding comment and processing
50 * nodes and trimming the text of the elements. In case of problems, it
51 * provides an xpath-like expression describing where the problem is.
57 public static void assertEquals(String aMsg,
58 org.w3c.dom.Document aExpected, org.w3c.dom.Document aActual) {
59 assertEquals(aMsg, convert(aExpected), convert(aActual));
63 * Checks equality of two XML documents excluding comment and processing
64 * nodes and trimming the text of the elements. In case of problems, it
65 * provides an xpath-like expression describing where the problem is.
71 public static void assertEquals(String aMsg, Document aExpected,
73 assertEquals(aMsg + "/" + aExpected.getRootElement().getName(),
74 aExpected.getRootElement(), aActual.getRootElement());
78 * Checks equality of two XML elements excluding comment and processing
79 * nodes and trimming the text of the elements. In case of problems, it
80 * provides an xpath-like expression describing where the problem is.
86 public static void assertEquals(String aMsg, Element aExpected,
89 TestCase.assertEquals(aMsg + "/name()", aExpected.getName(), aActual
93 TestCase.assertEquals(aMsg + "/text()", aExpected.getTextTrim(),
94 aActual.getTextTrim());
97 List<Attribute> expectedAttrs = aExpected.attributes();
98 Collections.sort(expectedAttrs, new AttributeComparator());
100 List<Attribute> actualAttrs = aActual.attributes();
101 Collections.sort(actualAttrs, new AttributeComparator());
103 TestCase.assertEquals("count(" + aMsg + "/@*)", expectedAttrs.size(),
106 for (int i = 0; i < expectedAttrs.size(); i++) {
107 String msg = aMsg + "/@" + expectedAttrs.get(i).getName();
108 assertEquals(msg, expectedAttrs.get(i), actualAttrs.get(i));
112 List<Element> expectedElems = aExpected.elements();
113 List<Element> actualElems = aActual.elements();
114 TestCase.assertEquals("count(" + aMsg + "/*)", expectedElems.size(),
117 // determine the how-manyth element of the given name we are at.
118 // Maps element name to the last used index (or null if not yet used)
119 Map<String, Integer> elementIndex = new TreeMap<String, Integer>();
121 for (int i = 0; i < expectedElems.size(); i++) {
122 String elemName = expectedElems.get(i).getName();
123 Integer index = elementIndex.get(elemName);
131 elementIndex.put(elemName, index);
133 String msg = aMsg + "/" + expectedElems.get(i).getName() + "[" +
136 assertEquals(msg, expectedElems.get(i), actualElems.get(i));
141 * Checks equality of two attributes. In case of problems, it provides an
142 * xpath-like expression describing where the problem is.
148 public static void assertEquals(String aMsg, Attribute aExpected,
150 TestCase.assertEquals(aMsg + ":name", aExpected.getName(), aActual
152 TestCase.assertEquals(aMsg + ":value", aExpected.getValue(), aActual
157 * Comparator which compares attributes by name.
159 private static final class AttributeComparator implements
160 Comparator<Attribute>, Serializable {
162 private static final long serialVersionUID = 7897287273519886301L;
167 * @see java.util.Comparator#compare(T, T)
169 public int compare(Attribute aAttribute1, Attribute aAttribute2) {
170 return aAttribute1.getName().compareTo(aAttribute2.getName());
175 * Converts a dom4j document into a w3c DOM document.
178 * Document to convert.
180 * @return W3C DOM document.
183 public static org.w3c.dom.Document convert(org.dom4j.Document aDocument)
184 throws DocumentException {
185 return new DOMWriter().write(aDocument);
189 * Converts a W3C DOM document into a dom4j document.
192 * Document to convert.
194 * @return Dom4j document.
196 public static org.dom4j.Document convert(org.w3c.dom.Document aDocument) {
197 return new DOMReader().read(aDocument);