(no commit message)
[utils] / support / test / 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 public final class XmlUtils {
35
36     /**
37      * Disabled constructor.
38      * 
39      */
40     private XmlUtils() {
41         // Empty
42     }
43
44     /**
45      * Checks equality of two XML documents excluding comment and processing
46      * nodes and trimming the text of the elements.
47      * 
48      * @param aMsg
49      * @param aExpected
50      * @param aActual
51      */
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
55                 .convert(aActual));
56     }
57
58     /**
59      * Checks equality of two XML documents excluding comment and processing
60      * nodes and trimming the text of the elements.
61      * 
62      * @param aMsg
63      * @param aExpected
64      * @param aActual
65      */
66     public static void assertEquals(String aMsg, Document aExpected,
67             Document aActual) {
68         assertEquals(aMsg, aExpected.getRootElement(), aActual.getRootElement());
69     }
70
71     /**
72      * Checks equality of two XML elements excluding comment and processing
73      * nodes and trimming the text of the elements.
74      * 
75      * @param aMsg
76      * @param aExpected
77      * @param aActual
78      */
79     public static void assertEquals(String aMsg, Element aExpected,
80             Element aActual) {
81
82         // Name.
83         TestCase.assertEquals(aMsg + "/name()", aExpected.getName(), aActual
84                 .getName());
85
86         // Text
87         TestCase.assertEquals(aMsg + "/text()", aExpected.getTextTrim(),
88                 aActual.getTextTrim());
89
90         // Attributes
91         List<Attribute> expectedAttrs = aExpected.attributes();
92         Collections.sort(expectedAttrs, new AttributeComparator());
93         List<Attribute> actualAttrs = aActual.attributes();
94         Collections.sort(actualAttrs, new AttributeComparator());
95
96         TestCase.assertEquals(aMsg + "/#attributes", expectedAttrs.size(),
97                 actualAttrs.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));
101         }
102
103         // Nested elements.
104         List<Element> expectedElems = aExpected.elements();
105         List<Element> actualElems = aActual.elements();
106         TestCase.assertEquals(aMsg + "/#elements", expectedElems.size(),
107                 actualElems.size());
108         // determine the how-manyth element of the given name we are at.
109         Map<String, Integer> elementIndex = new TreeMap<String, Integer>();
110         for (int i = 0; i < expectedElems.size(); i++) {
111             String elemName = expectedElems.get(i).getName();
112             Integer index = elementIndex.get(elemName);
113             if (index == null) {
114                 index = 1;
115             } else {
116                 index++;
117             }
118             elementIndex.put(elemName, index);
119             String msg = aMsg + "/" + expectedElems.get(i).getName() + "("
120                     + index + ")";
121
122             assertEquals(msg, expectedElems.get(i), actualElems.get(i));
123         }
124     }
125
126     /**
127      * Checks equality of two attributes.
128      * 
129      * @param aMsg
130      * @param aExpected
131      * @param aActual
132      */
133     public static void assertEquals(String aMsg, Attribute aExpected,
134             Attribute aActual) {
135         TestCase.assertEquals(aMsg + ":name", aExpected.getName(), aActual
136                 .getName());
137         TestCase.assertEquals(aMsg + ":value", aExpected.getValue(), aActual
138                 .getValue());
139     }
140
141     /**
142      * Comparator which compares attributes by name.
143      */
144     private static final class AttributeComparator implements
145             Comparator<Attribute> {
146         /*
147          * (non-Javadoc)
148          * 
149          * @see java.util.Comparator#compare(T, T)
150          */
151         public int compare(Attribute aAttribute1, Attribute aAttribute2) {
152             return aAttribute1.getName().compareTo(aAttribute2.getName());
153         }
154     }
155 }