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