(no commit message)
[utils] / support / general / src / test / java / org / wamblee / xml / XmlUtils.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
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 package org.wamblee.xml;
17
18 import java.io.Serializable;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.TreeMap;
24
25 import junit.framework.TestCase;
26
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;
33
34 /**
35  * XML test support utilities.
36  * 
37  * @author Erik Brakkee
38  */
39 public final class XmlUtils {
40     /**
41      * Disabled constructor.
42      * 
43      */
44     private XmlUtils() {
45         // Empty
46     }
47
48     /**
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.
52      * 
53      * @param aMsg
54      * @param aExpected
55      * @param aActual
56      */
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));
60     }
61
62     /**
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.
66      * 
67      * @param aMsg
68      * @param aExpected
69      * @param aActual
70      */
71     public static void assertEquals(String aMsg, Document aExpected,
72         Document aActual) {
73         assertEquals(aMsg + "/" + aExpected.getRootElement().getName(),
74             aExpected.getRootElement(), aActual.getRootElement());
75     }
76
77     /**
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.
81      * 
82      * @param aMsg
83      * @param aExpected
84      * @param aActual
85      */
86     public static void assertEquals(String aMsg, Element aExpected,
87         Element aActual) {
88         // Name.
89         TestCase.assertEquals(aMsg + "/name()", aExpected.getName(), aActual
90             .getName());
91
92         // Text
93         TestCase.assertEquals(aMsg + "/text()", aExpected.getTextTrim(),
94             aActual.getTextTrim());
95
96         // Attributes
97         List<Attribute> expectedAttrs = aExpected.attributes();
98         Collections.sort(expectedAttrs, new AttributeComparator());
99
100         List<Attribute> actualAttrs = aActual.attributes();
101         Collections.sort(actualAttrs, new AttributeComparator());
102
103         TestCase.assertEquals("count(" + aMsg + "/@*)", expectedAttrs.size(),
104             actualAttrs.size());
105
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));
109         }
110
111         // Nested elements.
112         List<Element> expectedElems = aExpected.elements();
113         List<Element> actualElems = aActual.elements();
114         TestCase.assertEquals("count(" + aMsg + "/*)", expectedElems.size(),
115             actualElems.size());
116
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>();
120
121         for (int i = 0; i < expectedElems.size(); i++) {
122             String elemName = expectedElems.get(i).getName();
123             Integer index = elementIndex.get(elemName);
124
125             if (index == null) {
126                 index = 1;
127             } else {
128                 index++;
129             }
130
131             elementIndex.put(elemName, index);
132
133             String msg = aMsg + "/" + expectedElems.get(i).getName() + "[" +
134                 index + "]";
135
136             assertEquals(msg, expectedElems.get(i), actualElems.get(i));
137         }
138     }
139
140     /**
141      * Checks equality of two attributes. In case of problems, it provides an
142      * xpath-like expression describing where the problem is.
143      * 
144      * @param aMsg
145      * @param aExpected
146      * @param aActual
147      */
148     public static void assertEquals(String aMsg, Attribute aExpected,
149         Attribute aActual) {
150         TestCase.assertEquals(aMsg + ":name", aExpected.getName(), aActual
151             .getName());
152         TestCase.assertEquals(aMsg + ":value", aExpected.getValue(), aActual
153             .getValue());
154     }
155
156     /**
157      * Comparator which compares attributes by name.
158      */
159     private static final class AttributeComparator implements
160         Comparator<Attribute>, Serializable {
161
162         private static final long serialVersionUID = 7897287273519886301L;
163
164         /*
165          * (non-Javadoc)
166          * 
167          * @see java.util.Comparator#compare(T, T)
168          */
169         public int compare(Attribute aAttribute1, Attribute aAttribute2) {
170             return aAttribute1.getName().compareTo(aAttribute2.getName());
171         }
172     }
173     
174     /**
175      * Converts a dom4j document into a w3c DOM document.
176      * 
177      * @param aDocument
178      *            Document to convert.
179      * 
180      * @return W3C DOM document.
181      * 
182      */
183     public static org.w3c.dom.Document convert(org.dom4j.Document aDocument)
184         throws DocumentException {
185         return new DOMWriter().write(aDocument);
186     } 
187
188     /**
189      * Converts a W3C DOM document into a dom4j document.
190      * 
191      * @param aDocument
192      *            Document to convert.
193      * 
194      * @return Dom4j document.
195      */
196     public static org.dom4j.Document convert(org.w3c.dom.Document aDocument) {
197         return new DOMReader().read(aDocument);
198     }
199     
200 }