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