Misc.java revision 893:f06f30b29f36
1/*
2 * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4313887
26 * @summary Unit test for java.nio.file.Files for miscellenous cases not
27 *   covered by other tests
28 * @library ..
29 */
30
31import java.nio.file.*;
32import java.io.IOException;
33import java.util.*;
34
35public class Misc {
36
37    static void npeExpected() {
38        throw new RuntimeException("NullPointerException expected");
39    }
40
41    public static void main(String[] args) throws IOException {
42        try {
43            Files.probeContentType(null);
44            npeExpected();
45        } catch (NullPointerException e) {
46        }
47
48        try {
49            Files.withDirectory(null, "*", new FileAction<Path>() {
50                public void invoke(Path entry) {
51                }
52            });
53            npeExpected();
54        } catch (NullPointerException e) {
55        }
56
57       try {
58            Files.withDirectory(Paths.get("."), (String)null, new FileAction<Path>() {
59                public void invoke(Path entry) {
60                }
61            });
62            npeExpected();
63        } catch (NullPointerException e) {
64        }
65
66        try {
67            Files.withDirectory(Paths.get("."), "*", null);
68            npeExpected();
69        } catch (NullPointerException e) {
70        }
71
72        // test propogation of IOException
73        Path tmpdir = TestUtil.createTemporaryDirectory();
74        try {
75            tmpdir.resolve("foo").createFile();
76            try {
77                Files.withDirectory(tmpdir, new FileAction<Path>() {
78                    public void invoke(Path entry) throws IOException {
79                        throw new IOException();
80                    }
81                });
82                throw new RuntimeException("IOException expected");
83            } catch (IOException e) {
84            }
85        } finally {
86            TestUtil.removeAll(tmpdir);
87        }
88
89        try {
90            Files.walkFileTree(null, EnumSet.noneOf(FileVisitOption.class),
91                Integer.MAX_VALUE, new SimpleFileVisitor<Path>(){});
92            npeExpected();
93        } catch (NullPointerException e) {
94        }
95
96        try {
97            Files.walkFileTree(Paths.get("."), null, Integer.MAX_VALUE,
98                new SimpleFileVisitor<Path>(){});
99            npeExpected();
100        } catch (NullPointerException e) {
101        }
102
103        try {
104            Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class),
105                -1, new SimpleFileVisitor<Path>(){});
106            throw new RuntimeException("IllegalArgumentExpected expected");
107        } catch (IllegalArgumentException e) {
108        }
109
110        try {
111            Set<FileVisitOption> opts = new HashSet<FileVisitOption>(1);
112            opts.add(null);
113            Files.walkFileTree(Paths.get("."), opts, Integer.MAX_VALUE,
114                new SimpleFileVisitor<Path>(){});
115            npeExpected();
116        } catch (NullPointerException e) {
117        }
118
119        try {
120            Files.walkFileTree(Paths.get("."), EnumSet.noneOf(FileVisitOption.class),
121                Integer.MAX_VALUE, null);
122            npeExpected();
123        } catch (NullPointerException e) {
124        }
125    }
126}
127