Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/RuntimeTests/shutdown/ShutdownHooks.java
41152 views
1
/*
2
* Copyright (c) 2009, 2021, 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
/*
25
* @test
26
* @bug 6829503
27
* @summary 1) Test Console and DeleteOnExitHook can be initialized
28
* while shutdown is in progress
29
* 2) Test if files that are added by the application shutdown
30
* hook are deleted on exit during shutdown
31
* @library /test/lib
32
* @run testng/othervm ShutdownHooks
33
*/
34
35
import jdk.test.lib.process.ProcessTools;
36
import org.testng.annotations.BeforeTest;
37
import org.testng.annotations.Test;
38
39
import java.io.*;
40
import java.nio.file.Files;
41
42
import static jdk.test.lib.Asserts.assertFalse;
43
44
public class ShutdownHooks {
45
46
private static final String TEST_FILE_NAME = "fileToBeDeleted";
47
48
private static final File TEST_FILE = new File(TEST_FILE_NAME);
49
50
private static final String TEST_CLASSES = System.getProperty(
51
"test.classes", ".");
52
53
@BeforeTest
54
public static void setUp() throws Exception {
55
// Make sure file does not exist before test
56
Files.deleteIfExists(TEST_FILE.toPath());
57
}
58
59
@Test
60
public void testShutdownHooks() throws Exception {
61
// Run in a new process in order to evaluate shutdown hook results
62
String[] testCommand = new String[] {"-classpath", TEST_CLASSES,
63
ShutdownHooksProcess.class.getName()};
64
ProcessTools.executeTestJvm(testCommand).shouldHaveExitValue(0);
65
66
String errorMsg = "File exists despite shutdown hook has been run";
67
assertFalse(Files.exists(TEST_FILE.toPath()), errorMsg);
68
}
69
70
// This class main will be the entry point for test subprocesses
71
static class ShutdownHooksProcess {
72
public static void main(String[] args) throws Exception {
73
Runtime.getRuntime().addShutdownHook(new Cleaner());
74
75
System.out.println("Writing to "+ TEST_FILE);
76
try (PrintWriter pw = new PrintWriter(TEST_FILE)) {
77
pw.println("Shutdown begins");
78
}
79
}
80
81
static class Cleaner extends Thread {
82
public void run() {
83
// register the Console's shutdown hook while the application
84
// shutdown hook is running
85
Console cons = System.console();
86
// register the DeleteOnExitHook while the application
87
// shutdown hook is running
88
TEST_FILE.deleteOnExit();
89
try (PrintWriter pw = new PrintWriter(TEST_FILE)) {
90
pw.println("File is being deleted");
91
} catch (FileNotFoundException e) {
92
throw new RuntimeException(e);
93
}
94
}
95
}
96
}
97
}
98
99