KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.xmlpull.v1.XmlPullParser;
14 import org.xmlpull.v1.XmlPullParserFactory;
15 import org.xmlpull.v1.XmlPullParserException;
16
17 /**
18  * Simple test for minimal XML tokenizing
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

22 public class TestSimpleToken extends UtilTestCase {
23     private XmlPullParserFactory factory;
24
25
26     public static void main (String JavaDoc[] args) {
27         junit.textui.TestRunner.run (new TestSuite(TestSimpleToken.class));
28     }
29
30     public TestSimpleToken(String JavaDoc name) {
31         super(name);
32     }
33
34     protected void setUp() throws XmlPullParserException {
35         factory = factoryNewInstance();
36         factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
37         assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
38         //System.out.println(getClass()+"-factory="+factory);
39
}
40
41     protected void tearDown() {
42     }
43
44     public void testVerySimpleToken() throws Exception JavaDoc {
45         XmlPullParser xpp = factory.newPullParser();
46         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
47         xpp.setInput(new StringReader JavaDoc("<foo><!--comment--><?target pi?></foo>"));
48         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
49         xpp.nextToken();
50         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false, 0);
51         xpp.nextToken();
52         checkParserStateNs(xpp, 1, xpp.COMMENT, null, 0, null, null, "comment", false, -1);
53         xpp.nextToken();
54         checkParserStateNs(xpp, 1, xpp.PROCESSING_INSTRUCTION, null, 0, null, null, "target pi", false, -1);
55         xpp.nextToken();
56         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
57         xpp.nextToken();
58         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
59     }
60
61     public void testSimpleToken() throws Exception JavaDoc {
62         XmlPullParser xpp = factory.newPullParser();
63         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
64
65         // check setInput semantics
66
assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType());
67         try {
68             xpp.nextToken();
69             fail("exception was expected of nextToken() if no input was set on parser");
70         } catch(XmlPullParserException ex) {}
71
72         xpp.setInput(null);
73         assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType());
74         try {
75             xpp.nextToken();
76             fail("exception was expected of next() if no input was set on parser");
77         } catch(XmlPullParserException ex) {}
78
79         xpp.setInput(null); //reset parser
80
// attempt to set roundtrip
81
try {
82             xpp.setFeature(FEATURE_XML_ROUNDTRIP, true);
83         } catch(Exception JavaDoc ex) {
84         }
85         // did we succeeded?
86
boolean roundtripSupported = xpp.getFeature(FEATURE_XML_ROUNDTRIP);
87
88
89         // check the simplest possible XML document - just one root element
90
for(int i = 1; i <= 2; ++i) {
91             xpp.setInput(new StringReader JavaDoc(i == 1 ? "<foo/>" : "<foo></foo>"));
92             boolean empty = (i == 1);
93             checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
94             xpp.nextToken();
95             checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, empty, 0);
96             if(roundtripSupported) {
97                 if(empty) {
98                     // System.out.println("tag='"+xpp.getText()+"'");
99
// String foo ="<foo/>";
100
// String foo2 = xpp.getText();
101
// System.out.println(foo.equals(foo2));
102
assertEquals("empty tag roundtrip",
103                                  printable("<foo/>"),
104                                  printable(xpp.getText()));
105                 } else {
106                     assertEquals("start tag roundtrip",
107                                  printable("<foo>"),
108                                  printable(xpp.getText()));
109                 }
110             }
111             xpp.nextToken();
112             checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
113             if(roundtripSupported) {
114                 if(empty) {
115                     assertEquals("empty tag roundtrip",
116                                  printable("<foo/>"),
117                                  printable(xpp.getText()));
118                 } else {
119                     assertEquals("end tag roundtrip",
120                                  printable("</foo>"),
121                                  printable(xpp.getText()));
122                 }
123             }
124             xpp.nextToken();
125             checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
126         }
127     }
128
129
130     public void testNormalization() throws Exception JavaDoc {
131         XmlPullParser xpp = factory.newPullParser();
132         assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
133         testNormalization(xpp);
134         try{ xpp.setFeature(FEATURE_XML_ROUNDTRIP, false); } catch(Exception JavaDoc ex) {}
135         testNormalization(xpp);
136         try{ xpp.setFeature(FEATURE_XML_ROUNDTRIP, true); } catch(Exception JavaDoc ex) {}
137         testNormalization(xpp);
138     }
139
140     public void testNormalization(XmlPullParser xpp) throws Exception JavaDoc {
141         xpp.setInput(new StringReader JavaDoc("<foo>\n \r\n \n\r</foo>"));
142         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
143         xpp.nextToken();
144         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "foo", null, false/*empty*/, 0);
145
146         boolean roundtrip = xpp.getFeature(FEATURE_XML_ROUNDTRIP) == false;
147         String JavaDoc text = nextTokenGathered(xpp, XmlPullParser.TEXT, true);
148         if(roundtrip) {
149             assertEquals(printable("\n \n \n\n"), printable(text));
150             assertEquals("\n \n \n\n", text);
151         } else {
152             assertEquals(printable("\n \r\n \n\r"), printable(text));
153             assertEquals("\n \r\n \n\r", text);
154         }
155
156         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "foo", null, false, -1);
157         xpp.nextToken();
158         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
159     }
160 }
161
162
Popular Tags