1 2 4 package org.xmlpull.v1.tests; 5 6 import java.io.IOException ; 7 8 import junit.framework.TestCase; 9 10 import org.xmlpull.v1.XmlPullParser; 11 import org.xmlpull.v1.XmlPullParserFactory; 12 import org.xmlpull.v1.XmlPullParserException; 13 14 19 public class UtilTestCase extends TestCase { 20 protected static final String FEATURE_XML_ROUNDTRIP= 21 "http://xmlpull.org/v1/doc/features.html#xml-roundtrip"; 22 protected final static String PROPERTY_XMLDECL_VERSION = 23 "http://xmlpull.org/v1/doc/properties.html#xmldecl-version"; 24 protected final static String PROPERTY_XMLDECL_STANDALONE = 25 "http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone"; 26 protected final static String PROPERTY_XMLDECL_CONTENT = 27 "http://xmlpull.org/v1/doc/properties.html#xmldecl-content"; 28 29 30 protected final static String TEST_XML = 31 "<root>\n"+ 32 "<foo>bar</foo>\r\n"+ 33 "<hugo xmlns=\"http://www.xmlpull.org/temp\"> \n\r \n"+ 34 " <hugochild>This is in a <!-- comment -->new namespace</hugochild>"+ 35 "</hugo>\t\n"+ 36 "<bar testattr='123abc' />"+ 37 "</root>\n"+ 38 "\n"+ 39 "<!-- an xml sample document without meaningful content -->\n"; 40 41 private static boolean printedFactoryName; 43 44 public UtilTestCase(String name) { 45 super(name); 46 } 47 48 public static XmlPullParserFactory factoryNewInstance() throws XmlPullParserException { 49 String property = System.getProperty(XmlPullParserFactory.PROPERTY_NAME); 50 XmlPullParserFactory factory = XmlPullParserFactory.newInstance( 54 property, 55 null ); 57 if(PackageTests.runnigAllTests() == false && printedFactoryName == false) { 59 System.out.println("factory="+factory+" property="+property); 60 printedFactoryName = true; 61 } 62 return factory; 63 } 64 65 70 public String nextTokenGathered(XmlPullParser xpp, int type, boolean expectedWhitespaces) 71 throws XmlPullParserException, IOException 72 { 73 74 xpp.nextToken(); 75 return gatherTokenText(xpp, type, expectedWhitespaces); 76 77 } 78 79 83 public String gatherTokenText(XmlPullParser xpp, int type, boolean expectedWhitespaces) 84 throws XmlPullParserException, IOException 85 { 86 StringBuffer buf = new StringBuffer (); 87 assertEquals(xpp.TYPES[ type ], xpp.TYPES[ xpp.getEventType() ]); 88 do { 89 buf.append(xpp.getText()); 90 if(expectedWhitespaces) { 91 assertTrue(xpp.isWhitespace()); 92 } 93 } while(xpp.nextToken() == type); 94 return buf.toString(); 95 96 } 97 98 public void checkParserState( 99 XmlPullParser xpp, 100 int depth, 101 int type, 102 String name, 103 String text, 104 boolean isEmpty, 105 int attribCount 106 ) throws XmlPullParserException, IOException 107 { 108 assertTrue("line number must be -1 or >= 1 not "+xpp.getLineNumber(), 109 xpp.getLineNumber() == -1 || xpp.getLineNumber() >= 1); 110 assertTrue("column number must be -1 or >= 0 not "+xpp.getColumnNumber(), 111 xpp.getColumnNumber() == -1 || xpp.getColumnNumber() >= 0); 112 113 assertEquals("PROCESS_NAMESPACES", false, xpp.getFeature(xpp.FEATURE_PROCESS_NAMESPACES)); 114 assertEquals("TYPES[getType()]", xpp.TYPES[type], xpp.TYPES[xpp.getEventType()]); 115 assertEquals("getType()", type, xpp.getEventType()); 116 assertEquals("getDepth()", depth, xpp.getDepth()); 117 assertEquals("getPrefix()", null, xpp.getPrefix()); 118 assertEquals("getNamespacesCount(getDepth())", 0, xpp.getNamespaceCount(depth)); 119 if(xpp.getEventType() == xpp.START_TAG || xpp.getEventType() == xpp.END_TAG) { 120 assertEquals("getNamespace()", "", xpp.getNamespace()); 121 } else { 122 assertEquals("getNamespace()", null, xpp.getNamespace()); 123 } 124 assertEquals("getName()", name, xpp.getName()); 125 126 if(xpp.getEventType() != xpp.START_TAG && xpp.getEventType() != xpp.END_TAG) { 127 assertEquals("getText()", printable(text), printable(xpp.getText())); 128 129 int [] holderForStartAndLength = new int[2]; 130 char[] buf = xpp.getTextCharacters(holderForStartAndLength); 131 if(buf != null) { 132 String s = new String (buf, holderForStartAndLength[0], holderForStartAndLength[1]); 133 assertEquals("getText(holder)", printable(text), printable(s)); 134 } else { 135 assertEquals("getTextCharacters()", null, text); 136 } 137 } 138 if(type == xpp.START_TAG) { 139 assertEquals("isEmptyElementTag()", isEmpty, xpp.isEmptyElementTag()); 140 } else { 141 try { 142 xpp.isEmptyElementTag(); 143 fail("isEmptyElementTag() must throw exception if parser not on START_TAG"); 144 } catch(XmlPullParserException ex) { 145 } 146 } 147 assertEquals("getAttributeCount()", attribCount, xpp.getAttributeCount()); 148 } 149 150 public void checkParserStateNs( 151 XmlPullParser xpp, 152 int depth, 153 int type, 154 int nsCount, 155 String namespace, 156 String name, 157 boolean isEmpty, 158 int attribCount 159 ) throws XmlPullParserException, IOException 160 { 161 assertTrue("line number must be -1 or >= 1 not "+xpp.getLineNumber(), 162 xpp.getLineNumber() == -1 || xpp.getLineNumber() >= 1); 163 assertTrue("column number must be -1 or >= 0 not "+xpp.getColumnNumber(), 164 xpp.getColumnNumber() == -1 || xpp.getColumnNumber() >= 0); 165 166 assertEquals("TYPES[getEventType()]", xpp.TYPES[type], xpp.TYPES[xpp.getEventType()]); 169 assertEquals("getEventType()", type, xpp.getEventType()); 170 assertEquals("getName()", name, xpp.getName()); 171 172 assertEquals("getDepth()", depth, xpp.getDepth()); 173 assertEquals("getNamespacesCount(getDepth())", nsCount, xpp.getNamespaceCount(depth)); 174 assertEquals("getNamespace()", namespace, xpp.getNamespace()); 175 176 if(type == xpp.START_TAG) { 177 assertEquals("isEmptyElementTag()", isEmpty, xpp.isEmptyElementTag()); 178 } else { 179 try { 180 xpp.isEmptyElementTag(); 181 fail("isEmptyElementTag() must throw exception if parser not on START_TAG"); 182 } catch(XmlPullParserException ex) { 183 } 184 } 185 assertEquals("getAttributeCount()", attribCount, xpp.getAttributeCount()); 186 } 187 188 public void checkParserStateNs( 189 XmlPullParser xpp, 190 int depth, 191 int type, 192 String prefix, 193 int nsCount, 194 String namespace, 195 String name, 196 boolean isEmpty, 197 int attribCount 198 ) throws XmlPullParserException, IOException 199 { 200 checkParserStateNs(xpp, depth, type, nsCount, namespace, name, isEmpty, attribCount); 201 assertEquals("getPrefix()", prefix, xpp.getPrefix()); 202 } 203 204 public void checkParserStateNs( 205 XmlPullParser xpp, 206 int depth, 207 int type, 208 String prefix, 209 int nsCount, 210 String namespace, 211 String name, 212 String text, 213 boolean isEmpty, 214 int attribCount 215 ) throws XmlPullParserException, IOException 216 { 217 checkParserStateNs(xpp, depth, type, prefix, nsCount, namespace, name, isEmpty, attribCount); 218 219 if(xpp.getEventType() != xpp.START_TAG && xpp.getEventType() != xpp.END_TAG) { 220 assertEquals("getText()", printable(text), printable(xpp.getText())); 221 222 int [] holderForStartAndLength = new int[2]; 223 char[] buf = xpp.getTextCharacters(holderForStartAndLength); 224 if(buf != null) { 225 String s = new String (buf, holderForStartAndLength[0], holderForStartAndLength[1]); 226 if(xpp.getEventType() != xpp.ENTITY_REF) { 228 assertEquals("getText(holder)", printable(text), printable(s)); 229 } else { 230 assertEquals("getText(holder) ENTITY_REF", printable(name), printable(s)); 231 } 232 } else { 233 assertEquals("getTextCharacters()", null, text); 234 } 235 236 } 237 238 239 } 240 241 public void checkAttrib( 242 XmlPullParser xpp, 243 int pos, 244 String name, 245 String value 246 ) throws XmlPullParserException, IOException 247 { 248 assertEquals("must be on START_TAG", xpp.START_TAG, xpp.getEventType()); 249 assertEquals("getAttributePrefix()",null, xpp.getAttributePrefix(pos)); 250 assertEquals("getAttributeNamespace()","", xpp.getAttributeNamespace(pos)); 251 assertEquals("getAttributeName()",name, xpp.getAttributeName(pos)); 252 assertEquals("getAttributeValue()",value, xpp.getAttributeValue(pos)); 253 assertEquals("getAttributeValue(name)",value, xpp.getAttributeValue(null, name)); 254 assertEquals("getAttributeType()","CDATA", xpp.getAttributeType(pos)); 255 assertEquals("isAttributeDefault()",false, xpp.isAttributeDefault(pos)); 256 } 257 258 259 public void checkAttribNs( 260 XmlPullParser xpp, 261 int pos, 262 String namespace, 263 String name, 264 String value 265 ) throws XmlPullParserException, IOException 266 { 267 assertEquals("must be on START_TAG", xpp.START_TAG, xpp.getEventType()); 268 assertEquals("getAttributeNamespace()",namespace, xpp.getAttributeNamespace(pos)); 269 assertEquals("getAttributeName()",name, xpp.getAttributeName(pos)); 270 assertEquals("getAttributeValue()",printable(value), printable(xpp.getAttributeValue(pos))); 271 assertEquals("getAttributeValue(ns,name)", 272 printable(value), printable(xpp.getAttributeValue(namespace, name))); 273 assertEquals("getAttributeType()","CDATA", xpp.getAttributeType(pos)); 274 assertEquals("isAttributeDefault()",false, xpp.isAttributeDefault(pos)); 275 } 276 277 public void checkAttribNs( 278 XmlPullParser xpp, 279 int pos, 280 String prefix, 281 String namespace, 282 String name, 283 String value 284 ) throws XmlPullParserException, IOException 285 { 286 checkAttribNs(xpp, pos, namespace, name, value); 287 assertEquals("getAttributePrefix()",prefix, xpp.getAttributePrefix(pos)); 288 } 289 290 public void checkNamespace( 291 XmlPullParser xpp, 292 int pos, 293 String prefix, 294 String uri, 295 boolean checkMapping 296 ) throws XmlPullParserException, IOException 297 { 298 assertEquals("getNamespacePrefix(pos)",prefix, xpp.getNamespacePrefix(pos)); 299 assertEquals("getNamespaceUri(pos)",uri, xpp.getNamespaceUri(pos)); 300 if(checkMapping) { 301 assertEquals("getNamespace(prefix)", uri, xpp.getNamespace (prefix)); 302 } 303 } 304 305 protected String printable(char ch) { 306 if(ch == '\n') { 307 return "\\n"; 308 } else if(ch == '\r') { 309 return "\\r"; 310 } else if(ch == '\t') { 311 return "\\t"; 312 } if(ch > 127 || ch < 32) { 313 StringBuffer buf = new StringBuffer ("\\u"); 314 String hex = Integer.toHexString((int)ch); 315 for (int i = 0; i < 4-hex.length(); i++) 316 { 317 buf.append('0'); 318 } 319 buf.append(hex); 320 return buf.toString(); 321 } 322 return ""+ch; 323 } 324 325 protected String printable(String s) { 326 if(s == null) return null; 327 StringBuffer buf = new StringBuffer (); 328 for(int i = 0; i < s.length(); ++i) { 329 buf.append(printable(s.charAt(i))); 330 } 331 s = buf.toString(); 332 return s; 333 } 334 335 } 336 337 | Popular Tags |