WriterTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 */ 16package tests.api.java.io; 17 18import java.io.IOException; 19import java.io.Writer; 20 21import junit.framework.TestCase; 22 23public class WriterTest extends TestCase { 24 25 /** 26 * @tests 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 * @tests 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 * @tests 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 class MockWriter extends Writer { 63 private char[] contents; 64 65 private int length; 66 67 private int offset; 68 69 MockWriter(int capacity) { 70 contents = new char[capacity]; 71 length = capacity; 72 offset = 0; 73 } 74 75 public synchronized void close() throws IOException { 76 flush(); 77 contents = null; 78 } 79 80 public synchronized void flush() throws IOException { 81 // do nothing 82 } 83 84 public void write(char[] buffer, int offset, int count) 85 throws IOException { 86 if (null == contents) { 87 throw new IOException(); 88 } 89 if (offset < 0 || count < 0 || offset >= buffer.length) { 90 throw new IndexOutOfBoundsException(); 91 } 92 count = Math.min(count, buffer.length - offset); 93 count = Math.min(count, this.length - this.offset); 94 for (int i = 0; i < count; i++) { 95 contents[this.offset + i] = buffer[offset + i]; 96 } 97 this.offset += count; 98 99 } 100 101 public char[] getContents() { 102 char[] result = new char[offset]; 103 for (int i = 0; i < offset; i++) { 104 result[i] = contents[i]; 105 } 106 return result; 107 } 108 } 109 110} 111