Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/walkFileTree/SkipSiblings.java
41155 views
1
/*
2
* Copyright (c) 2008, 2013, 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
* @summary Unit test for Files.walkFileTree to test SKIP_SIBLINGS return value
27
* @library ../..
28
* @compile SkipSiblings.java CreateFileTree.java
29
* @run main SkipSiblings
30
* @key randomness
31
*/
32
33
import java.nio.file.*;
34
import java.nio.file.attribute.*;
35
import java.io.IOException;
36
import java.util.*;
37
38
public class SkipSiblings {
39
40
static final Random rand = new Random();
41
static final Set<Path> skipped = new HashSet<Path>();
42
43
// check if this path's directory has been skipped
44
static void check(Path path) {
45
if (skipped.contains(path.getParent()))
46
throw new RuntimeException(path + " should not have been visited");
47
}
48
49
// indicates if the siblings of this path should be skipped
50
static boolean skip(Path path) {
51
Path parent = path.getParent();
52
if (parent != null && rand.nextBoolean()) {
53
skipped.add(parent);
54
return true;
55
}
56
return false;
57
}
58
59
public static void main(String[] args) throws Exception {
60
Path top = CreateFileTree.create();
61
try {
62
test(top);
63
} finally {
64
TestUtil.removeAll(top);
65
}
66
}
67
68
static void test(final Path start) throws IOException {
69
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
70
@Override
71
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
72
check(dir);
73
if (skip(dir))
74
return FileVisitResult.SKIP_SIBLINGS;
75
return FileVisitResult.CONTINUE;
76
}
77
@Override
78
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
79
check(file);
80
if (skip(file))
81
return FileVisitResult.SKIP_SIBLINGS;
82
return FileVisitResult.CONTINUE;
83
}
84
@Override
85
public FileVisitResult postVisitDirectory(Path dir, IOException x) {
86
if (x != null)
87
throw new RuntimeException(x);
88
check(dir);
89
if (rand.nextBoolean()) {
90
return FileVisitResult.CONTINUE;
91
} else {
92
return FileVisitResult.SKIP_SIBLINGS;
93
}
94
}
95
});
96
}
97
}
98
99