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