1 /* |
|
2 * Copyright (c) 2008, 2009, 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 |
|
26 * @summary Unit test for java.nio.file.Path |
|
27 * @library .. |
|
28 */ |
|
29 |
|
30 import java.nio.file.*; |
|
31 import java.nio.file.attribute.*; |
|
32 import java.io.IOException; |
|
33 import java.util.*; |
|
34 import java.util.concurrent.TimeUnit; |
|
35 |
|
36 /** |
|
37 * Exercises getAttribute/setAttribute/readAttributes methods. |
|
38 */ |
|
39 |
|
40 public class FileAttributes { |
|
41 |
|
42 static void assertTrue(boolean okay) { |
|
43 if (!okay) |
|
44 throw new RuntimeException("Assertion Failed"); |
|
45 } |
|
46 |
|
47 static void checkEqual(Object o1, Object o2) { |
|
48 if (o1 == null) { |
|
49 assertTrue(o2 == null); |
|
50 } else { |
|
51 assertTrue (o1.equals(o2)); |
|
52 } |
|
53 } |
|
54 |
|
55 // checks that two time values are within 1s of each other |
|
56 static void checkNearEqual(FileTime t1, FileTime t2) { |
|
57 long diff = Math.abs(t1.toMillis() - t2.toMillis()); |
|
58 assertTrue(diff <= 1000); |
|
59 } |
|
60 |
|
61 // Exercise getAttribute/setAttribute/readAttributes on basic attributes |
|
62 static void checkBasicAttributes(FileRef file, BasicFileAttributes attrs) |
|
63 throws IOException |
|
64 { |
|
65 // getAttribute |
|
66 checkEqual(attrs.size(), file.getAttribute("size")); |
|
67 checkEqual(attrs.lastModifiedTime(), file.getAttribute("basic:lastModifiedTime")); |
|
68 checkEqual(attrs.lastAccessTime(), file.getAttribute("lastAccessTime")); |
|
69 checkEqual(attrs.creationTime(), file.getAttribute("basic:creationTime")); |
|
70 assertTrue((Boolean)file.getAttribute("isRegularFile")); |
|
71 assertTrue(!(Boolean)file.getAttribute("basic:isDirectory")); |
|
72 assertTrue(!(Boolean)file.getAttribute("isSymbolicLink")); |
|
73 assertTrue(!(Boolean)file.getAttribute("basic:isOther")); |
|
74 checkEqual(attrs.fileKey(), file.getAttribute("basic:fileKey")); |
|
75 |
|
76 // setAttribute |
|
77 FileTime modTime = attrs.lastModifiedTime(); |
|
78 file.setAttribute("basic:lastModifiedTime", FileTime.fromMillis(0L)); |
|
79 checkEqual(Attributes.readBasicFileAttributes(file).lastModifiedTime(), |
|
80 FileTime.fromMillis(0L)); |
|
81 file.setAttribute("lastModifiedTime", modTime); |
|
82 checkEqual(Attributes.readBasicFileAttributes(file).lastModifiedTime(), modTime); |
|
83 |
|
84 Map<String,?> map; |
|
85 map = file.readAttributes("*"); |
|
86 assertTrue(map.size() >= 9); |
|
87 checkEqual(attrs.isRegularFile(), map.get("isRegularFile")); // check one |
|
88 |
|
89 map = file.readAttributes("basic:*"); |
|
90 assertTrue(map.size() >= 9); |
|
91 checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); // check one |
|
92 |
|
93 map = file.readAttributes("size,lastModifiedTime"); |
|
94 assertTrue(map.size() == 2); |
|
95 checkEqual(attrs.size(), map.get("size")); |
|
96 checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime")); |
|
97 |
|
98 map = file.readAttributes( |
|
99 "basic:lastModifiedTime,lastAccessTime,ShouldNotExist"); |
|
100 assertTrue(map.size() == 2); |
|
101 checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime")); |
|
102 checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); |
|
103 } |
|
104 |
|
105 // Exercise getAttribute/setAttribute/readAttributes on posix attributes |
|
106 static void checkPosixAttributes(FileRef file, PosixFileAttributes attrs) |
|
107 throws IOException |
|
108 { |
|
109 checkBasicAttributes(file, attrs); |
|
110 |
|
111 // getAttribute |
|
112 checkEqual(attrs.permissions(), file.getAttribute("posix:permissions")); |
|
113 checkEqual(attrs.owner(), file.getAttribute("posix:owner")); |
|
114 checkEqual(attrs.group(), file.getAttribute("posix:group")); |
|
115 |
|
116 // setAttribute |
|
117 Set<PosixFilePermission> orig = attrs.permissions(); |
|
118 Set<PosixFilePermission> newPerms = new HashSet<PosixFilePermission>(orig); |
|
119 newPerms.remove(PosixFilePermission.OTHERS_READ); |
|
120 newPerms.remove(PosixFilePermission.OTHERS_WRITE); |
|
121 newPerms.remove(PosixFilePermission.OTHERS_EXECUTE); |
|
122 file.setAttribute("posix:permissions", newPerms); |
|
123 checkEqual(Attributes.readPosixFileAttributes(file).permissions(), newPerms); |
|
124 file.setAttribute("posix:permissions", orig); |
|
125 checkEqual(Attributes.readPosixFileAttributes(file).permissions(), orig); |
|
126 file.setAttribute("posix:owner", attrs.owner()); |
|
127 file.setAttribute("posix:group", attrs.group()); |
|
128 |
|
129 // readAttributes |
|
130 Map<String,?> map; |
|
131 map = file.readAttributes("posix:*"); |
|
132 assertTrue(map.size() >= 12); |
|
133 checkEqual(attrs.permissions(), map.get("permissions")); // check one |
|
134 |
|
135 map = file.readAttributes("posix:size,owner,ShouldNotExist"); |
|
136 assertTrue(map.size() == 2); |
|
137 checkEqual(attrs.size(), map.get("size")); |
|
138 checkEqual(attrs.owner(), map.get("owner")); |
|
139 } |
|
140 |
|
141 // Exercise getAttribute/readAttributes on unix attributes |
|
142 static void checkUnixAttributes(FileRef file) throws IOException { |
|
143 // getAttribute |
|
144 int mode = (Integer)file.getAttribute("unix:mode"); |
|
145 long ino = (Long)file.getAttribute("unix:ino"); |
|
146 long dev = (Long)file.getAttribute("unix:dev"); |
|
147 long rdev = (Long)file.getAttribute("unix:rdev"); |
|
148 int nlink = (Integer)file.getAttribute("unix:nlink"); |
|
149 int uid = (Integer)file.getAttribute("unix:uid"); |
|
150 int gid = (Integer)file.getAttribute("unix:gid"); |
|
151 FileTime ctime = (FileTime)file.getAttribute("unix:ctime"); |
|
152 |
|
153 // readAttributes |
|
154 Map<String,?> map; |
|
155 map = file.readAttributes("unix:*"); |
|
156 assertTrue(map.size() >= 20); |
|
157 |
|
158 map = file.readAttributes("unix:size,uid,gid,ShouldNotExist"); |
|
159 assertTrue(map.size() == 3); |
|
160 checkEqual(map.get("size"), |
|
161 Attributes.readBasicFileAttributes(file).size()); |
|
162 } |
|
163 |
|
164 // Exercise getAttribute/setAttribute on dos attributes |
|
165 static void checkDosAttributes(FileRef file, DosFileAttributes attrs) |
|
166 throws IOException |
|
167 { |
|
168 checkBasicAttributes(file, attrs); |
|
169 |
|
170 // getAttribute |
|
171 checkEqual(attrs.isReadOnly(), file.getAttribute("dos:readonly")); |
|
172 checkEqual(attrs.isHidden(), file.getAttribute("dos:hidden")); |
|
173 checkEqual(attrs.isSystem(), file.getAttribute("dos:system")); |
|
174 checkEqual(attrs.isArchive(), file.getAttribute("dos:archive")); |
|
175 |
|
176 // setAttribute |
|
177 boolean value; |
|
178 |
|
179 value = attrs.isReadOnly(); |
|
180 file.setAttribute("dos:readonly", !value); |
|
181 checkEqual(Attributes.readDosFileAttributes(file).isReadOnly(), !value); |
|
182 file.setAttribute("dos:readonly", value); |
|
183 checkEqual(Attributes.readDosFileAttributes(file).isReadOnly(), value); |
|
184 |
|
185 value = attrs.isHidden(); |
|
186 file.setAttribute("dos:hidden", !value); |
|
187 checkEqual(Attributes.readDosFileAttributes(file).isHidden(), !value); |
|
188 file.setAttribute("dos:hidden", value); |
|
189 checkEqual(Attributes.readDosFileAttributes(file).isHidden(), value); |
|
190 |
|
191 value = attrs.isSystem(); |
|
192 file.setAttribute("dos:system", !value); |
|
193 checkEqual(Attributes.readDosFileAttributes(file).isSystem(), !value); |
|
194 file.setAttribute("dos:system", value); |
|
195 checkEqual(Attributes.readDosFileAttributes(file).isSystem(), value); |
|
196 |
|
197 value = attrs.isArchive(); |
|
198 file.setAttribute("dos:archive", !value); |
|
199 checkEqual(Attributes.readDosFileAttributes(file).isArchive(), !value); |
|
200 file.setAttribute("dos:archive", value); |
|
201 checkEqual(Attributes.readDosFileAttributes(file).isArchive(), value); |
|
202 |
|
203 // readAttributes |
|
204 Map<String,?> map; |
|
205 map = file.readAttributes("dos:*"); |
|
206 assertTrue(map.size() >= 13); |
|
207 checkEqual(attrs.isReadOnly(), map.get("readonly")); // check one |
|
208 |
|
209 map = file.readAttributes("dos:size,hidden,ShouldNotExist"); |
|
210 assertTrue(map.size() == 2); |
|
211 checkEqual(attrs.size(), map.get("size")); |
|
212 checkEqual(attrs.isHidden(), map.get("hidden")); |
|
213 } |
|
214 |
|
215 static void miscTests(Path file) throws IOException { |
|
216 // NPE tests |
|
217 try { |
|
218 file.getAttribute(null); |
|
219 throw new RuntimeException("NullPointerException expected"); |
|
220 } catch (NullPointerException npe) { } |
|
221 try { |
|
222 file.getAttribute("isRegularFile", (LinkOption[])null); |
|
223 throw new RuntimeException("NullPointerException expected"); |
|
224 } catch (NullPointerException npe) { } |
|
225 try { |
|
226 file.setAttribute(null, 0L); |
|
227 throw new RuntimeException("NullPointerException expected"); |
|
228 } catch (NullPointerException npe) { } |
|
229 } |
|
230 |
|
231 static void doTests(Path dir) throws IOException { |
|
232 Path file = dir.resolve("foo").createFile(); |
|
233 FileStore store = file.getFileStore(); |
|
234 try { |
|
235 checkBasicAttributes(file, |
|
236 Attributes.readBasicFileAttributes(file)); |
|
237 |
|
238 if (store.supportsFileAttributeView("posix")) |
|
239 checkPosixAttributes(file, |
|
240 Attributes.readPosixFileAttributes(file)); |
|
241 |
|
242 if (store.supportsFileAttributeView("unix")) |
|
243 checkUnixAttributes(file); |
|
244 |
|
245 if (store.supportsFileAttributeView("dos")) |
|
246 checkDosAttributes(file, |
|
247 Attributes.readDosFileAttributes(file)); |
|
248 |
|
249 miscTests(file); |
|
250 } finally { |
|
251 file.delete(); |
|
252 } |
|
253 } |
|
254 |
|
255 |
|
256 public static void main(String[] args) throws IOException { |
|
257 Path dir = TestUtil.createTemporaryDirectory(); |
|
258 try { |
|
259 doTests(dir); |
|
260 } finally { |
|
261 TestUtil.removeAll(dir); |
|
262 } |
|
263 } |
|
264 } |
|