2 * Copyright 2005 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.
17 package org.wamblee.xml;
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.Element;
32 * XML test support utilities.
34 public final class XmlUtils {
37 * Disabled constructor.
45 * Checks equality of two XML documents excluding comment and processing
46 * nodes and trimming the text of the elements.
52 public static void assertEquals(String aMsg,
53 org.w3c.dom.Document aExpected, org.w3c.dom.Document aActual) {
54 assertEquals(aMsg, DomUtils.convert(aExpected), DomUtils
59 * Checks equality of two XML documents excluding comment and processing
60 * nodes and trimming the text of the elements.
66 public static void assertEquals(String aMsg, Document aExpected,
68 assertEquals(aMsg, aExpected.getRootElement(), aActual.getRootElement());
72 * Checks equality of two XML elements excluding comment and processing
73 * nodes and trimming the text of the elements.
79 public static void assertEquals(String aMsg, Element aExpected,
83 TestCase.assertEquals(aMsg + "/name()", aExpected.getName(), aActual
87 TestCase.assertEquals(aMsg + "/text()", aExpected.getTextTrim(),
88 aActual.getTextTrim());
91 List<Attribute> expectedAttrs = aExpected.attributes();
92 Collections.sort(expectedAttrs, new AttributeComparator());
93 List<Attribute> actualAttrs = aActual.attributes();
94 Collections.sort(actualAttrs, new AttributeComparator());
96 TestCase.assertEquals(aMsg + "/count(@*)", expectedAttrs.size(),
98 for (int i = 0; i < expectedAttrs.size(); i++) {
99 String msg = aMsg + "/@" + expectedAttrs.get(i).getName();
100 assertEquals(msg, expectedAttrs.get(i), actualAttrs.get(i));
104 List<Element> expectedElems = aExpected.elements();
105 List<Element> actualElems = aActual.elements();
106 TestCase.assertEquals(aMsg + "/count(*)", expectedElems.size(),
108 // determine the how-manyth element of the given name we are at.
109 // Maps element name to the last used index (or null if not yet used)
110 Map<String, Integer> elementIndex = new TreeMap<String, Integer>();
111 for (int i = 0; i < expectedElems.size(); i++) {
112 String elemName = expectedElems.get(i).getName();
113 Integer index = elementIndex.get(elemName);
119 elementIndex.put(elemName, index);
120 String msg = aMsg + "/" + expectedElems.get(i).getName() + "["
123 assertEquals(msg, expectedElems.get(i), actualElems.get(i));
128 * Checks equality of two attributes.
134 public static void assertEquals(String aMsg, Attribute aExpected,
136 TestCase.assertEquals(aMsg + ":name", aExpected.getName(), aActual
138 TestCase.assertEquals(aMsg + ":value", aExpected.getValue(), aActual
143 * Comparator which compares attributes by name.
145 private static final class AttributeComparator implements
146 Comparator<Attribute> {
150 * @see java.util.Comparator#compare(T, T)
152 public int compare(Attribute aAttribute1, Attribute aAttribute2) {
153 return aAttribute1.getName().compareTo(aAttribute2.getName());