KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringReader JavaDoc;
11 import java.io.StringWriter JavaDoc;
12
13
14 import org.xmlpull.v1.XmlPullParser;
15 import org.xmlpull.v1.XmlPullParserFactory;
16 import org.xmlpull.v1.XmlPullParserException;
17
18 /**
19  * Conformance test to verify nextToken() behavior.
20  *
21  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
22  */

23 public class TestToken extends UtilTestCase {
24     private XmlPullParserFactory factory;
25
26     public TestToken(String JavaDoc name) {
27         super(name);
28     }
29
30     protected void setUp() throws XmlPullParserException {
31         factory = factoryNewInstance();
32         factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
33         assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
34         assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION));
35     }
36
37     protected void tearDown() {
38         factory = null;
39     }
40
41
42     public static void main (String JavaDoc[] args) {
43         junit.textui.TestRunner.run (new TestSuite(TestToken.class));
44     }
45
46     private static final String JavaDoc FOO_XML =
47         "<!DOCTYPE titlepage "+
48         "[<!ENTITY % active.links \"INCLUDE\">"+
49         " <!ENTITY test \"This is test! Do NOT Panic!\" >"+
50         "]>"+
51         "<foo attrName='attrVal'>bar<!--comment\r\ntest-->"+
52         "&test;&test;&lt;&#32;"+
53         "&amp;&gt;&apos;&quot;&#x20;&#x3C;"+
54         "<?pi ds\r\nda?><![CDATA[ vo<o ]]></foo>";
55     private static final String JavaDoc MISC_XML =
56         //"\n \r\n \n\r<!DOCTYPE titlepage SYSTEM \"http://www.foo.bar/dtds/typo.dtd\""+
57
//"<!--c2-->"+
58
//"<?xml version='1.0'?><!--c-->"+
59
//" \r\n <?c x?>"+
60
"\n \r\n \n\r"+
61         "<!--c--> \r\n"+
62         FOO_XML+
63         " \r\n";
64
65
66
67     public void testTokenEventEquivalency() throws Exception JavaDoc {
68         XmlPullParser xpp = factory.newPullParser();
69         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
70         xpp.setInput(new StringReader JavaDoc(MISC_XML));
71         boolean processDocdecl = xpp.getFeature(xpp.FEATURE_PROCESS_DOCDECL);
72
73         // make sure entity "test" can be resolved even when parser is not parsing DOCDECL
74
if(!processDocdecl) {
75             xpp.defineEntityReplacementText("test", "This is test! Do NOT Panic!");
76         }
77
78         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
79         xpp.next();
80         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false, 1);
81         checkAttribNs(xpp, 0, null, "", "attrName", "attrVal");
82         xpp.next();
83         checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null,
84                            "barThis is test! Do NOT Panic!This is test! Do NOT Panic!< &>'\" < vo<o ",
85                            false, -1);
86         xpp.next();
87         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
88         xpp.next();
89         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
90
91     }
92
93     public void testTokenTypes() throws Exception JavaDoc {
94         testTokenTypes(false, false);
95         testTokenTypes(false, true);
96     }
97
98     public void testTokenTypesInPrologAndEpilog() throws Exception JavaDoc {
99         testTokenTypes(true, false);
100         testTokenTypes(true, false);
101     }
102
103     // one step further - it has content ...
104
public void testTokenTypes(boolean checkPrologAndEpilog,
105                                boolean useRoundtrip) throws Exception JavaDoc {
106         XmlPullParser xpp = factory.newPullParser();
107         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
108         //System.out.println("using "+xpp);
109

110         if(checkPrologAndEpilog) {
111             xpp.setInput(new StringReader JavaDoc(MISC_XML));
112         } else {
113             xpp.setInput(new StringReader JavaDoc(FOO_XML));
114         }
115
116         // attempt to set roundtrip
117
try {
118             xpp.setFeature(FEATURE_XML_ROUNDTRIP, useRoundtrip);
119         } catch(Exception JavaDoc ex) { // make sure we ignore if failed to set roundtrip ...
120
}
121         // did we succeeded?
122
boolean roundtripSupported = xpp.getFeature(FEATURE_XML_ROUNDTRIP);
123         //boolean unnormalizedSupported = xpp.getFeature(FEATURE_UNNORMALIZED_XML);
124
if(!useRoundtrip && roundtripSupported != false) {
125             throw new RuntimeException JavaDoc(
126                 "disabling feature "+FEATURE_XML_ROUNDTRIP+" must be supported");
127         }
128
129         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
130         try {
131             xpp.isWhitespace();
132             fail("whitespace function must fail for START_DOCUMENT");
133         } catch(XmlPullParserException ex) {
134         }
135
136         if(checkPrologAndEpilog) {
137             xpp.nextToken();
138             if(xpp.getEventType() == xpp.IGNORABLE_WHITESPACE) {
139                 // xpp.nextToken();
140
// checkParserStateNs(xpp, 0, xpp.IGNORABLE_WHITESPACE, null, 0, null, null,
141
// "\n \r\n \n\r", false, -1);
142
// assertTrue(xpp.isWhitespace());
143
String JavaDoc text = gatherTokenText(xpp, xpp.IGNORABLE_WHITESPACE, true);
144                 if(roundtripSupported) {
145                     assertEquals(printable("\n \r\n \n\r"), printable(text));
146                     assertEquals("\n \r\n \n\r", text);
147                 } else {
148                     assertEquals(printable("\n \n \n\n"), printable(text));
149                     assertEquals("\n \n \n\n", text);
150                 }
151             }
152
153             checkParserStateNs(xpp, 0, xpp.COMMENT, null, 0, null, null, "c", false, -1);
154             try {
155                 xpp.isWhitespace();
156                 fail("whitespace function must fail for START_DOCUMENT");
157             } catch(XmlPullParserException ex) {
158             }
159
160             xpp.nextToken();
161             if(xpp.getEventType() == xpp.IGNORABLE_WHITESPACE) {
162                 // xpp.nextToken();
163
// checkParserStateNs(xpp, 0, xpp.IGNORABLE_WHITESPACE, null, 0, null, null, " \r\n", false, -1);
164
// assertTrue(xpp.isWhitespace());
165
//String text = nextTokenGathered(xpp, xpp.IGNORABLE_WHITESPACE, true);
166
String JavaDoc text = gatherTokenText(xpp, xpp.IGNORABLE_WHITESPACE, true);
167                 if(roundtripSupported) {
168                     assertEquals(printable(" \r\n"), printable(text));
169                 } else {
170                     assertEquals(printable(" \n"), printable(text));
171                 }
172             }
173
174
175         } else {
176             xpp.nextToken();
177         }
178         checkParserStateNs(xpp, 0, xpp.DOCDECL, null, 0, null, null, false, -1);
179         String JavaDoc expectedDocdecl = " titlepage "+
180             //"SYSTEM \"http://www.foo.bar/dtds/typo.dtd\""+
181
"[<!ENTITY % active.links \"INCLUDE\">"+
182             " <!ENTITY test \"This is test! Do NOT Panic!\" >]";
183         String JavaDoc gotDocdecl = xpp.getText();
184         if(roundtripSupported && gotDocdecl == null) {
185             fail("when roundtrip is enabled DOCDECL content must be reported");
186         }
187         if(gotDocdecl != null) {
188             assertEquals("DOCDECL content", expectedDocdecl, gotDocdecl);
189         }
190
191         try {
192             xpp.isWhitespace();
193             fail("whitespace function must fail for DOCDECL");
194         } catch(XmlPullParserException ex) {
195         }
196
197         // now parse elements
198
xpp.nextToken();
199         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false, 1);
200         if(roundtripSupported) {
201             assertEquals("start tag roundtrip", "<foo attrName='attrVal'>", xpp.getText());
202         }
203         checkAttribNs(xpp, 0, null, "", "attrName", "attrVal");
204         try {
205             xpp.isWhitespace();
206             fail("whitespace function must fail for START_DOCUMENT");
207         } catch(XmlPullParserException ex) {
208         }
209
210         {
211             String JavaDoc text = nextTokenGathered(xpp, xpp.TEXT, false);
212             assertEquals(printable("bar"), printable(text));
213         }
214
215         //xpp.nextToken();
216

