(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
23 import junit.framework.TestCase;
24
25 import org.dom4j.Attribute;
26 import org.dom4j.Document;
27 import org.dom4j.Element;
28
29
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 nodes and 
46      * trimming the text of the elements.  
47      * @param aMsg
48      * @param aExpected
49      * @param aActual
50      */
51     public static void assertEquals(String aMsg, Document aExpected, Document aActual) { 
52         assertEquals(aMsg, aExpected.getRootElement(), aActual.getRootElement());
53     }
54     
55     /**
56      * Checks equality of two XML elements excluding comment and processing nodes and trimming 
57      * the text of the elements.  
58      * @param aMsg
59      * @param aExpected
60      * @param aActual
61      */
62     public static void assertEquals(String aMsg, Element aExpected, Element aActual) {
63         
64         // Name.
65         TestCase.assertEquals(aMsg + "/name()", aExpected.getName(), aActual.getName());
66         
67         // Text
68         TestCase.assertEquals(aMsg + "/text()", aExpected.getTextTrim(), aActual.getTextTrim());
69       
70         // Attributes
71         List<Attribute> expectedAttrs = aExpected.attributes(); 
72         Collections.sort(expectedAttrs, new AttributeComparator());
73         List<Attribute> actualAttrs = aActual.attributes(); 
74         Collections.sort(actualAttrs, new AttributeComparator());
75         
76         TestCase.assertEquals(aMsg + "/#attributes", expectedAttrs.size(), actualAttrs.size());
77         for (int i = 0; i < expectedAttrs.size(); i++) { 
78             String msg = aMsg + "/attribute(" + i + ")"; 
79             assertEquals(msg, expectedAttrs.get(i), actualAttrs.get(i));
80         }
81         
82         // Nested elements. 
83         List<Element> expectedElems = aExpected.elements();
84         List<Element> actualElems = aActual.elements(); 
85         TestCase.assertEquals(aMsg + "/#elements", expectedElems.size(), actualElems.size());
86         for (int i = 0; i < expectedElems.size(); i++) { 
87             String msg = aMsg + "/element(" + i + ")"; 
88             assertEquals(msg, expectedElems.get(i), actualElems.get(i));
89         }
90     }
91     
92     /**
93      * Checks equality of two attributes.
94      * @param aMsg
95      * @param aExpected
96      * @param aActual
97      */
98     public static void assertEquals(String aMsg, Attribute aExpected, Attribute aActual) { 
99         TestCase.assertEquals(aMsg + "@", aExpected.getName() + ":name", aActual.getName());
100         TestCase.assertEquals(aMsg + "@" + aExpected.getName() + ":value", 
101                 aExpected.getValue(), aActual.getValue());    
102     }
103     
104     /**
105      * Comparator which compares attributes by name. 
106      */
107     private static final class AttributeComparator implements Comparator<Attribute> { 
108         /* (non-Javadoc)
109          * @see java.util.Comparator#compare(T, T)
110          */
111         public int compare(Attribute aAttribute1, Attribute aAttribute2) {
112             return aAttribute1.getName().compareTo(aAttribute2.getName());
113         }
114     }
115 }