26 * @summary Unit test for java.nio.file.attribute.BasicFileAttributeView 27 * @library ../.. 28 */ 29 30 import java.nio.file.*; 31 import java.nio.file.attribute.*; 32 import java.util.*; 33 import java.util.concurrent.TimeUnit; 34 import java.io.*; 35 36 public class Basic { 37 38 static void check(boolean okay, String msg) { 39 if (!okay) 40 throw new RuntimeException(msg); 41 } 42 43 static void checkAttributesOfDirectory(Path dir) 44 throws IOException 45 { 46 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir); 47 check(attrs.isDirectory(), "is a directory"); 48 check(!attrs.isRegularFile(), "is not a regular file"); 49 check(!attrs.isSymbolicLink(), "is not a link"); 50 check(!attrs.isOther(), "is not other"); 51 52 // last-modified-time should match java.io.File 53 File f = new File(dir.toString()); 54 check(f.lastModified() == attrs.lastModifiedTime().toMillis(), 55 "last-modified time should be the same"); 56 } 57 58 static void checkAttributesOfFile(Path dir, Path file) 59 throws IOException 60 { 61 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(file); 62 check(attrs.isRegularFile(), "is a regular file"); 63 check(!attrs.isDirectory(), "is not a directory"); 64 check(!attrs.isSymbolicLink(), "is not a link"); 65 check(!attrs.isOther(), "is not other"); 66 67 // size and last-modified-time should match java.io.File 68 File f = new File(file.toString()); 69 check(f.length() == attrs.size(), "size should be the same"); 70 check(f.lastModified() == attrs.lastModifiedTime().toMillis(), 71 "last-modified time should be the same"); 72 73 // copy last-modified time and file create time from directory to file, 74 // re-read attribtues, and check they match 75 BasicFileAttributeView view = 76 file.getFileAttributeView(BasicFileAttributeView.class); 77 BasicFileAttributes dirAttrs = Attributes.readBasicFileAttributes(dir); 78 view.setTimes(dirAttrs.lastModifiedTime(), null, null); 79 if (dirAttrs.creationTime() != null) { 80 view.setTimes(null, null, dirAttrs.creationTime()); 81 } 82 attrs = view.readAttributes(); 83 check(attrs.lastModifiedTime().equals(dirAttrs.lastModifiedTime()), 84 "last-modified time should be equal"); 85 if (dirAttrs.creationTime() != null) { 86 check(attrs.creationTime().equals(dirAttrs.creationTime()), 87 "create time should be the same"); 88 } 89 90 // security tests 91 check (!(attrs instanceof PosixFileAttributes), 92 "should not be able to cast to PosixFileAttributes"); 93 } 94 95 static void checkAttributesOfLink(Path link) 96 throws IOException 97 { 98 BasicFileAttributes attrs = Attributes 99 .readBasicFileAttributes(link, LinkOption.NOFOLLOW_LINKS); 100 check(attrs.isSymbolicLink(), "is a link"); 101 check(!attrs.isDirectory(), "is a directory"); 102 check(!attrs.isRegularFile(), "is not a regular file"); 103 check(!attrs.isOther(), "is not other"); 104 } 105 106 static void attributeReadWriteTests(Path dir) 107 throws IOException 108 { 109 // create file 110 Path file = dir.resolve("foo"); 111 OutputStream out = file.newOutputStream(); 112 try { 113 out.write("this is not an empty file".getBytes("UTF-8")); 114 } finally { 115 out.close(); 116 } 117 118 // check attributes of directory and file 119 checkAttributesOfDirectory(dir); 120 checkAttributesOfFile(dir, file); 121 122 // symbolic links may be supported 123 Path link = dir.resolve("link"); 124 try { 125 link.createSymbolicLink( file ); 126 } catch (UnsupportedOperationException x) { 127 return; 128 } catch (IOException x) { 129 return; 130 } 131 checkAttributesOfLink(link); 132 } 133 134 public static void main(String[] args) throws IOException { 135 // create temporary directory to run tests 136 Path dir = TestUtil.createTemporaryDirectory(); 137 try { 138 attributeReadWriteTests(dir); 139 } finally { 140 TestUtil.removeAll(dir); 141 } 142 } 143 } | 26 * @summary Unit test for java.nio.file.attribute.BasicFileAttributeView 27 * @library ../.. 28 */ 29 30 import java.nio.file.*; 31 import java.nio.file.attribute.*; 32 import java.util.*; 33 import java.util.concurrent.TimeUnit; 34 import java.io.*; 35 36 public class Basic { 37 38 static void check(boolean okay, String msg) { 39 if (!okay) 40 throw new RuntimeException(msg); 41 } 42 43 static void checkAttributesOfDirectory(Path dir) 44 throws IOException 45 { 46 BasicFileAttributes attrs = Files.readAttributes(dir, BasicFileAttributes.class); 47 check(attrs.isDirectory(), "is a directory"); 48 check(!attrs.isRegularFile(), "is not a regular file"); 49 check(!attrs.isSymbolicLink(), "is not a link"); 50 check(!attrs.isOther(), "is not other"); 51 52 // last-modified-time should match java.io.File 53 File f = new File(dir.toString()); 54 check(f.lastModified() == attrs.lastModifiedTime().toMillis(), 55 "last-modified time should be the same"); 56 } 57 58 static void checkAttributesOfFile(Path dir, Path file) 59 throws IOException 60 { 61 BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class); 62 check(attrs.isRegularFile(), "is a regular file"); 63 check(!attrs.isDirectory(), "is not a directory"); 64 check(!attrs.isSymbolicLink(), "is not a link"); 65 check(!attrs.isOther(), "is not other"); 66 67 // size and last-modified-time should match java.io.File 68 File f = new File(file.toString()); 69 check(f.length() == attrs.size(), "size should be the same"); 70 check(f.lastModified() == attrs.lastModifiedTime().toMillis(), 71 "last-modified time should be the same"); 72 73 // copy last-modified time and file create time from directory to file, 74 // re-read attribtues, and check they match 75 BasicFileAttributeView view = 76 Files.getFileAttributeView(file, BasicFileAttributeView.class); 77 BasicFileAttributes dirAttrs = Files.readAttributes(dir, BasicFileAttributes.class); 78 view.setTimes(dirAttrs.lastModifiedTime(), null, null); 79 if (dirAttrs.creationTime() != null) { 80 view.setTimes(null, null, dirAttrs.creationTime()); 81 } 82 attrs = view.readAttributes(); 83 check(attrs.lastModifiedTime().equals(dirAttrs.lastModifiedTime()), 84 "last-modified time should be equal"); 85 if (dirAttrs.creationTime() != null) { 86 check(attrs.creationTime().equals(dirAttrs.creationTime()), 87 "create time should be the same"); 88 } 89 90 // security tests 91 check (!(attrs instanceof PosixFileAttributes), 92 "should not be able to cast to PosixFileAttributes"); 93 } 94 95 static void checkAttributesOfLink(Path link) 96 throws IOException 97 { 98 BasicFileAttributes attrs = 99 Files.readAttributes(link, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); 100 check(attrs.isSymbolicLink(), "is a link"); 101 check(!attrs.isDirectory(), "is a directory"); 102 check(!attrs.isRegularFile(), "is not a regular file"); 103 check(!attrs.isOther(), "is not other"); 104 } 105 106 static void attributeReadWriteTests(Path dir) 107 throws IOException 108 { 109 // create file 110 Path file = dir.resolve("foo"); 111 try (OutputStream out = Files.newOutputStream(file)) { 112 out.write("this is not an empty file".getBytes("UTF-8")); 113 } 114 115 // check attributes of directory and file 116 checkAttributesOfDirectory(dir); 117 checkAttributesOfFile(dir, file); 118 119 // symbolic links may be supported 120 Path link = dir.resolve("link"); 121 try { 122 Files.createSymbolicLink(link, file); 123 } catch (UnsupportedOperationException x) { 124 return; 125 } catch (IOException x) { 126 return; 127 } 128 checkAttributesOfLink(link); 129 } 130 131 public static void main(String[] args) throws IOException { 132 // create temporary directory to run tests 133 Path dir = TestUtil.createTemporaryDirectory(); 134 try { 135 attributeReadWriteTests(dir); 136 } finally { 137 TestUtil.removeAll(dir); 138 } 139 } 140 } |