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