KickJava   Java API By Example, From Geeks To Geeks.

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


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 TestSimpleProcessDocdecl extends UtilTestCase {
23     private XmlPullParserFactory factory;
24
25     public static void main (String JavaDoc[] args) {
26         junit.textui.TestRunner.run (new TestSuite(TestSimpleProcessDocdecl.class));
27     }
28
29
30     public TestSimpleProcessDocdecl(String JavaDoc name) {
31         super(name);
32     }
33
34     protected void setUp() throws XmlPullParserException {
35         factory = factoryNewInstance();
36         assertEquals(false, factory.isNamespaceAware());
37         assertEquals(false, factory.isValidating());
38         //System.out.println("factory="+factory);
39
}
40
41     public void testProcessDocdecl() throws Exception JavaDoc {
42         XmlPullParser xpp = factory.newPullParser();
43         try {
44             xpp.setFeature(xpp.FEATURE_PROCESS_DOCDECL, true);
45         } catch(XmlPullParserException ex) {
46             return;
47         }
48         PackageTests.addNote("* feature "+xpp.FEATURE_PROCESS_DOCDECL+" is supported\n");
49         // setting validation MUST enables also PROCESS_DOCDECL
50
assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL));
51         // default is to have non-validating parser
52
assertEquals(false, xpp.getFeature(XmlPullParser.FEATURE_VALIDATION));
53
54         //http://www.w3.org/TR/REC-xml#NT-extSubsetDecl
55
// minimum validation
56
final String JavaDoc XML_MIN_PROLOG =
57             "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone='yes' ?>\n"+
58             "<!DOCTYPE greeting [\n"+
59             "<!ENTITY name \"world\">\n"+
60             "]>\n";
61
62         final String JavaDoc XML_MIN_VALID = XML_MIN_PROLOG+
63             "<greeting>Hello, &name;!</greeting>\n";
64
65         xpp.setInput(new StringReader JavaDoc( XML_MIN_VALID ));
66         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
67         assertNull(xpp.getProperty(PROPERTY_XMLDECL_VERSION));
68         assertNull(xpp.getProperty(PROPERTY_XMLDECL_STANDALONE));
69         assertNull(xpp.getProperty(PROPERTY_XMLDECL_CONTENT));
70
71         xpp.next();
72         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "greeting", null, false/*empty*/, 0);
73
74         //XMLDecl support is required when PROCESS DOCDECL enabled
75
assertEquals("1.0", xpp.getProperty(PROPERTY_XMLDECL_VERSION));
76         assertEquals(Boolean.TRUE, xpp.getProperty(PROPERTY_XMLDECL_STANDALONE));
77
78         xpp.next();
79         checkParserStateNs(xpp, 1, xpp.TEXT, null, 0, null, null, "Hello, world!", false, -1);
80         xpp.next();
81         checkParserStateNs(xpp, 1, xpp.END_TAG, null, 0, "", "greeting", null, false, -1);
82         xpp.next();
83         checkParserStateNs(xpp, 0, xpp.END_DOCUMENT, null, 0, null, null, null, false, -1);
84
85
86
87         //AND WRONG
88
final String JavaDoc XML_MIN_INVALID = XML_MIN_PROLOG+
89             "<greet>Hello, &world;!</greet>\n";
90         xpp.setInput(new StringReader JavaDoc( XML_MIN_INVALID ));
91         checkParserStateNs(xpp, 0, xpp.START_DOCUMENT, null, 0, null, null, null, false, -1);
92         xpp.next();
93         checkParserStateNs(xpp, 1, xpp.START_TAG, null, 0, "", "greet", null, false/*empty*/, 0);
94         try {
95             xpp.next();
96             fail("exception was expected of next() for undeclared entity");
97         } catch(XmlPullParserException ex) {}
98
99     }
100
101 }
102
103
Popular Tags