XMLReaderFactoryTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 dalvik.annotation.TestLevel; 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargetNew; 22 23import junit.framework.TestCase; 24 25import org.xml.sax.SAXException; 26import org.xml.sax.helpers.XMLReaderFactory; 27 28@TestTargetClass(XMLReaderFactory.class) 29public class XMLReaderFactoryTest extends TestCase { 30 31 @TestTargetNew( 32 level = TestLevel.SUFFICIENT, 33 method = "createXMLReader", 34 args = { }, 35 notes = "Checks everything except META-INF case" 36 ) 37 public void testCreateXMLReader() { 38 // Property not set at all 39 try { 40 XMLReaderFactory.createXMLReader(); 41 } catch (SAXException e) { 42 // Expected 43 } 44 45 // Unknown class 46 System.setProperty("org.xml.sax.driver", "foo.bar.XMLReader"); 47 48 try { 49 XMLReaderFactory.createXMLReader(); 50 } catch (SAXException e) { 51 // Expected 52 } 53 54 // Non-accessible class 55 System.setProperty("org.xml.sax.driver", 56 "tests.api.org.xml.sax.support.NoAccessXMLReader"); 57 58 try { 59 XMLReaderFactory.createXMLReader(); 60 } catch (SAXException e) { 61 // Expected 62 } 63 64 // Non-instantiable class 65 System.setProperty("org.xml.sax.driver", 66 "tests.api.org.xml.sax.support.NoInstanceXMLReader"); 67 68 try { 69 XMLReaderFactory.createXMLReader(); 70 } catch (SAXException e) { 71 // Expected 72 } 73 74 // Non-XMLReader class 75 System.setProperty("org.xml.sax.driver", 76 "tests.api.org.xml.sax.support.NoSubclassXMLReader"); 77 78 try { 79 XMLReaderFactory.createXMLReader(); 80 } catch (ClassCastException e) { 81 // Expected 82 } catch (SAXException e) { 83 throw new RuntimeException("Unexpected exception", e); 84 } 85 86 // Good one, finally 87 System.setProperty("org.xml.sax.driver", 88 "tests.api.org.xml.sax.support.DoNothingXMLReader"); 89 90 try { 91 XMLReaderFactory.createXMLReader(); 92 } catch (SAXException e) { 93 throw new RuntimeException("Unexpected exception", e); 94 } 95 96 } 97 98 @TestTargetNew( 99 level = TestLevel.COMPLETE, 100 method = "createXMLReader", 101 args = { String.class } 102 ) 103 public void testMakeParserString() { 104 // No class 105 try { 106 XMLReaderFactory.createXMLReader(null); 107 } catch (NullPointerException e) { 108 // Expected 109 } catch (SAXException e) { 110 throw new RuntimeException("Unexpected exception", e); 111 } 112 113 // Unknown class 114 try { 115 XMLReaderFactory.createXMLReader("foo.bar.XMLReader"); 116 } catch (SAXException e) { 117 // Expected 118 } 119 120 // Non-accessible class 121 try { 122 XMLReaderFactory.createXMLReader( 123 "tests.api.org.xml.sax.support.NoAccessXMLReader"); 124 } catch (SAXException e) { 125 // Expected 126 } 127 128 // Non-instantiable class 129 try { 130 XMLReaderFactory.createXMLReader( 131 "tests.api.org.xml.sax.support.NoInstanceXMLReader"); 132 } catch (SAXException e) { 133 // Expected 134 } 135 136 // Non-Parser class 137 try { 138 XMLReaderFactory.createXMLReader( 139 "tests.api.org.xml.sax.support.NoSubclassXMLReader"); 140 } catch (SAXException e) { 141 // Expected 142 } 143 144 // Good one, finally 145 try { 146 XMLReaderFactory.createXMLReader( 147 "tests.api.org.xml.sax.support.DoNothingXMLReader"); 148 } catch (SAXException e) { 149 throw new RuntimeException("Unexpected exception", e); 150 } 151 152 } 153 154} 155