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