ParserAdapterTest.java revision ab762bb740405d0fefcccf4a0899a234f995be13
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 java.io.IOException;
20
21import junit.framework.TestCase;
22
23import org.xml.sax.AttributeList;
24import org.xml.sax.Attributes;
25import org.xml.sax.ContentHandler;
26import org.xml.sax.DTDHandler;
27import org.xml.sax.EntityResolver;
28import org.xml.sax.ErrorHandler;
29import org.xml.sax.InputSource;
30import org.xml.sax.Locator;
31import org.xml.sax.Parser;
32import org.xml.sax.SAXException;
33import org.xml.sax.SAXNotRecognizedException;
34import org.xml.sax.SAXNotSupportedException;
35import org.xml.sax.helpers.AttributeListImpl;
36import org.xml.sax.helpers.LocatorImpl;
37import org.xml.sax.helpers.ParserAdapter;
38
39import tests.api.org.xml.sax.support.MethodLogger;
40import tests.api.org.xml.sax.support.MockHandler;
41import tests.api.org.xml.sax.support.MockParser;
42import tests.api.org.xml.sax.support.MockResolver;
43
44@SuppressWarnings("deprecation")
45public class ParserAdapterTest extends TestCase {
46
47    // Note: In many cases we can only test that delegation works
48    // properly. The rest is outside the scope of the specification.
49
50    private final static String FEATURES = "http://xml.org/sax/features/";
51
52    private final static String NAMESPACES = FEATURES + "namespaces";
53
54    private final static String NAMESPACE_PREFIXES = FEATURES
55                                                        + "namespace-prefixes";
56
57    private final static String XMLNS_URIs = FEATURES + "xmlns-uris";
58
59    private MethodLogger logger = new MethodLogger();
60
61    private MockHandler handler = new MockHandler(logger);
62
63    private Parser parser = new MockParser(logger);
64
65    private ParserAdapter adapter = new ParserAdapter(parser);
66
67    private void assertEquals(Object[] a, Object[] b) {
68        assertEquals(a.length, b.length);
69
70        for (int i = 0; i < a.length; i++) {
71            assertEquals("Element #" + i + " must be equal", a[i], b[i]);
72        }
73    }
74
75    @Override
76    public void setUp() {
77        adapter.setContentHandler(handler);
78        adapter.setDTDHandler(handler);
79        adapter.setErrorHandler(handler);
80    }
81
82    @Override protected void tearDown() throws Exception {
83        super.tearDown();
84    }
85
86    public void testParserAdapter() {
87        System.setProperty("org.xml.sax.parser",
88                "tests.api.org.xml.sax.support.DoNothingParser");
89
90        try {
91            new ParserAdapter();
92        } catch (SAXException e) {
93            throw new RuntimeException("Unexpected exception", e);
94        }
95    }
96
97    public void testParserAdapterParser() {
98        // Ordinary case
99        @SuppressWarnings("unused")
100        ParserAdapter adapter = new ParserAdapter(parser);
101
102        // Null case
103        try {
104            adapter = new ParserAdapter(null);
105            fail("NullPointerException expected");
106        } catch (NullPointerException e) {
107            // Expected
108        }
109    }
110
111    public void testGetSetFeature() {
112        String[] features = new String[] { NAMESPACES, NAMESPACE_PREFIXES,
113                XMLNS_URIs };
114
115        for (String s: features) {
116            try {
117                adapter.setFeature(s, true);
118                assertEquals(true, adapter.getFeature(s));
119
120                adapter.setFeature(s, false);
121                assertEquals(false, adapter.getFeature(s));
122            } catch (SAXException e) {
123                throw new RuntimeException("Unexpected exception", e);
124            }
125        }
126
127        try {
128            adapter.setFeature("http://argle.bargle", true);
129            fail("SAXNotRecognizedException expected");
130        } catch (SAXNotRecognizedException e) {
131            // Expected
132        } catch (SAXNotSupportedException e) {
133            throw new RuntimeException("Unexpected exception", e);
134        }
135    }
136
137    public void testGetSetProperty() {
138        try {
139            adapter.setProperty("http://argle.bargle", ":)");
140            fail("SAXNotRecognizedException expected");
141        } catch (SAXNotRecognizedException e) {
142            // Expected
143        } catch (SAXNotSupportedException e) {
144            throw new RuntimeException("Unexpected exception", e);
145        }
146
147        try {
148            adapter.getProperty("http://argle.bargle");
149            fail("SAXNotRecognizedException expected");
150        } catch (SAXNotRecognizedException e) {
151            // Expected
152        } catch (SAXNotSupportedException e) {
153            throw new RuntimeException("Unexpected exception", e);
154        }
155    }
156
157    public void testGetSetEntityResolver() {
158        EntityResolver resolver = new MockResolver();
159
160        adapter.setEntityResolver(resolver);
161        assertEquals(resolver, adapter.getEntityResolver());
162
163        adapter.setEntityResolver(null);
164        assertEquals(null, adapter.getEntityResolver());
165    }
166
167    public void testGetSetDTDHandler() {
168        adapter.setDTDHandler(null);
169        assertEquals(null, adapter.getDTDHandler());
170
171        adapter.setDTDHandler(handler);
172        assertEquals(handler, adapter.getDTDHandler());
173    }
174
175    public void testGetSetContentHandler() {
176        adapter.setContentHandler(null);
177        assertEquals(null, adapter.getContentHandler());
178
179        adapter.setContentHandler(handler);
180        assertEquals(handler, adapter.getContentHandler());
181    }
182
183    public void testGetSetErrorHandler() {
184        adapter.setErrorHandler(null);
185        assertEquals(null, adapter.getErrorHandler());
186
187        adapter.setErrorHandler(handler);
188        assertEquals(handler, adapter.getErrorHandler());
189    }
190
191    public void testParseString() {
192        try {
193            adapter.parse("foo");
194        } catch (SAXException e) {
195            throw new RuntimeException("Unexpected exception", e);
196        } catch (IOException e) {
197            throw new RuntimeException("Unexpected exception", e);
198        }
199
200        // The SAX RI creates an InputSource itself and then delegates to the
201        // "other" parse method.
202        assertEquals("parse", logger.getMethod());
203        assertEquals(InputSource.class, logger.getArgs()[0].getClass());
204    }
205
206    public void testParseInputSource() {
207        InputSource source = new InputSource("foo");
208
209        try {
210            adapter.parse(source);
211        } catch (SAXException e) {
212            throw new RuntimeException("Unexpected exception", e);
213        } catch (IOException e) {
214            throw new RuntimeException("Unexpected exception", e);
215        }
216
217        assertEquals("parse", logger.getMethod());
218        assertEquals(new Object[] { source }, logger.getArgs());
219    }
220
221    public void testSetDocumentLocator() {
222        Locator l = new LocatorImpl();
223
224        adapter.setDocumentLocator(l);
225
226        assertEquals(logger.size(), 1);
227        assertEquals("setDocumentLocator", logger.getMethod());
228        assertEquals(new Object[] { l }, logger.getArgs());
229
230        adapter.setDocumentLocator(null);
231
232        assertEquals(logger.size(), 2);
233        assertEquals("setDocumentLocator", logger.getMethod());
234        assertEquals(new Object[] { null }, logger.getArgs());
235    }
236
237    public void testStartDocument() {
238        try {
239            adapter.startDocument();
240        } catch (SAXException e) {
241            throw new RuntimeException("Unexpected exception", e);
242        }
243
244        assertEquals(logger.size(), 1);
245        assertEquals("startDocument", logger.getMethod());
246        assertEquals(new Object[] {}, logger.getArgs());
247    }
248
249    public void testEndDocument() {
250        try {
251            adapter.endDocument();
252        } catch (SAXException e) {
253            throw new RuntimeException("Unexpected exception", e);
254        }
255
256        assertEquals(logger.size(), 1);
257        assertEquals("endDocument", logger.getMethod());
258        assertEquals(new Object[] {}, logger.getArgs());
259    }
260
261    public void testStartElement() {
262        AttributeListImpl atts = new AttributeListImpl();
263        atts.addAttribute("john:doe", "int", "42");
264
265        try {
266            adapter.startDocument();
267            adapter.startElement("foo:bar", atts);
268        } catch (SAXException e) {
269            throw new RuntimeException("Unexpected exception", e);
270        }
271
272        assertEquals("startElement", logger.getMethod());
273        assertEquals("", logger.getArgs()[0]);
274        assertEquals("", logger.getArgs()[1]);
275        assertEquals("foo:bar", logger.getArgs()[2]);
276        assertEquals("john:doe", ((Attributes)logger.getArgs()[3]).getQName(0));
277    }
278
279    public void testEndElement() {
280        AttributeListImpl atts = new AttributeListImpl();
281        atts.addAttribute("john:doe", "int", "42");
282
283        try {
284            adapter.startDocument();
285            adapter.startElement("foo:bar", atts);
286            adapter.endElement("foo:bar");
287        } catch (SAXException e) {
288            throw new RuntimeException("Unexpected exception", e);
289        }
290
291        assertEquals("endElement", logger.getMethod());
292        assertEquals(new String[] { "", "", "foo:bar" }, logger.getArgs());
293    }
294
295    public void testCharacters() {
296        char[] ch = "Android".toCharArray();
297
298        try {
299            adapter.characters(ch, 2, 5);
300        } catch (SAXException e) {
301            throw new RuntimeException("Unexpected exception", e);
302        }
303
304        assertEquals(logger.size(), 1);
305        assertEquals("characters", logger.getMethod());
306        assertEquals(new Object[] { ch, 2, 5 }, logger.getArgs());
307    }
308
309    public void testIgnorableWhitespace() {
310        char[] ch = "     ".toCharArray();
311
312        try {
313            adapter.ignorableWhitespace(ch, 0, 5);
314        } catch (SAXException e) {
315            throw new RuntimeException("Unexpected exception", e);
316        }
317
318        assertEquals(logger.size(), 1);
319        assertEquals("ignorableWhitespace", logger.getMethod());
320        assertEquals(new Object[] { ch, 0, 5 }, logger.getArgs());
321    }
322
323    public void testProcessingInstruction() {
324        try {
325            adapter.processingInstruction("foo", "bar");
326        } catch (SAXException e) {
327            throw new RuntimeException("Unexpected exception", e);
328        }
329
330        assertEquals(logger.size(), 1);
331        assertEquals("processingInstruction", logger.getMethod());
332        assertEquals(new Object[] { "foo" , "bar" }, logger.getArgs());
333    }
334
335}
336