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.File;
040:
041: import junit.framework.TestCase;
042:
043: import org.w3c.dom.Attr;
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Element;
046: import org.w3c.dom.Node;
047: import org.w3c.dom.Text;
048:
049: /**
050: * JUnit test for NodeDescriptor
051: */
052: public class test_NodeDescriptor extends TestCase {
053: private StringBuffer stringBuffer;
054: private Document aDocument;
055: private NodeDetail nodeDetail;
056:
057: public void testAppendDocumentDetail() throws Exception {
058: nodeDetail = new NodeDetail("", aDocument, "/");
059: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
060: assertEquals("<" + NodeDescriptor.DOCUMENT_NODE_DESCRIPTION
061: + "<...>> at /", stringBuffer.toString());
062: }
063:
064: public void testAppendAttributeDetail() throws Exception {
065: String attrName = "attrName";
066: String attrValue = "attrValue";
067: Attr attr = aDocument.createAttribute(attrName);
068: attr.setValue(attrValue);
069: String tagName = "elemTag";
070: Element element = aDocument.createElement(tagName);
071: element.setAttributeNode(attr);
072: nodeDetail = new NodeDetail("", attr, "/elemTag");
073: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
074: assertEquals("<" + tagName + " " + attrName + "=\"" + attrValue
075: + "\"...> at /elemTag", stringBuffer.toString());
076: }
077:
078: public void testAppendElementDetail() throws Exception {
079: String tagName = "elemTag";
080: Element element = aDocument.createElement(tagName);
081: nodeDetail = new NodeDetail("", element, "/elemTag");
082: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
083: assertEquals("<" + tagName + "...> at /elemTag", stringBuffer
084: .toString());
085: }
086:
087: public void testAppendTextDetail() throws Exception {
088: String textString = "some text";
089: Text text = aDocument.createTextNode(textString);
090: String tagName = "elemTag";
091: Element element = aDocument.createElement(tagName);
092: element.appendChild(text);
093: nodeDetail = new NodeDetail("", text, "/elemTag/text()");
094: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
095: assertEquals("<" + tagName + " ...>" + textString + "</"
096: + tagName + "> at /elemTag/text()", stringBuffer
097: .toString());
098: }
099:
100: public void testAppendProcessingInstructionDetail()
101: throws Exception {
102: String target = "PItarget";
103: String data = "PIdata";
104: Node processingInstruction = aDocument
105: .createProcessingInstruction(target, data);
106: nodeDetail = new NodeDetail("", processingInstruction,
107: "/processing-instruction()");
108: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
109: assertEquals("<?" + target + " " + data
110: + "?> at /processing-instruction()", stringBuffer
111: .toString());
112: }
113:
114: public void testAppendCommentDetail() throws Exception {
115: String comments = "This is a comment";
116: Node comment = aDocument.createComment(comments);
117: nodeDetail = new NodeDetail("", comment, "/comment()");
118: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
119: assertEquals("<!--" + comments + "--> at /comment()",
120: stringBuffer.toString());
121: }
122:
123: public void testAppendCDataDetail() throws Exception {
124: String cData = "<>& etc";
125: Node cDataNote = aDocument.createCDATASection(cData);
126: nodeDetail = new NodeDetail("", cDataNote, "/text()");
127: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
128: assertEquals("<![CDATA[" + cData + "]]> at /text()",
129: stringBuffer.toString());
130: }
131:
132: public void testAppendDocTypeDetail() throws Exception {
133: File dtdA = File.createTempFile(getName() + "A", "dtd");
134: dtdA.deleteOnExit();
135: String systemOnlyDTD = "<!DOCTYPE blah SYSTEM \""
136: + dtdA.toURL().toExternalForm() + "\">";
137: String someContent = "<blah>ignored</blah>";
138: String xmlWithExternalDTD = systemOnlyDTD + someContent;
139:
140: aDocument = XMLUnit.buildControlDocument(xmlWithExternalDTD);
141: Node doctypeA = aDocument.getDoctype();
142: nodeDetail = new NodeDetail("", doctypeA, "/");
143: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
144: assertEquals(systemOnlyDTD + " at /", stringBuffer.toString());
145:
146: stringBuffer = new StringBuffer();
147: File dtdB = File.createTempFile(getName() + "B", "dtd");
148: dtdB.deleteOnExit();
149: String publicDTD = "<!DOCTYPE web-app "
150: + "PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN\" "
151: + "\"" + dtdB.toURL().toExternalForm() + "\">";
152: String someOtherContent = "<web-app><!--ignore me--></web-app>";
153: String xmlWithPublicDTD = publicDTD + someOtherContent;
154:
155: Document bDocument = XMLUnit
156: .buildControlDocument(xmlWithPublicDTD);
157: Node doctypeB = bDocument.getDoctype();
158: nodeDetail = new NodeDetail("", doctypeB, "/");
159: NodeDescriptor.appendNodeDetail(stringBuffer, nodeDetail);
160: assertEquals(publicDTD + " at /", stringBuffer.toString());
161: }
162:
163: public void setUp() throws Exception {
164: aDocument = XMLUnit.newControlParser().newDocument();
165: stringBuffer = new StringBuffer();
166: }
167:
168: /**
169: * Constructor for test_NodeDescriptor.
170: * @param name
171: */
172: public test_NodeDescriptor(String name) {
173: super(name);
174: }
175:
176: }
|