Home | History | Annotate | Download | only in sax
      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;
     18 
     19 import java.io.ByteArrayInputStream;
     20 import java.io.InputStream;
     21 import java.io.Reader;
     22 import java.io.StringReader;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import org.xml.sax.InputSource;
     27 
     28 public class InputSourceTest extends TestCase {
     29 
     30     public void testInputSource() {
     31         InputSource i = new InputSource();
     32 
     33         assertNull(i.getByteStream());
     34         assertNull(i.getCharacterStream());
     35         assertNull(i.getEncoding());
     36         assertNull(i.getPublicId());
     37         assertNull(i.getSystemId());
     38     }
     39 
     40     public void testInputSourceString() {
     41         InputSource i = new InputSource("Foo");
     42 
     43         assertNull(i.getByteStream());
     44         assertNull(i.getCharacterStream());
     45         assertNull(i.getEncoding());
     46         assertNull(i.getPublicId());
     47         assertEquals("Foo", i.getSystemId());
     48     }
     49 
     50     public void testInputSourceInputStream() {
     51         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
     52 
     53         // Ordinary case
     54         InputSource i = new InputSource(bais);
     55 
     56         assertEquals(bais, i.getByteStream());
     57         assertNull(i.getCharacterStream());
     58         assertNull(i.getEncoding());
     59         assertNull(i.getPublicId());
     60         assertNull(i.getSystemId());
     61 
     62         // No input stream
     63         i = new InputSource((InputStream)null);
     64 
     65         assertNull(i.getByteStream());
     66         assertNull(i.getCharacterStream());
     67         assertNull(i.getEncoding());
     68         assertNull(i.getPublicId());
     69         assertNull(i.getSystemId());
     70     }
     71 
     72     public void testInputSourceReader() {
     73         StringReader sr = new StringReader("Hello, world.");
     74 
     75         // Ordinary case
     76         InputSource i = new InputSource(sr);
     77 
     78         assertNull(i.getByteStream());
     79         assertEquals(sr, i.getCharacterStream());
     80         assertNull(i.getEncoding());
     81         assertNull(i.getPublicId());
     82         assertNull(i.getSystemId());
     83 
     84         // No reader
     85         i = new InputSource((Reader)null);
     86 
     87         assertNull(i.getByteStream());
     88         assertNull(i.getCharacterStream());
     89         assertNull(i.getEncoding());
     90         assertNull(i.getPublicId());
     91         assertNull(i.getSystemId());
     92     }
     93 
     94     public void testSetPublicIdGetPublicId() {
     95         InputSource i = new InputSource();
     96 
     97         i.setPublicId("Foo");
     98         assertEquals("Foo", i.getPublicId());
     99 
    100         i.setPublicId(null);
    101         assertNull(i.getPublicId());
    102     }
    103 
    104     public void testSetSystemIdGetSystemId() {
    105         InputSource i = new InputSource();
    106 
    107         i.setSystemId("Foo");
    108         assertEquals("Foo", i.getSystemId());
    109 
    110         i.setSystemId(null);
    111         assertNull(i.getSystemId());
    112     }
    113 
    114     public void testSetByteStreamGetByteStream() {
    115         ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
    116 
    117         InputSource i = new InputSource();
    118 
    119         // Ordinary case
    120         i.setByteStream(bais);
    121 
    122         assertEquals(bais, i.getByteStream());
    123 
    124         // No input stream
    125         i.setByteStream(null);
    126 
    127         assertNull(i.getByteStream());
    128     }
    129 
    130     public void testSetEncodingGetEncoding() {
    131         InputSource i = new InputSource();
    132 
    133         // Ordinary case
    134         i.setEncoding("Klingon");
    135 
    136         assertEquals("Klingon", i.getEncoding());
    137 
    138         // No encoding
    139         i.setEncoding(null);
    140 
    141         assertNull(i.getEncoding());
    142     }
    143 
    144     public void testSetCharacterStreamGetCharacterStream() {
    145         StringReader sr = new StringReader("Hello, world.");
    146 
    147         InputSource i = new InputSource();
    148 
    149         // Ordinary case
    150         i.setCharacterStream(sr);
    151 
    152         assertNull(i.getByteStream());
    153         assertEquals(sr, i.getCharacterStream());
    154         assertNull(i.getEncoding());
    155         assertNull(i.getPublicId());
    156         assertNull(i.getSystemId());
    157 
    158         // No reader
    159         i.setCharacterStream(null);
    160 
    161         assertNull(i.getByteStream());
    162         assertNull(i.getCharacterStream());
    163         assertNull(i.getEncoding());
    164         assertNull(i.getPublicId());
    165         assertNull(i.getSystemId());
    166     }
    167 
    168 }
    169