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 18package org.apache.harmony.tests.java.io; 19 20import java.io.BufferedWriter; 21import java.io.File; 22import java.io.FileInputStream; 23import java.io.FileReader; 24import java.io.FileWriter; 25import java.io.IOException; 26 27import junit.framework.TestCase; 28 29public class FileReaderTest extends TestCase { 30 31 FileReader br; 32 33 BufferedWriter bw; 34 35 FileInputStream fis; 36 37 File f; 38 39 /** 40 * java.io.FileReader#FileReader(java.io.File) 41 */ 42 public void test_ConstructorLjava_io_File() throws IOException { 43 bw = new BufferedWriter(new FileWriter(f.getPath())); 44 bw.write(" After test string", 0, 18); 45 bw.close(); 46 br = new FileReader(f); 47 char[] buf = new char[100]; 48 int r = br.read(buf); 49 br.close(); 50 assertEquals("Failed to read correct chars", " After test string", 51 new String(buf, 0, r)); 52 } 53 54 /** 55 * java.io.FileReader#FileReader(java.io.FileDescriptor) 56 */ 57 public void test_ConstructorLjava_io_FileDescriptor() throws IOException { 58 bw = new BufferedWriter(new FileWriter(f.getPath())); 59 bw.write(" After test string", 0, 18); 60 bw.close(); 61 FileInputStream fis = new FileInputStream(f.getPath()); 62 br = new FileReader(fis.getFD()); 63 char[] buf = new char[100]; 64 int r = br.read(buf); 65 br.close(); 66 fis.close(); 67 assertEquals("Failed to read correct chars", " After test string", 68 new String(buf, 0, r)); 69 } 70 71 /** 72 * java.io.FileReader#FileReader(java.lang.String) 73 */ 74 public void test_ConstructorLjava_lang_String() throws IOException { 75 bw = new BufferedWriter(new FileWriter(f.getPath())); 76 bw.write(" After test string", 0, 18); 77 bw.close(); 78 br = new FileReader(f.getPath()); 79 char[] buf = new char[100]; 80 int r = br.read(buf); 81 br.close(); 82 assertEquals("Failed to read correct chars", " After test string", 83 new String(buf, 0, r)); 84 } 85 86 /** 87 * Sets up the fixture, for example, open a network connection. This method 88 * is called before a test is executed. 89 */ 90 protected void setUp() throws IOException { 91 f = File.createTempFile("FileReaderTest", "tst"); 92 } 93 94 /** 95 * Tears down the fixture, for example, close a network connection. This 96 * method is called after a test is executed. 97 */ 98 protected void tearDown() { 99 try { 100 bw.close(); 101 } catch (Exception e) { 102 // Ignore 103 } 104 try { 105 br.close(); 106 } catch (Exception e) { 107 // Ignore 108 } 109 try { 110 if (fis != null) { 111 fis.close(); 112 } 113 } catch (Exception e) { 114 // Ignore 115 } 116 f.delete(); 117 } 118} 119