KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Simple test ot verify pull parser factory
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

22 public class TestSimple extends UtilTestCase {
23     private XmlPullParserFactory factory;
24
25     public TestSimple(String JavaDoc name) {
26         super(name);
27     }
28
29     protected void setUp() throws XmlPullParserException {
30         factory = factoryNewInstance();
31         //assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
32
//assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION));
33
assertEquals(false, factory.isNamespaceAware());
34         assertEquals(false, factory.isValidating());
35     }
36
37     protected void tearDown() {
38     }
39
40     public void testSimple() throws Exception JavaDoc {
41         XmlPullParser xpp = factory.newPullParser();
42         assertEquals(false, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
43
44         // this SHOULD always be OK
45
assertEquals("START_DOCUMENT", xpp.TYPES[xpp.START_DOCUMENT]);
46         assertEquals("END_DOCUMENT", xpp.TYPES[xpp.END_DOCUMENT]);
47         assertEquals("START_TAG", xpp.TYPES[xpp.START_TAG]);
48         assertEquals("END_TAG", xpp.TYPES[xpp.END_TAG]);
49         assertEquals("TEXT", xpp.TYPES[xpp.TEXT]);
50         assertEquals("CDSECT", xpp.TYPES[xpp.CDSECT]);
51         assertEquals("ENTITY_REF", xpp.TYPES[xpp.ENTITY_REF]);
52         assertEquals("IGNORABLE_WHITESPACE", xpp.TYPES[xpp.IGNORABLE_WHITESPACE]);
53         assertEquals("PROCESSING_INSTRUCTION", xpp.TYPES[xpp.PROCESSING_INSTRUCTION]);
54         assertEquals("COMMENT", xpp.TYPES[xpp.COMMENT]);
55         assertEquals("DOCDECL", xpp.TYPES[xpp.DOCDECL]);
56
57         // check setInput semantics
58
assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType());
59         try {
60             xpp.next();
61             fail("exception was expected of next() if no input was set on parser");
62         } catch(XmlPullParserException ex) {}
63
64         xpp.setInput(null);
65         assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType());
66         try {
67             xpp.next();
68             fail("exception was expected of next() if no input was set on parser");
69         } catch(XmlPullParserException ex) {}
70
71         try {
72             xpp.setInput(null, null);
73             fail("exception was expected of setInput() if input stream is null");
74         } catch(IllegalArgumentException JavaDoc ex) {}
75
76         xpp.setInput(null);
77         assertTrue("line number must be -1 or >= 1 not "+xpp.getLineNumber(),
78                    xpp.getLineNumber() == -1 || xpp.getLineNumber() >= 1);
79         assertTrue("column number must be -1 or >= 0 not "+xpp.getColumnNumber(),
80                    xpp.getColumnNumber() == -1 || xpp.getColumnNumber() >= 0);
81
82         // check the simplest possible XML document - just one root element
83
xpp.setInput(new StringReader JavaDoc("<foo></foo>"));
84         assertEquals(null, xpp.getInputEncoding());
85         checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1);
86         xpp.next();
87         checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false /*empty*/, 0);
88         xpp.next();
89         checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1);
90         xpp.next();
91         checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1);
92
93
94         //check taking input form input stream
95
byte[] binput = "<foo/>".getBytes("UTF-8"); //Xerces2 doe snot like UTF8 ...
96
xpp.setInput(new ByteArrayInputStream JavaDoc( binput ), "UTF-8" );
97         assertEquals("UTF-8", xpp.getInputEncoding());
98         //xpp.setInput(new StringReader( "<foo/>" ) );
99
checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1);
100         xpp.next();
101         checkParserState(xpp, 1, xpp.START_TAG, "foo", null, true /*empty*/, 0);
102         xpp.next();
103         checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1);
104         xpp.next();
105         checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1);
106
107         // one step further - it has an attribute and content ...
108
xpp.setInput(new StringReader JavaDoc("<foo attrName='attrVal'>bar<p:t>\r\n\t </p:t></foo>"));
109         checkParserState(xpp, 0, xpp.START_DOCUMENT, null, null, false, -1);
110         xpp.next();
111         checkParserState(xpp, 1, xpp.START_TAG, "foo", null, false, 1);
112         checkAttrib(xpp, 0, "attrName", "attrVal");
113         xpp.next();
114         checkParserState(xpp, 1, xpp.TEXT, null, "bar", false, -1);
115         assertEquals(false, xpp.isWhitespace());
116         xpp.next();
117         checkParserState(xpp, 2, xpp.START_TAG, "p:t", null, false, 0);
118         xpp.next();
119         checkParserState(xpp, 2, xpp.TEXT, null, "\n\t ", false, -1);
120         assertTrue(xpp.isWhitespace());
121         xpp.next();
122         checkParserState(xpp, 2, xpp.END_TAG, "p:t", null, false, -1);
123         xpp.next();
124         checkParserState(xpp, 1, xpp.END_TAG, "foo", null, false, -1);
125         xpp.next();
126         checkParserState(xpp, 0, xpp.END_DOCUMENT, null, null, false, -1);
127
128
129     }
130
131     public static void main (String JavaDoc[] args) {
132         junit.textui.TestRunner.run (new TestSuite(TestSimple.class));
133     }
134
135 }
136
137
Popular Tags