Path: blob/master/test/jdk/java/util/Formatter/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.UnsupportedEncodingException;36import java.nio.file.Files;37import java.util.Formatter;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/* Formatter(File file, String csn) */58try {59new Formatter(file, UNSUPPORTED_CHARSET);60fail();61} catch(FileNotFoundException|UnsupportedEncodingException e) {62pass();63}6465check(exists, file);6667try {68new Formatter(file, null);69fail();70} catch(FileNotFoundException|NullPointerException e) {71pass();72}7374check(exists, file);7576/* Formatter(String fileName, String csn) */77try {78new Formatter(file.getName(), UNSUPPORTED_CHARSET);79fail();80} catch(FileNotFoundException|UnsupportedEncodingException e) {81pass();82}8384check(exists, file);8586try {87new Formatter(file.getName(), null);88fail();89} catch(FileNotFoundException|NullPointerException e) {90pass();91}9293check(exists, file);94}9596private static void check(boolean exists, File file) {97if (exists) {98/* the file should be unchanged */99verifyContents(file);100} else {101/* the file should not have been created */102if (file.exists()) { fail(file + " should not have been created"); }103}104}105106private static void verifyContents(File file) {107try (FileInputStream fis = new FileInputStream(file)) {108byte[] contents = FILE_CONTENTS.getBytes();109int read, count = 0;110while ((read = fis.read()) != -1) {111if (read != contents[count++]) {112fail("file contents have been altered");113return;114}115}116} catch (IOException ioe) {117unexpected(ioe);118}119}120121//--------------------- Infrastructure ---------------------------122static volatile int passed = 0, failed = 0;123static void pass() {passed++;}124static void fail() {failed++; Thread.dumpStack();}125static void fail(String message) {System.out.println(message); fail(); }126static void unexpected(Throwable t) {failed++; t.printStackTrace();}127public static void main(String[] args) throws Throwable {128try {realMain(args);} catch (Throwable t) {unexpected(t);}129System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);130if (failed > 0) throw new AssertionError("Some tests failed");}131}132133134