author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8808 | fc799c458da8 |
child 27775 | 4ee8b208017c |
permissions | -rw-r--r-- |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8808
diff
changeset
|
2 |
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
4 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
8 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
14 |
* |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
18 |
* |
5506 | 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. |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
22 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
23 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
24 |
/* @test |
8808 | 25 |
* @bug 4313887 6838333 7017446 |
8158 | 26 |
* @summary Unit test for java.nio.file.Files |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
27 |
* @library .. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
28 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
29 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
30 |
import java.nio.file.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
31 |
import java.nio.file.attribute.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
32 |
import java.io.IOException; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
33 |
import java.util.*; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
34 |
import java.util.concurrent.TimeUnit; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
35 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
36 |
/** |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
37 |
* Exercises getAttribute/setAttribute/readAttributes methods. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
38 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
39 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
40 |
public class FileAttributes { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
41 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
42 |
static void assertTrue(boolean okay) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
43 |
if (!okay) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
44 |
throw new RuntimeException("Assertion Failed"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
45 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
46 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
47 |
static void checkEqual(Object o1, Object o2) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
48 |
if (o1 == null) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
49 |
assertTrue(o2 == null); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
50 |
} else { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
51 |
assertTrue (o1.equals(o2)); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
52 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
53 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
54 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
55 |
// checks that two time values are within 1s of each other |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
56 |
static void checkNearEqual(FileTime t1, FileTime t2) { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
57 |
long diff = Math.abs(t1.toMillis() - t2.toMillis()); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
58 |
assertTrue(diff <= 1000); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
59 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
60 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
61 |
// Exercise getAttribute/setAttribute/readAttributes on basic attributes |
8158 | 62 |
static void checkBasicAttributes(Path file, BasicFileAttributes attrs) |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
63 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
64 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
65 |
// getAttribute |
8158 | 66 |
checkEqual(attrs.size(), Files.getAttribute(file, "size")); |
67 |
checkEqual(attrs.lastModifiedTime(), Files.getAttribute(file, "basic:lastModifiedTime")); |
|
68 |
checkEqual(attrs.lastAccessTime(), Files.getAttribute(file, "lastAccessTime")); |
|
69 |
checkEqual(attrs.creationTime(), Files.getAttribute(file, "basic:creationTime")); |
|
70 |
assertTrue((Boolean)Files.getAttribute(file, "isRegularFile")); |
|
71 |
assertTrue(!(Boolean)Files.getAttribute(file, "basic:isDirectory")); |
|
72 |
assertTrue(!(Boolean)Files.getAttribute(file, "isSymbolicLink")); |
|
73 |
assertTrue(!(Boolean)Files.getAttribute(file, "basic:isOther")); |
|
74 |
checkEqual(attrs.fileKey(), Files.getAttribute(file, "basic:fileKey")); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
75 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
76 |
// setAttribute |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
77 |
FileTime modTime = attrs.lastModifiedTime(); |
8158 | 78 |
Files.setAttribute(file, "basic:lastModifiedTime", FileTime.fromMillis(0L)); |
79 |
checkEqual(Files.getLastModifiedTime(file), |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
80 |
FileTime.fromMillis(0L)); |
8158 | 81 |
Files.setAttribute(file, "lastModifiedTime", modTime); |
82 |
checkEqual(Files.getLastModifiedTime(file), modTime); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
83 |
|
8158 | 84 |
Map<String,Object> map; |
85 |
map = Files.readAttributes(file, "*"); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
86 |
assertTrue(map.size() >= 9); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
87 |
checkEqual(attrs.isRegularFile(), map.get("isRegularFile")); // check one |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
88 |
|
8158 | 89 |
map = Files.readAttributes(file, "basic:*"); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
90 |
assertTrue(map.size() >= 9); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
91 |
checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); // check one |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
92 |
|
8158 | 93 |
map = Files.readAttributes(file, "size,lastModifiedTime"); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
94 |
assertTrue(map.size() == 2); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
95 |
checkEqual(attrs.size(), map.get("size")); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
96 |
checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime")); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
97 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
98 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
99 |
// Exercise getAttribute/setAttribute/readAttributes on posix attributes |
8158 | 100 |
static void checkPosixAttributes(Path file, PosixFileAttributes attrs) |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
101 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
102 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
103 |
checkBasicAttributes(file, attrs); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
104 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
105 |
// getAttribute |
8158 | 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")); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
109 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
110 |
// setAttribute |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
111 |
Set<PosixFilePermission> orig = attrs.permissions(); |
8158 | 112 |
Set<PosixFilePermission> newPerms = new HashSet<>(orig); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
113 |
newPerms.remove(PosixFilePermission.OTHERS_READ); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
114 |
newPerms.remove(PosixFilePermission.OTHERS_WRITE); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
115 |
newPerms.remove(PosixFilePermission.OTHERS_EXECUTE); |
8158 | 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()); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
122 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
123 |
// readAttributes |
8158 | 124 |
Map<String,Object> map; |
125 |
map = Files.readAttributes(file, "posix:*"); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
126 |
assertTrue(map.size() >= 12); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
127 |
checkEqual(attrs.permissions(), map.get("permissions")); // check one |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
128 |
|
8808 | 129 |
map = Files.readAttributes(file, "posix:size,owner"); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
130 |
assertTrue(map.size() == 2); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
131 |
checkEqual(attrs.size(), map.get("size")); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
132 |
checkEqual(attrs.owner(), map.get("owner")); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
133 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
134 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
135 |
// Exercise getAttribute/readAttributes on unix attributes |
8158 | 136 |
static void checkUnixAttributes(Path file) throws IOException { |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
137 |
// getAttribute |
8158 | 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"); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
146 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
147 |
// readAttributes |
8158 | 148 |
Map<String,Object> map; |
149 |
map = Files.readAttributes(file, "unix:*"); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
150 |
assertTrue(map.size() >= 20); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
151 |
|
8808 | 152 |
map = Files.readAttributes(file, "unix:size,uid,gid"); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
153 |
assertTrue(map.size() == 3); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
154 |
checkEqual(map.get("size"), |
8158 | 155 |
Files.readAttributes(file, BasicFileAttributes.class).size()); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
156 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
157 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
158 |
// Exercise getAttribute/setAttribute on dos attributes |
8158 | 159 |
static void checkDosAttributes(Path file, DosFileAttributes attrs) |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
160 |
throws IOException |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
161 |
{ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
162 |
checkBasicAttributes(file, attrs); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
163 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
164 |
// getAttribute |
8158 | 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")); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
169 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
170 |
// setAttribute |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
171 |
boolean value; |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
172 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
173 |
value = attrs.isReadOnly(); |
8158 | 174 |
Files.setAttribute(file, "dos:readonly", !value); |
175 |
checkEqual(Files.readAttributes(file, DosFileAttributes.class).isReadOnly(), !value); |
|
176 |
Files.setAttribute(file, "dos:readonly", value); |
|
177 |
checkEqual(Files.readAttributes(file, DosFileAttributes.class).isReadOnly(), value); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
178 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
179 |
value = attrs.isHidden(); |
8158 | 180 |
Files.setAttribute(file, "dos:hidden", !value); |
181 |
checkEqual(Files.readAttributes(file, DosFileAttributes.class).isHidden(), !value); |
|
182 |
Files.setAttribute(file, "dos:hidden", value); |
|
183 |
checkEqual(Files.readAttributes(file, DosFileAttributes.class).isHidden(), value); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
184 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
185 |
value = attrs.isSystem(); |
8158 | 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); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
190 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
191 |
value = attrs.isArchive(); |
8158 | 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); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
196 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
197 |
// readAttributes |
8158 | 198 |
Map<String,Object> map; |
199 |
map = Files.readAttributes(file, "dos:*"); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
200 |
assertTrue(map.size() >= 13); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
201 |
checkEqual(attrs.isReadOnly(), map.get("readonly")); // check one |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
202 |
|
8808 | 203 |
map = Files.readAttributes(file, "dos:size,hidden"); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
204 |
assertTrue(map.size() == 2); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
205 |
checkEqual(attrs.size(), map.get("size")); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
206 |
checkEqual(attrs.isHidden(), map.get("hidden")); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
207 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
208 |
|
8808 | 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 |
||
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
232 |
static void miscTests(Path file) throws IOException { |
8808 | 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 |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
262 |
try { |
8158 | 263 |
Files.getAttribute(file, null); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
264 |
throw new RuntimeException("NullPointerException expected"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
265 |
} catch (NullPointerException npe) { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
266 |
try { |
8158 | 267 |
Files.getAttribute(file, "isRegularFile", (LinkOption[])null); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
268 |
throw new RuntimeException("NullPointerException expected"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
269 |
} catch (NullPointerException npe) { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
270 |
try { |
8158 | 271 |
Files.setAttribute(file, null, 0L); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
272 |
throw new RuntimeException("NullPointerException expected"); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
273 |
} catch (NullPointerException npe) { } |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
274 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
275 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
276 |
static void doTests(Path dir) throws IOException { |
8158 | 277 |
Path file = dir.resolve("foo"); |
278 |
Files.createFile(file); |
|
279 |
FileStore store = Files.getFileStore(file); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
280 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
281 |
checkBasicAttributes(file, |
8158 | 282 |
Files.readAttributes(file, BasicFileAttributes.class)); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
283 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
284 |
if (store.supportsFileAttributeView("posix")) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
285 |
checkPosixAttributes(file, |
8158 | 286 |
Files.readAttributes(file, PosixFileAttributes.class)); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
287 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
288 |
if (store.supportsFileAttributeView("unix")) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
289 |
checkUnixAttributes(file); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
290 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
291 |
if (store.supportsFileAttributeView("dos")) |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
292 |
checkDosAttributes(file, |
8158 | 293 |
Files.readAttributes(file, DosFileAttributes.class)); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
294 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
295 |
miscTests(file); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
296 |
} finally { |
8158 | 297 |
Files.delete(file); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
298 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
299 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
300 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
301 |
|
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
302 |
public static void main(String[] args) throws IOException { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
303 |
Path dir = TestUtil.createTemporaryDirectory(); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
304 |
try { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
305 |
doTests(dir); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
306 |
} finally { |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
307 |
TestUtil.removeAll(dir); |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
308 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
309 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
diff
changeset
|
310 |
} |