Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/FileStore/Basic.java
41153 views
1
/*
2
* Copyright (c) 2008, 2021, 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 8264400
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
34
import java.nio.file.*;
35
import java.nio.file.attribute.*;
36
import java.io.File;
37
import java.io.IOException;
38
39
import jdk.test.lib.Platform;
40
import jdk.test.lib.util.FileUtils;
41
42
public 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
if (Platform.isWindows()) {
84
/**
85
* Test: FileStore.equals() should not be case sensitive
86
*/
87
FileStore upper = Files.getFileStore(Path.of("C:\\"));
88
FileStore lower = Files.getFileStore(Path.of("c:\\"));
89
assertTrue(lower.equals(upper));
90
}
91
92
/**
93
* Test: File and FileStore attributes
94
*/
95
assertTrue(store1.supportsFileAttributeView("basic"));
96
assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));
97
assertTrue(store1.supportsFileAttributeView("posix") ==
98
store1.supportsFileAttributeView(PosixFileAttributeView.class));
99
assertTrue(store1.supportsFileAttributeView("dos") ==
100
store1.supportsFileAttributeView(DosFileAttributeView.class));
101
assertTrue(store1.supportsFileAttributeView("acl") ==
102
store1.supportsFileAttributeView(AclFileAttributeView.class));
103
assertTrue(store1.supportsFileAttributeView("user") ==
104
store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));
105
106
/**
107
* Test: Space atributes
108
*/
109
File f = file1.toFile();
110
long total = f.getTotalSpace();
111
long free = f.getFreeSpace();
112
long usable = f.getUsableSpace();
113
114
// check values are "close"
115
checkWithin1GB(total, store1.getTotalSpace());
116
checkWithin1GB(free, store1.getUnallocatedSpace());
117
checkWithin1GB(usable, store1.getUsableSpace());
118
119
// get values by name
120
checkWithin1GB(total, (Long)store1.getAttribute("totalSpace"));
121
checkWithin1GB(free, (Long)store1.getAttribute("unallocatedSpace"));
122
checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));
123
124
/**
125
* Test: Enumerate all FileStores
126
*/
127
if (FileUtils.areMountPointsAccessibleAndUnique()) {
128
FileStore prev = null;
129
for (FileStore store: FileSystems.getDefault().getFileStores()) {
130
System.out.format("%s (name=%s type=%s)\n", store, store.name(),
131
store.type());
132
133
// check space attributes are accessible
134
try {
135
store.getTotalSpace();
136
store.getUnallocatedSpace();
137
store.getUsableSpace();
138
} catch (NoSuchFileException nsfe) {
139
// ignore exception as the store could have been
140
// deleted since the iterator was instantiated
141
System.err.format("%s was not found\n", store);
142
} catch (AccessDeniedException ade) {
143
// ignore exception as the lack of ability to access the
144
// store due to lack of file permission or similar does not
145
// reflect whether the space attributes would be accessible
146
// were access to be permitted
147
System.err.format("%s is inaccessible\n", store);
148
}
149
150
// two distinct FileStores should not be equal
151
assertTrue(!store.equals(prev));
152
prev = store;
153
}
154
} else {
155
System.err.println
156
("Skipping FileStore check due to file system access failure");
157
}
158
}
159
}
160
161