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: import java.io.FileInputStream;
041: import java.io.FileOutputStream;
042: import java.io.IOException;
043: import java.io.InputStreamReader;
044: import java.io.OutputStreamWriter;
045:
046: import junit.framework.TestCase;
047: import junit.framework.TestSuite;
048:
049: /**
050: * JUnit test for DoctypeInputStream
051: */
052: public class test_DoctypeInputStream extends AbstractDoctypeTests {
053:
054: private File testFile;
055:
056: public void tearDown() {
057: if (testFile != null) {
058: testFile.delete();
059: }
060: }
061:
062: private FileInputStream testDocument(String content)
063: throws IOException {
064: testFile = File.createTempFile("xmlunit_", ".xml");
065: FileOutputStream fos = new FileOutputStream(testFile);
066: OutputStreamWriter w = new OutputStreamWriter(fos, "ISO-8859-1");
067: w.write(content);
068: w.close();
069:
070: return new FileInputStream(testFile);
071: }
072:
073: private static String readFully(DoctypeInputStream dis)
074: throws IOException {
075: StringBuffer buf = new StringBuffer();
076: char[] ch = new char[1024];
077: int numChars;
078: InputStreamReader reader = new InputStreamReader(dis,
079: "ISO-8859-1");
080: while ((numChars = reader.read(ch)) != -1) {
081: buf.append(ch, 0, numChars);
082: }
083: return buf.toString();
084: }
085:
086: protected void assertEquals(String expected, String input,
087: String docType, String systemId) throws IOException {
088: FileInputStream fis = null;
089: try {
090: fis = testDocument(input);
091: DoctypeInputStream doctypeInputStream = new DoctypeInputStream(
092: fis, "ISO-8859-1", docType, systemId);
093:
094: assertEquals(expected, readFully(doctypeInputStream));
095: } finally {
096: if (fis != null) {
097: fis.close();
098: }
099: }
100: }
101:
102: public void testGetContent() throws IOException {
103: String source = "WooPDeDoO!\nGooRanga!\n plIng! ";
104: DoctypeInputStream dis = new DoctypeInputStream(
105: new java.io.StringBufferInputStream(source), null,
106: "nonsense", "words");
107: assertEquals(source, dis.getContent(null));
108: // can get content indefinitely from this stream
109: assertEquals(source, dis.getContent("UTF-8"));
110: }
111:
112: public test_DoctypeInputStream(String name) {
113: super(name);
114: }
115: }
|