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