1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package tests.api.org.xml.sax.helpers; 18 19import java.io.IOException; 20 21import junit.framework.TestCase; 22 23import org.xml.sax.AttributeList; 24import org.xml.sax.Attributes; 25import org.xml.sax.ContentHandler; 26import org.xml.sax.DTDHandler; 27import org.xml.sax.EntityResolver; 28import org.xml.sax.ErrorHandler; 29import org.xml.sax.InputSource; 30import org.xml.sax.Locator; 31import org.xml.sax.Parser; 32import org.xml.sax.SAXException; 33import org.xml.sax.SAXNotRecognizedException; 34import org.xml.sax.SAXNotSupportedException; 35import org.xml.sax.helpers.AttributeListImpl; 36import org.xml.sax.helpers.LocatorImpl; 37import org.xml.sax.helpers.ParserAdapter; 38 39import tests.api.org.xml.sax.support.MethodLogger; 40import tests.api.org.xml.sax.support.MockHandler; 41import tests.api.org.xml.sax.support.MockParser; 42import tests.api.org.xml.sax.support.MockResolver; 43import dalvik.annotation.TestLevel; 44import dalvik.annotation.TestTargetClass; 45import dalvik.annotation.TestTargetNew; 46import dalvik.annotation.TestTargets; 47import tests.util.TestEnvironment; 48 49@SuppressWarnings("deprecation") 50@TestTargetClass(ParserAdapter.class) 51public class ParserAdapterTest extends TestCase { 52 53 // Note: In many cases we can only test that delegation works 54 // properly. The rest is outside the scope of the specification. 55 56 private final static String FEATURES = "http://xml.org/sax/features/"; 57 58 private final static String NAMESPACES = FEATURES + "namespaces"; 59 60 private final static String NAMESPACE_PREFIXES = FEATURES 61 + "namespace-prefixes"; 62 63 private final static String XMLNS_URIs = FEATURES + "xmlns-uris"; 64 65 private MethodLogger logger = new MethodLogger(); 66 67 private MockHandler handler = new MockHandler(logger); 68 69 private Parser parser = new MockParser(logger); 70 71 private ParserAdapter adapter = new ParserAdapter(parser); 72 73 private void assertEquals(Object[] a, Object[] b) { 74 assertEquals(a.length, b.length); 75 76 for (int i = 0; i < a.length; i++) { 77 assertEquals("Element #" + i + " must be equal", a[i], b[i]); 78 } 79 } 80 81 @Override 82 public void setUp() { 83 TestEnvironment.reset(); 84 adapter.setContentHandler(handler); 85 adapter.setDTDHandler(handler); 86 adapter.setErrorHandler(handler); 87 } 88 89 @Override protected void tearDown() throws Exception { 90 TestEnvironment.reset(); 91 super.tearDown(); 92 } 93 94 @TestTargetNew( 95 level = TestLevel.COMPLETE, 96 method = "ParserAdapter", 97 args = { } 98 ) 99 public void testParserAdapter() { 100 System.setProperty("org.xml.sax.parser", 101 "tests.api.org.xml.sax.support.DoNothingParser"); 102 103 try { 104 new ParserAdapter(); 105 } catch (SAXException e) { 106 throw new RuntimeException("Unexpected exception", e); 107 } 108 } 109 110 @TestTargetNew( 111 level = TestLevel.COMPLETE, 112 method = "ParserAdapter", 113 args = { Parser.class } 114 ) 115 public void testParserAdapterParser() { 116 // Ordinary case 117 @SuppressWarnings("unused") 118 ParserAdapter adapter = new ParserAdapter(parser); 119 120 // Null case 121 try { 122 adapter = new ParserAdapter(null); 123 fail("NullPointerException expected"); 124 } catch (NullPointerException e) { 125 // Expected 126 } 127 } 128 129 @TestTargets({ 130 @TestTargetNew( 131 level = TestLevel.COMPLETE, 132 method = "getFeature", 133 args = { String.class } 134 ), 135 @TestTargetNew( 136 level = TestLevel.COMPLETE, 137 method = "setFeature", 138 args = { String.class, boolean.class } 139 ) 140 }) 141 public void testGetSetFeature() { 142 String[] features = new String[] { NAMESPACES, NAMESPACE_PREFIXES, 143 XMLNS_URIs }; 144 145 for (String s: features) { 146 try { 147 adapter.setFeature(s, true); 148 assertEquals(true, adapter.getFeature(s)); 149 150 adapter.setFeature(s, false); 151 assertEquals(false, adapter.getFeature(s)); 152 } catch (SAXException e) { 153 throw new RuntimeException("Unexpected exception", e); 154 } 155 } 156 157 try { 158 adapter.setFeature("http://argle.bargle", true); 159 fail("SAXNotRecognizedException expected"); 160 } catch (SAXNotRecognizedException e) { 161 // Expected 162 } catch (SAXNotSupportedException e) { 163 throw new RuntimeException("Unexpected exception", e); 164 } 165 } 166 167 @TestTargets({ 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 method = "getProperty", 171 args = { String.class } 172 ), 173 @TestTargetNew( 174 level = TestLevel.COMPLETE, 175 method = "setProperty", 176 args = { String.class, Object.class } 177 ) 178 }) 179 public void testGetSetProperty() { 180 try { 181 adapter.setProperty("http://argle.bargle", ":)"); 182 fail("SAXNotRecognizedException expected"); 183 } catch (SAXNotRecognizedException e) { 184 // Expected 185 } catch (SAXNotSupportedException e) { 186 throw new RuntimeException("Unexpected exception", e); 187 } 188 189 try { 190 adapter.getProperty("http://argle.bargle"); 191 fail("SAXNotRecognizedException expected"); 192 } catch (SAXNotRecognizedException e) { 193 // Expected 194 } catch (SAXNotSupportedException e) { 195 throw new RuntimeException("Unexpected exception", e); 196 } 197 } 198 199 @TestTargets({ 200 @TestTargetNew( 201 level = TestLevel.COMPLETE, 202 method = "getEntityResolver", 203 args = { } 204 ), 205 @TestTargetNew( 206 level = TestLevel.COMPLETE, 207 method = "setEntityResolver", 208 args = { EntityResolver.class } 209 ) 210 }) 211 public void testGetSetEntityResolver() { 212 EntityResolver resolver = new MockResolver(); 213 214 adapter.setEntityResolver(resolver); 215 assertEquals(resolver, adapter.getEntityResolver()); 216 217 adapter.setEntityResolver(null); 218 assertEquals(null, adapter.getEntityResolver()); 219 } 220 221 @TestTargets({ 222 @TestTargetNew( 223 level = TestLevel.COMPLETE, 224 method = "getDTDHandler", 225 args = { } 226 ), 227 @TestTargetNew( 228 level = TestLevel.COMPLETE, 229 method = "setDTDHandler", 230 args = { DTDHandler.class } 231 ) 232 }) 233 public void testGetSetDTDHandler() { 234 adapter.setDTDHandler(null); 235 assertEquals(null, adapter.getDTDHandler()); 236 237 adapter.setDTDHandler(handler); 238 assertEquals(handler, adapter.getDTDHandler()); 239 } 240 241 @TestTargets({ 242 @TestTargetNew( 243 level = TestLevel.COMPLETE, 244 method = "getContentHandler", 245 args = { } 246 ), 247 @TestTargetNew( 248 level = TestLevel.COMPLETE, 249 method = "setContentHandler", 250 args = { ContentHandler.class } 251 ) 252 }) 253 public void testGetSetContentHandler() { 254 adapter.setContentHandler(null); 255 assertEquals(null, adapter.getContentHandler()); 256 257 adapter.setContentHandler(handler); 258 assertEquals(handler, adapter.getContentHandler()); 259 } 260 261 @TestTargets({ 262 @TestTargetNew( 263 level = TestLevel.COMPLETE, 264 method = "getErrorHandler", 265 args = { } 266 ), 267 @TestTargetNew( 268 level = TestLevel.COMPLETE, 269 method = "setErrorHandler", 270 args = { ErrorHandler.class } 271 ) 272 }) 273 public void testGetSetErrorHandler() { 274 adapter.setErrorHandler(null); 275 assertEquals(null, adapter.getErrorHandler()); 276 277 adapter.setErrorHandler(handler); 278 assertEquals(handler, adapter.getErrorHandler()); 279 } 280 281 @TestTargetNew( 282 level = TestLevel.COMPLETE, 283 method = "parse", 284 args = { String.class } 285 ) 286 public void testParseString() { 287 try { 288 adapter.parse("foo"); 289 } catch (SAXException e) { 290 throw new RuntimeException("Unexpected exception", e); 291 } catch (IOException e) { 292 throw new RuntimeException("Unexpected exception", e); 293 } 294 295 // The SAX RI creates an InputSource itself and then delegates to the 296 // "other" parse method. 297 assertEquals("parse", logger.getMethod()); 298 assertEquals(InputSource.class, logger.getArgs()[0].getClass()); 299 } 300 301 @TestTargetNew( 302 level = TestLevel.COMPLETE, 303 method = "parse", 304 args = { InputSource.class } 305 ) 306 public void testParseInputSource() { 307 InputSource source = new InputSource("foo"); 308 309 try { 310 adapter.parse(source); 311 } catch (SAXException e) { 312 throw new RuntimeException("Unexpected exception", e); 313 } catch (IOException e) { 314 throw new RuntimeException("Unexpected exception", e); 315 } 316 317 assertEquals("parse", logger.getMethod()); 318 assertEquals(new Object[] { source }, logger.getArgs()); 319 } 320 321 @TestTargetNew( 322 level = TestLevel.COMPLETE, 323 method = "setDocumentLocator", 324 args = { Locator.class } 325 ) 326 public void testSetDocumentLocator() { 327 Locator l = new LocatorImpl(); 328 329 adapter.setDocumentLocator(l); 330 331 assertEquals(logger.size(), 1); 332 assertEquals("setDocumentLocator", logger.getMethod()); 333 assertEquals(new Object[] { l }, logger.getArgs()); 334 335 adapter.setDocumentLocator(null); 336 337 assertEquals(logger.size(), 2); 338 assertEquals("setDocumentLocator", logger.getMethod()); 339 assertEquals(new Object[] { null }, logger.getArgs()); 340 } 341 342 @TestTargetNew( 343 level = TestLevel.COMPLETE, 344 method = "startDocument", 345 args = { } 346 ) 347 public void testStartDocument() { 348 try { 349 adapter.startDocument(); 350 } catch (SAXException e) { 351 throw new RuntimeException("Unexpected exception", e); 352 } 353 354 assertEquals(logger.size(), 1); 355 assertEquals("startDocument", logger.getMethod()); 356 assertEquals(new Object[] {}, logger.getArgs()); 357 } 358 359 @TestTargetNew( 360 level = TestLevel.COMPLETE, 361 method = "endDocument", 362 args = { } 363 ) 364 public void testEndDocument() { 365 try { 366 adapter.endDocument(); 367 } catch (SAXException e) { 368 throw new RuntimeException("Unexpected exception", e); 369 } 370 371 assertEquals(logger.size(), 1); 372 assertEquals("endDocument", logger.getMethod()); 373 assertEquals(new Object[] {}, logger.getArgs()); 374 } 375 376 @TestTargetNew( 377 level = TestLevel.COMPLETE, 378 method = "startElement", 379 args = { String.class, AttributeList.class } 380 ) 381 public void testStartElement() { 382 AttributeListImpl atts = new AttributeListImpl(); 383 atts.addAttribute("john:doe", "int", "42"); 384 385 try { 386 adapter.startDocument(); 387 adapter.startElement("foo:bar", atts); 388 } catch (SAXException e) { 389 throw new RuntimeException("Unexpected exception", e); 390 } 391 392 assertEquals("startElement", logger.getMethod()); 393 assertEquals("", logger.getArgs()[0]); 394 assertEquals("", logger.getArgs()[1]); 395 assertEquals("foo:bar", logger.getArgs()[2]); 396 assertEquals("john:doe", ((Attributes)logger.getArgs()[3]).getQName(0)); 397 } 398 399 @TestTargetNew( 400 level = TestLevel.COMPLETE, 401 method = "endElement", 402 args = { String.class } 403 ) 404 public void testEndElement() { 405 AttributeListImpl atts = new AttributeListImpl(); 406 atts.addAttribute("john:doe", "int", "42"); 407 408 try { 409 adapter.startDocument(); 410 adapter.startElement("foo:bar", atts); 411 adapter.endElement("foo:bar"); 412 } catch (SAXException e) { 413 throw new RuntimeException("Unexpected exception", e); 414 } 415 416 assertEquals("endElement", logger.getMethod()); 417 assertEquals(new String[] { "", "", "foo:bar" }, logger.getArgs()); 418 } 419 420 @TestTargetNew( 421 level = TestLevel.COMPLETE, 422 method = "characters", 423 args = { char[].class, int.class, int.class } 424 ) 425 public void testCharacters() { 426 char[] ch = "Android".toCharArray(); 427 428 try { 429 adapter.characters(ch, 2, 5); 430 } catch (SAXException e) { 431 throw new RuntimeException("Unexpected exception", e); 432 } 433 434 assertEquals(logger.size(), 1); 435 assertEquals("characters", logger.getMethod()); 436 assertEquals(new Object[] { ch, 2, 5 }, logger.getArgs()); 437 } 438 439 @TestTargetNew( 440 level = TestLevel.COMPLETE, 441 method = "ignorableWhitespace", 442 args = { char[].class, int.class, int.class } 443 ) 444 public void testIgnorableWhitespace() { 445 char[] ch = " ".toCharArray(); 446 447 try { 448 adapter.ignorableWhitespace(ch, 0, 5); 449 } catch (SAXException e) { 450 throw new RuntimeException("Unexpected exception", e); 451 } 452 453 assertEquals(logger.size(), 1); 454 assertEquals("ignorableWhitespace", logger.getMethod()); 455 assertEquals(new Object[] { ch, 0, 5 }, logger.getArgs()); 456 } 457 458 @TestTargetNew( 459 level = TestLevel.COMPLETE, 460 method = "processingInstruction", 461 args = { String.class, String.class } 462 ) 463 public void testProcessingInstruction() { 464 try { 465 adapter.processingInstruction("foo", "bar"); 466 } catch (SAXException e) { 467 throw new RuntimeException("Unexpected exception", e); 468 } 469 470 assertEquals(logger.size(), 1); 471 assertEquals("processingInstruction", logger.getMethod()); 472 assertEquals(new Object[] { "foo" , "bar" }, logger.getArgs()); 473 } 474 475} 476