Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/WatchService/FileTreeModifier.java
38828 views
1
/*
2
* Copyright (c) 2008, 2011, 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 4313887 6838333
26
* @summary Sanity test for Sun-specific FILE_TREE watch event modifier
27
* @library ..
28
*/
29
30
import java.nio.file.*;
31
import static java.nio.file.StandardWatchEventKinds.*;
32
import java.io.IOException;
33
import java.io.OutputStream;
34
import java.util.*;
35
import java.util.concurrent.*;
36
import static com.sun.nio.file.ExtendedWatchEventModifier.*;
37
38
public class FileTreeModifier {
39
40
static void checkExpectedEvent(WatchService watcher,
41
WatchEvent.Kind<?> expectedType,
42
Object expectedContext)
43
{
44
WatchKey key;
45
try {
46
key = watcher.take();
47
} catch (InterruptedException x) {
48
// should not happen
49
throw new RuntimeException(x);
50
}
51
WatchEvent<?> event = key.pollEvents().iterator().next();
52
System.out.format("Event: type=%s, count=%d, context=%s\n",
53
event.kind(), event.count(), event.context());
54
if (event.kind() != expectedType)
55
throw new RuntimeException("unexpected event");
56
if (!expectedContext.equals(event.context()))
57
throw new RuntimeException("unexpected context");
58
}
59
60
static void doTest(Path top) throws IOException {
61
FileSystem fs = top.getFileSystem();
62
WatchService watcher = fs.newWatchService();
63
64
// create directories
65
Path subdir = Files.createDirectories(top.resolve("a").resolve("b").resolve("c"));
66
67
// Test ENTRY_CREATE with FILE_TREE modifier.
68
69
WatchKey key = top.register(watcher,
70
new WatchEvent.Kind<?>[]{ ENTRY_CREATE }, FILE_TREE);
71
72
// create file in a/b/c and check we get create event
73
Path file = Files.createFile(subdir.resolve("foo"));
74
checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
75
key.reset();
76
77
// Test ENTRY_DELETE with FILE_TREE modifier.
78
79
WatchKey k = top.register(watcher,
80
new WatchEvent.Kind<?>[]{ ENTRY_DELETE }, FILE_TREE);
81
if (k != key)
82
throw new RuntimeException("Existing key not returned");
83
84
// delete a/b/c/foo and check we get delete event
85
Files.delete(file);
86
checkExpectedEvent(watcher, ENTRY_DELETE, top.relativize(file));
87
key.reset();
88
89
// Test changing registration to ENTRY_CREATE without modifier
90
91
k = top.register(watcher, new WatchEvent.Kind<?>[]{ ENTRY_CREATE });
92
if (k != key)
93
throw new RuntimeException("Existing key not returned");
94
95
// create a/b/c/foo
96
Files.createFile(file);
97
98
// check that key is not queued
99
WatchKey nextKey;
100
try {
101
nextKey = watcher.poll(3, TimeUnit.SECONDS);
102
} catch (InterruptedException e) {
103
throw new RuntimeException();
104
}
105
if (nextKey != null)
106
throw new RuntimeException("WatchKey not expected to be polled");
107
108
// create bar and check we get create event
109
file = Files.createFile(top.resolve("bar"));
110
checkExpectedEvent(watcher, ENTRY_CREATE, top.relativize(file));
111
key.reset();
112
113
// Test changing registration to <all> with FILE_TREE modifier
114
115
k = top.register(watcher,
116
new WatchEvent.Kind<?>[]{ ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY },
117
FILE_TREE);
118
if (k != key)
119
throw new RuntimeException("Existing key not returned");
120
121
// modify bar and check we get modify event
122
try (OutputStream out = Files.newOutputStream(file)) {
123
out.write("Double shot expresso please".getBytes("UTF-8"));
124
}
125
checkExpectedEvent(watcher, ENTRY_MODIFY, top.relativize(file));
126
key.reset();
127
}
128
129
130
public static void main(String[] args) throws IOException {
131
if (!System.getProperty("os.name").startsWith("Windows")) {
132
System.out.println("This is Windows-only test at this time!");
133
return;
134
}
135
136
Path dir = TestUtil.createTemporaryDirectory();
137
try {
138
doTest(dir);
139
} finally {
140
TestUtil.removeAll(dir);
141
}
142
}
143
}
144
145