Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/DirectoryStream/Basic.java
41153 views
1
/*
2
* Copyright (c) 2008, 2018, 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 Unit test for java.nio.file.DirectoryStream
27
* @library ..
28
*/
29
30
import java.nio.file.*;
31
import static java.nio.file.Files.*;
32
import java.util.*;
33
import java.io.IOException;
34
35
public class Basic {
36
static boolean found;
37
38
static void doTest(final Path dir) throws IOException {
39
DirectoryStream<Path> stream;
40
41
// test that directory is empty
42
try (DirectoryStream<Path> ds = newDirectoryStream(dir)) {
43
if (ds.iterator().hasNext())
44
throw new RuntimeException("directory not empty");
45
}
46
47
// create file in directory
48
final Path foo = Paths.get("foo");
49
createFile(dir.resolve(foo));
50
51
// iterate over directory and check there is one entry
52
stream = newDirectoryStream(dir);
53
found = false;
54
try {
55
for (Path entry: stream) {
56
if (entry.getFileName().equals(foo)) {
57
if (found)
58
throw new RuntimeException("entry already found");
59
found = true;
60
} else {
61
throw new RuntimeException("entry " + entry.getFileName() +
62
" not expected");
63
}
64
}
65
} finally {
66
stream.close();
67
}
68
if (!found)
69
throw new RuntimeException("entry not found");
70
71
// check filtering: f* should match foo
72
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
73
private PathMatcher matcher =
74
dir.getFileSystem().getPathMatcher("glob:f*");
75
public boolean accept(Path file) {
76
return matcher.matches(file.getFileName());
77
}
78
};
79
80
found = false;
81
try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
82
for (Path entry: ds) {
83
if (entry.getFileName().equals(foo))
84
found = true;
85
}
86
if (!found)
87
throw new RuntimeException(String.format("Error: entry: %s was not found", foo));
88
}
89
90
// check filtering: z* should not match any files
91
filter = new DirectoryStream.Filter<Path>() {
92
private PathMatcher matcher =
93
dir.getFileSystem().getPathMatcher("glob:z*");
94
public boolean accept(Path file) {
95
return matcher.matches(file.getFileName());
96
}
97
};
98
try (DirectoryStream<Path> ds = newDirectoryStream(dir, filter)) {
99
if (ds.iterator().hasNext())
100
throw new RuntimeException("no matching entries expected");
101
}
102
103
// check that an IOException thrown by a filter is propagated
104
filter = new DirectoryStream.Filter<Path>() {
105
public boolean accept(Path file) throws IOException {
106
throw new java.util.zip.ZipException();
107
}
108
};
109
stream = newDirectoryStream(dir, filter);
110
try {
111
stream.iterator().hasNext();
112
throw new RuntimeException("DirectoryIteratorException expected");
113
} catch (DirectoryIteratorException x) {
114
IOException cause = x.getCause();
115
if (!(cause instanceof java.util.zip.ZipException))
116
throw new RuntimeException("Expected IOException not propagated");
117
} finally {
118
stream.close();
119
}
120
121
// check that exception or error thrown by filter is not thrown
122
// by newDirectoryStream or iterator method.
123
stream = newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {
124
public boolean accept(Path file) {
125
throw new RuntimeException("Should not be visible");
126
}
127
});
128
try {
129
stream.iterator();
130
} finally {
131
stream.close();
132
}
133
134
// test NotDirectoryException
135
try {
136
newDirectoryStream(dir.resolve(foo));
137
throw new RuntimeException("NotDirectoryException not thrown");
138
} catch (NotDirectoryException x) {
139
}
140
141
// test UnsupportedOperationException
142
stream = newDirectoryStream(dir);
143
Iterator<Path> i = stream.iterator();
144
i.next();
145
try {
146
i.remove();
147
throw new RuntimeException("UnsupportedOperationException expected");
148
} catch (UnsupportedOperationException uoe) {
149
}
150
151
// test IllegalStateException
152
stream = newDirectoryStream(dir);
153
stream.iterator();
154
try {
155
// attempt to obtain second iterator
156
stream.iterator();
157
throw new RuntimeException("IllegalStateException not thrown as expected");
158
} catch (IllegalStateException x) {
159
}
160
stream.close();
161
162
stream = newDirectoryStream(dir);
163
stream.close();
164
try {
165
// attempt to obtain iterator after stream is closed
166
stream.iterator();
167
throw new RuntimeException("IllegalStateException not thrown as expected");
168
} catch (IllegalStateException x) {
169
}
170
171
// test that iterator reads to end of stream when closed
172
stream = newDirectoryStream(dir);
173
i = stream.iterator();
174
stream.close();
175
while (i.hasNext())
176
i.next();
177
178
stream = newDirectoryStream(dir);
179
i = stream.iterator();
180
stream.close();
181
try {
182
for (;;) i.next();
183
} catch (NoSuchElementException expected) { }
184
}
185
186
public static void main(String[] args) throws IOException {
187
Path dir = TestUtil.createTemporaryDirectory();
188
try {
189
doTest(dir);
190
} finally {
191
TestUtil.removeAll(dir);
192
}
193
}
194
}
195
196