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 6873621 6979526 7006126 7020517
26 * @summary Unit test for java.nio.file.FileStore
27 * @key intermittent
28 * @library .. /test/lib
29 * @build jdk.test.lib.Platform
30 *        jdk.test.lib.util.FileUtils
31 * @run main Basic
32 */
33
34import java.nio.file.*;
35import java.nio.file.attribute.*;
36import java.io.File;
37import java.io.IOException;
38
39import jdk.test.lib.util.FileUtils;
40
41
42public class Basic {
43
44    static final long G = 1024L * 1024L * 1024L;
45
46    public static void main(String[] args) throws IOException {
47        Path dir = TestUtil.createTemporaryDirectory();
48        try {
49            doTests(dir);
50        } finally {
51            TestUtil.removeAll(dir);
52        }
53    }
54
55    static void assertTrue(boolean okay) {
56        if (!okay)
57            throw new RuntimeException("Assertion failed");
58    }
59
60    static void checkWithin1GB(long value1, long value2) {
61        long diff = Math.abs(value1 - value2);
62        if (diff > G)
63            throw new RuntimeException("values differ by more than 1GB");
64    }
65
66    static void doTests(Path dir) throws IOException {
67        /**
68         * Test: Directory should be on FileStore that is writable
69         */
70        assertTrue(!Files.getFileStore(dir).isReadOnly());
71
72        /**
73         * Test: Two files should have the same FileStore
74         */
75        Path file1 = Files.createFile(dir.resolve("foo"));
76        Path file2 = Files.createFile(dir.resolve("bar"));
77        FileStore store1 = Files.getFileStore(file1);
78        FileStore store2 = Files.getFileStore(file2);
79        assertTrue(store1.equals(store2));
80        assertTrue(store2.equals(store1));
81        assertTrue(store1.hashCode() == store2.hashCode());
82
83        /**
84         * Test: File and FileStore attributes
85         */
86        assertTrue(store1.supportsFileAttributeView("basic"));
87        assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));
88        assertTrue(store1.supportsFileAttributeView("posix") ==
89            store1.supportsFileAttributeView(PosixFileAttributeView.class));
90        assertTrue(store1.supportsFileAttributeView("dos") ==
91            store1.supportsFileAttributeView(DosFileAttributeView.class));
92        assertTrue(store1.supportsFileAttributeView("acl") ==
93            store1.supportsFileAttributeView(AclFileAttributeView.class));
94        assertTrue(store1.supportsFileAttributeView("user") ==
95            store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));
96
97        /**
98         * Test: Space atributes
99         */
100        File f = file1.toFile();
101        long total = f.getTotalSpace();
102        long free = f.getFreeSpace();
103        long usable = f.getUsableSpace();
104
105        // check values are "close"
106        checkWithin1GB(total,  store1.getTotalSpace());
107        checkWithin1GB(free,   store1.getUnallocatedSpace());
108        checkWithin1GB(usable, store1.getUsableSpace());
109
110        // get values by name
111        checkWithin1GB(total,  (Long)store1.getAttribute("totalSpace"));
112        checkWithin1GB(free,   (Long)store1.getAttribute("unallocatedSpace"));
113        checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));
114
115        /**
116         * Test: Enumerate all FileStores
117         */
118        if (FileUtils.areFileSystemsAccessible()) {
119            FileStore prev = null;
120            for (FileStore store: FileSystems.getDefault().getFileStores()) {
121                System.out.format("%s (name=%s type=%s)\n", store, store.name(),
122                    store.type());
123
124                // check space attributes are accessible
125                try {
126                    store.getTotalSpace();
127                    store.getUnallocatedSpace();
128                    store.getUsableSpace();
129                } catch (NoSuchFileException nsfe) {
130                    // ignore exception as the store could have been
131                    // deleted since the iterator was instantiated
132                    System.err.format("%s was not found\n", store);
133                } catch (AccessDeniedException ade) {
134                    // ignore exception as the lack of ability to access the
135                    // store due to lack of file permission or similar does not
136                    // reflect whether the space attributes would be accessible
137                    // were access to be permitted
138                    System.err.format("%s is inaccessible\n", store);
139                }
140
141                // two distinct FileStores should not be equal
142                assertTrue(!store.equals(prev));
143                prev = store;
144            }
145        } else {
146            System.err.println
147                ("Skipping FileStore check due to file system access failure");
148        }
149    }
150}
151