1/*
2 * Copyright (c) 2008, 2017, 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 8132497
26 * @summary Unit test for java.nio.file.FileSystem
27 * @library .. /lib/testlibrary
28 * @build jdk.testlibrary.FileUtils
29 * @run main/othervm Basic
30 */
31
32import java.io.File;
33import java.io.IOException;
34import java.net.URI;
35import java.net.URISyntaxException;
36import java.nio.file.Files;
37import java.nio.file.FileStore;
38import java.nio.file.FileSystem;
39import java.nio.file.FileSystems;
40import java.nio.file.Path;
41import java.nio.file.Paths;
42import java.nio.file.ProviderNotFoundException;
43import java.util.HashMap;
44import jdk.testlibrary.FileUtils;
45
46/**
47 * Simple sanity checks for java.nio.file.FileSystem
48 */
49public class Basic {
50
51    static void check(boolean okay, String msg) {
52        if (!okay)
53            throw new RuntimeException(msg);
54    }
55
56    static void checkFileStores(FileSystem fs) throws IOException {
57        // sanity check method
58        if (FileUtils.areFileSystemsAccessible()) {
59            System.out.println("\n--- Begin FileStores ---");
60            for (FileStore store: fs.getFileStores()) {
61                System.out.println(store);
62            }
63            System.out.println("--- EndFileStores ---\n");
64        } else {
65            System.err.println
66                ("Skipping FileStore check due to file system access failure");
67        }
68    }
69
70    static void checkSupported(FileSystem fs, String... views) {
71        for (String view: views) {
72            check(fs.supportedFileAttributeViews().contains(view),
73                "support for '" + view + "' expected");
74        }
75    }
76
77    static void checkNoUOE() throws IOException, URISyntaxException {
78        String dir = System.getProperty("test.dir", ".");
79        String fileName = dir + File.separator + "foo.bar";
80        Path path = Paths.get(fileName);
81        Path file = Files.createFile(path);
82        try {
83            URI uri = new URI("jar", file.toUri().toString(), null);
84            System.out.println(uri);
85            FileSystem fs = FileSystems.newFileSystem(uri, new HashMap());
86            fs.close();
87        } catch (ProviderNotFoundException pnfe) {
88            System.out.println("Expected ProviderNotFoundException caught: "
89                + "\"" + pnfe.getMessage() + "\"");
90        }
91    }
92
93    public static void main(String[] args)
94        throws IOException, URISyntaxException {
95        String os = System.getProperty("os.name");
96        FileSystem fs = FileSystems.getDefault();
97
98        // close should throw UOE
99        try {
100            fs.close();
101            throw new RuntimeException("UnsupportedOperationException expected");
102        } catch (UnsupportedOperationException e) { }
103        check(fs.isOpen(), "should be open");
104
105        check(!fs.isReadOnly(), "should provide read-write access");
106
107        check(fs.provider().getScheme().equals("file"),
108            "should use 'file' scheme");
109
110        // sanity check FileStores
111        checkFileStores(fs);
112
113        // sanity check supportedFileAttributeViews
114        checkSupported(fs, "basic");
115        if (os.equals("SunOS"))
116            checkSupported(fs, "posix", "unix", "owner", "acl", "user");
117        if (os.equals("Linux"))
118            checkSupported(fs, "posix", "unix", "owner", "dos", "user");
119        if (os.contains("OS X"))
120            checkSupported(fs, "posix", "unix", "owner");
121        if (os.equals("Windows"))
122            checkSupported(fs, "owner", "dos", "acl", "user");
123
124        // sanity check non-throwing of UnsupportedOperationException by
125        // FileSystems.newFileSystem(URI, ..)
126        checkNoUOE();
127    }
128}
129