Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Scanner/FailingConstructors.java
38811 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.nio.file.Files;36import java.util.Scanner;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();49Files.write(file.toPath(), FILE_CONTENTS.getBytes());5051test(true, file);52file.delete();53}5455private static void test(boolean exists, File file) throws Throwable {56/* Scanner(File source, String charsetName) */57try {58new Scanner(file, UNSUPPORTED_CHARSET);59fail();60} catch(FileNotFoundException|IllegalArgumentException e) {61pass();62}6364check(exists, file);6566try {67new Scanner(file, null);68fail();69} catch(FileNotFoundException|NullPointerException e) {70pass();71}7273check(exists, file);7475/* Scanner(FileRef source, String charsetName) */76try {77new Scanner(file.toPath(), UNSUPPORTED_CHARSET);78fail();79} catch(FileNotFoundException|IllegalArgumentException e) {80pass();81}8283check(exists, file);8485try {86new Scanner(file.toPath(), null);87fail();88} catch(FileNotFoundException|NullPointerException e) {89pass();90}9192check(exists, file);93}9495private static void check(boolean exists, File file) {96if (exists) {97/* the file should be unchanged */98verifyContents(file);99} else {100/* the file should not have been created */101if (file.exists()) { fail(file + " should not have been created"); }102}103}104105private static void verifyContents(File file) {106try (FileInputStream fis = new FileInputStream(file)) {107byte[] contents = FILE_CONTENTS.getBytes();108int read, count = 0;109while ((read = fis.read()) != -1) {110if (read != contents[count++]) {111fail("file contents have been altered");112return;113}114}115} catch (IOException ioe) {116unexpected(ioe);117}118}119120//--------------------- Infrastructure ---------------------------121static volatile int passed = 0, failed = 0;122static void pass() {passed++;}123static void fail() {failed++; Thread.dumpStack();}124static void fail(String message) {System.out.println(message); fail(); }125static void unexpected(Throwable t) {failed++; t.printStackTrace();}126public static void main(String[] args) throws Throwable {127try {realMain(args);} catch (Throwable t) {unexpected(t);}128System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);129if (failed > 0) throw new AssertionError("Some tests failed");}130}131132133