001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.io;
019:
020: import java.io.BufferedWriter;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileReader;
024: import java.io.FileWriter;
025: import java.io.IOException;
026:
027: import junit.framework.TestCase;
028:
029: public class FileReaderTest extends TestCase {
030:
031: FileReader br;
032:
033: BufferedWriter bw;
034:
035: FileInputStream fis;
036:
037: File f;
038:
039: /**
040: * @tests java.io.FileReader#FileReader(java.io.File)
041: */
042: public void test_ConstructorLjava_io_File() throws IOException {
043: bw = new BufferedWriter(new FileWriter(f.getPath()));
044: bw.write(" After test string", 0, 18);
045: bw.close();
046: br = new FileReader(f);
047: char[] buf = new char[100];
048: int r = br.read(buf);
049: br.close();
050: assertEquals("Failed to read correct chars",
051: " After test string", new String(buf, 0, r));
052: }
053:
054: /**
055: * @tests java.io.FileReader#FileReader(java.io.FileDescriptor)
056: */
057: public void test_ConstructorLjava_io_FileDescriptor()
058: throws IOException {
059: bw = new BufferedWriter(new FileWriter(f.getPath()));
060: bw.write(" After test string", 0, 18);
061: bw.close();
062: FileInputStream fis = new FileInputStream(f.getPath());
063: br = new FileReader(fis.getFD());
064: char[] buf = new char[100];
065: int r = br.read(buf);
066: br.close();
067: fis.close();
068: assertEquals("Failed to read correct chars",
069: " After test string", new String(buf, 0, r));
070: }
071:
072: /**
073: * @tests java.io.FileReader#FileReader(java.lang.String)
074: */
075: public void test_ConstructorLjava_lang_String() throws IOException {
076: bw = new BufferedWriter(new FileWriter(f.getPath()));
077: bw.write(" After test string", 0, 18);
078: bw.close();
079: br = new FileReader(f.getPath());
080: char[] buf = new char[100];
081: int r = br.read(buf);
082: br.close();
083: assertEquals("Failed to read correct chars",
084: " After test string", new String(buf, 0, r));
085: }
086:
087: /**
088: * Sets up the fixture, for example, open a network connection. This method
089: * is called before a test is executed.
090: */
091: protected void setUp() {
092: f = new File(System.getProperty("user.home"), "reader.tst");
093: if (f.exists()) {
094: if (!f.delete()) {
095: fail("Unable to delete test file");
096: }
097: }
098: }
099:
100: /**
101: * Tears down the fixture, for example, close a network connection. This
102: * method is called after a test is executed.
103: */
104: protected void tearDown() {
105: try {
106: bw.close();
107: } catch (Exception e) {
108: // Ignore
109: }
110: try {
111: br.close();
112: } catch (Exception e) {
113: // Ignore
114: }
115: try {
116: if (fis != null) {
117: fis.close();
118: }
119: } catch (Exception e) {
120: // Ignore
121: }
122: f.delete();
123: }
124: }
|