001: /*
002: ******************************************************************
003: Copyright (c) 200, 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 java.io.StringReader;
040:
041: import junit.framework.TestCase;
042: import junit.framework.TestSuite;
043:
044: import org.w3c.dom.Node;
045:
046: /**
047: * JUnit test for NodeTest
048: */
049: public class test_NodeTest extends TestCase {
050: private NodeTest nodeTest;
051:
052: private class NodeTypeTester implements NodeTester {
053: private short type;
054:
055: public NodeTypeTester(short type) {
056: this .type = type;
057: }
058:
059: public void testNode(Node aNode, NodeTest forTest) {
060: assertEquals(type, aNode.getNodeType());
061: }
062:
063: public void noMoreNodes(NodeTest forTest) {
064: }
065: }
066:
067: private class RejectingNodeTester implements NodeTester {
068: public boolean completed;
069:
070: public void testNode(Node aNode, NodeTest forTest)
071: throws NodeTestException {
072: throw new NodeTestException("Reject all nodes", aNode);
073: }
074:
075: public void noMoreNodes(NodeTest forTest)
076: throws NodeTestException {
077: completed = true;
078: throw new NodeTestException("Rejection");
079: }
080: }
081:
082: public void testFiltering() throws Exception {
083: nodeTest = new NodeTest(new StringReader(
084: "<message><hello>folks</hello></message>"));
085: short nodeType = Node.ELEMENT_NODE;
086: nodeTest.performTest(new NodeTypeTester(nodeType), nodeType);
087:
088: nodeType = Node.TEXT_NODE;
089: nodeTest.performTest(new NodeTypeTester(nodeType), nodeType);
090:
091: nodeType = Node.COMMENT_NODE;
092: nodeTest.performTest(new NodeTypeTester(nodeType), nodeType);
093:
094: short[] nodeTypes = new short[] { Node.TEXT_NODE,
095: Node.COMMENT_NODE };
096: nodeTest.performTest(new NodeTypeTester(Node.TEXT_NODE),
097: nodeTypes);
098: }
099:
100: public void testNodeTesting() throws Exception {
101: nodeTest = new NodeTest(new StringReader(
102: "<keyboard><qwerty>standard</qwerty></keyboard>"));
103: RejectingNodeTester tester = new RejectingNodeTester();
104:
105: try {
106: nodeTest.performTest(tester, Node.TEXT_NODE);
107: fail("Expected NodeTestException");
108: } catch (NodeTestException e) {
109: assertEquals("not completed", false, tester.completed);
110: }
111:
112: try {
113: nodeTest.performTest(tester, Node.CDATA_SECTION_NODE);
114: fail("Expected NodeTestException");
115: } catch (NodeTestException e) {
116: assertEquals("completed", true, tester.completed);
117: }
118: }
119:
120: public test_NodeTest(String name) {
121: super (name);
122: }
123:
124: public static TestSuite suite() {
125: return new TestSuite(test_NodeTest.class);
126: }
127:
128: }
|