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;
42
43@SuppressWarnings("deprecation")
44public class XMLReaderAdapterTest extends TestCase {
45
46    // Note: In many cases we can only test that delegation works
47    // properly. The rest is outside the scope of the specification.
48
49    private MethodLogger logger = new MethodLogger();
50
51    private MockHandler handler = new MockHandler(logger);
52
53    private XMLReader reader = new MockReader(logger);
54
55    private XMLReaderAdapter adapter = new XMLReaderAdapter(reader);
56
57    private void assertEquals(Object[] a, Object[] b) {
58        assertEquals(a.length, b.length);
59
60        for (int i = 0; i < a.length; i++) {
61            assertEquals("Element #" + i + " must be equal", a[i], b[i]);
62        }
63    }
64
65    @Override
66    public void setUp() {
67        adapter.setDocumentHandler(handler);
68        adapter.setDTDHandler(handler);
69        adapter.setErrorHandler(handler);
70    }
71
72    @Override protected void tearDown() throws Exception {
73        super.tearDown();
74    }
75
76    public void testXMLReaderAdapter() {
77        System.setProperty("org.xml.sax.driver",
78                "tests.api.org.xml.sax.support.DoNothingXMLReader");
79
80        try {
81            new XMLReaderAdapter();
82        } catch (SAXException e) {
83            throw new RuntimeException("Unexpected exception", e);
84        }
85    }
86
87    public void testXMLReaderAdapterXMLReader() {
88        // Ordinary case
89        @SuppressWarnings("unused")
90        XMLReaderAdapter adapter = new XMLReaderAdapter(reader);
91
92        // Null case
93        try {
94            adapter = new XMLReaderAdapter(null);
95            fail("NullPointerException expected");
96        } catch (NullPointerException e) {
97            // Expected
98        }
99    }
100
101    public void testSetLocale() {
102        // SAX RI does not support this, hence always expect exception
103        try {
104            adapter.setLocale(Locale.getDefault());
105            fail("SAXException expected");
106        } catch (SAXException e) {
107            // Expected
108        }
109    }
110
111    public void testSetEntityResolver() {
112        EntityResolver resolver = new MockResolver();
113
114        // Ordinary case
115        adapter.setEntityResolver(resolver);
116        assertEquals(resolver, reader.getEntityResolver());
117
118        // null case
119        adapter.setEntityResolver(null);
120        assertEquals(null, reader.getEntityResolver());
121    }
122
123    public void testSetDTDHandler() {
124        // Ordinary case
125        assertEquals(handler, reader.getDTDHandler());
126
127        // null case
128        adapter.setDTDHandler(null);
129        assertEquals(null, reader.getDTDHandler());
130    }
131
132    public void testSetDocumentHandler() {
133        // There is no getter for the DocumentHandler, so we can only test
134        // indirectly whether is has been set correctly.
135        try {
136            adapter.startDocument();
137        } catch (SAXException e) {
138            throw new RuntimeException("Unexpected exception", e);
139        }
140
141        assertEquals("startDocument", logger.getMethod());
142        assertEquals(new Object[] { }, logger.getArgs());
143
144        // null case
145        adapter.setDocumentHandler(null);
146    }
147
148    public void testSetErrorHandler() {
149        // Ordinary case
150        assertEquals(handler, reader.getErrorHandler());
151
152        // null case
153        adapter.setErrorHandler(null);
154        assertEquals(null, reader.getErrorHandler());
155    }
156
157    public void testParseString() {
158        try {
159            adapter.parse("foo");
160        } catch (SAXException e) {
161            throw new RuntimeException("Unexpected exception", e);
162        } catch (IOException e) {
163            throw new RuntimeException("Unexpected exception", e);
164        }
165
166        // The SAX RI creates an InputSource itself and then delegates to the
167        // "other" parse method.
168        assertEquals("parse", logger.getMethod(0));
169        assertEquals(InputSource.class, logger.getArgs(0)[0].getClass());
170    }
171
172    public void testParseInputSource() {
173        InputSource source = new InputSource("foo");
174
175        try {
176            adapter.parse(source);
177        } catch (SAXException e) {
178            throw new RuntimeException("Unexpected exception", e);
179        } catch (IOException e) {
180            throw new RuntimeException("Unexpected exception", e);
181        }
182
183        assertEquals("parse", logger.getMethod());
184        assertEquals(new Object[] { source }, logger.getArgs());
185    }
186
187    public void testSetDocumentLocator() {
188        // Ordinary case
189        LocatorImpl locator = new LocatorImpl();
190        adapter.setDocumentLocator(locator);
191
192        assertEquals("setDocumentLocator", logger.getMethod());
193        assertEquals(new Object[] { locator }, logger.getArgs());
194
195        // null case (for the DocumentHandler itself!)
196        adapter.setDocumentHandler(null);
197        adapter.setDocumentLocator(locator);
198    }
199
200    public void testStartDocument() {
201        try {
202            adapter.startDocument();
203        } catch (SAXException e) {
204            throw new RuntimeException("Unexpected exception", e);
205        }
206
207        assertEquals(logger.size(), 1);
208        assertEquals("startDocument", logger.getMethod());
209        assertEquals(new Object[] {}, logger.getArgs());
210    }
211
212    public void testEndDocument() {
213        try {
214            adapter.endDocument();
215        } catch (SAXException e) {
216            throw new RuntimeException("Unexpected exception", e);
217        }
218
219        assertEquals(logger.size(), 1);
220        assertEquals("endDocument", logger.getMethod());
221        assertEquals(new Object[] {}, logger.getArgs());
222    }
223
224    public void testStartPrefixMapping() {
225        adapter.startPrefixMapping("foo", "http://some.uri");
226        assertEquals(logger.size(), 0);
227    }
228
229    public void testEndPrefixMapping() {
230        adapter.endPrefixMapping("foo");
231        assertEquals(logger.size(), 0);
232    }
233
234    public void testStartElement() {
235        AttributesImpl atts = new AttributesImpl();
236        atts.addAttribute("http://some.other.uri", "gabba", "gabba:hey",
237                "int", "42");
238
239        try {
240            adapter.startElement("http://some.uri", "bar", "foo:bar", atts);
241        } catch (SAXException e) {
242            throw new RuntimeException("Unexpected exception", e);
243        }
244
245        assertEquals(logger.size(), 1);
246        assertEquals("startElement", logger.getMethod());
247        assertEquals("foo:bar", logger.getArgs()[0]);
248        assertEquals("gabba:hey",
249                ((AttributeList)logger.getArgs()[1]).getName(0));
250    }
251
252    public void testEndElement() {
253        try {
254            adapter.endElement("http://some.uri", "bar", "foo:bar");
255        } catch (SAXException e) {
256            throw new RuntimeException("Unexpected exception", e);
257        }
258
259        assertEquals(logger.size(), 1);
260        assertEquals("endElement", logger.getMethod());
261        assertEquals(new Object[] { "foo:bar" }, logger.getArgs());
262    }
263
264    public void testCharacters() {
265        char[] ch = "Android".toCharArray();
266
267        try {
268            adapter.characters(ch, 2, 5);
269        } catch (SAXException e) {
270            throw new RuntimeException("Unexpected exception", e);
271        }
272
273        assertEquals(logger.size(), 1);
274        assertEquals("characters", logger.getMethod());
275        assertEquals(new Object[] { ch, 2, 5 }, logger.getArgs());
276    }
277
278    public void testIgnorableWhitespace() {
279        char[] ch = "     ".toCharArray();
280
281        try {
282            adapter.ignorableWhitespace(ch, 0, 5);
283        } catch (SAXException e) {
284            throw new RuntimeException("Unexpected exception", e);
285        }
286
287        assertEquals(logger.size(), 1);
288        assertEquals("ignorableWhitespace", logger.getMethod());
289        assertEquals(new Object[] { ch, 0, 5 }, logger.getArgs());
290    }
291
292    public void testProcessingInstruction() {
293        try {
294            adapter.processingInstruction("foo", "bar");
295        } catch (SAXException e) {
296            throw new RuntimeException("Unexpected exception", e);
297        }
298
299        assertEquals(logger.size(), 1);
300        assertEquals("processingInstruction", logger.getMethod());
301        assertEquals(new Object[] { "foo" , "bar" }, logger.getArgs());
302    }
303
304    public void testSkippedEntity() {
305        try {
306            adapter.skippedEntity("foo");
307        } catch (SAXException e) {
308            throw new RuntimeException("Unexpected exception", e);
309        }
310
311        assertEquals(logger.size(), 0);
312    }
313
314}
315