Path: blob/master/test/jdk/java/nio/file/Files/walkFileTree/TerminateWalk.java
41155 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Unit test for Files.walkFileTree to test TERMINATE return value26* @library ../..27* @compile TerminateWalk.java CreateFileTree.java28* @run main TerminateWalk29* @key randomness30*/3132import java.nio.file.*;33import java.nio.file.attribute.*;34import java.io.IOException;35import java.util.*;3637public class TerminateWalk {3839static final Random rand = new Random();40static boolean terminated;4142static FileVisitResult maybeTerminate() {43if (terminated)44throw new RuntimeException("FileVisitor invoked after termination");45if (rand.nextInt(10) == 0) {46terminated = true;47return FileVisitResult.TERMINATE;48} else {49return FileVisitResult.CONTINUE;50}51}5253public static void main(String[] args) throws Exception {54Path top = CreateFileTree.create();55try {56test(top);57} finally {58TestUtil.removeAll(top);59}60}6162static void test(Path start) throws IOException {63Files.walkFileTree(start, new SimpleFileVisitor<Path>() {64@Override65public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {66return maybeTerminate();67}68@Override69public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {70return maybeTerminate();71}72@Override73public FileVisitResult postVisitDirectory(Path dir, IOException x) {74return maybeTerminate();75}76});77}78}798081