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