(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.Element;
30
31 /**
32  * XML test support utilities.
33  * 
34  * @author Erik Brakkee
35  */
36 public final class XmlUtils {
37     /**
38      * Disabled constructor.
39      * 
40      */
41     private XmlUtils() {
42         // Empty
43     }
44
45     /**
46      * Checks equality of two XML documents excluding comment and processing
47      * nodes and trimming the text of the elements. In case of problems, it
48      * provides an xpath-like expression describing where the problem is.
49      * 
50      * @param aMsg
51      * @param aExpected
52      * @param aActual
53      */
54     public static void assertEquals(String aMsg,
55         org.w3c.dom.Document aExpected, org.w3c.dom.Document aActual) {
56         assertEquals(aMsg, DomUtils.convert(aExpected), DomUtils
57             .convert(aActual));
58     }
59
60     /**
61      * Checks equality of two XML documents excluding comment and processing
62      * nodes and trimming the text of the elements. In case of problems, it
63      * provides an xpath-like expression describing where the problem is.
64      * 
65      * @param aMsg
66      * @param aExpected
67      * @param aActual
68      */
69     public static void assertEquals(String aMsg, Document aExpected,
70         Document aActual) {
71         assertEquals(aMsg + "/" + aExpected.getRootElement().getName(),
72             aExpected.getRootElement(), aActual.getRootElement());
73     }
74
75     /**
76      * Checks equality of two XML elements excluding comment and processing
77      * nodes and trimming the text of the elements. In case of problems, it
78      * provides an xpath-like expression describing where the problem is.
79      * 
80      * @param aMsg
81      * @param aExpected
82      * @param aActual
83      */
84     public static void assertEquals(String aMsg, Element aExpected,
85         Element aActual) {
86         // Name.
87         TestCase.assertEquals(aMsg + "/name()", aExpected.getName(), aActual
88             .getName());
89
90         // Text
91         TestCase.assertEquals(aMsg + "/text()", aExpected.getTextTrim(),
92             aActual.getTextTrim());
93
94         // Attributes
95         List<Attribute> expectedAttrs = aExpected.attributes();
96         Collections.sort(expectedAttrs, new AttributeComparator());
97
98         List<Attribute> actualAttrs = aActual.attributes();
99         Collections.sort(actualAttrs, new AttributeComparator());
100
101         TestCase.assertEquals("count(" + aMsg + "/@*)", expectedAttrs.size(),
102             actualAttrs.size());
103
104         for (int i = 0; i < expectedAttrs.size(); i++) {
105             String msg = aMsg + "/@" + expectedAttrs.get(i).getName();
106             assertEquals(msg, expectedAttrs.get(i), actualAttrs.get(i));
107         }
108
109         // Nested elements.
110         List<Element> expectedElems = aExpected.elements();
111         List<Element> actualElems = aActual.elements();
112         TestCase.assertEquals("count(" + aMsg + "/*)", expectedElems.size(),
113             actualElems.size());
114
115         // determine the how-manyth element of the given name we are at.
116         // Maps element name to the last used index (or null if not yet used)
117         Map<String, Integer> elementIndex = new TreeMap<String, Integer>();
118
119         for (int i = 0; i < expectedElems.size(); i++) {
120             String elemName = expectedElems.get(i).getName();
121             Integer index = elementIndex.get(elemName);
122
123             if (index == null) {
124                 index = 1;
125             } else {
126                 index++;
127             }
128
129             elementIndex.put(elemName, index);
130
131             String msg = aMsg + "/" + expectedElems.get(i).getName() + "[" +
132                 index + "]";
133
134             assertEquals(msg, expectedElems.get(i), actualElems.get(i));
135         }
136     }
137
138     /**
139      * Checks equality of two attributes. In case of problems, it provides an
140      * xpath-like expression describing where the problem is.
141      * 
142      * @param aMsg
143      * @param aExpected
144      * @param aActual
145      */
146     public static void assertEquals(String aMsg, Attribute aExpected,
147         Attribute aActual) {
148         TestCase.assertEquals(aMsg + ":name", aExpected.getName(), aActual
149             .getName());
150         TestCase.assertEquals(aMsg + ":value", aExpected.getValue(), aActual
151             .getValue());
152     }
153
154     /**
155      * Comparator which compares attributes by name.
156      */
157     private static final class AttributeComparator implements
158         Comparator<Attribute>, Serializable {
159
160         private static final long serialVersionUID = 7897287273519886301L;
161
162         /*
163          * (non-Javadoc)
164          * 
165          * @see java.util.Comparator#compare(T, T)
166          */
167         public int compare(Attribute aAttribute1, Attribute aAttribute2) {
168             return aAttribute1.getName().compareTo(aAttribute2.getName());
169         }
170     }
171 }