1 /* 2 * Copyright (c) 2008, 2010, 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 6838333 6865748 26 * @summary Unit test for java.nio.file.Files for miscellenous cases not 27 * covered by other tests 28 * @library .. 29 */ 30 31 import java.nio.file.*; 32 import java.nio.file.attribute.Attributes; 33 import java.nio.file.attribute.BasicFileAttributes; 34 import java.io.IOException; 35 import java.util.*; 36 37 public class Misc { 38 39 static void npeExpected() { 40 throw new RuntimeException("NullPointerException expected"); 41 } 42 43 public static void main(String[] args) throws IOException { 44 45 // -- Files.createDirectories -- 46 47 Path dir = TestUtil.createTemporaryDirectory(); 48 try { 49 // no-op 50 Files.createDirectories(dir); 51 52 // create one directory 53 Path subdir = dir.resolve("a"); 54 Files.createDirectories(subdir); 55 if (!subdir.exists()) 56 throw new RuntimeException("directory not created"); 57 58 // create parents 59 subdir = subdir.resolve("b/c/d"); 60 Files.createDirectories(subdir); 61 if (!subdir.exists()) 62 throw new RuntimeException("directory not created"); 63 64 // existing file is not a directory 65 Path file = dir.resolve("x").createFile(); 66 try { 67 Files.createDirectories(file); 68 throw new RuntimeException("failure expected"); 69 } catch (FileAlreadyExistsException x) { } 70 try { 71 Files.createDirectories(file.resolve("y")); 72 throw new RuntimeException("failure expected"); 73 } catch (IOException x) { } 74 75 } finally { 76 TestUtil.removeAll(dir); 77 } 78 79 // --- NullPointerException -- 80 81 try { 82 Files.probeContentType(null); 83 npeExpected(); 84 } catch (NullPointerException e) { 85 } 86 try { 87 Files.walkFileTree(null, EnumSet.noneOf(FileVisitOption.class), 88 Integer.MAX_VALUE, new SimpleFileVisitor<Path>(){}); 89 npeExpected(); 90 } catch (NullPointerException e) { 91 } 92 try { 93 Files.walkFileTree(Paths.get("."), null, Integer.MAX_VALUE, 94 new SimpleFileVisitor<Path>(){}); 95 npeExpected(); 96 } catch (NullPointerException e) { 97 } 98 try { 99 Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class), 100 -1, new SimpleFileVisitor<Path>(){}); 101 throw new RuntimeException("IllegalArgumentExpected expected"); 102 } catch (IllegalArgumentException e) { 103 } 104 try { 105 Set<FileVisitOption> opts = new HashSet<FileVisitOption>(1); 106 opts.add(null); 107 Files.walkFileTree(Paths.get("."), opts, Integer.MAX_VALUE, 108 new SimpleFileVisitor<Path>(){}); 109 npeExpected(); 110 } catch (NullPointerException e) { 111 } 112 try { 113 Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class), 114 Integer.MAX_VALUE, null); 115 npeExpected(); 116 } catch (NullPointerException e) { 117 } 118 119 SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { }; 120 boolean ranTheGauntlet = false; 121 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(Paths.get(".")); 122 123 try { visitor.preVisitDirectory(null, attrs); 124 } catch (NullPointerException x0) { 125 try { visitor.preVisitDirectory(dir, null); 126 } catch (NullPointerException x1) { 127 try { visitor.visitFile(null, attrs); 128 } catch (NullPointerException x2) { 129 try { visitor.visitFile(dir, null); 130 } catch (NullPointerException x3) { 131 try { visitor.visitFileFailed(null, new IOException()); 132 } catch (NullPointerException x4) { 133 try { visitor.visitFileFailed(dir, null); 134 } catch (NullPointerException x5) { 135 try { visitor.postVisitDirectory(null, new IOException()); 136 } catch (NullPointerException x6) { 137 // if we get here then all visit* methods threw NPE as expected 138 ranTheGauntlet = true; 139 }}}}}}} 140 if (!ranTheGauntlet) 141 throw new RuntimeException("A visit method did not throw NPE"); 142 } 143 }