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.BufferedReader;
021: import java.io.BufferedWriter;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileOutputStream;
025: import java.io.FileReader;
026: import java.io.FileWriter;
027: import java.io.IOException;
028:
029: import junit.framework.TestCase;
030:
031: public class FileWriterTest extends TestCase {
032:
033: FileWriter fw;
034:
035: FileInputStream fis;
036:
037: BufferedWriter bw;
038:
039: File f;
040:
041: FileOutputStream fos;
042:
043: BufferedReader br;
044:
045: /**
046: * @tests java.io.FileWriter#FileWriter(java.io.File)
047: */
048: public void test_ConstructorLjava_io_File() throws IOException {
049: fos = new FileOutputStream(f.getPath());
050: fos.write("Test String".getBytes());
051: fos.close();
052: bw = new BufferedWriter(new FileWriter(f));
053: bw.write(" After test string", 0, 18);
054: bw.close();
055: br = new BufferedReader(new FileReader(f.getPath()));
056: char[] buf = new char[100];
057: int r = br.read(buf);
058: br.close();
059: assertEquals("Failed to write correct chars",
060: " After test string", new String(buf, 0, r));
061: }
062:
063: /**
064: * @tests java.io.FileWriter#FileWriter(java.io.File, boolean)
065: */
066: public void test_ConstructorLjava_io_FileZ() throws IOException {
067: FileWriter fileWriter = new FileWriter(f);
068:
069: String first = "The first string for testing. ";
070: fileWriter.write(first);
071: fileWriter.close();
072:
073: fileWriter = new FileWriter(f, true);
074: String second = "The second String for testing.";
075: fileWriter.write(second);
076: fileWriter.close();
077:
078: FileReader fileReader = new FileReader(f);
079: char[] out = new char[first.length() + second.length() + 10];
080: int length = fileReader.read(out);
081: fileReader.close();
082: assertEquals(first + second, new String(out, 0, length));
083:
084: fileWriter = new FileWriter(f);
085: first = "The first string for testing. ";
086: fileWriter.write(first);
087: fileWriter.close();
088:
089: fileWriter = new FileWriter(f, false);
090: second = "The second String for testing.";
091: fileWriter.write(second);
092: fileWriter.close();
093:
094: fileReader = new FileReader(f);
095: out = new char[first.length() + second.length() + 10];
096: length = fileReader.read(out);
097: fileReader.close();
098: assertEquals(second, new String(out, 0, length));
099: }
100:
101: /**
102: * @tests java.io.FileWriter#FileWriter(java.io.FileDescriptor)
103: */
104: public void test_ConstructorLjava_io_FileDescriptor()
105: throws IOException {
106: fos = new FileOutputStream(f.getPath());
107: fos.write("Test String".getBytes());
108: fos.close();
109: fis = new FileInputStream(f.getPath());
110: br = new BufferedReader(new FileReader(fis.getFD()));
111: char[] buf = new char[100];
112: int r = br.read(buf);
113: br.close();
114: fis.close();
115: assertTrue("Failed to write correct chars: "
116: + new String(buf, 0, r), new String(buf, 0, r)
117: .equals("Test String"));
118: }
119:
120: /**
121: * @tests java.io.FileWriter#FileWriter(java.lang.String)
122: */
123: public void test_ConstructorLjava_lang_String() throws IOException {
124: fos = new FileOutputStream(f.getPath());
125: fos.write("Test String".getBytes());
126: fos.close();
127: bw = new BufferedWriter(new FileWriter(f.getPath()));
128: bw.write(" After test string", 0, 18);
129: bw.close();
130: br = new BufferedReader(new FileReader(f.getPath()));
131: char[] buf = new char[100];
132: int r = br.read(buf);
133: br.close();
134: assertEquals("Failed to write correct chars",
135: " After test string", new String(buf, 0, r));
136: }
137:
138: /**
139: * @tests java.io.FileWriter#FileWriter(java.lang.String, boolean)
140: */
141: public void test_ConstructorLjava_lang_StringZ() throws IOException {
142: fos = new FileOutputStream(f.getPath());
143: fos.write("Test String".getBytes());
144: fos.close();
145: bw = new BufferedWriter(new FileWriter(f.getPath(), true));
146: bw.write(" After test string", 0, 18);
147: bw.close();
148: br = new BufferedReader(new FileReader(f.getPath()));
149: char[] buf = new char[100];
150: int r = br.read(buf);
151: br.close();
152: assertEquals("Failed to append to file",
153: "Test String After test string", new String(buf, 0, r));
154:
155: fos = new FileOutputStream(f.getPath());
156: fos.write("Test String".getBytes());
157: fos.close();
158: bw = new BufferedWriter(new FileWriter(f.getPath(), false));
159: bw.write(" After test string", 0, 18);
160: bw.close();
161: br = new BufferedReader(new FileReader(f.getPath()));
162: buf = new char[100];
163: r = br.read(buf);
164: br.close();
165: assertEquals("Failed to overwrite file", " After test string",
166: new String(buf, 0, r));
167: }
168:
169: /**
170: * Sets up the fixture, for example, open a network connection. This method
171: * is called before a test is executed.
172: */
173: @Override
174: protected void setUp() {
175: f = new File(System.getProperty("user.home"), "writer.tst");
176: if (f.exists()) {
177: if (!f.delete()) {
178: fail("Unable to delete test file");
179: }
180: }
181: }
182:
183: /**
184: * Tears down the fixture, for example, close a network connection. This
185: * method is called after a test is executed.
186: */
187: @Override
188: protected void tearDown() {
189: try {
190: bw.close();
191: } catch (Exception e) {
192: }
193: try {
194: fis.close();
195: } catch (Exception e) {
196: }
197: f.delete();
198: }
199: }
|