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.IOException;
040: import java.io.StringReader;
041:
042: import junit.framework.TestCase;
043: import junit.framework.TestSuite;
044:
045: /**
046: * JUnit test for DoctypeReader
047: */
048: public class test_DoctypeReader extends AbstractDoctypeTests {
049: private DoctypeReader doctypeReader;
050: private StringReader sourceReader;
051: private static final String NEWLINE = System
052: .getProperty("line.separator");
053:
054: public void testGetContent() throws IOException {
055: String source = "WooPDeDoO!" + NEWLINE + "GooRanga!" + NEWLINE
056: + " plIng! ";
057: sourceReader = new StringReader(source);
058: doctypeReader = new DoctypeReader(sourceReader, "nonsense",
059: "words");
060: assertEquals(source, doctypeReader.getContent());
061: // can get content indefinitely from this reader
062: assertEquals(source, doctypeReader.getContent());
063: }
064:
065: private void initDummyDoctypeReader() {
066: sourceReader = new StringReader("yabba");
067: doctypeReader = new DoctypeReader(sourceReader, "yabba",
068: "don\'t");
069: }
070:
071: public void testReplaceDoctypeInternalDTD() {
072: initDummyDoctypeReader();
073: StringBuffer buf = new StringBuffer(
074: test_Constants.CHUCK_JONES_RIP_DTD_DECL);
075: assertEquals("<!DOCTYPE ni SYSTEM \"shrubbery\">",
076: doctypeReader.replaceDoctype(buf, "ni", "shrubbery"));
077: }
078:
079: public void testReplaceDoctypeExternalDTD() {
080: initDummyDoctypeReader();
081: StringBuffer buf = new StringBuffer(
082: "<! DOCTYPE PUBLIC \"yak\" SYSTEM \"llama\">");
083: assertEquals("<! DOCTYPE ni SYSTEM \"shrubbery\">",
084: doctypeReader.replaceDoctype(buf, "ni", "shrubbery"));
085: }
086:
087: public void testReplaceDoctypeNoDTD() {
088: initDummyDoctypeReader();
089: StringBuffer buf = new StringBuffer(NO_DTD);
090: assertEquals("<!DOCTYPE ni SYSTEM \"shrubbery\">" + NO_DTD,
091: doctypeReader.replaceDoctype(buf, "ni", "shrubbery"));
092: }
093:
094: public void testReplaceDoctypeNoDTDButXMLDecl() {
095: initDummyDoctypeReader();
096: StringBuffer buf = new StringBuffer(
097: test_Constants.XML_DECLARATION + NO_DTD);
098: assertEquals(test_Constants.XML_DECLARATION
099: + "<!DOCTYPE ni SYSTEM \"shrubbery\">" + NO_DTD,
100: doctypeReader.replaceDoctype(buf, "ni", "shrubbery"));
101: }
102:
103: private static String readFully(DoctypeReader reader)
104: throws IOException {
105: StringBuffer buf = new StringBuffer();
106: char[] ch = new char[1024];
107: int numChars;
108: while ((numChars = reader.read(ch)) != -1) {
109: buf.append(ch, 0, numChars);
110: }
111: return buf.toString();
112: }
113:
114: protected void assertEquals(String expected, String input,
115: String docType, String systemId) throws IOException {
116: DoctypeReader doctypeReader = new DoctypeReader(
117: new StringReader(expected), docType, systemId);
118: assertEquals(expected, readFully(doctypeReader));
119: }
120:
121: public test_DoctypeReader(String name) {
122: super(name);
123: }
124: }
|