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 libcore.java.io; 19 20import java.io.BufferedWriter; 21import java.io.File; 22import java.io.FileInputStream; 23import java.io.FileReader; 24import java.io.FileWriter; 25import java.io.IOException; 26import java.io.InputStreamReader; 27import junit.framework.TestCase; 28 29public class OldFileWriterTest extends TestCase { 30 31 FileWriter fw; 32 33 FileInputStream fis; 34 35 BufferedWriter bw; 36 37 File f; 38 39 public void test_ConstructorLjava_io_File_IOException() { 40 File dir = new File(System.getProperty("java.io.tmpdir")); 41 42 try { 43 fw = new FileWriter(dir); 44 fail("Test 1: IOException expected."); 45 } catch (IOException e) { 46 // Expected. 47 } 48 } 49 50 public void test_ConstructorLjava_io_FileZ_IOException() { 51 File dir = new File(System.getProperty("java.io.tmpdir")); 52 53 try { 54 fw = new FileWriter(dir, true); 55 fail("Test 1: IOException expected."); 56 } catch (IOException e) { 57 // Expected. 58 } 59 } 60 61 public void test_ConstructorLjava_lang_String_IOException() { 62 try { 63 fw = new FileWriter(System.getProperty("java.io.tmpdir")); 64 fail("Test 1: IOException expected."); 65 } catch (IOException e) { 66 // Expected. 67 } 68 } 69 70 71 public void test_ConstructorLjava_lang_StringZ_IOException() { 72 try { 73 fw = new FileWriter(System.getProperty("java.io.tmpdir"), false); 74 fail("Test 1: IOException expected."); 75 } catch (IOException e) { 76 // Expected. 77 } 78 } 79 80 public void test_handleEarlyEOFChar_1() { 81 String str = "All work and no play makes Jack a dull boy\n"; 82 int NUMBER = 2048; 83 int j = 0; 84 int len = str.length() * NUMBER; 85 /* == 88064 *//* NUMBER compulsively written copies of the same string */ 86 char[] strChars = new char[len]; 87 for (int i = 0; i < NUMBER; ++i) { 88 for (int k = 0; k < str.length(); ++k) { 89 strChars[j++] = str.charAt(k); 90 } 91 } 92 File f = null; 93 FileWriter fw = null; 94 try { 95 f = File.createTempFile("ony", "by_one"); 96 fw = new FileWriter(f); 97 fw.write(strChars); 98 fw.close(); 99 InputStreamReader in = null; 100 FileInputStream fis = new FileInputStream(f); 101 in = new InputStreamReader(fis); 102 int b; 103 int errors = 0; 104 for (int offset = 0; offset < strChars.length; ++offset) { 105 b = in.read(); 106 if (b == -1) { 107 fail("Early EOF at offset " + offset + "\n"); 108 return; 109 } 110 } 111 assertEquals(0, errors); 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } 115 } 116 117 public void test_handleEarlyEOFChar_2() throws IOException { 118 int capacity = 65536; 119 byte[] bytes = new byte[capacity]; 120 byte[] bs = { 121 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' 122 }; 123 for (int i = 0; i < bytes.length; i++) { 124 bytes[i] = bs[i / 8192]; 125 } 126 String inputStr = new String(bytes); 127 int len = inputStr.length(); 128 File f = File.createTempFile("FileWriterBugTest ", null); 129 FileWriter writer = new FileWriter(f); 130 writer.write(inputStr); 131 writer.close(); 132 long flen = f.length(); 133 134 FileReader reader = new FileReader(f); 135 char[] outChars = new char[capacity]; 136 int outCount = reader.read(outChars); 137 String outStr = new String(outChars, 0, outCount); 138 139 f.deleteOnExit(); 140 assertEquals(len, flen); 141 assertEquals(inputStr, outStr); 142 } 143 144 protected void setUp() throws Exception { 145 f = File.createTempFile("writer", ".tst"); 146 147 if (f.exists()) 148 if (!f.delete()) { 149 fail("Unable to delete test file"); 150 } 151 } 152 153 protected void tearDown() { 154 try { 155 bw.close(); 156 } catch (Exception e) { 157 } 158 try { 159 fis.close(); 160 } catch (Exception e) { 161 } 162 f.delete(); 163 } 164} 165