XMLReaderAdapterTest.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 java.io.IOException;
20import java.util.Locale;
21
22import junit.framework.TestCase;
23
24import org.xml.sax.AttributeList;
25import org.xml.sax.Attributes;
26import org.xml.sax.DTDHandler;
27import org.xml.sax.DocumentHandler;
28import org.xml.sax.EntityResolver;
29import org.xml.sax.ErrorHandler;
30import org.xml.sax.InputSource;
31import org.xml.sax.Locator;
32import org.xml.sax.SAXException;
33import org.xml.sax.XMLReader;
34import org.xml.sax.helpers.AttributesImpl;
35import org.xml.sax.helpers.LocatorImpl;
36import org.xml.sax.helpers.XMLReaderAdapter;
37
38import tests.api.org.xml.sax.support.MethodLogger;
39import tests.api.org.xml.sax.support.MockHandler;
40import tests.api.org.xml.sax.support.MockReader;
41import tests.api.org.xml.sax.support.MockResolver;
42import dalvik.annotation.TestLevel;
43import dalvik.annotation.TestTargetClass;
44import dalvik.annotation.TestTargetNew;
45
46@SuppressWarnings("deprecation")
47@TestTargetClass(XMLReaderAdapter.class)
48public class XMLReaderAdapterTest extends TestCase {
49
50    // Note: In many cases we can only test that delegation works
51    // properly. The rest is outside the scope of the specification.
52
53    private MethodLogger logger = new MethodLogger();
54
55    private MockHandler handler = new MockHandler(logger);
56
57    private XMLReader reader = new MockReader(logger);
58
59    private XMLReaderAdapter adapter = new XMLReaderAdapter(reader);
60
61    private void assertEquals(Object[] a, Object[] b) {
62        assertEquals(a.length, b.length);
63
64        for (int i = 0; i < a.length; i++) {
65            assertEquals("Element #" + i + " must be equal", a[i], b[i]);
66        }
67    }
68
69    @Override
70    public void setUp() {
71        adapter.setDocumentHandler(handler);
72        adapter.setDTDHandler(handler);
73        adapter.setErrorHandler(handler);
74    }
75
76    @TestTargetNew(
77        level = TestLevel.COMPLETE,
78        method = "XMLReaderAdapter",
79        args = { }
80    )
81    public void testXMLReaderAdapter() {
82        System.setProperty("org.xml.sax.driver",
83                "tests.api.org.xml.sax.support.DoNothingXMLReader");
84
85        try {
86            new XMLReaderAdapter();
87        } catch (SAXException e) {
88            throw new RuntimeException("Unexpected exception", e);
89        }
90    }
91
92    @TestTargetNew(
93        level = TestLevel.COMPLETE,
94        method = "XMLReaderAdapter",
95        args = { XMLReader.class }
96    )
97    public void testXMLReaderAdapterXMLReader() {
98        // Ordinary case
99        @SuppressWarnings("unused")
100        XMLReaderAdapter adapter = new XMLReaderAdapter(reader);
101
102        // Null case
103        try {
104            adapter = new XMLReaderAdapter(null);
105            fail("NullPointerException expected");
106        } catch (NullPointerException e) {
107            // Expected
108        }
109    }
110
111    @TestTargetNew(
112        level = TestLevel.COMPLETE,
113        method = "setLocale",
114        args = { Locale.class }
115    )
116    public void testSetLocale() {
117        // SAX RI does not support this, hence always expect exception
118        try {
119            adapter.setLocale(Locale.getDefault());
120            fail("SAXException expected");
121        } catch (SAXException e) {
122            // Expected
123        }
124    }
125
126    @TestTargetNew(
127        level = TestLevel.COMPLETE,
128        method = "setEntityResolver",
129        args = { EntityResolver.class }
130    )
131    public void testSetEntityResolver() {
132        EntityResolver resolver = new MockResolver();
133
134        // Ordinary case
135        adapter.setEntityResolver(resolver);
136        assertEquals(resolver, reader.getEntityResolver());
137
138        // null case
139        adapter.setEntityResolver(null);
140        assertEquals(null, reader.getEntityResolver());
141    }
142
143    @TestTargetNew(
144        level = TestLevel.COMPLETE,
145        method = "setDTDHandler",
146        args = { DTDHandler.class }
147    )
148    public void testSetDTDHandler() {
149        // Ordinary case
150        assertEquals(handler, reader.getDTDHandler());
151
152        // null case
153        adapter.setDTDHandler(null);
154        assertEquals(null, reader.getDTDHandler());
155    }
156
157    @TestTargetNew(
158        level = TestLevel.COMPLETE,
159        method = "setDocumentHandler",
160        args = { DocumentHandler.class }
161    )
162    public void testSetDocumentHandler() {
163        // There is no getter for the DocumentHandler, so we can only test
164        // indirectly whether is has been set correctly.
165        try {
166            adapter.startDocument();
167        } catch (SAXException e) {
168            throw new RuntimeException("Unexpected exception", e);
169        }
170
171        assertEquals("startDocument", logger.getMethod());
172        assertEquals(new Object[] { }, logger.getArgs());
173
174        // null case
175        adapter.setDocumentHandler(null);
176    }
177
178    @TestTargetNew(
179        level = TestLevel.COMPLETE,
180        method = "setErrorHandler",
181        args = { ErrorHandler.class }
182    )
183    public void testSetErrorHandler() {
184        // Ordinary case
185        assertEquals(handler, reader.getErrorHandler());
186
187        // null case
188        adapter.setErrorHandler(null);
189        assertEquals(null, reader.getErrorHandler());
190    }
191
192    @TestTargetNew(
193        level = TestLevel.COMPLETE,
194        method = "parse",
195        args = { String.class }
196    )
197    public void testParseString() {
198        try {
199            adapter.parse("foo");
200        } catch (SAXException e) {
201            throw new RuntimeException("Unexpected exception", e);
202        } catch (IOException e) {
203            throw new RuntimeException("Unexpected exception", e);
204        }
205
206        // The SAX RI creates an InputSource itself and then delegates to the
207        // "other" parse method.
208        assertEquals("parse", logger.getMethod(0));
209        assertEquals(InputSource.class, logger.getArgs(0)[0].getClass());
210    }
211
212    @TestTargetNew(
213        level = TestLevel.COMPLETE,
214        method = "parse",
215        args = { InputSource.class }
216    )
217    public void testParseInputSource() {
218        InputSource source = new InputSource("foo");
219
220        try {
221            adapter.parse(source);
222        } catch (SAXException e) {
223            throw new RuntimeException("Unexpected exception", e);
224        } catch (IOException e) {
225            throw new RuntimeException("Unexpected exception", e);
226        }
227
228        assertEquals("parse", logger.getMethod());
229        assertEquals(new Object[] { source }, logger.getArgs());
230    }
231
232    @TestTargetNew(
233        level = TestLevel.COMPLETE,
234        method = "setDocumentLocator",
235        args = { Locator.class }
236    )
237    public void testSetDocumentLocator() {
238        // Ordinary case
239        LocatorImpl locator = new LocatorImpl();
240        adapter.setDocumentLocator(locator);
241
242        assertEquals("setDocumentLocator", logger.getMethod());
243        assertEquals(new Object[] { locator }, logger.getArgs());
244
245        // null case (for the DocumentHandler itself!)
246        adapter.setDocumentHandler(null);
247        adapter.setDocumentLocator(locator);
248    }
249
250    @TestTargetNew(
251        level = TestLevel.COMPLETE,
252        method = "startDocument",
253        args = { }
254    )
255    public void testStartDocument() {
256        try {
257            adapter.startDocument();
258        } catch (SAXException e) {
259            throw new RuntimeException("Unexpected exception", e);
260        }
261
262        assertEquals(logger.size(), 1);
263        assertEquals("startDocument", logger.getMethod());
264        assertEquals(new Object[] {}, logger.getArgs());
265    }
266
267    @TestTargetNew(
268        level = TestLevel.COMPLETE,
269        method = "endDocument",
270        args = { }
271    )
272    public void testEndDocument() {
273        try {
274            adapter.endDocument();
275        } catch (SAXException e) {
276            throw new RuntimeException("Unexpected exception", e);
277        }
278
279        assertEquals(logger.size(), 1);
280        assertEquals("endDocument", logger.getMethod());
281        assertEquals(new Object[] {}, logger.getArgs());
282    }
283
284    @TestTargetNew(
285        level = TestLevel.COMPLETE,
286        method = "startPrefixMapping",
287        args = { String.class, String.class }
288    )
289    public void testStartPrefixMapping() {
290        adapter.startPrefixMapping("foo", "http://some.uri");
291        assertEquals(logger.size(), 0);
292    }
293
294    @TestTargetNew(
295        level = TestLevel.COMPLETE,
296        method = "endPrefixMapping",
297        args = { String.class }
298    )
299    public void testEndPrefixMapping() {
300        adapter.endPrefixMapping("foo");
301        assertEquals(logger.size(), 0);
302    }
303
304    @TestTargetNew(
305        level = TestLevel.COMPLETE,
306        method = "startElement",
307        args = { String.class, String.class, String.class, Attributes.class }
308    )
309    public void testStartElement() {
310        AttributesImpl atts = new AttributesImpl();
311        atts.addAttribute("http://some.other.uri", "gabba", "gabba:hey",
312                "int", "42");
313
314        try {
315            adapter.startElement("http://some.uri", "bar", "foo:bar", atts);
316        } catch (SAXException e) {
317            throw new RuntimeException("Unexpected exception", e);
318        }
319
320        assertEquals(logger.size(), 1);
321        assertEquals("startElement", logger.getMethod());
322        assertEquals("foo:bar", logger.getArgs()[0]);
323        assertEquals("gabba:hey",
324                ((AttributeList)logger.getArgs()[1]).getName(0));
325    }
326
327    @TestTargetNew(
328        level = TestLevel.COMPLETE,
329        method = "endElement",
330        args = { String.class, String.class, String.class }
331    )
332    public void testEndElement() {
333        try {
334            adapter.endElement("http://some.uri", "bar", "foo:bar");
335        } catch (SAXException e) {
336            throw new RuntimeException("Unexpected exception", e);
337        }
338
339        assertEquals(logger.size(), 1);
340        assertEquals("endElement", logger.getMethod());
341        assertEquals(new Object[] { "foo:bar" }, logger.getArgs());
342    }
343
344    @TestTargetNew(
345        level = TestLevel.COMPLETE,
346        method = "characters",
347        args = { char[].class, int.class, int.class }
348    )
349    public void testCharacters() {
350        char[] ch = "Android".toCharArray();
351
352        try {
353            adapter.characters(ch, 2, 5);
354        } catch (SAXException e) {
355            throw new RuntimeException("Unexpected exception", e);
356        }
357
358        assertEquals(logger.size(), 1);
359        assertEquals("characters", logger.getMethod());
360        assertEquals(new Object[] { ch, 2, 5 }, logger.getArgs());
361    }
362
363    @TestTargetNew(
364        level = TestLevel.COMPLETE,
365        method = "ignorableWhitespace",
366        args = { char[].class, int.class, int.class }
367    )
368    public void testIgnorableWhitespace() {
369        char[] ch = "     ".toCharArray();
370
371        try {
372            adapter.ignorableWhitespace(ch, 0, 5);
373        } catch (SAXException e) {
374            throw new RuntimeException("Unexpected exception", e);
375        }
376
377        assertEquals(logger.size(), 1);
378        assertEquals("ignorableWhitespace", logger.getMethod());
379        assertEquals(new Object[] { ch, 0, 5 }, logger.getArgs());
380    }
381
382    @TestTargetNew(
383        level = TestLevel.COMPLETE,
384        method = "processingInstruction",
385        args = { String.class, String.class }
386    )
387    public void testProcessingInstruction() {
388        try {
389            adapter.processingInstruction("foo", "bar");
390        } catch (SAXException e) {
391            throw new RuntimeException("Unexpected exception", e);
392        }
393
394        assertEquals(logger.size(), 1);
395        assertEquals("processingInstruction", logger.getMethod());
396        assertEquals(new Object[] { "foo" , "bar" }, logger.getArgs());
397    }
398
399    @TestTargetNew(
400        level = TestLevel.COMPLETE,
401        method = "skippedEntity",
402        args = { String.class }
403    )
404    public void testSkippedEntity() {
405        try {
406            adapter.skippedEntity("foo");
407        } catch (SAXException e) {
408            throw new RuntimeException("Unexpected exception", e);
409        }
410
411        assertEquals(logger.size(), 0);
412    }
413
414}
415