Path: blob/master/test/jdk/java/nio/file/Files/walkFileTree/Nulls.java
41155 views
/*1* Copyright (c) 2011, 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/* @test24* @bug 4313887 686574825* @summary Unit test for java.nio.file.Files.walkFileTree26*/2728import java.nio.file.*;29import java.nio.file.attribute.BasicFileAttributes;30import java.io.IOException;31import java.util.*;3233public class Nulls {3435static void npeExpected() {36throw new RuntimeException("NullPointerException expected");37}3839public static void main(String[] args) throws IOException {40try {41Files.walkFileTree(null, EnumSet.noneOf(FileVisitOption.class),42Integer.MAX_VALUE, new SimpleFileVisitor<Path>(){});43npeExpected();44} catch (NullPointerException e) {45}46try {47Files.walkFileTree(Paths.get("."), null, Integer.MAX_VALUE,48new SimpleFileVisitor<Path>(){});49npeExpected();50} catch (NullPointerException e) {51}52try {53Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class),54-1, new SimpleFileVisitor<Path>(){});55throw new RuntimeException("IllegalArgumentExpected expected");56} catch (IllegalArgumentException e) {57}58try {59Set<FileVisitOption> opts = new HashSet<>(1);60opts.add(null);61Files.walkFileTree(Paths.get("."), opts, Integer.MAX_VALUE,62new SimpleFileVisitor<Path>(){});63npeExpected();64} catch (NullPointerException e) {65}66try {67Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class),68Integer.MAX_VALUE, null);69npeExpected();70} catch (NullPointerException e) {71}7273SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { };74boolean ranTheGauntlet = false;75Path dir = Paths.get(".");76BasicFileAttributes attrs = Files.readAttributes(dir, BasicFileAttributes.class);7778try { visitor.preVisitDirectory(null, attrs);79} catch (NullPointerException x0) {80try { visitor.preVisitDirectory(dir, null);81} catch (NullPointerException x1) {82try { visitor.visitFile(null, attrs);83} catch (NullPointerException x2) {84try { visitor.visitFile(dir, null);85} catch (NullPointerException x3) {86try { visitor.visitFileFailed(null, new IOException());87} catch (NullPointerException x4) {88try { visitor.visitFileFailed(dir, null);89} catch (NullPointerException x5) {90try { visitor.postVisitDirectory(null, new IOException());91} catch (NullPointerException x6) {92// if we get here then all visit* methods threw NPE as expected93ranTheGauntlet = true;94}}}}}}}95if (!ranTheGauntlet)96throw new RuntimeException("A visit method did not throw NPE");97}98}99100101