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 tests.api.java.io; 19 20import java.io.BufferedWriter; 21import java.io.File; 22import java.io.FileInputStream; 23import java.io.FileNotFoundException; 24import java.io.FileReader; 25import java.io.FileWriter; 26 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetClass; 29import dalvik.annotation.TestTargetNew; 30 31@TestTargetClass(FileReader.class) 32public class FileReaderTest extends junit.framework.TestCase { 33 34 FileReader br; 35 36 BufferedWriter bw; 37 38 FileInputStream fis; 39 40 File f; 41 42 /** 43 * @tests java.io.FileReader#FileReader(java.io.File) 44 */ 45 @TestTargetNew( 46 level = TestLevel.COMPLETE, 47 method = "FileReader", 48 args = {java.io.File.class} 49 ) 50 public void test_ConstructorLjava_io_File() { 51 // Test for method java.io.FileReader(java.io.File) 52 try { 53 bw = new BufferedWriter(new FileWriter(f.getPath())); 54 bw.write(" After test string", 0, 18); 55 bw.close(); 56 br = new FileReader(f); 57 char[] buf = new char[100]; 58 int r = br.read(buf); 59 br.close(); 60 assertEquals("Test 1: Failed to read correct chars", 61 " After test string", new String(buf, 0, r)); 62 } catch (Exception e) { 63 fail("Exception during Constructor test " + e.toString()); 64 } 65 66 File noFile = new File(System.getProperty("java.io.tmpdir"), "noreader.tst"); 67 try { 68 br = new FileReader(noFile); 69 fail("Test 2: FileNotFoundException expected."); 70 } catch (FileNotFoundException e) { 71 // Expected. 72 } 73 } 74 75 /** 76 * @tests java.io.FileReader#FileReader(java.io.FileDescriptor) 77 */ 78 @TestTargetNew( 79 level = TestLevel.COMPLETE, 80 notes = "Verifies FileReader(java.io.FileDescriptor) constructor.", 81 method = "FileReader", 82 args = {java.io.FileDescriptor.class} 83 ) 84 public void test_ConstructorLjava_io_FileDescriptor() { 85 // Test for method java.io.FileReader(java.io.FileDescriptor) 86 try { 87 bw = new BufferedWriter(new FileWriter(f.getPath())); 88 bw.write(" After test string", 0, 18); 89 bw.close(); 90 FileInputStream fis = new FileInputStream(f.getPath()); 91 br = new FileReader(fis.getFD()); 92 char[] buf = new char[100]; 93 int r = br.read(buf); 94 br.close(); 95 fis.close(); 96 assertEquals("Failed to read correct chars", 97 " After test string", new String(buf, 0, r)); 98 } catch (Exception e) { 99 fail("Exception during Constructor test " + e.toString()); 100 } 101 } 102 103 /** 104 * @tests java.io.FileReader#FileReader(java.lang.String) 105 */ 106 @TestTargetNew( 107 level = TestLevel.COMPLETE, 108 method = "FileReader", 109 args = {java.lang.String.class} 110 ) 111 public void test_ConstructorLjava_lang_String() { 112 // Test for method java.io.FileReader(java.lang.String) 113 try { 114 bw = new BufferedWriter(new FileWriter(f.getPath())); 115 bw.write(" After test string", 0, 18); 116 bw.close(); 117 br = new FileReader(f.getPath()); 118 char[] buf = new char[100]; 119 int r = br.read(buf); 120 br.close(); 121 assertEquals("Test 1: Failed to read correct chars", 122 " After test string", new String(buf, 0, r)); 123 } catch (Exception e) { 124 fail("Exception during Constructor test " + e.toString()); 125 } 126 127 try { 128 br = new FileReader(System.getProperty("java.io.tmpdir") + "/noreader.tst"); 129 fail("Test 2: FileNotFoundException expected."); 130 } catch (FileNotFoundException e) { 131 // Expected. 132 } 133 } 134 135 /** 136 * Sets up the fixture, for example, open a network connection. This method 137 * is called before a test is executed. 138 */ 139 protected void setUp() { 140 141 f = new File(System.getProperty("java.io.tmpdir"), "reader.tst"); 142 143 if (f.exists()) { 144 if (!f.delete()) { 145 fail("Unable to delete test file"); 146 } 147 } 148 } 149 150 /** 151 * Tears down the fixture, for example, close a network connection. This 152 * method is called after a test is executed. 153 */ 154 protected void tearDown() { 155 156 try { 157 bw.close(); 158 br.close(); 159 } catch (Exception e) { 160 } 161 162 try { 163 if (fis != null) 164 fis.close(); 165 } catch (Exception e) { 166 } 167 f.delete(); 168 } 169} 170