1/*
2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @bug  6526376
26   @summary DeleteOnExitHook.add() produces NullPointerException
27 */
28
29import java.io.*;
30
31/* NullPointerException in exec'ed process if fails.
32 * This testcase is timing sensitive. It may sometimes pass even with this bug
33 * present, but will never fail without it.
34 */
35
36public class DeleteOnExitNPE implements Runnable
37{
38    public static void main(String[] args) throws Exception {
39        if (args.length == 0) {
40            runTest();
41        } else {
42            doTest();
43        }
44    }
45
46    public static void runTest() throws Exception {
47        String cmd = System.getProperty("java.home") + File.separator +
48                     "bin" + File.separator + "java" +
49                     " -classpath " + System.getProperty("test.classes");
50        Process process = Runtime.getRuntime().exec(cmd +  " DeleteOnExitNPE -test");
51        BufferedReader isReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
52        BufferedReader esReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
53
54        process.waitFor();
55
56        boolean failed = false;
57        String str;
58        while ((str = isReader.readLine()) != null) {
59            failed = true;
60            System.out.println(str);
61        }
62        while ((str = esReader.readLine()) != null) {
63            failed = true;
64            System.err.println(str);
65        }
66
67        if (failed)
68            throw new RuntimeException("Failed: No output should have been received from the process");
69    }
70
71    public static void doTest() {
72        try {
73            File file = File.createTempFile("DeleteOnExitNPE", null);
74            file.deleteOnExit();
75
76            Thread thread = new Thread(new DeleteOnExitNPE());
77            thread.start();
78
79            try {
80                Thread.sleep(2000);
81            } catch (InterruptedException ie) {
82                ie.printStackTrace();
83            }
84
85            System.exit(0);
86        } catch (IOException ioe) {
87            ioe.printStackTrace();
88        }
89    }
90
91    public void run() {
92        File file = new File("xxyyzz");
93
94        try {
95            for (;;) {
96                file.deleteOnExit();
97            }
98        } catch (IllegalStateException ise) {
99            // ignore. This is ok.
100            // Trying to add a file to the list of files marked as deleteOnExit when
101            // shutdown is in progress and the DeleteOnExitHook is running.
102        }
103    }
104}
105