1 2 4 package org.xmlpull.v1.tests; 5 6 import junit.framework.TestCase; 8 import junit.framework.TestSuite; 9 10 import java.io.StringReader ; 11 import java.io.IOException ; 12 13 import org.xmlpull.v1.XmlPullParser; 14 import org.xmlpull.v1.XmlPullParserFactory; 15 import org.xmlpull.v1.XmlPullParserException; 16 17 22 public class TestAttributes extends UtilTestCase { 23 private XmlPullParserFactory factory; 24 25 public TestAttributes(String 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 , XmlPullParserException 40 { 41 final String 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 (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 63 String xsiNull = pp.getAttributeValue( 64 "http://www.w3.org/1999/XMLSchema/instance", "null"); 65 assertEquals("1", xsiNull); 66 67 String xsiType = pp.getAttributeValue( 68 "http://www.w3.org/1999/XMLSchema/instance", "type"); 69 assertEquals("ns2:string", xsiType); 70 71 72 String typeName = getQNameLocal(xsiType); 73 assertEquals("string", typeName); 74 String 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 getQNameLocal(String qname) { 87 if(qname == null) return null; 88 int pos = qname.indexOf(':'); 89 return qname.substring(pos + 1); 90 } 91 92 private String getQNameUri(XmlPullParser pp, String 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 prefix = qname.substring(0, pos); 98 return pp.getNamespace(prefix); 99 } 100 101 public void testAttribUniq() throws IOException , XmlPullParserException 102 { 103 104 final String 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 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 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 duplicateXmlns = 123 "<m:test xmlns:m='Some-Namespace-URI' xmlns:m='Some-Namespace-URI'"+ 124 ""+ 125 "/>\n"+ 126 ""; 127 128 final String 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 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 declaringDefaultEmptyNs = 201 "<m:test xmlns='' xmlns:m='uri'/>"; 202 203 parseOneElement(pp, declaringDefaultEmptyNs, false); 205 206 parseOneElement(pp, declaringDefaultEmptyNs, true); 209 210 final String declaringPrefixedEmptyNs = 211 "<m:test xmlns:m='' />"; 212 213 parseOneElement(pp, declaringPrefixedEmptyNs, false); 215 216 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 buf, 231 final boolean supportNamespaces) 232 throws IOException , XmlPullParserException 233 { 234 pp.setInput(new StringReader (buf)); 236 pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, supportNamespaces); 238 pp.next(); 240 if(supportNamespaces) { 242 assertEquals("test", pp.getName()); 243 } else { 244 assertEquals("m:test", pp.getName()); 245 } 246 } 247 248 public void testAttribValueNormalization() throws IOException , XmlPullParserException 249 { 250 final String XML_ATTRS = 251 "<javadoc packagenames=\"${packages}\""+ 252 " bottom=\"<table width='80%%'><tr><td width='50%%'><p "+ 253 " align='center'><a HREF='http://www.xmlpull.org/'>\" "+ 254 "/>"; 255 256 XmlPullParser pp = factory.newPullParser(); 257 pp.setInput(new StringReader (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 } 270 public static void main (String [] args) { 271 junit.textui.TestRunner.run (new TestSuite(TestAttributes.class)); 272 } 273 274 } 275 276 | Popular Tags |