3377N/A/*
3377N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3377N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3377N/A *
3377N/A * This code is free software; you can redistribute it and/or modify it
3377N/A * under the terms of the GNU General Public License version 2 only, as
3377N/A * published by the Free Software Foundation.
3377N/A *
3377N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3377N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3377N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3377N/A * version 2 for more details (a copy is included in the LICENSE file that
3377N/A * accompanied this code).
3377N/A *
3377N/A * You should have received a copy of the GNU General Public License version
3377N/A * 2 along with this work; if not, write to the Free Software Foundation,
3377N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3377N/A *
3377N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3377N/A * or visit www.oracle.com if you need additional information or have any
3377N/A * questions.
3377N/A */
3377N/A
3377N/A/**
3377N/A * @test
3377N/A * @bug 7000511
3377N/A * @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
3377N/A * exception thrown
3377N/A */
3377N/A
3377N/Aimport java.io.File;
3377N/Aimport java.io.FileInputStream;
3377N/Aimport java.io.FileOutputStream;
3377N/Aimport java.io.FileNotFoundException;
3377N/Aimport java.io.IOException;
3646N/Aimport java.nio.file.Files;
3377N/Aimport java.util.Scanner;
3377N/A
3377N/Apublic class FailingConstructors {
3377N/A static final String fileName = "FailingConstructorsTest";
3377N/A static final String UNSUPPORTED_CHARSET = "unknownCharset";
3377N/A static final String FILE_CONTENTS = "This is a small file!";
3377N/A
3377N/A private static void realMain(String[] args) throws Throwable {
3377N/A test(false, new File(fileName));
3377N/A
3377N/A /* create the file and write its contents */
3377N/A File file = File.createTempFile(fileName, null);
3377N/A file.deleteOnExit();
3646N/A Files.write(file.toPath(), FILE_CONTENTS.getBytes());
3377N/A
3377N/A test(true, file);
3377N/A file.delete();
3377N/A }
3377N/A
3377N/A private static void test(boolean exists, File file) throws Throwable {
3377N/A /* Scanner(File source, String charsetName) */
3377N/A try {
3377N/A new Scanner(file, UNSUPPORTED_CHARSET);
3377N/A fail();
3377N/A } catch(FileNotFoundException|IllegalArgumentException e) {
3377N/A pass();
3377N/A }
3377N/A
3377N/A check(exists, file);
3377N/A
3377N/A try {
3377N/A new Scanner(file, null);
3377N/A fail();
3377N/A } catch(FileNotFoundException|NullPointerException e) {
3377N/A pass();
3377N/A }
3377N/A
3377N/A check(exists, file);
3377N/A
3377N/A /* Scanner(FileRef source, String charsetName) */
3377N/A try {
3377N/A new Scanner(file.toPath(), UNSUPPORTED_CHARSET);
3377N/A fail();
3377N/A } catch(FileNotFoundException|IllegalArgumentException e) {
3377N/A pass();
3377N/A }
3377N/A
3377N/A check(exists, file);
3377N/A
3377N/A try {
3377N/A new Scanner(file.toPath(), null);
3377N/A fail();
3377N/A } catch(FileNotFoundException|NullPointerException e) {
3377N/A pass();
3377N/A }
3377N/A
3377N/A check(exists, file);
3377N/A }
3377N/A
3377N/A private static void check(boolean exists, File file) {
3377N/A if (exists) {
3377N/A /* the file should be unchanged */
3377N/A verifyContents(file);
3377N/A } else {
3377N/A /* the file should not have been created */
3377N/A if (file.exists()) { fail(file + " should not have been created"); }
3377N/A }
3377N/A }
3377N/A
3377N/A private static void verifyContents(File file) {
3377N/A try (FileInputStream fis = new FileInputStream(file)) {
3377N/A byte[] contents = FILE_CONTENTS.getBytes();
3377N/A int read, count = 0;
3377N/A while ((read = fis.read()) != -1) {
3377N/A if (read != contents[count++]) {
3377N/A fail("file contents have been altered");
3377N/A return;
3377N/A }
3377N/A }
3377N/A } catch (IOException ioe) {
3377N/A unexpected(ioe);
3377N/A }
3377N/A }
3377N/A
3377N/A //--------------------- Infrastructure ---------------------------
3377N/A static volatile int passed = 0, failed = 0;
3377N/A static void pass() {passed++;}
3377N/A static void fail() {failed++; Thread.dumpStack();}
3377N/A static void fail(String message) {System.out.println(message); fail(); }
3377N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
3377N/A public static void main(String[] args) throws Throwable {
3377N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
3377N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
3377N/A if (failed > 0) throw new AssertionError("Some tests failed");}
3377N/A}