SkipSiblings.java revision 7018:8b07b318f713
1252995Sdteske/* 2252995Sdteske * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. 3252995Sdteske * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4263791Sdteske * 5252995Sdteske * This code is free software; you can redistribute it and/or modify it 6252995Sdteske * under the terms of the GNU General Public License version 2 only, as 7252995Sdteske * published by the Free Software Foundation. 8252995Sdteske * 9252995Sdteske * This code is distributed in the hope that it will be useful, but WITHOUT 10252995Sdteske * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11252995Sdteske * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12252995Sdteske * version 2 for more details (a copy is included in the LICENSE file that 13252995Sdteske * accompanied this code). 14252995Sdteske * 15252995Sdteske * You should have received a copy of the GNU General Public License version 16252995Sdteske * 2 along with this work; if not, write to the Free Software Foundation, 17252995Sdteske * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18252995Sdteske * 19252995Sdteske * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20252995Sdteske * or visit www.oracle.com if you need additional information or have any 21252995Sdteske * questions. 22252995Sdteske */ 23252995Sdteske 24252995Sdteske/* 25252995Sdteske * @test 26252995Sdteske * @summary Unit test for Files.walkFileTree to test SKIP_SIBLINGS return value 27252995Sdteske * @compile SkipSiblings.java CreateFileTree.java 28252995Sdteske * @run main SkipSiblings 29252995Sdteske */ 30252995Sdteske 31252995Sdteskeimport java.nio.file.*; 32252995Sdteskeimport java.nio.file.attribute.*; 33252995Sdteskeimport java.io.IOException; 34252995Sdteskeimport java.util.*; 35252995Sdteske 36252995Sdteskepublic class SkipSiblings { 37263791Sdteske 38252995Sdteske static final Random rand = new Random(); 39252995Sdteske static final Set<Path> skipped = new HashSet<Path>(); 40252995Sdteske 41252995Sdteske // check if this path's directory has been skipped 42252995Sdteske static void check(Path path) { 43263791Sdteske if (skipped.contains(path.getParent())) 44263791Sdteske throw new RuntimeException(path + " should not have been visited"); 45252995Sdteske } 46252995Sdteske 47252995Sdteske // indicates if the siblings of this path should be skipped 48252995Sdteske static boolean skip(Path path) { 49252995Sdteske Path parent = path.getParent(); 50252995Sdteske if (parent != null && rand.nextBoolean()) { 51252995Sdteske skipped.add(parent); 52252995Sdteske return true; 53252995Sdteske } 54252995Sdteske return false; 55252995Sdteske } 56252995Sdteske 57252995Sdteske public static void main(String[] args) throws Exception { 58252995Sdteske Path dir = CreateFileTree.create(); 59252995Sdteske 60252995Sdteske Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { 61252995Sdteske @Override 62252995Sdteske public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { 63252995Sdteske check(dir); 64252995Sdteske if (skip(dir)) 65252995Sdteske return FileVisitResult.SKIP_SIBLINGS; 66252995Sdteske return FileVisitResult.CONTINUE; 67252995Sdteske } 68252995Sdteske @Override 69263791Sdteske public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { 70263791Sdteske check(file); 71263791Sdteske if (skip(file)) 72263791Sdteske return FileVisitResult.SKIP_SIBLINGS; 73263791Sdteske return FileVisitResult.CONTINUE; 74263791Sdteske } 75263791Sdteske @Override 76263791Sdteske public FileVisitResult postVisitDirectory(Path dir, IOException x) { 77252995Sdteske if (x != null) 78252995Sdteske throw new RuntimeException(x); 79263791Sdteske check(dir); 80252995Sdteske if (rand.nextBoolean()) { 81252995Sdteske return FileVisitResult.CONTINUE; 82252995Sdteske } else { 83252995Sdteske return FileVisitResult.SKIP_SIBLINGS; 84252995Sdteske } 85252995Sdteske } 86252995Sdteske }); 87263791Sdteske } 88252995Sdteske} 89252995Sdteske