Home | History | Annotate | Download | only in io
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  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 package org.apache.harmony.tests.java.io;
     17 
     18 import java.io.IOException;
     19 import java.io.Writer;
     20 
     21 import junit.framework.TestCase;
     22 
     23 public class WriterTest extends TestCase {
     24 
     25     /**
     26      * java.io.Writer#append(char)
     27      */
     28     public void test_appendChar() throws IOException {
     29         char testChar = ' ';
     30         MockWriter writer = new MockWriter(20);
     31         writer.append(testChar);
     32         assertEquals(String.valueOf(testChar), String.valueOf(writer
     33                 .getContents()));
     34         writer.close();
     35     }
     36 
     37     /**
     38      * java.io.Writer#append(CharSequence)
     39      */
     40     public void test_appendCharSequence() throws IOException {
     41         String testString = "My Test String";
     42         MockWriter writer = new MockWriter(20);
     43         writer.append(testString);
     44         assertEquals(testString, String.valueOf(writer.getContents()));
     45         writer.close();
     46 
     47     }
     48 
     49     /**
     50      * java.io.Writer#append(CharSequence, int, int)
     51      */
     52     public void test_appendCharSequenceIntInt() throws IOException {
     53         String testString = "My Test String";
     54         MockWriter writer = new MockWriter(20);
     55         writer.append(testString, 1, 3);
     56         assertEquals(testString.substring(1, 3), String.valueOf(writer
     57                 .getContents()));
     58         writer.close();
     59 
     60     }
     61 
     62 
     63     /**
     64      * java.io.Writer#write(String)
     65      */
     66     public void test_writeLjava_lang_String() throws IOException {
     67         // Regression for HARMONY-51
     68         Object lock = new Object();
     69         Writer wr = new MockLockWriter(lock);
     70         wr.write("Some string");
     71         wr.close();
     72     }
     73 
     74     class MockLockWriter extends Writer {
     75         final Object myLock;
     76 
     77         MockLockWriter(Object lock) {
     78             super(lock);
     79             myLock = lock;
     80         }
     81 
     82         @Override
     83         public synchronized void close() throws IOException {
     84             // do nothing
     85         }
     86 
     87         @Override
     88         public synchronized void flush() throws IOException {
     89             // do nothing
     90         }
     91 
     92         @Override
     93         public void write(char[] arg0, int arg1, int arg2) throws IOException {
     94             assertTrue(Thread.holdsLock(myLock));
     95         }
     96     }
     97 
     98 
     99     class MockWriter extends Writer {
    100         private char[] contents;
    101 
    102         private int length;
    103 
    104         private int offset;
    105 
    106         MockWriter(int capacity) {
    107             contents = new char[capacity];
    108             length = capacity;
    109             offset = 0;
    110         }
    111 
    112         public synchronized void close() throws IOException {
    113             flush();
    114             contents = null;
    115         }
    116 
    117         public synchronized void flush() throws IOException {
    118             // do nothing
    119         }
    120 
    121         public void write(char[] buffer, int offset, int count)
    122                 throws IOException {
    123             if (null == contents) {
    124                 throw new IOException();
    125             }
    126             if (offset < 0 || count < 0 || offset >= buffer.length) {
    127                 throw new IndexOutOfBoundsException();
    128             }
    129             count = Math.min(count, buffer.length - offset);
    130             count = Math.min(count, this.length - this.offset);
    131             for (int i = 0; i < count; i++) {
    132                 contents[this.offset + i] = buffer[offset + i];
    133             }
    134             this.offset += count;
    135 
    136         }
    137 
    138         public char[] getContents() {
    139             char[] result = new char[offset];
    140             for (int i = 0; i < offset; i++) {
    141                 result[i] = contents[i];
    142             }
    143             return result;
    144         }
    145     }
    146 
    147 }
    148