KickJava   Java API By Example, From Geeks To Geeks.

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


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_VALIDATION (when supported)
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

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