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.nio.file.Files; 37 import java.util.Scanner; 38 39 public class FailingConstructors { 40 static final String fileName = "FailingConstructorsTest"; 41 static final String UNSUPPORTED_CHARSET = "unknownCharset"; 42 static final String FILE_CONTENTS = "This is a small file!"; 43 44 private static void realMain(String[] args) throws Throwable { 45 test(false, new File(fileName)); 46 47 /* create the file and write its contents */ 48 File file = File.createTempFile(fileName, null); 49 file.deleteOnExit(); 50 Files.write(file.toPath(), FILE_CONTENTS.getBytes()); 51 52 test(true, file); 53 file.delete(); 54 } 55 56 private static void test(boolean exists, File file) throws Throwable { 57 /* Scanner(File source, String charsetName) */ 58 try { 59 new Scanner(file, UNSUPPORTED_CHARSET); 60 fail(); 61 } catch(FileNotFoundException|IllegalArgumentException e) { 62 pass(); 63 } 64 65 check(exists, file); 66 67 try { 68 new Scanner(file, null); 69 fail(); 70 } catch(FileNotFoundException|NullPointerException e) { 71 pass(); 72 } 73 74 check(exists, file); 75 76 /* Scanner(FileRef source, String charsetName) */ 77 try { 78 new Scanner(file.toPath(), UNSUPPORTED_CHARSET); 79 fail(); 80 } catch(FileNotFoundException|IllegalArgumentException e) { 81 pass(); 82 } 83 84 check(exists, file); 85 86 try { 87 new Scanner(file.toPath(), null); 88 fail(); 89 } catch(FileNotFoundException|NullPointerException e) { 90 pass(); 91 } 92 93 check(exists, file); 94 } 95 96 private static void check(boolean exists, File file) { 97 if (exists) { 98 /* the file should be unchanged */ 99 verifyContents(file); 100 } else { 101 /* the file should not have been created */ 102 if (file.exists()) { fail(file + " should not have been created"); } 103 } 104 } 105 106 private static void verifyContents(File file) { 107 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.nio.charset.Charset; 37 import java.nio.file.Files; 38 import java.util.Scanner; 39 40 public class FailingConstructors { 41 static final String fileName = "FailingConstructorsTest"; 42 static final String UNSUPPORTED_CHARSET = "unknownCharset"; 43 static final String FILE_CONTENTS = "This is a small file!"; 44 45 private static void realMain(String[] args) throws Throwable { 46 test(false, new File(fileName)); 47 48 /* create the file and write its contents */ 49 File file = File.createTempFile(fileName, null); 50 file.deleteOnExit(); 51 Files.write(file.toPath(), FILE_CONTENTS.getBytes()); 52 53 test(true, file); 54 file.delete(); 55 } 56 57 private static void test(boolean exists, File file) throws Throwable { 58 /* Scanner(File source, String charsetName) */ 59 try { 60 new Scanner(file, UNSUPPORTED_CHARSET); 61 fail(); 62 } catch(FileNotFoundException|IllegalArgumentException e) { 63 pass(); 64 } 65 66 check(exists, file); 67 68 try { 69 new Scanner(file, (String)null); 70 fail(); 71 } catch(FileNotFoundException|NullPointerException e) { 72 pass(); 73 } 74 75 try { 76 new Scanner(file, (Charset)null); 77 fail(); 78 } catch(FileNotFoundException|NullPointerException e) { 79 pass(); 80 } 81 82 check(exists, file); 83 84 /* Scanner(FileRef source, String charsetName) */ 85 try { 86 new Scanner(file.toPath(), UNSUPPORTED_CHARSET); 87 fail(); 88 } catch(FileNotFoundException|IllegalArgumentException e) { 89 pass(); 90 } 91 92 check(exists, file); 93 94 try { 95 new Scanner(file.toPath(), (String)null); 96 fail(); 97 } catch(FileNotFoundException|NullPointerException e) { 98 pass(); 99 } 100 101 try { 102 new Scanner(file.toPath(), (Charset)null); 103 fail(); 104 } catch(FileNotFoundException|NullPointerException e) { 105 pass(); 106 } 107 108 check(exists, file); 109 } 110 111 private static void check(boolean exists, File file) { 112 if (exists) { 113 /* the file should be unchanged */ 114 verifyContents(file); 115 } else { 116 /* the file should not have been created */ 117 if (file.exists()) { fail(file + " should not have been created"); } 118 } 119 } 120 121 private static void verifyContents(File file) { 122 try (FileInputStream fis = new FileInputStream(file)) { |