001: /*
002: * Copyright 2004 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.unittest;
018:
019: import com.sun.syndication.io.XmlReader;
020: import com.sun.syndication.io.impl.XmlFixerReader;
021: import junit.framework.TestCase;
022: import org.jdom.input.SAXBuilder;
023:
024: import java.io.*;
025:
026: /**
027: * @author pat, tucu
028: *
029: */
030: public class TestXmlFixerReader extends TestCase {
031: private static final String XML_PROLOG = "<?xml version=\"1.0\" ?>";
032:
033: public void testTrim() throws Exception {
034: _testValidTrim("", "<hello></hello>");
035: _testValidTrim("", XML_PROLOG + "<hello></hello>");
036: _testValidTrim(" ", "<hello></hello>");
037: _testValidTrim(" ", XML_PROLOG + "<hello></hello>");
038: _testValidTrim(" \n", "<hello></hello>");
039: _testValidTrim(" \n", XML_PROLOG + "<hello></hello>");
040: _testValidTrim("<!-- - -- -->", "<hello></hello>");
041: _testValidTrim("<!-- - -- -->", XML_PROLOG + "<hello></hello>");
042: _testValidTrim(" <!-- - -- -->", "<hello></hello>");
043: _testValidTrim(" <!-- - -- -->", XML_PROLOG + "<hello></hello>");
044: _testValidTrim(" <!-- - -- --> ", "<hello></hello>");
045: _testValidTrim(" <!-- - -- --> ", XML_PROLOG
046: + "<hello></hello>");
047: _testValidTrim(" <!-- - -- --> <!-- - -- --> ",
048: "<hello></hello>");
049: _testValidTrim(" <!-- - -- --> <!-- - -- --> ", XML_PROLOG
050: + "<hello></hello>");
051: _testValidTrim(" <!-- - -- --> \n <!-- - -- --> ",
052: "<hello></hello>");
053: _testValidTrim(" <!-- - -- --> \n <!-- - -- --> ", XML_PROLOG
054: + "<hello></hello>");
055:
056: _testInvalidTrim("x", "<hello></hello>");
057: _testInvalidTrim("x", XML_PROLOG + "<hello></hello>");
058: _testInvalidTrim(" x", "<hello></hello>");
059: _testInvalidTrim(" x", XML_PROLOG + "<hello></hello>");
060: _testInvalidTrim(" x\n", "<hello></hello>");
061: _testInvalidTrim(" x\n", XML_PROLOG + "<hello></hello>");
062: _testInvalidTrim("<!-- - -- - ->", "<hello></hello>");
063: _testInvalidTrim("<!-- - -- - ->", XML_PROLOG
064: + "<hello></hello>");
065: _testInvalidTrim(" <!-- - -- -- >", "<hello></hello>");
066: _testInvalidTrim(" <!-- - -- -- >", XML_PROLOG
067: + "<hello></hello>");
068: _testInvalidTrim(" <!-- - -- -->x ", "<hello></hello>");
069: _testInvalidTrim(" <!-- - -- -->x ", XML_PROLOG
070: + "<hello></hello>");
071: _testInvalidTrim(" <!-- - -- --> x <!-- - -- --> ",
072: "<hello></hello>");
073: _testInvalidTrim(" <!-- - -- --> x <!-- - -- --> ", XML_PROLOG
074: + "<hello></hello>");
075: _testInvalidTrim(" <!-- - -- --> x\n <!-- - -- --> ",
076: "<hello></hello>");
077: _testInvalidTrim(" <!-- - -- --> x\n <!-- - -- --> ",
078: XML_PROLOG + "<hello></hello>");
079: }
080:
081: public void testHtmlEntities() throws Exception {
082: _testValidEntities("<hello></hello>");
083: _testValidEntities(XML_PROLOG + "<hello></hello>");
084: _testValidEntities(" <!-- just in case -->\n" + XML_PROLOG
085: + "<hello></hello>");
086:
087: _testValidEntities("<hello>'¥ú¥</hello>");
088: _testValidEntities(XML_PROLOG
089: + "<hello>'¥ú¥</hello>");
090: _testValidEntities(" <!-- just in case -->\n" + XML_PROLOG
091: + "<hello>'¥ú¥</hello>");
092:
093: _testValidEntities("<hello>ΠΡ#913;Ρ</hello>");
094: _testValidEntities(XML_PROLOG
095: + "<hello>ΠΡΑΡ</hello>");
096: _testValidEntities(" <!-- just in case -->\n" + XML_PROLOG
097: + "<hello>ΠΡΑΡ</hello>");
098:
099: _testValidEntities("<hello>Œ—–—</hello>");
100: _testValidEntities(XML_PROLOG
101: + "<hello>Œ—–—</hello>");
102: _testValidEntities(" <!-- just in case -->\n" + XML_PROLOG
103: + "<hello>Œ—–—</hello>");
104:
105: _testInvalidEntities("<hello>'&yexn;ú¥</hello>");
106: _testInvalidEntities(XML_PROLOG
107: + "<hello>'&yexn;ú¥</hello>");
108: _testInvalidEntities(" <!-- just in case -->\n" + XML_PROLOG
109: + "<hello>'&yexn;ú¥</hello>");
110:
111: _testInvalidEntities("<hello>Π&Rhox;#913;Ρ</hello>");
112: _testInvalidEntities(XML_PROLOG
113: + "<hello>Π&Rhox;ΑΡ</hello>");
114: _testInvalidEntities(" <!-- just in case -->\n" + XML_PROLOG
115: + "<hello>Π&Rhox;ΑΡ</hello>");
116:
117: _testInvalidEntities("<hello>'¥x50;¥</hello>");
118: _testInvalidEntities(XML_PROLOG
119: + "<hello>'¥x50;¥</hello>");
120: _testInvalidEntities(" <!-- just in case -->\n" + XML_PROLOG
121: + "<hello>'¥x50;¥</hello>");
122:
123: _testInvalidEntities("<hello>ΠΡ	x13;Ρ</hello>");
124: _testInvalidEntities(XML_PROLOG
125: + "<hello>ΠΡ	x13;Ρ</hello>");
126: _testInvalidEntities(" <!-- just in case -->\n" + XML_PROLOG
127: + "<hello>ΠΡ	x13;Ρ</hello>");
128: }
129:
130: protected void _testXmlParse(String garbish, String xmlDoc)
131: throws Exception {
132: InputStream is = getStream(garbish, xmlDoc);
133: Reader reader = new XmlReader(is);
134: reader = new XmlFixerReader(reader);
135: SAXBuilder saxBuilder = new SAXBuilder();
136: saxBuilder.build(reader);
137: }
138:
139: protected void _testValidTrim(String garbish, String xmlDoc)
140: throws Exception {
141: _testXmlParse(garbish, xmlDoc);
142: }
143:
144: protected void _testInvalidTrim(String garbish, String xmlDoc)
145: throws Exception {
146: try {
147: _testXmlParse(garbish, xmlDoc);
148: assertTrue(false);
149: } catch (Exception ex) {
150: }
151: }
152:
153: protected void _testValidEntities(String xmlDoc) throws Exception {
154: _testXmlParse("", xmlDoc);
155: }
156:
157: protected void _testInvalidEntities(String xmlDoc) throws Exception {
158: try {
159: _testXmlParse("", xmlDoc);
160: assertTrue(false);
161: } catch (Exception ex) {
162: }
163: }
164:
165: // XML Stream generator
166:
167: protected InputStream getStream(String garbish, String xmlDoc)
168: throws IOException {
169: ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
170: Writer writer = new OutputStreamWriter(baos);
171: writer.write(garbish);
172: writer.write(xmlDoc);
173: writer.close();
174: return new ByteArrayInputStream(baos.toByteArray());
175: }
176:
177: }
|