217         checkParserStateNs(xpp, 1, xpp.COMMENT, null, 0, null, null, false, -1);
218         try {
219             xpp.isWhitespace();
220             fail("whitespace function must fail for COMMENT");
221         } catch(XmlPullParserException ex) {
222         }
223         {
224             String JavaDoc text = xpp.getText();
225                 if(roundtripSupported) {
226                     assertEquals(printable("comment\r\ntest"), printable(text));
227                 } else {
228                     assertEquals(printable("comment\ntest"), printable(text));
229                 }
230             }
231
232         boolean processDocdecl = xpp.getFeature(xpp.FEATURE_PROCESS_DOCDECL);
233
234         // uresolved entity must be reurned as null by nextToken()
235
xpp.nextToken();
236         if(!processDocdecl) {
237             checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null,
238                                "test", null, false, -1);
239         } else {
240             checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null,
241                                "test", "This is test! Do NOT Panic!", false, -1);
242
243         }
244
245         // now we check if we can resolve entity
246
if(!processDocdecl) {
247             xpp.defineEntityReplacementText("test", "This is test! Do NOT Panic!");
248         }
249         xpp.nextToken();
250         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null,
251                            "test", "This is test! Do NOT Panic!", false, -1);
252         try {
253             xpp.isWhitespace();
254             fail("whitespace function must fail for ENTITY_RED");
255         } catch(XmlPullParserException ex) {
256         }
257
258         // check standard entities and char refs
259
xpp.nextToken();
260         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "lt", "<", false, -1);
261         try {
262             xpp.isWhitespace();
263             fail("whitespace function must fail for ENTITY_REF");
264         } catch(XmlPullParserException ex) {
265         }
266
267         xpp.nextToken();
268         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "#32", " ", false, -1);
269         try {
270             xpp.isWhitespace();
271             fail("whitespace function must fail for ENTITY_REF");
272         } catch(XmlPullParserException ex) {
273         }
274
275         xpp.nextToken();
276         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "amp", "&", false, -1);
277
278         xpp.nextToken();
279         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "gt", ">", false, -1);
280
281         xpp.nextToken();
282         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "apos", "'", false, -1);
283
284         xpp.nextToken();
285         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "quot", "\"", false, -1);
286
287         xpp.nextToken();
288         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "#x20", " ", false, -1);
289
290         xpp.nextToken();
291         checkParserStateNs(xpp, 1, xpp.ENTITY_REF, null, 0, null, "#x3C", "<", false, -1);
292
293         xpp.nextToken();
294         checkParserStateNs(xpp, 1, xpp.PROCESSING_INSTRUCTION, null, 0, null, null, false, -1);
295         try {
296             xpp.isWhitespace();
297             fail("whitespace function must fail for START_DOCUMENT");
298         } catch(XmlPullParserException ex) {
299         }
300         {
301             String JavaDoc text = xpp.getText();
302                 if(roundtripSupported) {
303                     assertEquals(printable("pi ds\r\nda"), printable(text));
304                 } else {
305                     assertEquals(printable("pi ds\nda"), printable(text));
306                 }
307             }
308
309         xpp.nextToken();
310         checkParserStateNs(xpp, 1, xpp.CDSECT, null, 0, null, null, " vo<o ", false, -1);
311         assertEquals(false, xpp.isWhitespace());
312
313         xpp.nextToken();
314         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
315         if(roundtripSupported) {
316             assertEquals("end tag roundtrip", "</foo>", xpp.getText());
317         }
318         try {
319             xpp.isWhitespace();
320             fail("whitespace function must fail for END_TAG");
321         } catch(XmlPullParserException ex) {
322         }
323
324         xpp.nextToken();
325         if(checkPrologAndEpilog) {
326             //if(unnormalizedSupported) {
327
if(xpp.getEventType() == xpp.IGNORABLE_WHITESPACE) {
328
329                 // xpp.nextToken();
330
// checkParserStateNs(xpp, 0, xpp.IGNORABLE_WHITESPACE, null, 0, null, null,
331
// " \r\n", false, -1);
332
// assertTrue(xpp.isWhitespace());
333
//String text = nextTokenGathered(xpp, xpp.IGNORABLE_WHITESPACE, true);
334
String JavaDoc text = gatherTokenText(xpp, xpp.IGNORABLE_WHITESPACE, true);
335                 if(roundtripSupported) {
336                     assertEquals(printable(" \r\n"), printable(text));
337                 } else {
338                     assertEquals(printable(" \n"), printable(text));
339                 }
340             }
341         }
342         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
343         try {
344             xpp.isWhitespace();
345             fail("whitespace function must fail for END_DOCUMENT");
346         } catch(XmlPullParserException ex) {
347         }
348
349     }
350
351     // one step further - it has content ...
352
public void testXmlRoundtrip() throws Exception JavaDoc {
353         XmlPullParser xpp = factory.newPullParser();
354         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
355
356
357         // reset parser
358
xpp.setInput(null);
359         // attempt to set roundtrip
360
try {
361             xpp.setFeature(FEATURE_XML_ROUNDTRIP, true);
362         } catch(Exception JavaDoc ex) {
363         }
364         // did we succeeded?
365
boolean roundtripSupported = xpp.getFeature(FEATURE_XML_ROUNDTRIP);
366
367         if(!roundtripSupported) {
368             return;
369         }
370         PackageTests.addNote("* feature "+FEATURE_XML_ROUNDTRIP+" is supported\n");
371
372         StringWriter JavaDoc sw = new StringWriter JavaDoc();
373         String JavaDoc s;
374         //StringWriter st = new StringWriter();
375
xpp.setInput(new StringReader JavaDoc(MISC_XML));
376         int[] holderForStartAndLength = new int[2];
377         char[] buf;
378         while(xpp.nextToken() != xpp.END_DOCUMENT) {
379             switch(xpp.getEventType()) {
380                 case XmlPullParser.START_TAG:
381                     buf = xpp.getTextCharacters(holderForStartAndLength);
382                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
383                     assertEquals("roundtrip START_TAG", xpp.getText(), s);
384                     sw.write(s);
385                     break;
386                 case XmlPullParser.END_TAG:
387                     buf = xpp.getTextCharacters(holderForStartAndLength);
388                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
389                     assertEquals("roundtrip END_TAG", xpp.getText(), s);
390                     sw.write(s);
391                     break;
392                 case XmlPullParser.TEXT:
393                     buf = xpp.getTextCharacters(holderForStartAndLength);
394                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
395                     assertEquals("roundtrip TEXT", xpp.getText(), s);
396                     sw.write(s);
397                     break;
398                 case XmlPullParser.IGNORABLE_WHITESPACE:
399                     buf = xpp.getTextCharacters(holderForStartAndLength);
400                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
401                     assertEquals("roundtrip IGNORABLE_WHITESPACE", xpp.getText(), s);
402                     sw.write(s);
403                     break;
404                 case XmlPullParser.CDSECT:
405                     sw.write("<![CDATA[");
406                     buf = xpp.getTextCharacters(holderForStartAndLength);
407                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
408                     assertEquals("roundtrip CDSECT", xpp.getText(), s);
409                     sw.write(s);
410                     sw.write("]]>");
411                     break;
412                 case XmlPullParser.PROCESSING_INSTRUCTION:
413                     sw.write("<?");
414                     buf = xpp.getTextCharacters(holderForStartAndLength);
415                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
416                     assertEquals("roundtrip PROCESSING_INSTRUCTION", xpp.getText(), s);
417                     sw.write(s);
418                     sw.write("?>");
419                     break;
420                 case XmlPullParser.COMMENT:
421                     sw.write("<!--");
422                     buf = xpp.getTextCharacters(holderForStartAndLength);
423                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
424                     assertEquals("roundtrip COMMENT", xpp.getText(), s);
425                     sw.write(s);
426                     sw.write("-->");
427                     break;
428                 case XmlPullParser.ENTITY_REF:
429                     sw.write("&");
430                     buf = xpp.getTextCharacters(holderForStartAndLength);
431                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
432                     assertEquals("roundtrip ENTITY_REF", xpp.getName(), s);
433                     sw.write(s);
434                     sw.write(";");
435                     break;
436                 case XmlPullParser.DOCDECL:
437                     sw.write("<!DOCTYPE");
438                     buf = xpp.getTextCharacters(holderForStartAndLength);
439                     s = new String JavaDoc(buf, holderForStartAndLength[0], holderForStartAndLength[1]);
440                     assertEquals("roundtrip DOCDECL", xpp.getText(), s);
441                     sw.write(s);
442                     sw.write(">");
443                     break;
444                 default:
445                     throw new RuntimeException JavaDoc("unknown token type");
446             }
447         }
448         sw.close();
449         String JavaDoc RESULT_XML_BUF = sw.toString();
450         assertEquals("rountrip XML", printable(MISC_XML), printable(RESULT_XML_BUF));
451     }
452
453
454 }
455
456
Popular Tags