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