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