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.Files
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 }
77 FileTime modTime = attrs.lastModifiedTime();
78 Files.setAttribute(file, "basic:lastModifiedTime", FileTime.fromMillis(0L));
79 checkEqual(Files.getLastModifiedTime(file),
80 FileTime.fromMillis(0L));
81 Files.setAttribute(file, "lastModifiedTime", modTime);
82 checkEqual(Files.getLastModifiedTime(file), modTime);
83
84 Map<String,Object> map;
85 map = Files.readAttributes(file, "*");
86 assertTrue(map.size() >= 9);
87 checkEqual(attrs.isRegularFile(), map.get("isRegularFile")); // check one
88
89 map = Files.readAttributes(file, "basic:*");
90 assertTrue(map.size() >= 9);
91 checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); // check one
92
93 map = Files.readAttributes(file, "size,lastModifiedTime");
94 assertTrue(map.size() == 2);
95 checkEqual(attrs.size(), map.get("size"));
96 checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime"));
97
98 map = Files.readAttributes(file,
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(Path file, PosixFileAttributes attrs)
107 throws IOException
108 {
109 checkBasicAttributes(file, attrs);
110
111 // getAttribute
112 checkEqual(attrs.permissions(), Files.getAttribute(file, "posix:permissions"));
113 checkEqual(attrs.owner(), Files.getAttribute(file, "posix:owner"));
114 checkEqual(attrs.group(), Files.getAttribute(file, "posix:group"));
115
116 // setAttribute
117 Set<PosixFilePermission> orig = attrs.permissions();
118 Set<PosixFilePermission> newPerms = new HashSet<>(orig);
119 newPerms.remove(PosixFilePermission.OTHERS_READ);
120 newPerms.remove(PosixFilePermission.OTHERS_WRITE);
121 newPerms.remove(PosixFilePermission.OTHERS_EXECUTE);
122 Files.setAttribute(file, "posix:permissions", newPerms);
123 checkEqual(Files.getPosixFilePermissions(file), newPerms);
124 Files.setAttribute(file, "posix:permissions", orig);
125 checkEqual(Files.getPosixFilePermissions(file), orig);
126 Files.setAttribute(file, "posix:owner", attrs.owner());
127 Files.setAttribute(file, "posix:group", attrs.group());
128
129 // readAttributes
130 Map<String,Object> map;
131 map = Files.readAttributes(file, "posix:*");
132 assertTrue(map.size() >= 12);
133 checkEqual(attrs.permissions(), map.get("permissions")); // check one
134
135 map = Files.readAttributes(file, "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(Path file) throws IOException {
143 // getAttribute
144 int mode = (Integer)Files.getAttribute(file, "unix:mode");
145 long ino = (Long)Files.getAttribute(file, "unix:ino");
146 long dev = (Long)Files.getAttribute(file, "unix:dev");
147 long rdev = (Long)Files.getAttribute(file, "unix:rdev");
148 int nlink = (Integer)Files.getAttribute(file, "unix:nlink");
149 int uid = (Integer)Files.getAttribute(file, "unix:uid");
150 int gid = (Integer)Files.getAttribute(file, "unix:gid");
151 FileTime ctime = (FileTime)Files.getAttribute(file, "unix:ctime");
152
153 // readAttributes
154 Map<String,Object> map;
155 map = Files.readAttributes(file, "unix:*");
156 assertTrue(map.size() >= 20);
157
158 map = Files.readAttributes(file, "unix:size,uid,gid,ShouldNotExist");
159 assertTrue(map.size() == 3);
160 checkEqual(map.get("size"),
161 Files.readAttributes(file, BasicFileAttributes.class).size());
162 }
163
164 // Exercise getAttribute/setAttribute on dos attributes
165 static void checkDosAttributes(Path file, DosFileAttributes attrs)
166 throws IOException
167 {
168 checkBasicAttributes(file, attrs);
169
170 // getAttribute
171 checkEqual(attrs.isReadOnly(), Files.getAttribute(file, "dos:readonly"));
172 checkEqual(attrs.isHidden(), Files.getAttribute(file, "dos:hidden"));
173 checkEqual(attrs.isSystem(), Files.getAttribute(file, "dos:system"));
174 checkEqual(attrs.isArchive(), Files.getAttribute(file, "dos:archive"));
175
176 // setAttribute
177 boolean value;
178
189 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isHidden(), value);
190
191 value = attrs.isSystem();
192 Files.setAttribute(file, "dos:system", !value);
193 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isSystem(), !value);
194 Files.setAttribute(file, "dos:system", value);
195 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isSystem(), value);
196
197 value = attrs.isArchive();
198 Files.setAttribute(file, "dos:archive", !value);
199 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isArchive(), !value);
200 Files.setAttribute(file, "dos:archive", value);
201 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isArchive(), value);
202
203 // readAttributes
204 Map<String,Object> map;
205 map = Files.readAttributes(file, "dos:*");
206 assertTrue(map.size() >= 13);
207 checkEqual(attrs.isReadOnly(), map.get("readonly")); // check one
208
209 map = Files.readAttributes(file, "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 Files.getAttribute(file, null);
219 throw new RuntimeException("NullPointerException expected");
220 } catch (NullPointerException npe) { }
221 try {
222 Files.getAttribute(file, "isRegularFile", (LinkOption[])null);
223 throw new RuntimeException("NullPointerException expected");
224 } catch (NullPointerException npe) { }
225 try {
226 Files.setAttribute(file, 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");
233 Files.createFile(file);
234 FileStore store = Files.getFileStore(file);
235 try {
236 checkBasicAttributes(file,
237 Files.readAttributes(file, BasicFileAttributes.class));
|
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 7017446
26 * @summary Unit test for java.nio.file.Files
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 }
77 FileTime modTime = attrs.lastModifiedTime();
78 Files.setAttribute(file, "basic:lastModifiedTime", FileTime.fromMillis(0L));
79 checkEqual(Files.getLastModifiedTime(file),
80 FileTime.fromMillis(0L));
81 Files.setAttribute(file, "lastModifiedTime", modTime);
82 checkEqual(Files.getLastModifiedTime(file), modTime);
83
84 Map<String,Object> map;
85 map = Files.readAttributes(file, "*");
86 assertTrue(map.size() >= 9);
87 checkEqual(attrs.isRegularFile(), map.get("isRegularFile")); // check one
88
89 map = Files.readAttributes(file, "basic:*");
90 assertTrue(map.size() >= 9);
91 checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); // check one
92
93 map = Files.readAttributes(file, "size,lastModifiedTime");
94 assertTrue(map.size() == 2);
95 checkEqual(attrs.size(), map.get("size"));
96 checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime"));
97 }
98
99 // Exercise getAttribute/setAttribute/readAttributes on posix attributes
100 static void checkPosixAttributes(Path file, PosixFileAttributes attrs)
101 throws IOException
102 {
103 checkBasicAttributes(file, attrs);
104
105 // getAttribute
106 checkEqual(attrs.permissions(), Files.getAttribute(file, "posix:permissions"));
107 checkEqual(attrs.owner(), Files.getAttribute(file, "posix:owner"));
108 checkEqual(attrs.group(), Files.getAttribute(file, "posix:group"));
109
110 // setAttribute
111 Set<PosixFilePermission> orig = attrs.permissions();
112 Set<PosixFilePermission> newPerms = new HashSet<>(orig);
113 newPerms.remove(PosixFilePermission.OTHERS_READ);
114 newPerms.remove(PosixFilePermission.OTHERS_WRITE);
115 newPerms.remove(PosixFilePermission.OTHERS_EXECUTE);
116 Files.setAttribute(file, "posix:permissions", newPerms);
117 checkEqual(Files.getPosixFilePermissions(file), newPerms);
118 Files.setAttribute(file, "posix:permissions", orig);
119 checkEqual(Files.getPosixFilePermissions(file), orig);
120 Files.setAttribute(file, "posix:owner", attrs.owner());
121 Files.setAttribute(file, "posix:group", attrs.group());
122
123 // readAttributes
124 Map<String,Object> map;
125 map = Files.readAttributes(file, "posix:*");
126 assertTrue(map.size() >= 12);
127 checkEqual(attrs.permissions(), map.get("permissions")); // check one
128
129 map = Files.readAttributes(file, "posix:size,owner");
130 assertTrue(map.size() == 2);
131 checkEqual(attrs.size(), map.get("size"));
132 checkEqual(attrs.owner(), map.get("owner"));
133 }
134
135 // Exercise getAttribute/readAttributes on unix attributes
136 static void checkUnixAttributes(Path file) throws IOException {
137 // getAttribute
138 int mode = (Integer)Files.getAttribute(file, "unix:mode");
139 long ino = (Long)Files.getAttribute(file, "unix:ino");
140 long dev = (Long)Files.getAttribute(file, "unix:dev");
141 long rdev = (Long)Files.getAttribute(file, "unix:rdev");
142 int nlink = (Integer)Files.getAttribute(file, "unix:nlink");
143 int uid = (Integer)Files.getAttribute(file, "unix:uid");
144 int gid = (Integer)Files.getAttribute(file, "unix:gid");
145 FileTime ctime = (FileTime)Files.getAttribute(file, "unix:ctime");
146
147 // readAttributes
148 Map<String,Object> map;
149 map = Files.readAttributes(file, "unix:*");
150 assertTrue(map.size() >= 20);
151
152 map = Files.readAttributes(file, "unix:size,uid,gid");
153 assertTrue(map.size() == 3);
154 checkEqual(map.get("size"),
155 Files.readAttributes(file, BasicFileAttributes.class).size());
156 }
157
158 // Exercise getAttribute/setAttribute on dos attributes
159 static void checkDosAttributes(Path file, DosFileAttributes attrs)
160 throws IOException
161 {
162 checkBasicAttributes(file, attrs);
163
164 // getAttribute
165 checkEqual(attrs.isReadOnly(), Files.getAttribute(file, "dos:readonly"));
166 checkEqual(attrs.isHidden(), Files.getAttribute(file, "dos:hidden"));
167 checkEqual(attrs.isSystem(), Files.getAttribute(file, "dos:system"));
168 checkEqual(attrs.isArchive(), Files.getAttribute(file, "dos:archive"));
169
170 // setAttribute
171 boolean value;
172
183 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isHidden(), value);
184
185 value = attrs.isSystem();
186 Files.setAttribute(file, "dos:system", !value);
187 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isSystem(), !value);
188 Files.setAttribute(file, "dos:system", value);
189 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isSystem(), value);
190
191 value = attrs.isArchive();
192 Files.setAttribute(file, "dos:archive", !value);
193 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isArchive(), !value);
194 Files.setAttribute(file, "dos:archive", value);
195 checkEqual(Files.readAttributes(file, DosFileAttributes.class).isArchive(), value);
196
197 // readAttributes
198 Map<String,Object> map;
199 map = Files.readAttributes(file, "dos:*");
200 assertTrue(map.size() >= 13);
201 checkEqual(attrs.isReadOnly(), map.get("readonly")); // check one
202
203 map = Files.readAttributes(file, "dos:size,hidden");
204 assertTrue(map.size() == 2);
205 checkEqual(attrs.size(), map.get("size"));
206 checkEqual(attrs.isHidden(), map.get("hidden"));
207 }
208
209 static void checkBadSet(Path file, String attribute, Object value)
210 throws IOException
211 {
212 try {
213 Files.setAttribute(file, attribute, 0);
214 throw new RuntimeException("IllegalArgumentException expected");
215 } catch (IllegalArgumentException ignore) { }
216 }
217
218 static void checkBadGet(Path file, String attribute) throws IOException {
219 try {
220 Files.getAttribute(file, attribute);
221 throw new RuntimeException("IllegalArgumentException expected");
222 } catch (IllegalArgumentException ignore) { }
223 }
224
225 static void checkBadRead(Path file, String attribute) throws IOException {
226 try {
227 Files.readAttributes(file, attribute);
228 throw new RuntimeException("IllegalArgumentException expected");
229 } catch (IllegalArgumentException ignore) { }
230 }
231
232 static void miscTests(Path file) throws IOException {
233 // unsupported views
234 try {
235 Files.setAttribute(file, "foo:bar", 0);
236 throw new RuntimeException("UnsupportedOperationException expected");
237 } catch (UnsupportedOperationException ignore) { }
238 try {
239 Files.getAttribute(file, "foo:bar");
240 throw new RuntimeException("UnsupportedOperationException expected");
241 } catch (UnsupportedOperationException ignore) { }
242 try {
243 Files.readAttributes(file, "foo:*");
244 throw new RuntimeException("UnsupportedOperationException expected");
245 } catch (UnsupportedOperationException ignore) { }
246
247 // bad args
248 checkBadSet(file, "", 0);
249 checkBadSet(file, "basic:", 0);
250 checkBadSet(file, "basic:foobar", 0);
251 checkBadGet(file, "");
252 checkBadGet(file, "basic:");
253 checkBadGet(file, "basic:foobar");
254 checkBadGet(file, "basic:size,lastModifiedTime");
255 checkBadGet(file, "basic:*");
256 checkBadRead(file, "");
257 checkBadRead(file, "basic:");
258 checkBadRead(file, "basic:foobar");
259 checkBadRead(file, "basic:size,foobar");
260
261 // nulls
262 try {
263 Files.getAttribute(file, null);
264 throw new RuntimeException("NullPointerException expected");
265 } catch (NullPointerException npe) { }
266 try {
267 Files.getAttribute(file, "isRegularFile", (LinkOption[])null);
268 throw new RuntimeException("NullPointerException expected");
269 } catch (NullPointerException npe) { }
270 try {
271 Files.setAttribute(file, null, 0L);
272 throw new RuntimeException("NullPointerException expected");
273 } catch (NullPointerException npe) { }
274 }
275
276 static void doTests(Path dir) throws IOException {
277 Path file = dir.resolve("foo");
278 Files.createFile(file);
279 FileStore store = Files.getFileStore(file);
280 try {
281 checkBasicAttributes(file,
282 Files.readAttributes(file, BasicFileAttributes.class));
|