Misc.java revision 1541
893N/A/*
893N/A * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
893N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
893N/A * CA 95054 USA or visit www.sun.com if you need additional information or
893N/A * have any questions.
893N/A */
893N/A
893N/A/* @test
1541N/A * @bug 4313887 6838333 6865748
893N/A * @summary Unit test for java.nio.file.Files for miscellenous cases not
893N/A * covered by other tests
893N/A * @library ..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
1541N/Aimport java.nio.file.attribute.Attributes;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/A
893N/Apublic class Misc {
893N/A
893N/A static void npeExpected() {
893N/A throw new RuntimeException("NullPointerException expected");
893N/A }
893N/A
893N/A public static void main(String[] args) throws IOException {
1319N/A
1319N/A // -- Files.createDirectories --
1319N/A
1319N/A Path dir = TestUtil.createTemporaryDirectory();
1319N/A try {
1319N/A // no-op
1319N/A Files.createDirectories(dir);
1319N/A
1319N/A // create one directory
1319N/A Path subdir = dir.resolve("a");
1319N/A Files.createDirectories(subdir);
1319N/A if (!subdir.exists())
1319N/A throw new RuntimeException("directory not created");
1319N/A
1319N/A // create parents
1319N/A subdir = subdir.resolve("b/c/d");
1319N/A Files.createDirectories(subdir);
1319N/A if (!subdir.exists())
1319N/A throw new RuntimeException("directory not created");
1319N/A
1319N/A // existing file is not a directory
1319N/A Path file = dir.resolve("x").createFile();
1319N/A try {
1319N/A Files.createDirectories(file);
1319N/A throw new RuntimeException("failure expected");
1319N/A } catch (FileAlreadyExistsException x) { }
1319N/A try {
1319N/A Files.createDirectories(file.resolve("y"));
1319N/A throw new RuntimeException("failure expected");
1319N/A } catch (IOException x) { }
1319N/A
1319N/A } finally {
1319N/A TestUtil.removeAll(dir);
1319N/A }
1319N/A
1319N/A // --- NullPointerException --
1319N/A
893N/A try {
893N/A Files.probeContentType(null);
893N/A npeExpected();
893N/A } catch (NullPointerException e) {
893N/A }
893N/A try {
893N/A Files.walkFileTree(null, EnumSet.noneOf(FileVisitOption.class),
893N/A Integer.MAX_VALUE, new SimpleFileVisitor<Path>(){});
893N/A npeExpected();
893N/A } catch (NullPointerException e) {
893N/A }
893N/A try {
893N/A Files.walkFileTree(Paths.get("."), null, Integer.MAX_VALUE,
893N/A new SimpleFileVisitor<Path>(){});
893N/A npeExpected();
893N/A } catch (NullPointerException e) {
893N/A }
893N/A try {
893N/A Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class),
893N/A -1, new SimpleFileVisitor<Path>(){});
893N/A throw new RuntimeException("IllegalArgumentExpected expected");
893N/A } catch (IllegalArgumentException e) {
893N/A }
893N/A try {
893N/A Set<FileVisitOption> opts = new HashSet<FileVisitOption>(1);
893N/A opts.add(null);
893N/A Files.walkFileTree(Paths.get("."), opts, Integer.MAX_VALUE,
893N/A new SimpleFileVisitor<Path>(){});
893N/A npeExpected();
893N/A } catch (NullPointerException e) {
893N/A }
893N/A try {
893N/A Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class),
893N/A Integer.MAX_VALUE, null);
893N/A npeExpected();
893N/A } catch (NullPointerException e) {
893N/A }
1541N/A
1541N/A SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { };
1541N/A boolean ranTheGauntlet = false;
1541N/A try { visitor.preVisitDirectory(null);
1541N/A } catch (NullPointerException x0) {
1541N/A try { visitor.preVisitDirectoryFailed(null, new IOException());
1541N/A } catch (NullPointerException x1) {
1541N/A try { visitor.preVisitDirectoryFailed(dir, null);
1541N/A } catch (NullPointerException x2) {
1541N/A try { visitor.visitFile(null, Attributes.readBasicFileAttributes(Paths.get(".")));
1541N/A } catch (NullPointerException x3) {
1541N/A try { visitor.visitFile(dir, null);
1541N/A } catch (NullPointerException x4) {
1541N/A try { visitor.visitFileFailed(null, new IOException());
1541N/A } catch (NullPointerException x5) {
1541N/A try { visitor.visitFileFailed(dir, null);
1541N/A } catch (NullPointerException x6) {
1541N/A try { visitor.postVisitDirectory(null, new IOException());
1541N/A } catch (NullPointerException x7) {
1541N/A // if we get here then all visit* methods threw NPE as expected
1541N/A ranTheGauntlet = true;
1541N/A }}}}}}}}
1541N/A if (!ranTheGauntlet)
1541N/A throw new RuntimeException("A visit method did not throw NPE");
893N/A }
893N/A}