jdk/test/java/nio/file/Files/walkFileTree/SkipSubtree.java
changeset 17170 9f33b89c7978
child 17203 be1cff0d6750
equal deleted inserted replaced
17169:5e5039c3181d 17170:9f33b89c7978
       
     1 /*
       
     2  * Copyright (c) 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_SUBTREE return value
       
    27  * @compile SkipSubtree.java CreateFileTree.java
       
    28  * @run main SkipSubtree
       
    29  */
       
    30 import java.nio.file.*;
       
    31 import java.nio.file.attribute.BasicFileAttributes;
       
    32 import java.io.IOException;
       
    33 import java.util.HashSet;
       
    34 import java.util.Random;
       
    35 import java.util.Set;
       
    36 
       
    37 public class SkipSubtree {
       
    38 
       
    39     static final Random rand = new Random();
       
    40     static final Set<Path> skipped = new HashSet<>();
       
    41 
       
    42     // check if this path should have been skipped
       
    43     static void check(Path path) {
       
    44         do {
       
    45             if (skipped.contains(path))
       
    46                 throw new RuntimeException(path + " should not have been visited");
       
    47             path = path.getParent();
       
    48         } while (path != null);
       
    49     }
       
    50 
       
    51     // indicates if the subtree should be skipped
       
    52     static boolean skip(Path path) {
       
    53         if (rand.nextInt(3) == 0) {
       
    54             skipped.add(path);
       
    55             return true;
       
    56         }
       
    57         return false;
       
    58     }
       
    59 
       
    60     public static void main(String[] args) throws Exception {
       
    61         Path dir = CreateFileTree.create();
       
    62 
       
    63         Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
       
    64             @Override
       
    65             public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
       
    66                 check(dir);
       
    67                 if (skip(dir))
       
    68                     return FileVisitResult.SKIP_SUBTREE;
       
    69                 return FileVisitResult.CONTINUE;
       
    70             }
       
    71             @Override
       
    72             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
       
    73                 check(file);
       
    74                 return FileVisitResult.CONTINUE;
       
    75             }
       
    76             @Override
       
    77             public FileVisitResult postVisitDirectory(Path dir, IOException x) {
       
    78                 if (x != null)
       
    79                     throw new RuntimeException(x);
       
    80                 check(dir);
       
    81                 return FileVisitResult.CONTINUE;
       
    82             }
       
    83         });
       
    84     }
       
    85 }