1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package com.sun.syndication.unittest;
18  
19  import com.sun.syndication.io.XmlReader;
20  import com.sun.syndication.io.impl.XmlFixerReader;
21  import junit.framework.TestCase;
22  import org.jdom.input.SAXBuilder;
23  
24  import java.io.*;
25  
26  /***
27   * @author pat, tucu
28   *
29   */
30  public class TestXmlFixerReader extends TestCase {
31      private static final String XML_PROLOG = "<?xml version=\"1.0\" ?>";
32  
33      public void testTrim() throws Exception {
34          _testValidTrim("","<hello></hello>");
35          _testValidTrim("",XML_PROLOG+"<hello></hello>");
36          _testValidTrim(" ","<hello></hello>");
37          _testValidTrim(" ",XML_PROLOG+"<hello></hello>");
38          _testValidTrim(" \n","<hello></hello>");
39          _testValidTrim(" \n",XML_PROLOG+"<hello></hello>");
40          _testValidTrim("<!-- - -- -->","<hello></hello>");
41          _testValidTrim("<!-- - -- -->",XML_PROLOG+"<hello></hello>");
42          _testValidTrim(" <!-- - -- -->","<hello></hello>");
43          _testValidTrim(" <!-- - -- -->",XML_PROLOG+"<hello></hello>");
44          _testValidTrim(" <!-- - -- --> ","<hello></hello>");
45          _testValidTrim(" <!-- - -- --> ",XML_PROLOG+"<hello></hello>");
46          _testValidTrim(" <!-- - -- --> <!-- - -- --> ","<hello></hello>");
47          _testValidTrim(" <!-- - -- --> <!-- - -- --> ",XML_PROLOG+"<hello></hello>");
48          _testValidTrim(" <!-- - -- --> \n <!-- - -- --> ","<hello></hello>");
49          _testValidTrim(" <!-- - -- --> \n <!-- - -- --> ",XML_PROLOG+"<hello></hello>");
50  
51          _testInvalidTrim("x","<hello></hello>");
52          _testInvalidTrim("x",XML_PROLOG+"<hello></hello>");
53          _testInvalidTrim(" x","<hello></hello>");
54          _testInvalidTrim(" x",XML_PROLOG+"<hello></hello>");
55          _testInvalidTrim(" x\n","<hello></hello>");
56          _testInvalidTrim(" x\n",XML_PROLOG+"<hello></hello>");
57          _testInvalidTrim("<!-- - -- - ->","<hello></hello>");
58          _testInvalidTrim("<!-- - -- - ->",XML_PROLOG+"<hello></hello>");
59          _testInvalidTrim(" <!-- - -- -- >","<hello></hello>");
60          _testInvalidTrim(" <!-- - -- -- >",XML_PROLOG+"<hello></hello>");
61          _testInvalidTrim(" <!-- - -- -->x ","<hello></hello>");
62          _testInvalidTrim(" <!-- - -- -->x ",XML_PROLOG+"<hello></hello>");
63          _testInvalidTrim(" <!-- - -- --> x <!-- - -- --> ","<hello></hello>");
64          _testInvalidTrim(" <!-- - -- --> x <!-- - -- --> ",XML_PROLOG+"<hello></hello>");
65          _testInvalidTrim(" <!-- - -- --> x\n <!-- - -- --> ","<hello></hello>");
66          _testInvalidTrim(" <!-- - -- --> x\n <!-- - -- --> ",XML_PROLOG+"<hello></hello>");
67      }
68  
69      public void testAmpHandling() throws Exception {
70          String input = "&amp; &aa &";
71          BufferedReader reader = new BufferedReader(new XmlFixerReader(new StringReader(input)));
72          String output = reader.readLine();
73          reader.close();
74          assertEquals("&amp; &amp;aa &amp;", output);
75      }
76  
77      public void testHtmlEntities() throws Exception {
78          _testValidEntities("<hello></hello>");
79          _testValidEntities(XML_PROLOG+"<hello></hello>");
80          _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello></hello>");
81  
82          _testValidEntities("<hello>&apos;&yen;&#250;&yen;</hello>");
83          _testValidEntities(XML_PROLOG+"<hello>&apos;&yen;&#250;&yen;</hello>");
84          _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&apos;&yen;&#250;&yen;</hello>");
85  
86          _testValidEntities("<hello>&Pi;&Rho;#913;&Rho;</hello>");
87          _testValidEntities(XML_PROLOG+"<hello>&Pi;&Rho;&#913;&Rho;</hello>");
88          _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&Pi;&Rho;&#913;&Rho;</hello>");
89  
90          _testValidEntities("<hello>&OElig;&mdash;&#8211;&mdash;</hello>");
91          _testValidEntities(XML_PROLOG+"<hello>&OElig;&mdash;&#8211;&mdash;</hello>");
92          _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&OElig;&mdash;&#8211;&mdash;</hello>");        
93          
94          _testInvalidEntities("<hello>&apos;&yexn;&#250;&yen;</hello>");
95          _testInvalidEntities(XML_PROLOG+"<hello>&apos;&yexn;&#250;&yen;</hello>");
96          _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&apos;&yexn;&#250;&yen;</hello>");
97  
98          _testInvalidEntities("<hello>&Pi;&Rhox;#913;&Rho;</hello>");
99          _testInvalidEntities(XML_PROLOG+"<hello>&Pi;&Rhox;&#913;&Rho;</hello>");
100         _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&Pi;&Rhox;&#913;&Rho;</hello>");        
101         
102         _testInvalidEntities("<hello>&apos;&yen;&#2x50;&yen;</hello>");
103         _testInvalidEntities(XML_PROLOG+"<hello>&apos;&yen;&#2x50;&yen;</hello>");
104         _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&apos;&yen;&#2x50;&yen;</hello>");
105 
106         _testInvalidEntities("<hello>&Pi;&Rho;&#9x13;&Rho;</hello>");
107         _testInvalidEntities(XML_PROLOG+"<hello>&Pi;&Rho;&#9x13;&Rho;</hello>");
108         _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&Pi;&Rho;&#9x13;&Rho;</hello>");
109     }
110 
111     protected void _testXmlParse(String garbish,String xmlDoc) throws Exception {
112         InputStream is = getStream(garbish,xmlDoc);
113         Reader reader = new XmlReader(is);
114         reader = new XmlFixerReader(reader);
115         SAXBuilder saxBuilder = new SAXBuilder();
116         saxBuilder.build(reader);
117     }
118 
119     protected void _testValidTrim(String garbish,String xmlDoc) throws Exception {
120         _testXmlParse(garbish,xmlDoc);
121     }
122 
123     protected void _testInvalidTrim(String garbish,String xmlDoc) throws Exception {
124         try {
125             _testXmlParse(garbish,xmlDoc);
126             assertTrue(false);
127         }
128         catch (Exception ex) {
129         }
130     }
131 
132     protected void _testValidEntities(String xmlDoc) throws Exception {
133         _testXmlParse("",xmlDoc);
134     }
135 
136     protected void _testInvalidEntities(String xmlDoc) throws Exception {
137         try {
138             _testXmlParse("",xmlDoc);
139             assertTrue(false);
140         }
141         catch (Exception ex) {
142         }
143     }
144 
145     // XML Stream generator
146 
147     protected InputStream getStream(String garbish,String xmlDoc) throws IOException {
148         ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
149         Writer writer = new OutputStreamWriter(baos);
150         writer.write(garbish);
151         writer.write(xmlDoc);
152         writer.close();
153         return new ByteArrayInputStream(baos.toByteArray());
154     }
155 
156 
157 }