KickJava   Java API By Example, From Geeks To Geeks.

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


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 attribute uniqueness is ensured.
19  *
20  * @author <a HREF="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
21  */

22 public class TestAttributes extends UtilTestCase {
23     private XmlPullParserFactory factory;
24
25     public TestAttributes(String JavaDoc name) {
26         super(name);
27     }
28
29     protected void setUp() throws XmlPullParserException {
30         factory = factoryNewInstance();
31         factory.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
32         assertEquals(true, factory.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));
33         assertEquals(false, factory.getFeature(XmlPullParser.FEATURE_VALIDATION));
34     }
35
36     protected void tearDown() {
37     }
38
39     public void testAttribs() throws IOException JavaDoc, XmlPullParserException
40     {
41         final String JavaDoc XML_ATTRS =
42             "<event xmlns:xsi='http://www.w3.org/1999/XMLSchema/instance' encodingStyle=\"test\">"+
43             "<type>my-event</type>"+
44             "<handback xsi:type='ns2:string' xmlns:ns2='http://www.w3.org/1999/XMLSchema' xsi:null='1'/>"+
45             "</event>";
46
47         XmlPullParser pp = factory.newPullParser();
48         pp.setInput(new StringReader JavaDoc(XML_ATTRS));
49
50         assertEquals(XmlPullParser.START_TAG, pp.next());
51         assertEquals("event", pp.getName());
52         assertEquals(XmlPullParser.START_TAG, pp.next());
53         assertEquals("type", pp.getName());
54         assertEquals(XmlPullParser.TEXT, pp.next());
55         assertEquals("my-event", pp.getText());
56         assertEquals(pp.next(), XmlPullParser.END_TAG);
57         assertEquals("type", pp.getName());
58         assertEquals(XmlPullParser.START_TAG, pp.next());
59         assertEquals("handback", pp.getName());
60         //assertEquals(XmlPullParser.CONTENT, pp.next());
61
//assertEquals("", pp.readContent());
62

63         String JavaDoc xsiNull = pp.getAttributeValue(
64             "http://www.w3.org/1999/XMLSchema/instance", "null");
65         assertEquals("1", xsiNull);
66
67         String JavaDoc xsiType = pp.getAttributeValue(
68             "http://www.w3.org/1999/XMLSchema/instance", "type");
69         assertEquals("ns2:string", xsiType);
70
71
72         String JavaDoc typeName = getQNameLocal(xsiType);
73         assertEquals("string", typeName);
74         String JavaDoc typeNS = getQNameUri(pp, xsiType);
75         assertEquals("http://www.w3.org/1999/XMLSchema", typeNS);
76
77         assertEquals(pp.next(), XmlPullParser.END_TAG);
78         assertEquals("handback", pp.getName());
79         assertEquals(pp.next(), XmlPullParser.END_TAG);
80         assertEquals("event", pp.getName());
81
82         assertEquals(pp.next(), XmlPullParser.END_DOCUMENT);
83
84     }
85
86     private String JavaDoc getQNameLocal(String JavaDoc qname) {
87         if(qname == null) return null;
88         int pos = qname.indexOf(':');
89         return qname.substring(pos + 1);
90     }
91
92     private String JavaDoc getQNameUri(XmlPullParser pp, String JavaDoc qname) throws XmlPullParserException {
93         if(qname == null) return null;
94         int pos = qname.indexOf(':');
95         if(pos == -1) throw new XmlPullParserException(
96                 "qname des not have prefix");
97         String JavaDoc prefix = qname.substring(0, pos);
98         return pp.getNamespace(prefix);
99     }
100
101     public void testAttribUniq() throws IOException JavaDoc, XmlPullParserException
102     {
103
104         final String JavaDoc attribsOk =
105             "<m:test xmlns:m='Some-Namespace-URI' xmlns:n='Some-Namespace-URI'"+
106             " a='a' b='b' m:a='c' n:b='d' n:x='e'"+
107             "/>\n"+
108             "";
109
110         final String JavaDoc duplicateAttribs =
111             "<m:test xmlns:m='Some-Namespace-URI' xmlns:n='Some-Namespace-URI'"+
112             " a='a' b='b' m:a='a' n:b='b' a='x'"+
113             "/>\n"+
114             "";
115
116         final String JavaDoc duplicateNsAttribs =
117             "<m:test xmlns:m='Some-Namespace-URI' xmlns:n='Some-Namespace-URI'"+
118             " a='a' b='b' m:a='a' n:b='b' n:a='a'"+
119             "/>\n"+
120             "";
121
122         final String JavaDoc duplicateXmlns =
123             "<m:test xmlns:m='Some-Namespace-URI' xmlns:m='Some-Namespace-URI'"+
124             ""+
125             "/>\n"+
126             "";
127
128         final String JavaDoc duplicateAttribXmlnsDefault =
129             "<m:test xmlns='Some-Namespace-URI' xmlns:m='Some-Namespace-URI'"+
130             " a='a' b='b' m:b='b' m:a='x'"+
131             "/>\n"+
132             "";
133
134         XmlPullParser pp = factory.newPullParser();
135         parseOneElement(pp, attribsOk, false);
136         assertEquals("a", pp.getAttributeValue(null, "a"));
137         assertEquals("b", pp.getAttributeValue(null, "b"));
138         assertEquals("c", pp.getAttributeValue(null, "m:a"));
139         assertEquals("d", pp.getAttributeValue(null, "n:b"));
140         assertEquals("e", pp.getAttributeValue(null, "n:x"));
141
142         parseOneElement(pp, attribsOk, true);
143
144         assertEquals("a", pp.getAttributeValue("","a"));
145         assertEquals("b", pp.getAttributeValue("","b"));
146         assertEquals(null, pp.getAttributeValue("", "m:a"));
147         assertEquals(null, pp.getAttributeValue("", "n:b"));
148         assertEquals(null, pp.getAttributeValue("", "n:x"));
149
150         assertEquals("c", pp.getAttributeValue("Some-Namespace-URI", "a"));
151         assertEquals("d", pp.getAttributeValue("Some-Namespace-URI", "b"));
152         assertEquals("e", pp.getAttributeValue("Some-Namespace-URI", "x"));
153
154         parseOneElement(pp, duplicateNsAttribs, false);
155         parseOneElement(pp, duplicateAttribXmlnsDefault, false);
156         parseOneElement(pp, duplicateAttribXmlnsDefault, true);
157
158         Exception JavaDoc ex;
159
160         ex = null;
161         try {
162             parseOneElement(pp, duplicateAttribs, false);
163         } catch(XmlPullParserException rex) {
164             ex = rex;
165         }
166         assertNotNull(ex);
167
168         ex = null;
169         try {
170             parseOneElement(pp, duplicateAttribs, true);
171         } catch(XmlPullParserException rex) {
172             ex = rex;
173         }
174         assertNotNull(ex);
175
176         ex = null;
177         try {
178             parseOneElement(pp, duplicateXmlns, false);
179         } catch(XmlPullParserException rex) {
180             ex = rex;
181         }
182         assertNotNull(ex);
183
184         ex = null;
185         try {
186             parseOneElement(pp, duplicateXmlns, true);
187         } catch(XmlPullParserException rex) {
188             ex = rex;
189         }
190         assertNotNull(ex);
191
192         ex = null;
193         try {
194             parseOneElement(pp, duplicateNsAttribs, true);
195         } catch(XmlPullParserException rex) {
196             ex = rex;
197         }
198         assertNotNull(ex);
199
200         final String JavaDoc declaringDefaultEmptyNs =
201             "<m:test xmlns='' xmlns:m='uri'/>";
202
203         // allowed when namespaces disabled
204
parseOneElement(pp, declaringDefaultEmptyNs, false);
205
206         // allowed to redeclare defaut anemspace to empty string, see:
207
// http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-decl
208
parseOneElement(pp, declaringDefaultEmptyNs, true);
209
210         final String JavaDoc declaringPrefixedEmptyNs =
211             "<m:test xmlns:m='' />";
212
213         // allowed when namespaces disabled
214
parseOneElement(pp, declaringPrefixedEmptyNs, false);
215
216         // otherwise it is error to declare '' for non-default NS as described in
217
// http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-decl
218
ex = null;
219         try {
220             parseOneElement(pp, declaringPrefixedEmptyNs, true);
221         } catch(XmlPullParserException rex) {
222             ex = rex;
223         }
224         assertNotNull(ex);
225
226     }
227
228     private void parseOneElement(
229         final XmlPullParser pp,
230         final String JavaDoc buf,
231         final boolean supportNamespaces)
232         throws IOException JavaDoc, XmlPullParserException
233     {
234         //pp.setInput(buf.toCharArray());
235
pp.setInput(new StringReader JavaDoc(buf));
236         //pp.setNamespaceAware(supportNamespaces);
237
pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, supportNamespaces);
238         //pp.setAllowedMixedContent(false);
239
pp.next();
240         //pp.readStartTag(stag);
241
if(supportNamespaces) {
242             assertEquals("test", pp.getName());
243         } else {
244             assertEquals("m:test", pp.getName());
245         }
246     }
247
248     public void testAttribValueNormalization() throws IOException JavaDoc, XmlPullParserException
249     {
250         final String JavaDoc XML_ATTRS =
251             "<javadoc packagenames=\"${packages}\""+
252             " bottom=\"&lt;table width='80%%'&gt;&lt;tr&gt;&lt;td width='50%%'&gt;&lt;p "+
253             " align='center'&gt;&lt;a HREF='http://www.xmlpull.org/'&gt;\" "+
254             "/>";
255
256         XmlPullParser pp = factory.newPullParser();
257         pp.setInput(new StringReader JavaDoc(XML_ATTRS));
258
259         assertEquals(XmlPullParser.START_TAG, pp.next());
260         assertEquals("javadoc", pp.getName());
261         assertEquals("${packages}", pp.getAttributeValue(0));
262         assertEquals("${packages}", pp.getAttributeValue("", "packagenames"));
263         assertEquals(
264                 "<table width='80%%'><tr><td width='50%%'><p align='center'><a HREF='http://www.xmlpull.org/'>",
265                 pp.getAttributeValue("", "bottom")
266         );
267
268         //System.out.println(pp.getAttributeValue("", "bottom"));
269
}
270     public static void main (String JavaDoc[] args) {
271         junit.textui.TestRunner.run (new TestSuite(TestAttributes.class));
272     }
273
274 }
275
276
Popular Tags