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 package test.java.util.Scanner; 32 33 import java.io.File; 34 import java.io.FileInputStream; 35 import java.io.FileOutputStream; 36 import java.io.FileNotFoundException; 37 import java.io.IOException; 38 import java.nio.charset.Charset; 39 import java.nio.file.Files; 40 import java.util.Scanner; 41 42 public class FailingConstructors { 43 static final String fileName = "FailingConstructorsTest"; 44 static final String UNSUPPORTED_CHARSET = "unknownCharset"; 45 static final String FILE_CONTENTS = "This is a small file!"; 46 realMain(String[] args)47 private static void realMain(String[] args) throws Throwable { 48 test(false, new File(fileName)); 49 50 /* create the file and write its contents */ 51 File file = File.createTempFile(fileName, null); 52 file.deleteOnExit(); 53 Files.write(file.toPath(), FILE_CONTENTS.getBytes()); 54 55 test(true, file); 56 file.delete(); 57 } 58 test(boolean exists, File file)59 private static void test(boolean exists, File file) throws Throwable { 60 /* Scanner(File source, String charsetName) */ 61 try { 62 new Scanner(file, UNSUPPORTED_CHARSET); 63 fail(); 64 } catch(FileNotFoundException|IllegalArgumentException e) { 65 pass(); 66 } 67 68 check(exists, file); 69 70 try { 71 new Scanner(file, (String)null); 72 fail(); 73 // Android-changed: on Android this constructor throws IAE, not NPE. 74 } catch(FileNotFoundException|IllegalArgumentException e) { 75 pass(); 76 } 77 78 try { 79 new Scanner(file, (Charset)null); 80 fail(); 81 } catch(FileNotFoundException|NullPointerException e) { 82 pass(); 83 } 84 85 check(exists, file); 86 87 /* Scanner(FileRef source, String charsetName) */ 88 try { 89 new Scanner(file.toPath(), UNSUPPORTED_CHARSET); 90 fail(); 91 } catch(FileNotFoundException|IllegalArgumentException e) { 92 pass(); 93 } 94 95 check(exists, file); 96 97 try { 98 new Scanner(file.toPath(), (String)null); 99 fail(); 100 } catch(FileNotFoundException|NullPointerException e) { 101 pass(); 102 } 103 104 try { 105 new Scanner(file.toPath(), (Charset)null); 106 fail(); 107 } catch(FileNotFoundException|NullPointerException e) { 108 pass(); 109 } 110 111 check(exists, file); 112 } 113 check(boolean exists, File file)114 private static void check(boolean exists, File file) { 115 if (exists) { 116 /* the file should be unchanged */ 117 verifyContents(file); 118 } else { 119 /* the file should not have been created */ 120 if (file.exists()) { fail(file + " should not have been created"); } 121 } 122 } 123 verifyContents(File file)124 private static void verifyContents(File file) { 125 try (FileInputStream fis = new FileInputStream(file)) { 126 byte[] contents = FILE_CONTENTS.getBytes(); 127 int read, count = 0; 128 while ((read = fis.read()) != -1) { 129 if (read != contents[count++]) { 130 fail("file contents have been altered"); 131 return; 132 } 133 } 134 } catch (IOException ioe) { 135 unexpected(ioe); 136 } 137 } 138 139 //--------------------- Infrastructure --------------------------- 140 static volatile int passed = 0, failed = 0; pass()141 static void pass() {passed++;} fail()142 static void fail() {failed++; Thread.dumpStack();} fail(String message)143 static void fail(String message) {System.out.println(message); fail(); } unexpected(Throwable t)144 static void unexpected(Throwable t) {failed++; t.printStackTrace();} main(String[] args)145 public static void main(String[] args) throws Throwable { 146 try {realMain(args);} catch (Throwable t) {unexpected(t);} 147 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 148 if (failed > 0) throw new AssertionError("Some tests failed");} 149 } 150