KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmldb > xupdate > unittests > XhiveNodeMatcher


1 package org.xmldb.xupdate.unittests;
2
3 /**
4  * (c) 2002 X-Hive Corporation B.V. (www.x-hive.com)
5  * XhiveNodeMatcher
6  *
7  * [DESCRIPTION]
8  * Compares two nodes.
9  *
10  */

11
12 import junit.framework.Assert;
13 import org.w3c.dom.*;
14
15 public class XhiveNodeMatcher {
16
17   public XhiveNodeMatcher() {
18   }
19
20   protected static String JavaDoc nodeTypeToString(int type) {
21     switch (type) {
22       case Node.ELEMENT_NODE:
23         return "ELEMENT_NODE";
24       case Node.ATTRIBUTE_NODE:
25         return "ATTRIBUTE_NODE";
26       case Node.TEXT_NODE:
27         return "TEXT_NODE";
28       case Node.CDATA_SECTION_NODE:
29         return "CDATA_SECTION_NODE";
30       case Node.ENTITY_REFERENCE_NODE:
31         return "ENTITY_REFERENCE_NODE";
32       case Node.ENTITY_NODE:
33         return "ENTITY_NODE";
34       case Node.PROCESSING_INSTRUCTION_NODE:
35         return "PROCESSING_INSTRUCTION_NODE";
36       case Node.COMMENT_NODE:
37         return "COMMENT_NODE";
38       case Node.DOCUMENT_NODE:
39         return "DOCUMENT_NODE";
40       case Node.DOCUMENT_TYPE_NODE:
41         return "DOCUMENT_TYPE_NODE";
42       case Node.DOCUMENT_FRAGMENT_NODE:
43         return "DOCUMENT_FRAGMENT_NODE";
44       case Node.NOTATION_NODE:
45         return "NOTATION_NODE";
46     }
47     return "UNKNOWN NODE TYPE";
48   }
49
50   public static void compareNodes(Node origin, Node target, boolean clone, boolean deep) {
51     //System.out.println( "Comparing nodes " + origin + ", " + target );
52
if (!(origin == null && target == null)) {
53       if (origin != null) {
54         Assert.assertNotNull("NodeMatcher : Missing node, origin tagname : " + origin.getNodeName(), target);
55       } else {
56         Assert.assertNull("NodeMatcher : extra node: " + (target != null ? target.getNodeName() : ""), target);
57       }
58       // Node Type
59
Assert.assertEquals("NodeMatcher : node types do not match", nodeTypeToString(origin.getNodeType()),
60               nodeTypeToString(target.getNodeType()));
61       // Node name
62
Assert.assertEquals("NodeMatcher : node names do not match.", origin.getNodeName(), target.getNodeName());
63       // Node value
64
Assert.assertEquals("NodeMatcher : node values do not match.", origin.getNodeValue(), target.getNodeValue());
65       // TODO : Find a way to compare readonly
66
// Local name
67
Assert.assertEquals("NodeMatcher : local names do not match.", origin.getLocalName(), target.getLocalName());
68       // Namespace URI
69
Assert.assertEquals("NodeMatcher : namespace URI's do not match (localName=" + origin.getLocalName() + ")."
70               , origin.getNamespaceURI(), target.getNamespaceURI());
71       // Prefix
72
Assert.assertEquals("NodeMatcher : prefixes do not match.", origin.getPrefix(), target.getPrefix());
73       // hasAttributes
74
if (origin.hasAttributes()) {
75         Assert.assertTrue("NodeMatcher : hasAttributes() should return true for node with name " + origin.getNodeName()
76                 , target.hasAttributes());
77       }
78       // hasChildren
79
if (clone || (origin.getNodeType() != Node.ENTITY_REFERENCE_NODE)) {
80         if (origin.hasChildNodes() && deep) {
81           Assert.assertTrue("NodeMatcher : hasChildren() should return true for node with name " + origin.getNodeName()
82                  , target.hasChildNodes());
83         }
84       }
85       // Attributes
86
if (origin.getNodeType() == Node.ELEMENT_NODE) {
87         //System.out.println("Comparing elments");
88
compareNamedNodeMaps(origin.getAttributes(), target.getAttributes(), clone, deep);
89       }
90       if (origin.getNodeType() == Node.ATTRIBUTE_NODE) {
91         //System.out.println("Comparing attributes");
92
compareAttrs((Attr) origin, (Attr) target, clone, deep);
93       }
94       if (origin.getNodeType() == Node.ENTITY_NODE) {
95         //System.out.println("Comparing entity");
96
compareEntities((Entity) origin, (Entity) target);
97       }
98       if (origin.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
99         //System.out.println("Comparing pi");
100
comparePIs((ProcessingInstruction) origin, (ProcessingInstruction) target);
101       }
102       if (origin.getNodeType() == Node.NOTATION_NODE) {
103         //System.out.println("Comparing notation");
104
compareNotations((Notation) origin, (Notation) target);
105       }
106       if (origin.getNodeType() == Node.DOCUMENT_NODE) {
107         //System.out.println("Comparing documents");
108
compareDocuments((Document) origin, (Document) target, clone, deep);
109       }
110       // TODO (ADQ) : Add special case for libraries (comparing catalogs)
111
if ((deep) && (clone || (origin.getNodeType() != Node.ENTITY_REFERENCE_NODE))) {
112         //System.out.println("Comparing children of "+origin.getNodeName());
113
// Compare children
114
Node originPointer = origin.getFirstChild();
115         Node targetPointer = target.getFirstChild();
116         while (originPointer != null) {
117           //System.out.println("Comparing child "+originPointer.getNodeName());
118
Assert.assertNotNull("NodeMatcher : Child list for node does not match, missing node "
119                   + originPointer.getNodeName(), targetPointer);
120           compareNodes(originPointer, targetPointer, clone, deep);
121           originPointer = originPointer.getNextSibling();
122           targetPointer = targetPointer.getNextSibling();
123         }
124         if (targetPointer != null && originPointer == null) {
125           Assert.fail("NodeMatcher : Child list does not match, node should not be there :"
126                   + targetPointer.getNodeName() + ", " + targetPointer.getNodeValue());
127         }
128       }
129     }
130   }
131
132   public static void compareNodeLists(NodeList origin, NodeList target, boolean clone, boolean deep) {
133     if (origin != null) {
134       Assert.assertNotNull("NodeMatcher : expected nodelist not found", target);
135       Assert.assertEquals("NodeMatcher : nodelist item count does not match.", origin.getLength(), target.getLength());
136
137       for (int i = 0; i < origin.getLength(); i++) {
138         Node originNode = origin.item(i);
139         Node targetNode = target.item(i);
140         compareNodes(originNode, targetNode, clone, deep);
141       }
142     } else {
143       Assert.assertNull("NodeMatcher : unexpected nodelist found", target);
144     }
145   }
146
147   public static void compareNamedNodeMaps(NamedNodeMap origin, NamedNodeMap target, boolean clone, boolean deep) {
148     if (origin != null) {
149       Assert.assertNotNull("NodeMatcher : expected namednodemap not found", target);
150       Assert.assertEquals("NodeMatcher : nodemap item count does not match.", origin.getLength(), target.getLength());
151
152       for (int i = 0; i < origin.getLength(); i++) {
153         Node originNode = origin.item(i);
154         // Lengths checked before, so no need to check whether all targets are in origin
155
Node targetNode = lookupTargetNode(target, originNode);
156         compareNodes(originNode, targetNode, clone, deep);
157       }
158     } else {
159       Assert.assertNull("NodeMatcher : unexpected namednodemap found", target);
160     }
161   }
162
163   /**
164    * NamedNodeMaps are unordered, so lookup the item in the other list
165    * called from compareNamedNodeMaps
166    */

167   private static Node lookupTargetNode(NamedNodeMap target, Node originNode) {
168     // Namespace or not namespace?
169
if (originNode.getLocalName() == null) {
170       return target.getNamedItem(originNode.getNodeName());
171     } else {
172       return target.getNamedItemNS(originNode.getNamespaceURI(), originNode.getLocalName());
173     }
174   }
175
176   protected static void compareAttrs(Attr origin, Attr target, boolean clone, boolean deep) {
177     if (clone) {
178       if (origin.getSpecified()) {
179         Assert.assertTrue("NodeMatcher : attribute specified does not match", target.getSpecified());
180       }
181     }
182   }
183
184   protected static void compareEntities(Entity origin, Entity target) {
185     Assert.assertEquals("NodeMatcher : entity public id's do not match.", origin.getPublicId(), target.getPublicId());
186     Assert.assertEquals("NodeMatcher : entity system id's do not match.", origin.getSystemId(), target.getSystemId());
187     Assert.assertEquals("NodeMatcher : entity notation names do not match.", origin.getNotationName()
188             , target.getNotationName());
189   }
190
191   protected static void comparePIs(ProcessingInstruction origin, ProcessingInstruction target) {
192     Assert.assertEquals("NodeMatcher : processing instruction targets do not match."
193             , origin.getTarget(), target.getTarget());
194     Assert.assertEquals("NodeMatcher : processing instruction data does not match."
195             , origin.getData(), target.getData());
196   }
197
198   protected static void compareNotations(Notation origin, Notation target) {
199     Assert.assertEquals("NodeMatcher : notation public id's do not match.", origin.getPublicId(), target.getPublicId());
200     Assert.assertEquals("NodeMatcher : notation system id's do not match.", origin.getSystemId(), target.getSystemId());
201   }
202
203   /**
204    * Compare two doctype nodes.
205    * Presently, this matching is only used from TreeDiffTest, maybe this should be enabled in
206    * compareNodes in this method as well (but this may lead to problems with Xerces documents, which probably
207    * have slightly different information).
208    */

209   public static void compareDocTypes(DocumentType origin, DocumentType target, boolean clone, boolean deep) {
210     if (origin == null) {
211       Assert.assertNull("NodeMatcher: One doctype is null and one is not", target);
212     } else {
213       Assert.assertNotNull("NodeMatcher: One doctype is null and one is not", target);
214     }
215     Assert.assertEquals("NodeMatcher : document type public id's do not match."
216             , origin.getPublicId(), target.getPublicId());
217     Assert.assertEquals("NodeMatcher : document type system id's do not match."
218             , origin.getSystemId(), target.getSystemId());
219     Assert.assertEquals("NodeMatcher : document type names do not match."
220             , origin.getName(), target.getName());
221     compareNamedNodeMaps(origin.getEntities(), target.getEntities(), clone, deep);
222     compareNamedNodeMaps(origin.getNotations(), target.getNotations(), clone, deep);
223   }
224
225   protected static void compareDocuments(Document origin, Document target, boolean clone, boolean deep) {
226     DocumentType originDocumentType = origin.getDoctype();
227     DocumentType targetDocumentType = target.getDoctype();
228     compareNodes(originDocumentType, targetDocumentType, clone, deep);
229   }
230
231 }
232
Popular Tags