|
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.attribute.Attributes |
|
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 Basic { |
|
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 // Exercise getAttribute/setAttribute/readAttributes on basic attributes |
|
56 static void checkBasicAttributes(FileRef file, BasicFileAttributes attrs) |
|
57 throws IOException |
|
58 { |
|
59 // getAttribute |
|
60 checkEqual(attrs.size(), Attributes.getAttribute(file, "size")); |
|
61 checkEqual(attrs.lastModifiedTime(), |
|
62 Attributes.getAttribute(file, "basic:lastModifiedTime")); |
|
63 checkEqual(attrs.lastAccessTime(), |
|
64 Attributes.getAttribute(file, "lastAccessTime")); |
|
65 checkEqual(attrs.creationTime(), |
|
66 Attributes.getAttribute(file, "basic:creationTime")); |
|
67 assertTrue((Boolean)Attributes.getAttribute(file, "isRegularFile")); |
|
68 assertTrue(!(Boolean)Attributes.getAttribute(file, "basic:isDirectory")); |
|
69 assertTrue(!(Boolean)Attributes.getAttribute(file, "isSymbolicLink")); |
|
70 assertTrue(!(Boolean)Attributes.getAttribute(file, "basic:isOther")); |
|
71 checkEqual(attrs.linkCount(), |
|
72 (Integer)Attributes.getAttribute(file, "linkCount")); |
|
73 checkEqual(attrs.fileKey(), Attributes.getAttribute(file, "basic:fileKey")); |
|
74 |
|
75 // setAttribute |
|
76 if (attrs.resolution() == TimeUnit.MILLISECONDS) { |
|
77 long modTime = attrs.lastModifiedTime(); |
|
78 Attributes.setAttribute(file, "basic:lastModifiedTime", 0L); |
|
79 assertTrue(Attributes.readBasicFileAttributes(file).lastModifiedTime() == 0L); |
|
80 Attributes.setAttribute(file, "lastModifiedTime", modTime); |
|
81 assertTrue(Attributes.readBasicFileAttributes(file).lastModifiedTime() == modTime); |
|
82 } |
|
83 |
|
84 // readAttributes |
|
85 Map<String,?> map; |
|
86 map = Attributes.readAttributes(file, "*"); |
|
87 assertTrue(map.size() >= 11); |
|
88 checkEqual(attrs.isRegularFile(), map.get("isRegularFile")); // check one |
|
89 |
|
90 map = Attributes.readAttributes(file, "basic:*"); |
|
91 assertTrue(map.size() >= 11); |
|
92 checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); // check one |
|
93 |
|
94 map = Attributes.readAttributes(file, "size,lastModifiedTime"); |
|
95 assertTrue(map.size() == 2); |
|
96 checkEqual(attrs.size(), map.get("size")); |
|
97 checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime")); |
|
98 |
|
99 map = Attributes.readAttributes(file, |
|
100 "basic:lastModifiedTime,lastAccessTime,linkCount,ShouldNotExist"); |
|
101 assertTrue(map.size() == 3); |
|
102 checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime")); |
|
103 checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); |
|
104 checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); |
|
105 } |
|
106 |
|
107 // Exercise getAttribute/setAttribute/readAttributes on posix attributes |
|
108 static void checkPosixAttributes(FileRef file, PosixFileAttributes attrs) |
|
109 throws IOException |
|
110 { |
|
111 checkBasicAttributes(file, attrs); |
|
112 |
|
113 // getAttribute |
|
114 checkEqual(attrs.permissions(), |
|
115 Attributes.getAttribute(file, "posix:permissions")); |
|
116 checkEqual(attrs.owner(), |
|
117 Attributes.getAttribute(file, "posix:owner")); |
|
118 checkEqual(attrs.group(), |
|
119 Attributes.getAttribute(file, "posix:group")); |
|
120 |
|
121 // setAttribute |
|
122 Set<PosixFilePermission> orig = attrs.permissions(); |
|
123 Set<PosixFilePermission> newPerms = new HashSet<PosixFilePermission>(orig); |
|
124 newPerms.remove(PosixFilePermission.OTHERS_READ); |
|
125 newPerms.remove(PosixFilePermission.OTHERS_WRITE); |
|
126 newPerms.remove(PosixFilePermission.OTHERS_EXECUTE); |
|
127 Attributes.setAttribute(file, "posix:permissions", newPerms); |
|
128 checkEqual(Attributes.readPosixFileAttributes(file).permissions(), newPerms); |
|
129 Attributes.setAttribute(file, "posix:permissions", orig); |
|
130 checkEqual(Attributes.readPosixFileAttributes(file).permissions(), orig); |
|
131 Attributes.setAttribute(file, "posix:owner", attrs.owner()); |
|
132 Attributes.setAttribute(file, "posix:group", attrs.group()); |
|
133 |
|
134 // readAttributes |
|
135 Map<String,?> map; |
|
136 map = Attributes.readAttributes(file, "posix:*"); |
|
137 assertTrue(map.size() >= 14); |
|
138 checkEqual(attrs.permissions(), map.get("permissions")); // check one |
|
139 |
|
140 map = Attributes.readAttributes(file, "posix:size,owner,ShouldNotExist"); |
|
141 assertTrue(map.size() == 2); |
|
142 checkEqual(attrs.size(), map.get("size")); |
|
143 checkEqual(attrs.owner(), map.get("owner")); |
|
144 } |
|
145 |
|
146 // Exercise getAttribute/setAttribute/readAttributes on unix attributes |
|
147 static void checkUnixAttributes(FileRef file) throws IOException { |
|
148 // getAttribute |
|
149 int mode = (Integer)Attributes.getAttribute(file, "unix:mode"); |
|
150 long ino = (Long)Attributes.getAttribute(file, "unix:ino"); |
|
151 long dev = (Long)Attributes.getAttribute(file, "unix:dev"); |
|
152 long rdev = (Long)Attributes.getAttribute(file, "unix:rdev"); |
|
153 int uid = (Integer)Attributes.getAttribute(file, "unix:uid"); |
|
154 int gid = (Integer)Attributes.getAttribute(file, "unix:gid"); |
|
155 long ctime = (Long)Attributes.getAttribute(file, "unix:ctime"); |
|
156 |
|
157 // readAttributes |
|
158 Map<String,?> map; |
|
159 map = Attributes.readAttributes(file, "unix:*"); |
|
160 assertTrue(map.size() >= 21); |
|
161 |
|
162 map = Attributes.readAttributes(file, "unix:size,uid,gid,ShouldNotExist"); |
|
163 assertTrue(map.size() == 3); |
|
164 checkEqual(map.get("size"), |
|
165 Attributes.readBasicFileAttributes(file).size()); |
|
166 } |
|
167 |
|
168 // Exercise getAttribute/setAttribute/readAttributes on dos attributes |
|
169 static void checkDosAttributes(FileRef file, DosFileAttributes attrs) |
|
170 throws IOException |
|
171 { |
|
172 checkBasicAttributes(file, attrs); |
|
173 |
|
174 // getAttribute |
|
175 checkEqual(attrs.isReadOnly(), |
|
176 Attributes.getAttribute(file, "dos:readonly")); |
|
177 checkEqual(attrs.isHidden(), |
|
178 Attributes.getAttribute(file, "dos:hidden")); |
|
179 checkEqual(attrs.isSystem(), |
|
180 Attributes.getAttribute(file, "dos:system")); |
|
181 checkEqual(attrs.isArchive(), |
|
182 Attributes.getAttribute(file, "dos:archive")); |
|
183 |
|
184 // setAttribute |
|
185 boolean value; |
|
186 |
|
187 value = attrs.isReadOnly(); |
|
188 Attributes.setAttribute(file, "dos:readonly", !value); |
|
189 checkEqual(Attributes.readDosFileAttributes(file).isReadOnly(), !value); |
|
190 Attributes.setAttribute(file, "dos:readonly", value); |
|
191 checkEqual(Attributes.readDosFileAttributes(file).isReadOnly(), value); |
|
192 |
|
193 value = attrs.isHidden(); |
|
194 Attributes.setAttribute(file, "dos:hidden", !value); |
|
195 checkEqual(Attributes.readDosFileAttributes(file).isHidden(), !value); |
|
196 Attributes.setAttribute(file, "dos:hidden", value); |
|
197 checkEqual(Attributes.readDosFileAttributes(file).isHidden(), value); |
|
198 |
|
199 value = attrs.isSystem(); |
|
200 Attributes.setAttribute(file, "dos:system", !value); |
|
201 checkEqual(Attributes.readDosFileAttributes(file).isSystem(), !value); |
|
202 Attributes.setAttribute(file, "dos:system", value); |
|
203 checkEqual(Attributes.readDosFileAttributes(file).isSystem(), value); |
|
204 |
|
205 value = attrs.isArchive(); |
|
206 Attributes.setAttribute(file, "dos:archive", !value); |
|
207 checkEqual(Attributes.readDosFileAttributes(file).isArchive(), !value); |
|
208 Attributes.setAttribute(file, "dos:archive", value); |
|
209 checkEqual(Attributes.readDosFileAttributes(file).isArchive(), value); |
|
210 |
|
211 // readAttributes |
|
212 Map<String,?> map; |
|
213 map = Attributes.readAttributes(file, "dos:*"); |
|
214 assertTrue(map.size() >= 15); |
|
215 checkEqual(attrs.isReadOnly(), map.get("readonly")); // check one |
|
216 |
|
217 map = Attributes.readAttributes(file, "dos:size,hidden,ShouldNotExist"); |
|
218 assertTrue(map.size() == 2); |
|
219 checkEqual(attrs.size(), map.get("size")); |
|
220 checkEqual(attrs.isHidden(), map.get("hidden")); |
|
221 } |
|
222 |
|
223 static void doTests(Path dir) throws IOException { |
|
224 Path file = dir.resolve("foo").createFile(); |
|
225 FileStore store = file.getFileStore(); |
|
226 try { |
|
227 checkBasicAttributes(file, |
|
228 Attributes.readBasicFileAttributes(file)); |
|
229 |
|
230 if (store.supportsFileAttributeView("posix")) |
|
231 checkPosixAttributes(file, |
|
232 Attributes.readPosixFileAttributes(file)); |
|
233 |
|
234 if (store.supportsFileAttributeView("unix")) |
|
235 checkUnixAttributes(file); |
|
236 |
|
237 if (store.supportsFileAttributeView("dos")) |
|
238 checkDosAttributes(file, |
|
239 Attributes.readDosFileAttributes(file)); |
|
240 } finally { |
|
241 file.delete(); |
|
242 } |
|
243 } |
|
244 |
|
245 |
|
246 public static void main(String[] args) throws IOException { |
|
247 Path dir = TestUtil.createTemporaryDirectory(); |
|
248 try { |
|
249 doTests(dir); |
|
250 } finally { |
|
251 TestUtil.removeAll(dir); |
|
252 } |
|
253 } |
|
254 } |