001: /*
002: ******************************************************************
003: Copyright (c) 2001, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036: package org.custommonkey.xmlunit;
037:
038: import junit.framework.TestCase;
039:
040: import org.w3c.dom.Attr;
041: import org.w3c.dom.CDATASection;
042: import org.w3c.dom.Comment;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Element;
045: import org.w3c.dom.Node;
046: import org.w3c.dom.NodeList;
047: import org.w3c.dom.ProcessingInstruction;
048: import org.w3c.dom.Text;
049:
050: /**
051: * Testcase for XpathNodeTracker
052: */
053: public class test_XpathNodeTracker extends TestCase {
054: private XpathNodeTracker xpathNodeTracker;
055: private static final Node DUMMY_NODE = null;
056:
057: public void testRootNode() {
058: xpathNodeTracker.visitedNode(DUMMY_NODE, "diary");
059: assertEquals("root node", "/diary[1]", xpathNodeTracker
060: .toXpathString());
061: }
062:
063: public void testOneLevelOfChildren() {
064: xpathNodeTracker.visitedNode(DUMMY_NODE, "diary");
065:
066: xpathNodeTracker.indent();
067: assertEquals("before first child", "/diary[1]",
068: xpathNodeTracker.toXpathString());
069:
070: xpathNodeTracker.visitedNode(DUMMY_NODE, "event");
071: assertEquals("first child", "/diary[1]/event[1]",
072: xpathNodeTracker.toXpathString());
073:
074: xpathNodeTracker.visitedNode(DUMMY_NODE, "event");
075: assertEquals("2nd child", "/diary[1]/event[2]",
076: xpathNodeTracker.toXpathString());
077:
078: xpathNodeTracker.visitedNode(DUMMY_NODE, "event");
079: assertEquals("3rd child", "/diary[1]/event[3]",
080: xpathNodeTracker.toXpathString());
081:
082: xpathNodeTracker.visitedNode(DUMMY_NODE, "reminder");
083: assertEquals("4th child", "/diary[1]/reminder[1]",
084: xpathNodeTracker.toXpathString());
085:
086: xpathNodeTracker.visitedNode(DUMMY_NODE, "event");
087: assertEquals("5th child", "/diary[1]/event[4]",
088: xpathNodeTracker.toXpathString());
089: }
090:
091: public void testTwoLevelsOfChildren() {
092: xpathNodeTracker.visitedNode(DUMMY_NODE, "diary");
093:
094: xpathNodeTracker.indent();
095: xpathNodeTracker.visitedNode(DUMMY_NODE, "event");
096:
097: xpathNodeTracker.indent();
098: xpathNodeTracker.visitedNode(DUMMY_NODE, "details");
099: assertEquals("indented", "/diary[1]/event[1]/details[1]",
100: xpathNodeTracker.toXpathString());
101:
102: xpathNodeTracker.outdent();
103: xpathNodeTracker.visitedNode(DUMMY_NODE, "event");
104: assertEquals("outdented", "/diary[1]/event[2]",
105: xpathNodeTracker.toXpathString());
106:
107: xpathNodeTracker.indent();
108: xpathNodeTracker.visitedNode(DUMMY_NODE, "details");
109: assertEquals("re-indented", "/diary[1]/event[2]/details[1]",
110: xpathNodeTracker.toXpathString());
111:
112: xpathNodeTracker.outdent();
113: assertEquals("re-outdented", "/diary[1]/event[2]",
114: xpathNodeTracker.toXpathString());
115:
116: xpathNodeTracker.outdent();
117: assertEquals("outdented to root node", "/diary[1]",
118: xpathNodeTracker.toXpathString());
119: }
120:
121: public void testNodes() throws Exception {
122: Document doc = XMLUnit.newControlParser().newDocument();
123: Element element = doc.createElementNS(
124: "http://example.com/xmlunit", "eg:root");
125: xpathNodeTracker.visited(element);
126: assertEquals("root element", "/root[1]", xpathNodeTracker
127: .toXpathString());
128:
129: Attr attr = doc.createAttributeNS("http://example.com/xmlunit",
130: "eg:type");
131: attr.setValue("qwerty");
132: element.setAttributeNodeNS(attr);
133: xpathNodeTracker.visited(attr);
134: assertEquals("root element attribute", "/root[1]/@type",
135: xpathNodeTracker.toXpathString());
136:
137: xpathNodeTracker.indent();
138:
139: Comment comment = doc.createComment("testing a comment");
140: xpathNodeTracker.visited(comment);
141: assertEquals("comment", "/root[1]/comment()[1]",
142: xpathNodeTracker.toXpathString());
143:
144: ProcessingInstruction pi = doc.createProcessingInstruction(
145: "target", "data");
146: xpathNodeTracker.visited(pi);
147: assertEquals("p-i", "/root[1]/processing-instruction()[1]",
148: xpathNodeTracker.toXpathString());
149:
150: Text text = doc.createTextNode("some text");
151: xpathNodeTracker.visited(text);
152: assertEquals("text", "/root[1]/text()[1]", xpathNodeTracker
153: .toXpathString());
154:
155: CDATASection cdata = doc.createCDATASection("some characters");
156: xpathNodeTracker.visited(cdata);
157: assertEquals("cdata", "/root[1]/text()[2]", xpathNodeTracker
158: .toXpathString());
159: }
160:
161: public void testRepeatNodesForTestTracker() throws Exception {
162: Document doc = XMLUnit.newControlParser().newDocument();
163: final Element element = doc.createElement("repeated");
164: final Element copy = doc.createElement("repeated");
165:
166: NodeList nodeList = new NodeList() {
167: public Node item(int index) {
168: switch (index) {
169: case 0:
170: return element;
171: case 1:
172: return copy;
173: default:
174: return null;
175: }
176: }
177:
178: public int getLength() {
179: return 2;
180: }
181: };
182: xpathNodeTracker.preloadNodeList(nodeList);
183:
184: xpathNodeTracker.visited(element);
185: assertEquals("root element", "/repeated[1]", xpathNodeTracker
186: .toXpathString());
187:
188: xpathNodeTracker.visited(element);
189: assertEquals("visited root element again", "/repeated[1]",
190: xpathNodeTracker.toXpathString());
191:
192: xpathNodeTracker.visited(copy);
193: assertEquals("visited copy of root element", "/repeated[2]",
194: xpathNodeTracker.toXpathString());
195: }
196:
197: public void testRepeatNodesForControlTracker() throws Exception {
198: Document doc = XMLUnit.newControlParser().newDocument();
199: Element element = doc.createElement("repeated");
200:
201: xpathNodeTracker.visited(element);
202: assertEquals("root element", "/repeated[1]", xpathNodeTracker
203: .toXpathString());
204:
205: xpathNodeTracker.visited(element);
206: assertEquals("visited root element again", "/repeated[2]",
207: xpathNodeTracker.toXpathString());
208: }
209:
210: // bug 1047364
211: public void testEmptyIndentOutdentRootNode() {
212: xpathNodeTracker.indent();
213: xpathNodeTracker.outdent();
214: xpathNodeTracker.visitedNode(DUMMY_NODE, "diary");
215: assertEquals("root node", "/diary[1]", xpathNodeTracker
216: .toXpathString());
217: }
218:
219: public void setUp() {
220: xpathNodeTracker = new XpathNodeTracker();
221: xpathNodeTracker.reset();
222: }
223:
224: /**
225: * Constructor for test_XpathNodeTracker.
226: * @param name
227: */
228: public test_XpathNodeTracker(String name) {
229: super(name);
230: }
231:
232: }
|