Path: blob/master/test/jdk/java/io/File/DeleteOnExitNPE.java
41149 views
/*1* Copyright (c) 2007, 2010, 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/* @test24@bug 652637625@summary DeleteOnExitHook.add() produces NullPointerException26*/2728import java.io.*;2930/* NullPointerException in exec'ed process if fails.31* This testcase is timing sensitive. It may sometimes pass even with this bug32* present, but will never fail without it.33*/3435public class DeleteOnExitNPE implements Runnable36{37public static void main(String[] args) throws Exception {38if (args.length == 0) {39runTest();40} else {41doTest();42}43}4445public static void runTest() throws Exception {46String cmd = System.getProperty("java.home") + File.separator +47"bin" + File.separator + "java" +48" -classpath " + System.getProperty("test.classes");49Process process = Runtime.getRuntime().exec(cmd + " DeleteOnExitNPE -test");50BufferedReader isReader = new BufferedReader(new InputStreamReader(process.getInputStream()));51BufferedReader esReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));5253process.waitFor();5455boolean failed = false;56String str;57while ((str = isReader.readLine()) != null) {58failed = true;59System.out.println(str);60}61while ((str = esReader.readLine()) != null) {62failed = true;63System.err.println(str);64}6566if (failed)67throw new RuntimeException("Failed: No output should have been received from the process");68}6970public static void doTest() {71try {72File file = File.createTempFile("DeleteOnExitNPE", null);73file.deleteOnExit();7475Thread thread = new Thread(new DeleteOnExitNPE());76thread.start();7778try {79Thread.sleep(2000);80} catch (InterruptedException ie) {81ie.printStackTrace();82}8384System.exit(0);85} catch (IOException ioe) {86ioe.printStackTrace();87}88}8990public void run() {91File file = new File("xxyyzz");9293try {94for (;;) {95file.deleteOnExit();96}97} catch (IllegalStateException ise) {98// ignore. This is ok.99// Trying to add a file to the list of files marked as deleteOnExit when100// shutdown is in progress and the DeleteOnExitHook is running.101}102}103}104105106