001: /*
002: ******************************************************************
003: Copyright (c) 2007, 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:
037: package org.custommonkey.xmlunit;
038:
039: import junit.framework.Assert;
040: import junit.framework.TestCase;
041:
042: import org.w3c.dom.CDATASection;
043: import org.w3c.dom.Comment;
044: import org.w3c.dom.DocumentType;
045: import org.w3c.dom.Element;
046: import org.w3c.dom.Entity;
047: import org.w3c.dom.EntityReference;
048: import org.w3c.dom.Node;
049: import org.w3c.dom.Notation;
050: import org.w3c.dom.ProcessingInstruction;
051: import org.w3c.dom.Text;
052:
053: /**
054: * JUnit test for AbstractNodeTester
055: */
056: public class test_AbstractNodeTester extends TestCase {
057:
058: public void testExactlyOncePerMethod() throws Exception {
059: String testXml = "<!DOCTYPE foo [" + "<!ELEMENT foo (#PCDATA)>"
060: + "<!ATTLIST foo attr CDATA #IMPLIED>"
061: + "<!ENTITY my \"hello\">"
062: + "<!NOTATION notation PUBLIC \"pub\">" + "]>"
063: + "<foo attr=\"value\">" + "<!--comment-->"
064: + "<?target processing-instruction?>" + "bar" + "&my;"
065: + "<![CDATA[baz]]>" + "</foo>";
066: NodeTest nt = new NodeTest(testXml);
067: ExactlyOncePerMethod tester = new ExactlyOncePerMethod();
068: nt.performTest(tester, new short[] { Node.ATTRIBUTE_NODE,
069: Node.CDATA_SECTION_NODE, Node.COMMENT_NODE,
070: Node.DOCUMENT_FRAGMENT_NODE, Node.DOCUMENT_NODE,
071: Node.DOCUMENT_TYPE_NODE, Node.ELEMENT_NODE,
072: Node.ENTITY_NODE, Node.ENTITY_REFERENCE_NODE,
073: Node.NOTATION_NODE, Node.PROCESSING_INSTRUCTION_NODE,
074: Node.TEXT_NODE, });
075: tester.verify();
076: }
077:
078: private class ExactlyOncePerMethod extends AbstractNodeTester {
079:
080: private boolean cdataCalled;
081: private boolean commentCalled;
082: private boolean elementCalled;
083: private boolean piCalled;
084: private boolean textCalled;
085: private boolean noMoreNodesCalled;
086:
087: public void testCDATASection(CDATASection cdata) {
088: Assert.assertFalse("testCDATASection called", cdataCalled);
089: cdataCalled = true;
090: Assert.assertEquals("baz", cdata.getNodeValue());
091: }
092:
093: public void testComment(Comment comment) {
094: Assert.assertFalse("testComment called", commentCalled);
095: commentCalled = true;
096: Assert.assertEquals("comment", comment.getNodeValue());
097: }
098:
099: public void testElement(Element element) {
100: Assert.assertFalse("testElement called", elementCalled);
101: elementCalled = true;
102: Assert.assertEquals("foo", element.getNodeName());
103: Assert.assertEquals("value", element.getAttribute("attr"));
104: }
105:
106: public void testProcessingInstruction(
107: ProcessingInstruction instr) {
108: Assert.assertFalse("testProcessingInstruction called",
109: piCalled);
110: piCalled = true;
111: Assert.assertEquals("target", instr.getTarget());
112: Assert.assertEquals("processing-instruction", instr
113: .getData());
114: }
115:
116: public void testText(Text text) {
117: Assert.assertFalse("testText called", textCalled);
118: textCalled = true;
119: Assert.assertEquals("barhello", text.getNodeValue());
120: }
121:
122: public void noMoreNodes(NodeTest t) {
123: Assert.assertFalse("noMoreNodes called", noMoreNodesCalled);
124: noMoreNodesCalled = true;
125: }
126:
127: void verify() {
128: Assert.assertTrue("testCDATASection not called",
129: cdataCalled);
130: Assert.assertTrue("testComment not called", commentCalled);
131: Assert.assertTrue("testElement not called", elementCalled);
132: Assert.assertTrue("testProcessingInstruction not called",
133: piCalled);
134: Assert.assertTrue("testText not called", textCalled);
135: Assert.assertTrue("noMoreNodes not called",
136: noMoreNodesCalled);
137: }
138: }
139: }
|