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