Home | History | Annotate | Download | only in helpers
      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 
     17 package tests.api.org.xml.sax.helpers;
     18 
     19 import java.io.IOException;
     20 
     21 import junit.framework.TestCase;
     22 
     23 import org.xml.sax.Attributes;
     24 import org.xml.sax.ContentHandler;
     25 import org.xml.sax.DTDHandler;
     26 import org.xml.sax.EntityResolver;
     27 import org.xml.sax.ErrorHandler;
     28 import org.xml.sax.InputSource;
     29 import org.xml.sax.Locator;
     30 import org.xml.sax.SAXException;
     31 import org.xml.sax.SAXNotRecognizedException;
     32 import org.xml.sax.SAXNotSupportedException;
     33 import org.xml.sax.SAXParseException;
     34 import org.xml.sax.XMLReader;
     35 import org.xml.sax.helpers.AttributesImpl;
     36 import org.xml.sax.helpers.LocatorImpl;
     37 import org.xml.sax.helpers.XMLFilterImpl;
     38 
     39 import tests.api.org.xml.sax.support.MethodLogger;
     40 import tests.api.org.xml.sax.support.MockFilter;
     41 import tests.api.org.xml.sax.support.MockHandler;
     42 import tests.api.org.xml.sax.support.MockResolver;
     43 
     44 public class XMLFilterImplTest 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 XMLFilterImpl parent = new MockFilter(logger);
     54 
     55     private XMLFilterImpl child = new XMLFilterImpl(parent);
     56 
     57     private XMLFilterImpl orphan = new XMLFilterImpl();
     58 
     59     private void assertEquals(Object[] a, Object[] b) {
     60         assertEquals(a.length, b.length);
     61 
     62         for (int i = 0; i < a.length; i++) {
     63             assertEquals("Element #" + i + " must be equal", a[i], b[i]);
     64         }
     65     }
     66 
     67     public void setUp() {
     68         parent.setContentHandler(handler);
     69         parent.setDTDHandler(handler);
     70         parent.setErrorHandler(handler);
     71 
     72         child.setContentHandler(handler);
     73         child.setDTDHandler(handler);
     74         child.setErrorHandler(handler);
     75     }
     76 
     77     public void testXMLFilterImpl() {
     78         assertEquals(null, parent.getParent());
     79     }
     80 
     81     public void testXMLFilterImplXMLReader() {
     82         // Ordinary case
     83         assertEquals(null, parent.getParent());
     84 
     85         // null case
     86         XMLFilterImpl filter = new XMLFilterImpl(null);
     87         assertEquals(null, filter.getParent());
     88     }
     89 
     90     public void testGetSetParent() {
     91         child.setParent(null);
     92         assertEquals(null, child.getParent());
     93 
     94         child.setParent(parent);
     95         assertEquals(parent, child.getParent());
     96     }
     97 
     98     public void testGetSetFeature() {
     99         // Ordinary case
    100         try {
    101             child.setFeature("foo", true);
    102             assertEquals(true, child.getFeature("foo"));
    103 
    104             child.setFeature("foo", false);
    105             assertEquals(false, child.getFeature("foo"));
    106         } catch (SAXNotRecognizedException e) {
    107             throw new RuntimeException("Unexpected exception", e);
    108         } catch (SAXNotSupportedException e) {
    109             throw new RuntimeException("Unexpected exception", e);
    110         }
    111 
    112         // No parent case
    113         try {
    114             orphan.setFeature("foo", false);
    115             fail("SAXNotRecognizedException expected");
    116         } catch (SAXNotRecognizedException e) {
    117             // Expected
    118         } catch (SAXNotSupportedException e) {
    119             throw new RuntimeException("Unexpected exception", e);
    120         }
    121     }
    122 
    123     public void testGetSetProperty() {
    124         // Ordinary case
    125         try {
    126             child.setProperty("foo", "bar");
    127             assertEquals("bar", child.getProperty("foo"));
    128 
    129             child.setProperty("foo", null);
    130             assertEquals(null, child.getProperty("foo"));
    131         } catch (SAXNotRecognizedException e) {
    132             throw new RuntimeException("Unexpected exception", e);
    133         } catch (SAXNotSupportedException e) {
    134             throw new RuntimeException("Unexpected exception", e);
    135         }
    136 
    137         // No parent case
    138         try {
    139             orphan.setProperty("foo", "bar");
    140             fail("SAXNotRecognizedException expected");
    141         } catch (SAXNotRecognizedException e) {
    142             // Expected
    143         } catch (SAXNotSupportedException e) {
    144             throw new RuntimeException("Unexpected exception", e);
    145         }
    146     }
    147 
    148     public void testGetSetEntityResolver() {
    149         EntityResolver resolver = new MockResolver();
    150 
    151         parent.setEntityResolver(resolver);
    152         assertEquals(resolver, parent.getEntityResolver());
    153 
    154         parent.setEntityResolver(null);
    155         assertEquals(null, parent.getEntityResolver());
    156     }
    157 
    158     public void testGetSetDTDHandler() {
    159         parent.setDTDHandler(null);
    160         assertEquals(null, parent.getDTDHandler());
    161 
    162         parent.setDTDHandler(handler);
    163         assertEquals(handler, parent.getDTDHandler());
    164     }
    165 
    166     public void testGetSetContentHandler() {
    167         parent.setContentHandler(null);
    168         assertEquals(null, parent.getContentHandler());
    169 
    170         parent.setContentHandler(handler);
    171         assertEquals(handler, parent.getContentHandler());
    172     }
    173 
    174     public void testGetSetErrorHandler() {
    175         parent.setErrorHandler(null);
    176         assertEquals(null, parent.getErrorHandler());
    177 
    178         parent.setErrorHandler(handler);
    179         assertEquals(handler, parent.getErrorHandler());
    180     }
    181 
    182     public void testParseInputSource() {
    183         InputSource is = new InputSource();
    184 
    185         // Ordinary case
    186         try {
    187             child.parse(is);
    188         } catch (SAXException e) {
    189             throw new RuntimeException("Unexpected exception", e);
    190         } catch (IOException e) {
    191             throw new RuntimeException("Unexpected exception", e);
    192         }
    193 
    194         assertEquals(1, logger.size());
    195         assertEquals("parse", logger.getMethod());
    196 
    197         // No parent case
    198         try {
    199             orphan.parse(is);
    200             fail("NullPointerException expected");
    201         } catch (NullPointerException e) {
    202             // Expected
    203         } catch (SAXException e) {
    204             throw new RuntimeException("Unexpected exception", e);
    205         } catch (IOException e) {
    206             throw new RuntimeException("Unexpected exception", e);
    207         }
    208     }
    209 
    210     public void testParseString() {
    211         // Ordinary case
    212         try {
    213             child.parse("foo");
    214         } catch (SAXException e) {
    215             throw new RuntimeException("Unexpected exception", e);
    216         } catch (IOException e) {
    217             throw new RuntimeException("Unexpected exception", e);
    218         }
    219 
    220         assertEquals(1, logger.size());
    221         assertEquals("parse", logger.getMethod());
    222 
    223         // No parent case
    224         try {
    225             orphan.parse("foo");
    226             fail("NullPointerException expected");
    227         } catch (NullPointerException e) {
    228             // Expected
    229         } catch (SAXException e) {
    230             throw new RuntimeException("Unexpected exception", e);
    231         } catch (IOException e) {
    232             throw new RuntimeException("Unexpected exception", e);
    233         }
    234     }
    235 
    236     public void testResolveEntity() {
    237         InputSource expected = new InputSource();
    238 
    239         MockResolver resolver = new MockResolver();
    240         resolver.addEntity("foo", "bar", expected);
    241 
    242         InputSource result = null;
    243 
    244         parent.setEntityResolver(resolver);
    245 
    246         // Ordinary case
    247         try {
    248             result = parent.resolveEntity("foo", "bar");
    249         } catch (SAXException e) {
    250             throw new RuntimeException("Unexpected exception", e);
    251         } catch (IOException e) {
    252             throw new RuntimeException("Unexpected exception", e);
    253         }
    254 
    255         assertEquals(expected, result);
    256 
    257         // No entity resolver case
    258         parent.setEntityResolver(null);
    259 
    260         try {
    261             result = parent.resolveEntity("foo", "bar");
    262         } catch (SAXException e) {
    263             throw new RuntimeException("Unexpected exception", e);
    264         } catch (IOException e) {
    265             throw new RuntimeException("Unexpected exception", e);
    266         }
    267 
    268         assertEquals(null, result);
    269     }
    270 
    271     public void testNotationDecl() {
    272         try {
    273             parent.notationDecl("foo", "bar", "foobar");
    274         } catch (SAXException e) {
    275             throw new RuntimeException("Unexpected exception", e);
    276         }
    277 
    278         assertEquals(logger.size(), 1);
    279         assertEquals("notationDecl", logger.getMethod());
    280         assertEquals(new Object[] { "foo", "bar", "foobar" },
    281                 logger.getArgs());
    282     }
    283 
    284     public void testUnparsedEntityDecl() {
    285         try {
    286             parent.unparsedEntityDecl("foo", "bar", "gabba", "hey");
    287         } catch (SAXException e) {
    288             throw new RuntimeException("Unexpected exception", e);
    289         }
    290 
    291         assertEquals(logger.size(), 1);
    292         assertEquals("unparsedEntityDecl", logger.getMethod());
    293         assertEquals(new Object[] { "foo", "bar", "gabba", "hey" },
    294                 logger.getArgs());
    295     }
    296 
    297     public void testSetDocumentLocator() {
    298         Locator l = new LocatorImpl();
    299 
    300         child.setDocumentLocator(l);
    301 
    302         assertEquals(logger.size(), 1);
    303         assertEquals("setDocumentLocator", logger.getMethod());
    304         assertEquals(new Object[] { l }, logger.getArgs());
    305 
    306         child.setDocumentLocator(null);
    307 
    308         assertEquals(logger.size(), 2);
    309         assertEquals("setDocumentLocator", logger.getMethod());
    310         assertEquals(new Object[] { null }, logger.getArgs());
    311     }
    312 
    313     public void testStartDocument() {
    314         try {
    315             parent.startDocument();
    316         } catch (SAXException e) {
    317             throw new RuntimeException("Unexpected exception", e);
    318         }
    319 
    320         assertEquals(logger.size(), 1);
    321         assertEquals("startDocument", logger.getMethod());
    322         assertEquals(new Object[] {}, logger.getArgs());
    323     }
    324 
    325     public void testEndDocument() {
    326         try {
    327             parent.endDocument();
    328         } catch (SAXException e) {
    329             throw new RuntimeException("Unexpected exception", e);
    330         }
    331 
    332         assertEquals(logger.size(), 1);
    333         assertEquals("endDocument", logger.getMethod());
    334         assertEquals(new Object[] {}, logger.getArgs());
    335     }
    336 
    337     public void testStartPrefixMapping() {
    338         try {
    339             parent.startPrefixMapping("foo", "http://some.uri");
    340         } catch (SAXException e) {
    341             throw new RuntimeException("Unexpected exception", e);
    342         }
    343 
    344         assertEquals(logger.size(), 1);
    345         assertEquals("startPrefixMapping", logger.getMethod());
    346         assertEquals(new Object[] { "foo", "http://some.uri" },
    347                 logger.getArgs());
    348     }
    349 
    350     public void testEndPrefixMapping() {
    351         try {
    352             parent.endPrefixMapping("foo");
    353         } catch (SAXException e) {
    354             throw new RuntimeException("Unexpected exception", e);
    355         }
    356 
    357         assertEquals(logger.size(), 1);
    358         assertEquals("endPrefixMapping", logger.getMethod());
    359         assertEquals(new Object[] { "foo" }, logger.getArgs());
    360     }
    361 
    362     public void testStartElement() {
    363         Attributes atts = new AttributesImpl();
    364 
    365         try {
    366             parent.startElement("http://some.uri", "bar", "foo:bar", atts);
    367         } catch (SAXException e) {
    368             throw new RuntimeException("Unexpected exception", e);
    369         }
    370 
    371         assertEquals(logger.size(), 1);
    372         assertEquals("startElement", logger.getMethod());
    373         assertEquals(new Object[] { "http://some.uri", "bar", "foo:bar", atts },
    374                 logger.getArgs());
    375     }
    376 
    377     public void testEndElement() {
    378         try {
    379             parent.endElement("http://some.uri", "bar", "foo:bar");
    380          } catch (SAXException e) {
    381              throw new RuntimeException("Unexpected exception", e);
    382          }
    383 
    384          assertEquals(logger.size(), 1);
    385          assertEquals("endElement", logger.getMethod());
    386          assertEquals(new Object[] { "http://some.uri", "bar", "foo:bar" },
    387                  logger.getArgs());
    388     }
    389 
    390     public void testCharacters() {
    391         char[] ch = "Android".toCharArray();
    392 
    393         try {
    394             parent.characters(ch, 2, 5);
    395         } catch (SAXException e) {
    396             throw new RuntimeException("Unexpected exception", e);
    397         }
    398 
    399         assertEquals(logger.size(), 1);
    400         assertEquals("characters", logger.getMethod());
    401         assertEquals(new Object[] { ch, 2, 5 }, logger.getArgs());
    402     }
    403 
    404     public void testIgnorableWhitespace() {
    405         char[] ch = "     ".toCharArray();
    406 
    407         try {
    408             parent.ignorableWhitespace(ch, 0, 5);
    409         } catch (SAXException e) {
    410             throw new RuntimeException("Unexpected exception", e);
    411         }
    412 
    413         assertEquals(logger.size(), 1);
    414         assertEquals("ignorableWhitespace", logger.getMethod());
    415         assertEquals(new Object[] { ch, 0, 5 }, logger.getArgs());
    416     }
    417 
    418     public void testProcessingInstruction() {
    419         try {
    420             parent.processingInstruction("foo", "bar");
    421         } catch (SAXException e) {
    422             throw new RuntimeException("Unexpected exception", e);
    423         }
    424 
    425         assertEquals(logger.size(), 1);
    426         assertEquals("processingInstruction", logger.getMethod());
    427         assertEquals(new Object[] { "foo", "bar" }, logger.getArgs());
    428     }
    429 
    430     public void testSkippedEntity() {
    431         try {
    432             parent.skippedEntity("foo");
    433         } catch (SAXException e) {
    434             throw new RuntimeException("Unexpected exception", e);
    435         }
    436 
    437         assertEquals(logger.size(), 1);
    438         assertEquals("skippedEntity", logger.getMethod());
    439         assertEquals(new Object[] { "foo" }, logger.getArgs());
    440     }
    441 
    442     public void testWarning() {
    443         SAXParseException exception = new SAXParseException("Oops!", null);
    444 
    445         try {
    446             parent.warning(exception);
    447         } catch (SAXException e) {
    448             throw new RuntimeException("Unexpected exception", e);
    449         }
    450 
    451         assertEquals(logger.size(), 1);
    452         assertEquals("warning", logger.getMethod());
    453         assertEquals(new Object[] { exception }, logger.getArgs());
    454     }
    455 
    456     public void testError() {
    457         SAXParseException exception = new SAXParseException("Oops!", null);
    458 
    459         try {
    460             parent.error(exception);
    461         } catch (SAXException e) {
    462             throw new RuntimeException("Unexpected exception", e);
    463         }
    464 
    465         assertEquals(logger.size(), 1);
    466         assertEquals("error", logger.getMethod());
    467         assertEquals(new Object[] { exception }, logger.getArgs());
    468     }
    469 
    470     public void testFatalError() {
    471         SAXParseException exception = new SAXParseException("Oops!", null);
    472 
    473         try {
    474             parent.fatalError(exception);
    475         } catch (SAXException e) {
    476             throw new RuntimeException("Unexpected exception", e);
    477         }
    478 
    479         assertEquals(logger.size(), 1);
    480         assertEquals("fatalError", logger.getMethod());
    481         assertEquals(new Object[] { exception }, logger.getArgs());
    482     }
    483 
    484 }
    485