KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
12
13 import org.xmlpull.v1.XmlPullParser;
14 import org.xmlpull.v1.XmlPullParserFactory;
15 import org.xmlpull.v1.XmlPullParserException;
16
17 /**
18  * Test if entity replacement works ok.
19  * This test is designe to work bboth for validating and non validating parsers!
20  *
21  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
22  */

23 public class TestEntityReplacement extends UtilTestCase {
24     private XmlPullParserFactory factory;
25
26     public TestEntityReplacement(String JavaDoc name) {
27         super(name);
28     }
29
30     protected void setUp() throws XmlPullParserException {
31         factory = factoryNewInstance();
32         factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
33         assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
34         //assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION));
35
}
36
37     protected void tearDown() {
38     }
39
40     public void testEntityReplacement() throws IOException JavaDoc, XmlPullParserException
41     {
42         // taken from http://www.w3.org/TR/REC-xml#sec-entexpand
43
final String JavaDoc XML_ENTITY_EXPANSION =
44             "<?xml version='1.0'?>\n"+
45             "<!DOCTYPE test [\n"+
46             "<!ELEMENT test (#PCDATA) >\n"+
47             "<!ENTITY % xx '&#37;zz;'>\n"+
48             "<!ENTITY % zz '&#60;!ENTITY tricky \"error-prone\" >' >\n"+
49             "%xx;\n"+
50             "]>"+
51             "<test>This sample shows a &tricky; method.</test>";
52
53         XmlPullParser pp = factory.newPullParser();
54         // default parser must work!!!!
55
pp.setInput(new StringReader JavaDoc( XML_ENTITY_EXPANSION ) );
56         if(pp.getFeature( pp.FEATURE_PROCESS_DOCDECL ) == false) {
57             pp.defineEntityReplacementText("tricky", "error-prone");
58         }
59         testEntityReplacement(pp);
60
61         // now we try for no FEATURE_PROCESS_DOCDECL
62
pp.setInput(new StringReader JavaDoc( XML_ENTITY_EXPANSION ) );
63         try {
64             pp.setFeature( pp.FEATURE_PROCESS_DOCDECL, false );
65         } catch( Exception JavaDoc ex ){
66         }
67         if( pp.getFeature( pp.FEATURE_PROCESS_DOCDECL ) == false ) {
68             pp.defineEntityReplacementText("tricky", "error-prone");
69             testEntityReplacement(pp);
70         }
71
72         // try to use FEATURE_PROCESS_DOCDECL if supported
73
pp.setInput(new StringReader JavaDoc( XML_ENTITY_EXPANSION ) );
74         try {
75             pp.setFeature( pp.FEATURE_PROCESS_DOCDECL, true );
76             //PackageTests.addNote("* feature "+pp.FEATURE_PROCESS_DOCDECL+" is supported\n");
77
} catch( Exception JavaDoc ex ){
78         }
79         if( pp.getFeature( pp.FEATURE_PROCESS_DOCDECL ) ) {
80             testEntityReplacement(pp);
81         }
82
83         // try to use FEATURE_VALIDATION if supported
84
pp.setInput(new StringReader JavaDoc( XML_ENTITY_EXPANSION ) );
85         try {
86             pp.setFeature( pp.FEATURE_VALIDATION, true );
87             //PackageTests.addNote("* feature "+pp.FEATURE_VALIDATION+" is supported\n");
88
} catch( Exception JavaDoc ex ){
89         }
90         if( pp.getFeature( pp.FEATURE_VALIDATION ) ) {
91             testEntityReplacement(pp);
92         }
93
94     }
95
96     public void testEntityReplacement(XmlPullParser pp) throws IOException JavaDoc, XmlPullParserException
97     {
98         pp.next();
99         checkParserStateNs(pp, 1, pp.START_TAG,
100                            null, 0, "", "test", null, false, 0);
101         pp.next();
102         checkParserStateNs(pp, 1, pp.TEXT, null, 0, null, null,
103                            "This sample shows a error-prone method.", false, -1);
104         pp.next();
105         checkParserStateNs(pp, 1, pp.END_TAG,
106                            null, 0, "", "test", null, false, -1);
107         pp.nextToken();
108         checkParserStateNs(pp, 0, pp.END_DOCUMENT, null, 0, null, null, null, false, -1);
109
110     }
111
112
113     public static void main (String JavaDoc[] args) {
114         junit.textui.TestRunner.run (new TestSuite(TestEntityReplacement.class));
115     }
116
117 }
118
119
Popular Tags