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