Path: blob/master/test/jdk/java/util/Scanner/FailingConstructors.java
41149 views
/*1* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 7000511 819057726* @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when27* exception thrown28*/2930import java.io.File;31import java.io.FileInputStream;32import java.io.FileOutputStream;33import java.io.FileNotFoundException;34import java.io.IOException;35import java.nio.charset.Charset;36import java.nio.file.Files;37import java.util.Scanner;3839public class FailingConstructors {40static final String fileName = "FailingConstructorsTest";41static final String UNSUPPORTED_CHARSET = "unknownCharset";42static final String FILE_CONTENTS = "This is a small file!";4344private static void realMain(String[] args) throws Throwable {45test(false, new File(fileName));4647/* create the file and write its contents */48File file = File.createTempFile(fileName, null);49file.deleteOnExit();50Files.write(file.toPath(), FILE_CONTENTS.getBytes());5152test(true, file);53file.delete();54}5556private static void test(boolean exists, File file) throws Throwable {57/* Scanner(File source, String charsetName) */58try {59new Scanner(file, UNSUPPORTED_CHARSET);60fail();61} catch(FileNotFoundException|IllegalArgumentException e) {62pass();63}6465check(exists, file);6667try {68new Scanner(file, (String)null);69fail();70} catch(FileNotFoundException|NullPointerException e) {71pass();72}7374try {75new Scanner(file, (Charset)null);76fail();77} catch(FileNotFoundException|NullPointerException e) {78pass();79}8081check(exists, file);8283/* Scanner(FileRef source, String charsetName) */84try {85new Scanner(file.toPath(), UNSUPPORTED_CHARSET);86fail();87} catch(FileNotFoundException|IllegalArgumentException e) {88pass();89}9091check(exists, file);9293try {94new Scanner(file.toPath(), (String)null);95fail();96} catch(FileNotFoundException|NullPointerException e) {97pass();98}99100try {101new Scanner(file.toPath(), (Charset)null);102fail();103} catch(FileNotFoundException|NullPointerException e) {104pass();105}106107check(exists, file);108}109110private static void check(boolean exists, File file) {111if (exists) {112/* the file should be unchanged */113verifyContents(file);114} else {115/* the file should not have been created */116if (file.exists()) { fail(file + " should not have been created"); }117}118}119120private static void verifyContents(File file) {121try (FileInputStream fis = new FileInputStream(file)) {122byte[] contents = FILE_CONTENTS.getBytes();123int read, count = 0;124while ((read = fis.read()) != -1) {125if (read != contents[count++]) {126fail("file contents have been altered");127return;128}129}130} catch (IOException ioe) {131unexpected(ioe);132}133}134135//--------------------- Infrastructure ---------------------------136static volatile int passed = 0, failed = 0;137static void pass() {passed++;}138static void fail() {failed++; Thread.dumpStack();}139static void fail(String message) {System.out.println(message); fail(); }140static void unexpected(Throwable t) {failed++; t.printStackTrace();}141public static void main(String[] args) throws Throwable {142try {realMain(args);} catch (Throwable t) {unexpected(t);}143System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);144if (failed > 0) throw new AssertionError("Some tests failed");}145}146147148