KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmlpull > v1 > tests > TestProcessDocdecl


1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 // for license see accompanying LICENSE_TESTS.txt file (available also at http://www.xmlpull.org)
3

4 package org.xmlpull.v1.tests;
5
6 //import junit.framework.Test;
7
import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.StringReader JavaDoc;
12
13 import org.xmlpull.v1.XmlPullParser;
14 import org.xmlpull.v1.XmlPullParserFactory;
15 import org.xmlpull.v1.XmlPullParserException;
16
17 /**
18  * Test FEATURE_PROCESS_DOCDECL (when supported)
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

22 public class TestProcessDocdecl extends UtilTestCase {
23     private XmlPullParserFactory factory;
24
25     public static void main (String JavaDoc[] args) {
26         junit.textui.TestRunner.run (new TestSuite(TestProcessDocdecl.class));
27     }
28
29
30     public TestProcessDocdecl(String JavaDoc name) {
31         super(name);
32     }
33
34     protected void setUp() throws XmlPullParserException {
35         factory = factoryNewInstance();
36         //assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
37
//assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION));
38
assertEquals(false, factory.isNamespaceAware());
39         assertEquals(false, factory.isValidating());
40         //System.out.println("factory="+factory);
41
}
42
43     private XmlPullParser newParser(boolean useNamespaces, boolean useValidation)
44         throws XmlPullParserException
45     {
46         XmlPullParser xpp = factory.newPullParser();
47         xpp.setFeature(xpp.FEATURE_PROCESS_NAMESPACES, useNamespaces);
48         assertEquals(useNamespaces, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
49         try {
50             xpp.setFeature(xpp.FEATURE_PROCESS_DOCDECL, true);
51         } catch(XmlPullParserException ex) {
52             return null;
53         }
54         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL));
55         try {
56             xpp.setFeature(xpp.FEATURE_VALIDATION, useValidation);
57         } catch(XmlPullParserException ex) {
58             return null;
59         }
60         assertEquals(useValidation, xpp.getFeature(XmlPullParser.FEATURE_VALIDATION));
61         return xpp;
62     }
63
64     protected void tearDown() {
65     }
66
67     public void testSimpleEntity() throws Exception JavaDoc {
68         testSimpleEntity(false, false);
69         testSimpleEntity(true, false);
70         testSimpleEntity(false, true);
71         testSimpleEntity(true, true);
72     }
73
74     public void testAttributeNormalization() throws Exception JavaDoc {
75         //TODO check that Xerces NON-validating normalization of NMTOKEN attributes
76
//http://www.w3.org/TR/REC-xml#AVNormalize
77

78     }
79
80     public void testSimpleEntity(boolean useNamespaces, boolean useValidation) throws Exception JavaDoc {
81         XmlPullParser xpp = newParser(useNamespaces, useValidation);
82         if(xpp == null) return;
83
84         //http://www.w3.org/TR/REC-xml#intern-replacement
85
final String JavaDoc XML_SIMPLE_ENT_PROLOG =
86             "<?xml version='1.0'?>\n"+
87             "<!DOCTYPE test [\n"+
88             "<!ENTITY % YN '\"Yes\"' >\n"+
89             //"<!ENTITY WhatHeSaid \"He said %YN;\" >\n"+
90
"<!ELEMENT test (#PCDATA) >\n"+
91             // "<!ENTITY % pub \"&#xc9;ditions Gallimard\" >\n"+
92
// "<!ENTITY rights \"All rights reserved\" >\n"+
93
// "<!ENTITY book \"La Peste: Albert Camus,\n"+
94
"<!ENTITY pub \"&#xc9;ditions Gallimard\" >\n"+
95             "<!ENTITY rights \"All rights reserved\" >\n"+
96             "<!ENTITY book \"La Peste: Albert Camus,\n"+
97             "&#xA9; 1947 &pub;. &rights;\" >\n"+
98             "]>\n";
99         final String JavaDoc XML_SIMPLE_ENT = XML_SIMPLE_ENT_PROLOG+
100             "<test>Publication: &book; </test>\n";
101
102         // final String PUB_ENTITY_INTERNAL_REPLACEMENT =
103
// "La Peste: Albert Camus,\n"+
104
// "© 1947 Éditions Gallimard. &rights;";
105
final String JavaDoc PUB_ENTITY_INTERNAL_REPLACEMENT =
106             "La Peste: Albert Camus,\n"+
107             "&pub;. &rights;";
108
109
110         final String JavaDoc PUB_ENTITY_REPLACEMENT =
111             "La Peste: Albert Camus,\n"+
112             "© 1947 Éditions Gallimard. All rights reserved";
113
114         //next
115
xpp.setInput(new StringReader JavaDoc( XML_SIMPLE_ENT ));
116         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
117         xpp.next();
118         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false/*empty*/, 0);
119         xpp.next();
120         String JavaDoc expectedContent = "Publication: "+PUB_ENTITY_REPLACEMENT+" ";
121         checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, expectedContent, false, -1);
122         xpp.next();
123         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1);
124         xpp.next();
125         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
126
127         //nextToken
128
}
129
130     public void testEntityWithMarkup() throws Exception JavaDoc {
131         testEntityWithMarkup(false, false);
132         testEntityWithMarkup(true, false);
133         testEntityWithMarkup(true, true);
134         testEntityWithMarkup(false, true);
135     }
136
137     public void testEntityWithMarkup(boolean useNamespaces, boolean useValidation)
138         throws Exception JavaDoc
139     {
140         XmlPullParser xpp = newParser(useNamespaces, useValidation);
141         if(xpp == null) return;
142
143         //http://www.w3.org/TR/REC-xml#sec-entexpand
144
// derived from
145
final String JavaDoc XML_REPLACE_ENT =
146             "<?xml version='1.0'?>\n"+
147             "<!DOCTYPE test [\n"+
148             "<!ELEMENT test (#PCDATA|p)* >\n"+
149             "<!ELEMENT p (#PCDATA) >\n"+
150             "<!ENTITY example \"<p>An ampersand (&#38;#38;) may be escaped\n"+
151             "numerically (&#38;#38;#38;) or with a general entity\n"+
152             "(&amp;amp;).</p>\" >\n"+
153             "]>\n"+
154             "<test>&example; </test> ";
155
156         final String JavaDoc EXAMPLE_ENTITY_INTERNAL_REPLACEMENT =
157             "<p>An ampersand (&#38;) may be escaped\n"+
158             "numerically (&#38;#38;) or with a general entity\n"+
159             "(&amp;amp;).</p>\n";
160
161         final String JavaDoc EXAMPLE_ENTITY_TEXT_EVENT =
162             "An ampersand (&) may be escaped\n"+
163             "numerically (&#38;) or with a general entity\n"+
164             "(&amp;).";
165
166         //next
167
xpp.setInput(new StringReader JavaDoc( XML_REPLACE_ENT ));
168         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
169         xpp.next();
170         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false/*empty*/, 0);
171         xpp.next();
172         checkParserStateNs(xpp, 2, xpp.START_TAG, null, 0, "", "p", null, false/*empty*/, 0);
173         xpp.next();
174         String JavaDoc expectedContent = EXAMPLE_ENTITY_TEXT_EVENT;
175         checkParserStateNs(xpp, 2, xpp.TEXT, null, 0, null, null, expectedContent, false, -1);
176         xpp.next();
177         checkParserStateNs(xpp, 2, xpp.END_TAG, null, 0, "", "p", null, false/*empty*/, -1);
178         xpp.next();
179         checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, " ", false, -1);
180         xpp.next();
181         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1);
182         xpp.next();
183         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
184     }
185
186     public void testTricky() throws Exception JavaDoc {
187         testTricky(false, false);
188         testTricky(true, false);
189         testTricky(true, true);
190         testTricky(false, true);
191     }
192
193     public void testTricky(boolean useNamespaces, boolean useValidation)
194         throws Exception JavaDoc
195     {
196         XmlPullParser xpp = newParser(useNamespaces, useValidation);
197         if(xpp == null) return;
198
199         // derived from
200
final String JavaDoc XML_TRICKY =
201             "<?xml version='1.0'?>\n"+
202             "<!DOCTYPE test [\n"+
203             "<!ELEMENT test (#PCDATA) >\n"+
204             "<!ENTITY % xx '&#37;zz;'>\n"+
205             "<!ENTITY % zz '&#60;!ENTITY tricky \"error-prone\" >' >\n"+
206             "%xx;\n"+
207             "]>\n"+
208             "<test>This sample shows a &tricky; method.</test>\n";
209
210
211         final String JavaDoc EXPECTED_XML_TRICKY =
212             "This sample shows a error-prone method.";
213
214         //next
215
xpp.setInput(new StringReader JavaDoc( XML_TRICKY ));
216         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
217         xpp.next();
218         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "test", null, false/*empty*/, 0);
219         xpp.next();
220         String JavaDoc expectedContent = EXPECTED_XML_TRICKY;
221         checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, expectedContent, false, -1);
222         xpp.next();
223         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "test", null, false, -1);
224         xpp.next();
225         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
226
227         //TODO nextToken()
228

229     }
230
231 }
232
233
Popular Tags