Path: blob/master/test/jdk/java/io/PrintWriter/FailingConstructors.java
41149 views
/*1* Copyright (c) 2011, 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 700051126* @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.io.PrintWriter;36import java.io.UnsupportedEncodingException;3738public class FailingConstructors {39static final String fileName = "FailingConstructorsTest";40static final String UNSUPPORTED_CHARSET = "unknownCharset";41static final String FILE_CONTENTS = "This is a small file!";4243private static void realMain(String[] args) throws Throwable {44test(false, new File(fileName));4546/* create the file and write its contents */47File file = File.createTempFile(fileName, null);48file.deleteOnExit();49FileOutputStream fos = new FileOutputStream(file);50fos.write(FILE_CONTENTS.getBytes());51fos.close();5253test(true, file);54file.delete();55}5657private static void test(boolean exists, File file) throws Throwable {58/* PrintWriter(File file, String csn) */59try {60new PrintWriter(file, UNSUPPORTED_CHARSET);61fail();62} catch(FileNotFoundException|UnsupportedEncodingException e) {63pass();64}6566check(exists, file);6768try {69new PrintWriter(file, (String)null);70fail();71} catch(FileNotFoundException|NullPointerException e) {72pass();73}7475check(exists, file);7677/* PrintWriter(String fileName, String csn) */78try {79new PrintWriter(file.getName(), UNSUPPORTED_CHARSET);80fail();81} catch(FileNotFoundException|UnsupportedEncodingException e) {82pass();83}8485check(exists, file);8687try {88new PrintWriter(file.getName(), (String)null);89fail();90} catch(FileNotFoundException|NullPointerException e) {91pass();92}9394check(exists, file);95}9697private static void check(boolean exists, File file) {98if (exists) {99/* the file should be unchanged */100verifyContents(file);101} else {102/* the file should not have been created */103if (file.exists()) { fail(file + " should not have been created"); }104}105}106107private static void verifyContents(File file) {108try (FileInputStream fis = new FileInputStream(file)) {109byte[] contents = FILE_CONTENTS.getBytes();110int read, count = 0;111while ((read = fis.read()) != -1) {112if (read != contents[count++]) {113fail("file contents have been altered");114return;115}116}117} catch (IOException ioe) {118unexpected(ioe);119}120}121122//--------------------- Infrastructure ---------------------------123static volatile int passed = 0, failed = 0;124static void pass() {passed++;}125static void fail() {failed++; Thread.dumpStack();}126static void fail(String message) {System.out.println(message); fail(); }127static void unexpected(Throwable t) {failed++; t.printStackTrace();}128public static void main(String[] args) throws Throwable {129try {realMain(args);} catch (Throwable t) {unexpected(t);}130System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);131if (failed > 0) throw new AssertionError("Some tests failed");}132}133134135