SkipSiblings.java revision 11822:110f7f35760f
1129590Smarius/* 2129590Smarius * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. 3129590Smarius * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4129590Smarius * 5129590Smarius * This code is free software; you can redistribute it and/or modify it 6129590Smarius * under the terms of the GNU General Public License version 2 only, as 7129590Smarius * published by the Free Software Foundation. 8129590Smarius * 9129590Smarius * This code is distributed in the hope that it will be useful, but WITHOUT 10129590Smarius * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11129590Smarius * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12129590Smarius * version 2 for more details (a copy is included in the LICENSE file that 13129590Smarius * accompanied this code). 14129590Smarius * 15129590Smarius * You should have received a copy of the GNU General Public License version 16129590Smarius * 2 along with this work; if not, write to the Free Software Foundation, 17129590Smarius * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18129590Smarius * 19129590Smarius * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20129590Smarius * or visit www.oracle.com if you need additional information or have any 21129590Smarius * questions. 22129590Smarius */ 23129590Smarius 24129590Smarius/* 25129590Smarius * @test 26129590Smarius * @summary Unit test for Files.walkFileTree to test SKIP_SIBLINGS return value 27129590Smarius * @library ../.. 28129590Smarius * @compile SkipSiblings.java CreateFileTree.java 29129590Smarius * @run main SkipSiblings 30129590Smarius * @key randomness 31129590Smarius */ 32129590Smarius 33129590Smariusimport java.nio.file.*; 34129590Smariusimport java.nio.file.attribute.*; 35129590Smariusimport java.io.IOException; 36129590Smariusimport java.util.*; 37129590Smarius 38129590Smariuspublic class SkipSiblings { 39129590Smarius 40129590Smarius static final Random rand = new Random(); 41129590Smarius static final Set<Path> skipped = new HashSet<Path>(); 42129590Smarius 43129590Smarius // check if this path's directory has been skipped 44129590Smarius static void check(Path path) { 45129590Smarius if (skipped.contains(path.getParent())) 46129590Smarius throw new RuntimeException(path + " should not have been visited"); 47129590Smarius } 48129590Smarius 49129590Smarius // indicates if the siblings of this path should be skipped 50129590Smarius static boolean skip(Path path) { 51129590Smarius Path parent = path.getParent(); 52129590Smarius if (parent != null && rand.nextBoolean()) { 53129590Smarius skipped.add(parent); 54129590Smarius return true; 55129590Smarius } 56129590Smarius return false; 57129590Smarius } 58129590Smarius 59129590Smarius public static void main(String[] args) throws Exception { 60129590Smarius Path top = CreateFileTree.create(); 61129590Smarius try { 62129590Smarius test(top); 63129590Smarius } finally { 64129590Smarius TestUtil.removeAll(top); 65129590Smarius } 66129590Smarius } 67129590Smarius 68129590Smarius static void test(final Path start) throws IOException { 69129590Smarius Files.walkFileTree(start, new SimpleFileVisitor<Path>() { 70129590Smarius @Override 71129590Smarius public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { 72129590Smarius check(dir); 73129590Smarius if (skip(dir)) 74129590Smarius return FileVisitResult.SKIP_SIBLINGS; 75129590Smarius return FileVisitResult.CONTINUE; 76129590Smarius } 77132788Skan @Override 78132788Skan public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { 79132788Skan check(file); 80132788Skan if (skip(file)) 81132788Skan return FileVisitResult.SKIP_SIBLINGS; 82132788Skan return FileVisitResult.CONTINUE; 83132788Skan } 84129590Smarius @Override 85129590Smarius public FileVisitResult postVisitDirectory(Path dir, IOException x) { 86129590Smarius if (x != null) 87129590Smarius throw new RuntimeException(x); 88129590Smarius check(dir); 89129590Smarius if (rand.nextBoolean()) { 90129590Smarius return FileVisitResult.CONTINUE; 91129590Smarius } else { 92129590Smarius return FileVisitResult.SKIP_SIBLINGS; 93129590Smarius } 94129590Smarius } 95129590Smarius }); 96129590Smarius } 97129590Smarius} 98129590Smarius