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.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 }