1 /* 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 7000511 27 * @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when 28 * exception thrown 29 */ 30 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.FileOutputStream; 34 import java.io.FileNotFoundException; 35 import java.io.IOException; 36 import java.io.PrintStream; 37 import java.io.UnsupportedEncodingException; 38 import java.nio.file.Files; 39 import java.nio.file.Path; 40 41 public class FailingConstructors { 42 static final String fileName = "FailingConstructorsTest"; 43 static final String UNSUPPORTED_CHARSET = "unknownCharset"; 44 static final String FILE_CONTENTS = "This is a small file!"; 45 46 private static void realMain(String[] args) throws Throwable { 51 try { 52 Files.write(path, FILE_CONTENTS.getBytes()); 53 test(true, path.toFile()); 54 } finally { 55 Files.delete(path); 56 } 57 } 58 59 private static void test(boolean exists, File file) throws Throwable { 60 /* PrintStream(File file, String csn) */ 61 try { 62 new PrintStream(file, UNSUPPORTED_CHARSET); 63 fail(); 64 } catch(FileNotFoundException|UnsupportedEncodingException e) { 65 pass(); 66 } 67 68 check(exists, file); 69 70 try { 71 new PrintStream(file, null); 72 fail(); 73 } catch(FileNotFoundException|NullPointerException e) { 74 pass(); 75 } 76 77 check(exists, file); 78 79 /* PrintStream(String fileName, String csn) */ 80 try { 81 new PrintStream(file.getName(), UNSUPPORTED_CHARSET); 82 fail(); 83 } catch(FileNotFoundException|UnsupportedEncodingException e) { 84 pass(); 85 } 86 87 check(exists, file); 88 89 try { 90 new PrintStream(file.getName(), null); 91 fail(); 92 } catch(FileNotFoundException|NullPointerException e) { 93 pass(); 94 } 95 96 check(exists, file); 97 } 98 99 private static void check(boolean exists, File file) { 100 if (exists) { 101 /* the file should be unchanged */ 102 verifyContents(file); 103 } else { 104 /* the file should not have been created */ 105 if (file.exists()) { fail(file + " should not have been created"); } 106 } 107 } 108 109 private static void verifyContents(File file) { 110 try (FileInputStream fis = new FileInputStream(file)) { | 1 /* 2 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 7000511 8190577 27 * @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when 28 * exception thrown 29 */ 30 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.FileOutputStream; 34 import java.io.FileNotFoundException; 35 import java.io.IOException; 36 import java.io.PrintStream; 37 import java.io.UnsupportedEncodingException; 38 import java.nio.file.Files; 39 import java.nio.file.Path; 40 41 public class FailingConstructors { 42 static final String fileName = "FailingConstructorsTest"; 43 static final String UNSUPPORTED_CHARSET = "unknownCharset"; 44 static final String FILE_CONTENTS = "This is a small file!"; 45 46 private static void realMain(String[] args) throws Throwable { 51 try { 52 Files.write(path, FILE_CONTENTS.getBytes()); 53 test(true, path.toFile()); 54 } finally { 55 Files.delete(path); 56 } 57 } 58 59 private static void test(boolean exists, File file) throws Throwable { 60 /* PrintStream(File file, String csn) */ 61 try { 62 new PrintStream(file, UNSUPPORTED_CHARSET); 63 fail(); 64 } catch(FileNotFoundException|UnsupportedEncodingException e) { 65 pass(); 66 } 67 68 check(exists, file); 69 70 try { 71 new PrintStream(file, (String)null); 72 fail(); 73 } catch(FileNotFoundException|NullPointerException e) { 74 pass(); 75 } 76 77 check(exists, file); 78 79 /* PrintStream(String fileName, String csn) */ 80 try { 81 new PrintStream(file.getName(), UNSUPPORTED_CHARSET); 82 fail(); 83 } catch(FileNotFoundException|UnsupportedEncodingException e) { 84 pass(); 85 } 86 87 check(exists, file); 88 89 try { 90 new PrintStream(file.getName(), (String)null); 91 fail(); 92 } catch(FileNotFoundException|NullPointerException e) { 93 pass(); 94 } 95 96 check(exists, file); 97 } 98 99 private static void check(boolean exists, File file) { 100 if (exists) { 101 /* the file should be unchanged */ 102 verifyContents(file); 103 } else { 104 /* the file should not have been created */ 105 if (file.exists()) { fail(file + " should not have been created"); } 106 } 107 } 108 109 private static void verifyContents(File file) { 110 try (FileInputStream fis = new FileInputStream(file)) { |