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