Home | History | Annotate | Download | only in io
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.tests.java.io;
     19 
     20 import java.io.IOException;
     21 import java.io.StringReader;
     22 
     23 public class StringReaderTest extends junit.framework.TestCase {
     24 
     25     String testString = "This is a test string";
     26 
     27     StringReader sr;
     28 
     29     /**
     30      * java.io.StringReader#StringReader(java.lang.String)
     31      */
     32     public void test_ConstructorLjava_lang_String() {
     33         // Test for method java.io.StringReader(java.lang.String)
     34         assertTrue("Used in tests", true);
     35     }
     36 
     37     /**
     38      * java.io.StringReader#close()
     39      */
     40     public void test_close() throws Exception {
     41         // Test for method void java.io.StringReader.close()
     42         try {
     43             sr = new StringReader(testString);
     44             sr.close();
     45             char[] buf = new char[10];
     46             sr.read(buf, 0, 2);
     47             fail("Close failed");
     48         } catch (java.io.IOException e) {
     49             return;
     50         }
     51     }
     52 
     53     /**
     54      * java.io.StringReader#mark(int)
     55      */
     56     public void test_markI() throws Exception {
     57         // Test for method void java.io.StringReader.mark(int)
     58         sr = new StringReader(testString);
     59         sr.skip(5);
     60         sr.mark(0);
     61         sr.skip(5);
     62         sr.reset();
     63         char[] buf = new char[10];
     64         sr.read(buf, 0, 2);
     65         assertTrue("Failed to return to mark", new String(buf, 0, 2)
     66                 .equals(testString.substring(5, 7)));
     67     }
     68 
     69     /**
     70      * java.io.StringReader#markSupported()
     71      */
     72     public void test_markSupported() {
     73         // Test for method boolean java.io.StringReader.markSupported()
     74 
     75         sr = new StringReader(testString);
     76         assertTrue("markSupported returned false", sr.markSupported());
     77     }
     78 
     79     /**
     80      * java.io.StringReader#read()
     81      */
     82     public void test_read() throws Exception {
     83         // Test for method int java.io.StringReader.read()
     84         sr = new StringReader(testString);
     85         int r = sr.read();
     86         assertEquals("Failed to read char", 'T', r);
     87         sr = new StringReader(new String(new char[] { '\u8765' }));
     88         assertTrue("Wrong double byte char", sr.read() == '\u8765');
     89     }
     90 
     91     /**
     92      * java.io.StringReader#read(char[], int, int)
     93      */
     94     public void test_read$CII() throws Exception {
     95         // Test for method int java.io.StringReader.read(char [], int, int)
     96         sr = new StringReader(testString);
     97         char[] buf = new char[testString.length()];
     98         int r = sr.read(buf, 0, testString.length());
     99         assertTrue("Failed to read chars", r == testString.length());
    100         assertTrue("Read chars incorrectly", new String(buf, 0, r)
    101                 .equals(testString));
    102     }
    103 
    104     /**
    105      * java.io.StringReader#ready()
    106      */
    107     public void test_ready() throws Exception {
    108         // Test for method boolean java.io.StringReader.ready()
    109         sr = new StringReader(testString);
    110         assertTrue("Steam not ready", sr.ready());
    111         sr.close();
    112         int r = 0;
    113         try {
    114             sr.ready();
    115         } catch (IOException e) {
    116             r = 1;
    117         }
    118         assertEquals("Expected IOException not thrown in read()", 1, r);
    119     }
    120 
    121     /**
    122      * java.io.StringReader#reset()
    123      */
    124     public void test_reset() throws Exception {
    125         // Test for method void java.io.StringReader.reset()
    126         sr = new StringReader(testString);
    127         sr.skip(5);
    128         sr.mark(0);
    129         sr.skip(5);
    130         sr.reset();
    131         char[] buf = new char[10];
    132         sr.read(buf, 0, 2);
    133         assertTrue("Failed to reset properly", new String(buf, 0, 2)
    134                 .equals(testString.substring(5, 7)));
    135     }
    136 
    137     /**
    138      * java.io.StringReader#skip(long)
    139      */
    140     public void test_skipJ() throws Exception {
    141         // Test for method long java.io.StringReader.skip(long)
    142         sr = new StringReader(testString);
    143         sr.skip(5);
    144         char[] buf = new char[10];
    145         sr.read(buf, 0, 2);
    146         assertTrue("Failed to skip properly", new String(buf, 0, 2)
    147                 .equals(testString.substring(5, 7)));
    148     }
    149 
    150     // Regression test for HARMONY-5077
    151     static boolean finish = false;
    152 
    153     public void test_synchronization() {
    154         String anything = "Hello world";
    155         final StringReader sr = new StringReader(anything);
    156         Thread other = new Thread(new Runnable() {
    157             public void run() {
    158                 sr.close();
    159                 finish = true;
    160             }
    161 
    162             ;
    163         });
    164 
    165         synchronized (anything) {
    166             other.start();
    167             while (!finish) {
    168                 Thread.yield();
    169             }
    170         }
    171     }
    172 }
    173