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