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