Path: blob/master/test/jdk/java/io/PrintStream/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.io.PrintStream;36import java.io.UnsupportedEncodingException;37import java.nio.file.Files;38import java.nio.file.Path;3940public class FailingConstructors {41static final String fileName = "FailingConstructorsTest";42static final String UNSUPPORTED_CHARSET = "unknownCharset";43static final String FILE_CONTENTS = "This is a small file!";4445private static void realMain(String[] args) throws Throwable {46test(false, new File(fileName));4748/* create the file and write its contents */49Path path = Files.createTempFile(fileName, null);50try {51Files.write(path, FILE_CONTENTS.getBytes());52test(true, path.toFile());53} finally {54Files.delete(path);55}56}5758private static void test(boolean exists, File file) throws Throwable {59/* PrintStream(File file, String csn) */60try {61new PrintStream(file, UNSUPPORTED_CHARSET);62fail();63} catch(FileNotFoundException|UnsupportedEncodingException e) {64pass();65}6667check(exists, file);6869try {70new PrintStream(file, (String)null);71fail();72} catch(FileNotFoundException|NullPointerException e) {73pass();74}7576check(exists, file);7778/* PrintStream(String fileName, String csn) */79try {80new PrintStream(file.getName(), UNSUPPORTED_CHARSET);81fail();82} catch(FileNotFoundException|UnsupportedEncodingException e) {83pass();84}8586check(exists, file);8788try {89new PrintStream(file.getName(), (String)null);90fail();91} catch(FileNotFoundException|NullPointerException e) {92pass();93}9495check(exists, file);96}9798private static void check(boolean exists, File file) {99if (exists) {100/* the file should be unchanged */101verifyContents(file);102} else {103/* the file should not have been created */104if (file.exists()) { fail(file + " should not have been created"); }105}106}107108private static void verifyContents(File file) {109try (FileInputStream fis = new FileInputStream(file)) {110byte[] contents = FILE_CONTENTS.getBytes();111int read, count = 0;112while ((read = fis.read()) != -1) {113if (read != contents[count++]) {114fail("file contents have been altered");115return;116}117}118} catch (IOException ioe) {119unexpected(ioe);120}121}122123//--------------------- Infrastructure ---------------------------124static volatile int passed = 0, failed = 0;125static void pass() {passed++;}126static void fail() {failed++; Thread.dumpStack();}127static void fail(String message) {System.out.println(message); fail(); }128static void unexpected(Throwable t) {failed++; t.printStackTrace();}129public static void main(String[] args) throws Throwable {130try {realMain(args);} catch (Throwable t) {unexpected(t);}131System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);132if (failed > 0) throw new AssertionError("Some tests failed");}133}134135136