KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > unittest > TestXmlFixerReader


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 JavaDoc XML_PROLOG = "<?xml version=\"1.0\" ?>";
32
33     public void testTrim() throws Exception JavaDoc {
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 testHtmlEntities() throws Exception JavaDoc {
70         _testValidEntities("<hello></hello>");
71         _testValidEntities(XML_PROLOG+"<hello></hello>");
72         _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello></hello>");
73
74         _testValidEntities("<hello>&apos;&yen;&#250;&yen;</hello>");
75         _testValidEntities(XML_PROLOG+"<hello>&apos;&yen;&#250;&yen;</hello>");
76         _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&apos;&yen;&#250;&yen;</hello>");
77
78         _testInvalidEntities("<hello>&apos;&yexn;&#250;&yen;</hello>");
79         _testInvalidEntities(XML_PROLOG+"<hello>&apos;&yexn;&#250;&yen;</hello>");
80         _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&apos;&yexn;&#250;&yen;</hello>");
81
82         _testInvalidEntities("<hello>&apos;&yen;&#2x50;&yen;</hello>");
83         _testInvalidEntities(XML_PROLOG+"<hello>&apos;&yen;&#2x50;&yen;</hello>");
84         _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>&apos;&yen;&#2x50;&yen;</hello>");
85
86     }
87
88     protected void _testXmlParse(String JavaDoc garbish,String JavaDoc xmlDoc) throws Exception JavaDoc {
89         InputStream is = getStream(garbish,xmlDoc);
90         Reader reader = new XmlReader(is);
91         reader = new XmlFixerReader(reader);
92         SAXBuilder saxBuilder = new SAXBuilder();
93         saxBuilder.build(reader);
94     }
95
96     protected void _testValidTrim(String JavaDoc garbish,String JavaDoc xmlDoc) throws Exception JavaDoc {
97         _testXmlParse(garbish,xmlDoc);
98     }
99
100     protected void _testInvalidTrim(String JavaDoc garbish,String JavaDoc xmlDoc) throws Exception JavaDoc {
101         try {
102             _testXmlParse(garbish,xmlDoc);
103             assertTrue(false);
104         }
105         catch (Exception JavaDoc ex) {
106         }
107     }
108
109     protected void _testValidEntities(String JavaDoc xmlDoc) throws Exception JavaDoc {
110         _testXmlParse("",xmlDoc);
111     }
112
113     protected void _testInvalidEntities(String JavaDoc xmlDoc) throws Exception JavaDoc {
114         try {
115             _testXmlParse("",xmlDoc);
116             assertTrue(false);
117         }
118         catch (Exception JavaDoc ex) {
119         }
120     }
121
122     // XML Stream generator
123

124     protected InputStream getStream(String JavaDoc garbish,String JavaDoc xmlDoc) throws IOException {
125         ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
126         Writer writer = new OutputStreamWriter(baos);
127         writer.write(garbish);
128         writer.write(xmlDoc);
129         writer.close();
130         return new ByteArrayInputStream(baos.toByteArray());
131     }
132
133
134 }
135
Popular Tags