1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 testHtmlEntities() throws Exception {
70 _testValidEntities("<hello></hello>");
71 _testValidEntities(XML_PROLOG+"<hello></hello>");
72 _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello></hello>");
73
74 _testValidEntities("<hello>'¥ú¥</hello>");
75 _testValidEntities(XML_PROLOG+"<hello>'¥ú¥</hello>");
76 _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>'¥ú¥</hello>");
77
78 _testValidEntities("<hello>ΠΡ#913;Ρ</hello>");
79 _testValidEntities(XML_PROLOG+"<hello>ΠΡΑΡ</hello>");
80 _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>ΠΡΑΡ</hello>");
81
82 _testValidEntities("<hello>Œ—–—</hello>");
83 _testValidEntities(XML_PROLOG+"<hello>Œ—–—</hello>");
84 _testValidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>Œ—–—</hello>");
85
86 _testInvalidEntities("<hello>'&yexn;ú¥</hello>");
87 _testInvalidEntities(XML_PROLOG+"<hello>'&yexn;ú¥</hello>");
88 _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>'&yexn;ú¥</hello>");
89
90 _testInvalidEntities("<hello>Π&Rhox;#913;Ρ</hello>");
91 _testInvalidEntities(XML_PROLOG+"<hello>Π&Rhox;ΑΡ</hello>");
92 _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>Π&Rhox;ΑΡ</hello>");
93
94 _testInvalidEntities("<hello>'¥x50;¥</hello>");
95 _testInvalidEntities(XML_PROLOG+"<hello>'¥x50;¥</hello>");
96 _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>'¥x50;¥</hello>");
97
98 _testInvalidEntities("<hello>ΠΡ	x13;Ρ</hello>");
99 _testInvalidEntities(XML_PROLOG+"<hello>ΠΡ	x13;Ρ</hello>");
100 _testInvalidEntities(" <!-- just in case -->\n"+XML_PROLOG+"<hello>ΠΡ	x13;Ρ</hello>");
101 }
102
103 protected void _testXmlParse(String garbish,String xmlDoc) throws Exception {
104 InputStream is = getStream(garbish,xmlDoc);
105 Reader reader = new XmlReader(is);
106 reader = new XmlFixerReader(reader);
107 SAXBuilder saxBuilder = new SAXBuilder();
108 saxBuilder.build(reader);
109 }
110
111 protected void _testValidTrim(String garbish,String xmlDoc) throws Exception {
112 _testXmlParse(garbish,xmlDoc);
113 }
114
115 protected void _testInvalidTrim(String garbish,String xmlDoc) throws Exception {
116 try {
117 _testXmlParse(garbish,xmlDoc);
118 assertTrue(false);
119 }
120 catch (Exception ex) {
121 }
122 }
123
124 protected void _testValidEntities(String xmlDoc) throws Exception {
125 _testXmlParse("",xmlDoc);
126 }
127
128 protected void _testInvalidEntities(String xmlDoc) throws Exception {
129 try {
130 _testXmlParse("",xmlDoc);
131 assertTrue(false);
132 }
133 catch (Exception ex) {
134 }
135 }
136
137
138
139 protected InputStream getStream(String garbish,String xmlDoc) throws IOException {
140 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
141 Writer writer = new OutputStreamWriter(baos);
142 writer.write(garbish);
143 writer.write(xmlDoc);
144 writer.close();
145 return new ByteArrayInputStream(baos.toByteArray());
146 }
147
148
149 }