2826N/A/*
3909N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
2826N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2826N/A *
2826N/A * This code is free software; you can redistribute it and/or modify it
2826N/A * under the terms of the GNU General Public License version 2 only, as
2826N/A * published by the Free Software Foundation.
2826N/A *
2826N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2826N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2826N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2826N/A * version 2 for more details (a copy is included in the LICENSE file that
2826N/A * accompanied this code).
2826N/A *
2826N/A * You should have received a copy of the GNU General Public License version
2826N/A * 2 along with this work; if not, write to the Free Software Foundation,
2826N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2826N/A *
2826N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2826N/A * or visit www.oracle.com if you need additional information or have any
2826N/A * questions.
2826N/A */
2826N/A
2826N/Aimport java.nio.file.*;
2826N/Aimport java.nio.file.attribute.*;
2826N/Aimport java.io.IOException;
2826N/Aimport java.util.*;
2826N/A
2826N/A/**
2826N/A * Unit test for Files.walkFileTree to test maxDepth parameter
2826N/A */
2826N/A
2826N/Apublic class MaxDepth {
2826N/A public static void main(String[] args) throws Exception {
2826N/A final Path top = Paths.get(args[0]);
2826N/A
2826N/A for (int i=0; i<5; i++) {
2826N/A Set<FileVisitOption> opts = Collections.emptySet();
2826N/A final int maxDepth = i;
2826N/A Files.walkFileTree(top, opts, maxDepth, new SimpleFileVisitor<Path>() {
2826N/A // compute depth based on relative path to top directory
2826N/A private int depth(Path file) {
2826N/A Path rp = file.relativize(top);
3471N/A return (rp.getFileName().toString().equals("")) ? 0 : rp.getNameCount();
2826N/A }
2826N/A
2826N/A @Override
2826N/A public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
2826N/A int d = depth(dir);
2826N/A if (d == maxDepth)
2826N/A throw new RuntimeException("Should not open directories at maxDepth");
2826N/A if (d > maxDepth)
2826N/A throw new RuntimeException("Too deep");
2826N/A return FileVisitResult.CONTINUE;
2826N/A }
2826N/A
2826N/A @Override
2826N/A public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
2826N/A int d = depth(file);
2826N/A if (d > maxDepth)
2826N/A throw new RuntimeException("Too deep");
2826N/A return FileVisitResult.CONTINUE;
2826N/A }
2826N/A });
2826N/A }
2826N/A }
2826N/A}