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.BufferedReader;
21import java.io.BufferedWriter;
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.FileOutputStream;
25import java.io.FileReader;
26import java.io.FileWriter;
27import java.io.IOException;
28import java.io.InputStreamReader;
29
30import dalvik.annotation.TestLevel;
31import dalvik.annotation.TestTargetClass;
32import dalvik.annotation.TestTargetNew;
33
34@TestTargetClass(FileWriter.class)
35public class FileWriterTest extends junit.framework.TestCase {
36
37    FileWriter fw;
38
39    FileInputStream fis;
40
41    BufferedWriter bw;
42
43    File f;
44
45    FileOutputStream fos;
46
47    BufferedReader br;
48
49    /**
50     * @tests java.io.FileWriter#FileWriter(java.io.File)
51     */
52    @TestTargetNew(
53        level = TestLevel.PARTIAL_COMPLETE,
54        method = "FileWriter",
55        args = {java.io.File.class}
56    )
57    public void test_ConstructorLjava_io_File() {
58        // Test for method java.io.FileWriter(java.io.File)
59        try {
60            fos = new FileOutputStream(f.getPath());
61            fos.write("Test String".getBytes());
62            fos.close();
63            bw = new BufferedWriter(new FileWriter(f));
64            bw.write(" After test string", 0, 18);
65            bw.close();
66            br = new BufferedReader(new FileReader(f.getPath()));
67            char[] buf = new char[100];
68            int r = br.read(buf);
69            br.close();
70            assertEquals("Failed to write correct chars", " After test string", new String(buf, 0, r)
71                    );
72        } catch (Exception e) {
73            fail("Exception during Constructor test " + e.toString());
74        }
75    }
76
77    /**
78     * @tests java.io.FileWriter#FileWriter(java.io.File)
79     */
80    @TestTargetNew(
81        level = TestLevel.PARTIAL_COMPLETE,
82        notes = "Checks IOException.",
83        method = "FileWriter",
84        args = {java.io.File.class}
85    )
86    public void test_ConstructorLjava_io_File_IOException() {
87        File dir = new File(System.getProperty("java.io.tmpdir"));
88
89        try {
90            fw = new FileWriter(dir);
91            fail("Test 1: IOException expected.");
92        } catch (IOException e) {
93            // Expected.
94        }
95    }
96
97    /**
98     * @tests java.io.FileWriter#FileWriter(java.io.File, boolean)
99     */
100    @TestTargetNew(
101        level = TestLevel.PARTIAL_COMPLETE,
102        notes = "Verifies FileWriter(java.io.File, boolean) constructor.",
103        method = "FileWriter",
104        args = {java.io.File.class, boolean.class}
105    )
106    public void test_ConstructorLjava_io_FileZ() throws Exception {
107        // Test for method java.io.FileWriter(java.io.File)
108        //append = true
109        {
110            FileWriter fileWriter = new FileWriter(f);
111
112            String first = "The first string for testing. ";
113            fileWriter.write(first);
114            fileWriter.close();
115
116            fileWriter = new FileWriter(f, true);
117            String second = "The second String for testing.";
118            fileWriter.write(second);
119            fileWriter.close();
120
121            FileReader fileReader = new FileReader(f);
122            char[] out = new char[first.length() + second.length() + 10];
123            int length = fileReader.read(out);
124            fileReader.close();
125            assertEquals(first + second, new String(out, 0, length));
126        }
127        //append = false
128        {
129            FileWriter fileWriter = new FileWriter(f);
130            String first = "The first string for testing. ";
131            fileWriter.write(first);
132            fileWriter.close();
133
134            fileWriter = new FileWriter(f, false);
135            String second = "The second String for testing.";
136            fileWriter.write(second);
137            fileWriter.close();
138
139            FileReader fileReader = new FileReader(f);
140            char[] out = new char[first.length() + second.length() + 10];
141            int length = fileReader.read(out);
142            fileReader.close();
143            assertEquals(second, new String(out, 0, length));
144        }
145    }
146
147    /**
148     * @tests java.io.FileWriter#FileWriter(java.io.File, boolean)
149     */
150    @TestTargetNew(
151        level = TestLevel.PARTIAL_COMPLETE,
152        notes = "Checks IOException.",
153        method = "FileWriter",
154        args = {java.io.File.class, boolean.class}
155    )
156    public void test_ConstructorLjava_io_FileZ_IOException() {
157        File dir = new File(System.getProperty("java.io.tmpdir"));
158
159        try {
160            fw = new FileWriter(dir, true);
161            fail("Test 1: IOException expected.");
162        } catch (IOException e) {
163            // Expected.
164        }
165    }
166
167    /**
168     * @tests java.io.FileWriter#FileWriter(java.io.FileDescriptor)
169     */
170    @TestTargetNew(
171        level = TestLevel.COMPLETE,
172        notes = "Verifies FileWriter(java.io.FileDescriptor) constructor.",
173        method = "FileWriter",
174        args = {java.io.FileDescriptor.class}
175    )
176    public void test_ConstructorLjava_io_FileDescriptor() {
177        // Test for method java.io.FileWriter(java.io.FileDescriptor)
178        try {
179            fos = new FileOutputStream(f.getPath());
180            fos.write("Test String".getBytes());
181            fos.close();
182            fis = new FileInputStream(f.getPath());
183            br = new BufferedReader(new FileReader(fis.getFD()));
184            char[] buf = new char[100];
185            int r = br.read(buf);
186            br.close();
187            fis.close();
188            assertTrue("Failed to write correct chars: "
189                    + new String(buf, 0, r), new String(buf, 0, r)
190                    .equals("Test String"));
191        } catch (Exception e) {
192            fail("Exception during Constructor test " + e.toString());
193        }
194    }
195
196    /**
197     * @tests java.io.FileWriter#FileWriter(java.lang.String)
198     */
199    @TestTargetNew(
200        level = TestLevel.PARTIAL_COMPLETE,
201        method = "FileWriter",
202        args = {java.lang.String.class}
203    )
204    public void test_ConstructorLjava_lang_String() {
205        // Test for method java.io.FileWriter(java.lang.String)
206        try {
207            fos = new FileOutputStream(f.getPath());
208            fos.write("Test String".getBytes());
209            fos.close();
210            bw = new BufferedWriter(new FileWriter(f.getPath()));
211            bw.write(" After test string", 0, 18);
212            bw.close();
213            br = new BufferedReader(new FileReader(f.getPath()));
214            char[] buf = new char[100];
215            int r = br.read(buf);
216            br.close();
217            assertEquals("Failed to write correct chars", " After test string", new String(buf, 0, r)
218                    );
219        } catch (Exception e) {
220            fail("Exception during Constructor test " + e.toString());
221        }
222    }
223
224    /**
225     * @tests java.io.FileWriter#FileWriter(java.lang.String)
226     */
227    @TestTargetNew(
228        level = TestLevel.PARTIAL_COMPLETE,
229        notes = "Checks IOException.",
230        method = "FileWriter",
231        args = {java.lang.String.class}
232    )
233    public void test_ConstructorLjava_lang_String_IOException() {
234        try {
235            fw = new FileWriter(System.getProperty("java.io.tmpdir"));
236            fail("Test 1: IOException expected.");
237        } catch (IOException e) {
238            // Expected.
239        }
240    }
241
242    /**
243     * @tests java.io.FileWriter#FileWriter(java.lang.String, boolean)
244     */
245    @TestTargetNew(
246        level = TestLevel.PARTIAL_COMPLETE,
247        method = "FileWriter",
248        args = {java.lang.String.class, boolean.class}
249    )
250    public void test_ConstructorLjava_lang_StringZ() {
251        // Test for method java.io.FileWriter(java.lang.String, boolean)
252
253        try {
254            fos = new FileOutputStream(f.getPath());
255            fos.write("Test String".getBytes());
256            fos.close();
257            bw = new BufferedWriter(new FileWriter(f.getPath(), true));
258            bw.write(" After test string", 0, 18);
259            bw.close();
260            br = new BufferedReader(new FileReader(f.getPath()));
261            char[] buf = new char[100];
262            int r = br.read(buf);
263            br.close();
264            assertEquals("Failed to append to file", "Test String After test string", new String(buf, 0, r)
265                    );
266
267            fos = new FileOutputStream(f.getPath());
268            fos.write("Test String".getBytes());
269            fos.close();
270            bw = new BufferedWriter(new FileWriter(f.getPath(), false));
271            bw.write(" After test string", 0, 18);
272            bw.close();
273            br = new BufferedReader(new FileReader(f.getPath()));
274            buf = new char[100];
275            r = br.read(buf);
276            br.close();
277            assertEquals("Failed to overwrite file", " After test string", new String(buf, 0, r)
278                    );
279        } catch (Exception e) {
280            fail("Exception during Constructor test " + e.toString());
281        }
282
283    }
284
285    /**
286     * @tests java.io.FileWriter#FileWriter(java.lang.String, boolean)
287     */
288    @TestTargetNew(
289        level = TestLevel.PARTIAL_COMPLETE,
290        notes = "Checks IOException.",
291        method = "FileWriter",
292        args = {java.lang.String.class, boolean.class}
293    )
294    public void test_ConstructorLjava_lang_StringZ_IOException() {
295        try {
296            fw = new FileWriter(System.getProperty("java.io.tmpdir"), false);
297            fail("Test 1: IOException expected.");
298        } catch (IOException e) {
299            // Expected.
300        }
301    }
302
303    @TestTargetNew(
304        level = TestLevel.PARTIAL_COMPLETE,
305        notes = "",
306        method = "write",
307        args = {char[].class, int.class, int.class}
308    )
309    public void test_handleEarlyEOFChar_1() {
310        String str = "All work and no play makes Jack a dull boy\n"; //$NON-NLS-1$
311        int NUMBER = 2048;
312        int j = 0;
313        int len = str.length() * NUMBER;
314        /* == 88064 *//* NUMBER compulsively written copies of the same string */
315        char[] strChars = new char[len];
316        for (int i = 0; i < NUMBER; ++i) {
317            for (int k = 0; k < str.length(); ++k) {
318                strChars[j++] = str.charAt(k);
319            }
320        }
321        File f = null;
322        FileWriter fw = null;
323        try {
324            f = File.createTempFile("ony", "by_one");
325            fw = new FileWriter(f);
326            fw.write(strChars);
327            fw.close();
328            InputStreamReader in = null;
329            FileInputStream fis = new FileInputStream(f);
330            in = new InputStreamReader(fis);
331            int b;
332            int errors = 0;
333            for (int offset = 0; offset < strChars.length; ++offset) {
334                b = in.read();
335                if (b == -1) {
336                    fail("Early EOF at offset " + offset + "\n");
337                    return;
338                }
339            }
340            assertEquals(0, errors);
341        } catch (IOException e) {
342            e.printStackTrace();
343        }
344    }
345
346    @TestTargetNew(
347        level = TestLevel.PARTIAL_COMPLETE,
348        notes = "",
349        method = "write",
350        args = {char[].class, int.class, int.class}
351    )
352    public void test_handleEarlyEOFChar_2() throws IOException {
353        int capacity = 65536;
354        byte[] bytes = new byte[capacity];
355        byte[] bs = {
356                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'
357        };
358        for (int i = 0; i < bytes.length; i++) {
359            bytes[i] = bs[i / 8192];
360        }
361        String inputStr = new String(bytes);
362        int len = inputStr.length();
363        File f = File.createTempFile("FileWriterBugTest ", null); //$NON-NLS-1$
364        FileWriter writer = new FileWriter(f);
365        writer.write(inputStr);
366        writer.close();
367        long flen = f.length();
368
369        FileReader reader = new FileReader(f);
370        char[] outChars = new char[capacity];
371        int outCount = reader.read(outChars);
372        String outStr = new String(outChars, 0, outCount);
373
374        f.deleteOnExit();
375        assertEquals(len, flen);
376        assertEquals(inputStr, outStr);
377    }
378
379
380    /**
381     * Sets up the fixture, for example, open a network connection. This method
382     * is called before a test is executed.
383     */
384    protected void setUp() throws Exception {
385
386        f = File.createTempFile("writer", ".tst");
387
388        if (f.exists())
389            if (!f.delete()) {
390                fail("Unable to delete test file");
391            }
392    }
393
394    /**
395     * Tears down the fixture, for example, close a network connection. This
396     * method is called after a test is executed.
397     */
398    protected void tearDown() {
399        try {
400            bw.close();
401        } catch (Exception e) {
402        }
403        try {
404            fis.close();
405        } catch (Exception e) {
406        }
407        f.delete();
408    }
409}
410