KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 import org.xmlpull.v1.XmlPullParser;
13 import org.xmlpull.v1.XmlPullParserFactory;
14 import org.xmlpull.v1.XmlPullParserException;
15
16 /**
17  * Tests checking miscellaneous features.
18  *
19  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
20  */

21 public class TestMisc extends UtilTestCase {
22     private XmlPullParserFactory factory;
23
24     public TestMisc(String JavaDoc name) {
25         super(name);
26     }
27
28     protected void setUp() throws XmlPullParserException {
29         factory = factoryNewInstance();
30         factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
31         assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
32     }
33
34     protected void tearDown() {
35     }
36
37     public void testNextTag() throws Exception JavaDoc {
38         final String JavaDoc INPUT_XML = "<t> <test1>foo</test1>\n<test2> </test2>\n </t>";
39         XmlPullParser pp = factory.newPullParser();
40         pp.setInput( new StringReader JavaDoc( INPUT_XML ) );
41         pp.nextTag();
42         pp.require( pp.START_TAG, null, "t");
43         pp.nextTag();
44         pp.require( pp.START_TAG, null, "test1");
45         assertEquals( "foo", pp.nextText() );
46         pp.require( pp.END_TAG, null, "test1");
47
48         pp.nextTag();
49         pp.require( pp.START_TAG, null, "test2");
50         pp.nextTag();
51         pp.require( pp.END_TAG, null, "test2");
52
53
54         pp.nextTag();
55         pp.require( pp.END_TAG, null, "t");
56         pp.next();
57         pp.require( pp.END_DOCUMENT, null, null);
58
59     }
60
61     // public void testReadText() throws Exception {
62
// final String INPUT_XML = "<test>foo</test>";
63
// XmlPullParser pp = factory.newPullParser();
64
// pp.setInput( new StringReader( INPUT_XML ) );
65
// assertEquals( "", pp.readText() );
66
// pp.next();
67
// assertEquals( "", pp.readText() );
68
// pp.next();
69
// assertEquals( "foo", pp.readText() );
70
// assertEquals( pp.TYPES[ pp.END_TAG ], pp.TYPES[ pp.getEventType() ]);
71
// }
72

73     public void testNextText() throws Exception JavaDoc {
74         final String JavaDoc INPUT_XML =
75             "<t><test1>foo</test1><test2></test2><test3/><test4>bar</test4></t>";
76         XmlPullParser pp = factory.newPullParser();
77         pp.setInput( new StringReader JavaDoc( INPUT_XML ) );
78         pp.next();
79         pp.require( pp.START_TAG, null, "t");
80         pp.next();
81         pp.require( pp.START_TAG, null, "test1");
82         assertEquals( "foo", pp.nextText() );
83         pp.require( pp.END_TAG, null, "test1");
84
85         pp.next();
86         pp.require( pp.START_TAG, null, "test2");
87         assertEquals( "", pp.nextText() );
88         pp.require( pp.END_TAG, null, "test2");
89
90         pp.next();
91         pp.require( pp.START_TAG, null, "test3");
92         assertEquals( "", pp.nextText() );
93         pp.require( pp.END_TAG, null, "test3");
94
95         pp.next();
96         pp.require( pp.START_TAG, null, "test4");
97         //pp.next();
98
//pp.require( pp.TEXT, null, null);
99
assertEquals( "bar", pp.nextText() );
100         pp.require( pp.END_TAG, null, "test4");
101
102         pp.next();
103         pp.require( pp.END_TAG, null, "t");
104         pp.next();
105         pp.require( pp.END_DOCUMENT, null, null);
106
107         // now check for error conditions
108
pp.setInput( new StringReader JavaDoc( INPUT_XML ) );
109         pp.next();
110         pp.require( pp.START_TAG, null, "t");
111         pp.next();
112         pp.require( pp.START_TAG, null, "test1");
113         pp.next();
114         pp.require( pp.TEXT, null, null);
115         try {
116             pp.nextText();
117             fail("if current tag is TEXT no next text content can be returned!");
118         } catch(XmlPullParserException ex) {}
119
120         pp.setInput( new StringReader JavaDoc( INPUT_XML ) );
121         pp.next();
122         pp.require( pp.START_TAG, null, "t");
123         try {
124             pp.nextText();
125             fail("if next tag is START_TAG no text content can be returned!");
126         } catch(XmlPullParserException ex) {}
127
128         pp.setInput( new StringReader JavaDoc( INPUT_XML ) );
129         pp.next();
130         pp.require( pp.START_TAG, null, "t");
131         pp.next();
132         pp.require( pp.START_TAG, null, "test1");
133         pp.next();
134         pp.next();
135         pp.require( pp.END_TAG, null, "test1");
136         try {
137             pp.nextText();
138             fail("if current tag is END_TAG no text content can be returned!");
139         } catch(XmlPullParserException ex) {}
140
141     }
142
143     public void testRequire() throws Exception JavaDoc {
144         //public void require (int type, String namespace, String name)
145
final String JavaDoc INPUT_XML = "<test><t>foo</t><m:s xmlns:m='URI'>\t</m:s></test>";
146         XmlPullParser pp = factory.newPullParser();
147         pp.setInput( new StringReader JavaDoc( INPUT_XML ) );
148         pp.require( pp.START_DOCUMENT, null, null);
149         pp.next();
150         pp.require( pp.START_TAG, null, "test");
151         pp.require( pp.START_TAG, "", null);
152         pp.require( pp.START_TAG, "", "test");
153         pp.next();
154         pp.require( pp.START_TAG, "", "t");
155         pp.next();
156         pp.require( pp.TEXT, null, null);
157         pp.next();
158         pp.require( pp.END_TAG, "", "t");
159
160         pp.next();
161         pp.require( pp.START_TAG, "URI", "s");
162
163         pp.next();
164         pp.require( pp.TEXT, null, null);
165         assertEquals("\t", pp.getText());
166         pp.next();
167         pp.require( pp.END_TAG, "URI", "s");
168
169         pp.next();
170         pp.require( pp.END_TAG, "", "test");
171         pp.next();
172         pp.require( pp.END_DOCUMENT, null, null);
173
174         //now check that require will NOT skip white space
175
pp = factory.newPullParser();
176         pp.setInput( new StringReader JavaDoc( "<m:s xmlns:m='URI'>\t</m:s>" ) );
177         pp.require( pp.START_DOCUMENT, null, null);
178         pp.next();
179         pp.require( pp.START_TAG, "URI", "s");
180         pp.next();
181         try {
182             pp.require( pp.END_TAG, "URI", "s");
183             fail("require() MUST NOT skip white spaces");
184         } catch(XmlPullParserException ex){}
185
186     }
187
188     public void testPI() throws Exception JavaDoc {
189         XmlPullParser pp = factory.newPullParser();
190         //System.out.println(getClass()+"-pp="+pp);
191

192         pp.setInput( new StringReader JavaDoc( "<foo><?XmLsdsd test?></foo>" ) );
193         pp.require( pp.START_DOCUMENT, null, null);
194         pp.next();
195         pp.require( pp.START_TAG, null, "foo");
196         pp.next();
197         pp.require( pp.END_TAG, null, "foo");
198
199         pp.setInput( new StringReader JavaDoc( "<foo><?XmL test?></foo>" ) );
200         pp.require( pp.START_DOCUMENT, null, null);
201         pp.next();
202         pp.require( pp.START_TAG, null, "foo");
203         try {
204             pp.next();
205             fail("expected exception for invalid PI starting with xml");
206         } catch(XmlPullParserException ex){}
207
208         pp.setInput( new StringReader JavaDoc( "<foo><?pi test?></foo>" ) );
209         pp.require( pp.START_DOCUMENT, null, null);
210         pp.next();
211         pp.require( pp.START_TAG, null, "foo");
212         pp.nextToken();
213         pp.require( pp.PROCESSING_INSTRUCTION, null, null);
214         assertEquals("PI", "pi test", pp.getText());
215         pp.next();
216         pp.require( pp.END_TAG, null, "foo");
217
218
219     }
220
221     public void testXmlDecl() throws Exception JavaDoc {
222         XmlPullParser pp = factory.newPullParser();
223
224         pp.setInput( new StringReader JavaDoc( "<?xml version='1.0'?><foo/>" ) );
225         pp.require( pp.START_DOCUMENT, null, null);
226         pp.next();
227         pp.require( pp.START_TAG, null, "foo");
228         pp.next();
229         pp.require( pp.END_TAG, null, "foo");
230         pp.next();
231         pp.require( pp.END_DOCUMENT, null, null);
232
233         pp.setInput( new StringReader JavaDoc(
234                         "<?xml version=\"1.0\" \t encoding='UTF-8' \nstandalone='yes' ?><foo/>" ));
235         pp.require( pp.START_DOCUMENT, null, null);
236         pp.next();
237         pp.require( pp.START_TAG, null, "foo");
238
239         pp.setInput( new StringReader JavaDoc(
240                         "<?xml version=\"1.0\" \t encoding='UTF-8' \nstandalone='yes' ?><foo/>" ));
241         pp.require( pp.START_DOCUMENT, null, null);
242         assertNull(pp.getProperty(PROPERTY_XMLDECL_VERSION));
243         assertNull(pp.getProperty(PROPERTY_XMLDECL_STANDALONE));
244         assertNull(pp.getProperty(PROPERTY_XMLDECL_CONTENT));
245
246         pp.next();
247         pp.require( pp.START_TAG, null, "foo");
248         String JavaDoc xmlDeclVersion = (String JavaDoc) pp.getProperty(PROPERTY_XMLDECL_VERSION);
249         if(xmlDeclVersion != null) {
250             assertEquals("XMLDecl version","1.0", xmlDeclVersion);
251             PackageTests.addNote("* property "+PROPERTY_XMLDECL_VERSION+" is supported\n");
252         }
253         Boolean JavaDoc xmlDeclStandalone = (Boolean JavaDoc) pp.getProperty(PROPERTY_XMLDECL_STANDALONE);
254         if(xmlDeclStandalone != null) {
255             assertTrue("XMLDecl standalone",xmlDeclStandalone.booleanValue());
256             PackageTests.addNote("* property "+PROPERTY_XMLDECL_STANDALONE+" is supported\n");
257         }
258         String JavaDoc xmlDeclContent = (String JavaDoc) pp.getProperty(PROPERTY_XMLDECL_CONTENT);
259         if(xmlDeclContent != null) {
260             String JavaDoc expected = " version=\"1.0\" \t encoding='UTF-8' \nstandalone='yes' ";
261             assertEquals("XMLDecl content", printable(expected), printable(xmlDeclContent));
262             PackageTests.addNote("* property "+PROPERTY_XMLDECL_CONTENT+" is supported\n");
263         }
264
265
266         // XML decl without required verion
267
pp.setInput( new StringReader JavaDoc( "<?xml test?><foo/>" ) );
268         pp.require( pp.START_DOCUMENT, null, null);
269
270         // check that proerties are reset
271
assertNull(pp.getProperty(PROPERTY_XMLDECL_VERSION));
272         assertNull(pp.getProperty(PROPERTY_XMLDECL_STANDALONE));
273         assertNull(pp.getProperty(PROPERTY_XMLDECL_CONTENT));
274
275         try {
276             pp.next();
277             fail("expected exception for invalid XML declaration without version");
278         } catch(XmlPullParserException ex){}
279
280
281         pp.setInput( new StringReader JavaDoc(
282                         "<?xml version=\"1.0\" standalone='yes' encoding=\"UTF-8\" ?>\n<foo/>" ));
283         pp.require( pp.START_DOCUMENT, null, null);
284         try {
285             pp.next();
286             fail("expected exception for invalid XML declaration with standalone at wrong position");
287         } catch(XmlPullParserException ex){}
288
289
290         pp.setInput( new StringReader JavaDoc(
291                         "<?xml version=\"1.0\" \t encoding='UTF-8' \nstandalone='yes' ?>"
292                             +"<!--comment--><foo/>" ));
293         pp.require( pp.START_DOCUMENT, null, null);
294         // make sure that XMLDecl is not reported
295
pp.nextToken();
296         pp.require( pp.COMMENT, null, null);
297         pp.nextToken();
298         pp.require( pp.START_TAG, null, "foo");
299         pp.next();
300         pp.require( pp.END_TAG, null, "foo");
301         pp.next();
302         pp.require( pp.END_DOCUMENT, null, null);
303
304     }
305
306     public void testComments() throws Exception JavaDoc {
307         XmlPullParser pp = factory.newPullParser();
308         pp.setInput( new StringReader JavaDoc( "<foo><!-- B+, B or B---></foo>" ) );
309         pp.require( pp.START_DOCUMENT, null, null);
310         pp.next();
311         pp.require( pp.START_TAG, null, "foo");
312         try {
313             pp.next();
314             fail("expected eception for invalid comment ending with --->");
315         } catch(XmlPullParserException ex){}
316     }
317
318     public void testReportNamespaceAttributes() throws Exception JavaDoc {
319         XmlPullParser pp = factory.newPullParser();
320         assertEquals(true, pp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
321
322         try {
323             pp.setFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, true);
324         }catch(XmlPullParserException ex) {
325             // skip rest of test if parser does nto support reporting
326
return;
327         }
328         PackageTests.addNote("* feature "+pp.FEATURE_REPORT_NAMESPACE_ATTRIBUTES+" is supported\n");
329         // see XML Namespaces spec for namespace URIs for 'xml' and 'xmlns'
330
// xml is bound to http://www.w3.org/XML/1998/namespace
331
// "(...) The prefix xmlns is used only for namespace bindings
332
// and is not itself bound to any namespace name. (...)
333
// however it is typically bound to "http://www.w3.org/2000/xmlns/"
334
// in some contexts such as DOM
335
// http://www.w3.org/TR/REC-xml-names/#ns-using
336
final String JavaDoc XML_MISC_ATTR =
337             "<test xmlns='Some-Namespace-URI' xmlns:n='Some-Other-URI'"+
338             " a='a' b='b' xmlns:m='Another-URI' m:a='c' n:b='d' n:x='e' xml:lang='en'"+
339             "/>\n"+
340             "";
341         pp.setInput(new StringReader JavaDoc(XML_MISC_ATTR));
342         pp.next();
343         //pp.readStartTag(stag);
344
assertEquals("test", pp.getName());
345         assertEquals("Some-Namespace-URI", pp.getNamespace());
346
347         assertEquals("a", pp.getAttributeValue("","a"));
348         assertEquals("b", pp.getAttributeValue("","b"));
349         assertEquals(null, pp.getAttributeValue("", "m:a"));
350         assertEquals(null, pp.getAttributeValue("", "n:b"));
351         assertEquals(null, pp.getAttributeValue("", "n:x"));
352
353         assertEquals("c", pp.getAttributeValue("Another-URI", "a"));
354         assertEquals("d", pp.getAttributeValue("Some-Other-URI", "b"));
355         assertEquals("e", pp.getAttributeValue("Some-Other-URI", "x"));
356         assertEquals("en", pp.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang"));
357
358
359         checkAttribNs(pp, 0, null, "", "xmlns", "Some-Namespace-URI");
360         checkAttribNs(pp, 1, "xmlns", "http://www.w3.org/2000/xmlns/","n","Some-Other-URI");
361         checkAttribNs(pp, 2, null, "", "a", "a");
362         checkAttribNs(pp, 3, null, "", "b", "b");
363         checkAttribNs(pp, 4, "xmlns", "http://www.w3.org/2000/xmlns/","m","Another-URI");
364         checkAttribNs(pp, 5, "m", "Another-URI","a","c");
365         checkAttribNs(pp, 6, "n", "Some-Other-URI","b","d");
366         checkAttribNs(pp, 7, "n", "Some-Other-URI","x","e");
367         checkAttribNs(pp, 8, "xml", "http://www.w3.org/XML/1998/namespace", "lang", "en");
368     }
369
370
371     public void testRoundtripNext() throws Exception JavaDoc {
372         // check that entiy reference is reported in start tag as unexpanded when roundtrip is enabled
373
final String JavaDoc SAMPLE_XML =
374             "<foo attEol='a\r\nv\n\ra\n\r\r\nio\rn\nica' attr=\"baz &amp; bar\"/>";
375
376         XmlPullParser pp = factory.newPullParser();
377         assertEquals(true, pp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
378         pp.setInput(new StringReader JavaDoc(SAMPLE_XML));
379
380         try {
381             pp.setFeature(FEATURE_XML_ROUNDTRIP, true);
382         } catch(Exception JavaDoc ex) {
383         }
384         // did we succeeded?
385
boolean roundtripSupported = pp.getFeature(FEATURE_XML_ROUNDTRIP);
386
387
388         pp.next();
389         String JavaDoc attrValue = pp.getAttributeValue(pp.NO_NAMESPACE, "attr");
390         assertEquals("baz & bar", attrValue);
391         String JavaDoc attEolValue = pp.getAttributeValue(pp.NO_NAMESPACE, "attEol");
392         assertEquals("a v a io n ica", attEolValue);
393         if(roundtripSupported) {
394             String JavaDoc text = pp.getText();
395             assertEquals(SAMPLE_XML, text);
396         }
397
398     }
399
400     public static void main (String JavaDoc[] args) {
401         junit.textui.TestRunner.run (new TestSuite(TestMisc.class));
402     }
403
404 }
405
406
Popular Tags