Basic.java revision 9330:8b1f1c2a400f
1/* 2 * Copyright (c) 2008, 2013, 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 30import java.nio.file.*; 31import java.nio.file.attribute.*; 32import java.util.*; 33import java.util.concurrent.TimeUnit; 34import java.io.*; 35 36public 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 in seconds 53 File f = new File(dir.toString()); 54 check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS), 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 in seconds 68 File f = new File(file.toString()); 69 check(f.length() == attrs.size(), "size should be the same"); 70 check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS), 71 "last-modified time should be the same"); 72 73 // copy last-modified 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 80 attrs = view.readAttributes(); 81 check(attrs.lastModifiedTime().equals(dirAttrs.lastModifiedTime()), 82 "last-modified time should be equal"); 83 84 // security tests 85 check (!(attrs instanceof PosixFileAttributes), 86 "should not be able to cast to PosixFileAttributes"); 87 } 88 89 static void checkAttributesOfLink(Path link) 90 throws IOException 91 { 92 BasicFileAttributes attrs = 93 Files.readAttributes(link, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); 94 check(attrs.isSymbolicLink(), "is a link"); 95 check(!attrs.isDirectory(), "is a directory"); 96 check(!attrs.isRegularFile(), "is not a regular file"); 97 check(!attrs.isOther(), "is not other"); 98 } 99 100 static void attributeReadWriteTests(Path dir) 101 throws IOException 102 { 103 // create file 104 Path file = dir.resolve("foo"); 105 try (OutputStream out = Files.newOutputStream(file)) { 106 out.write("this is not an empty file".getBytes("UTF-8")); 107 } 108 109 // check attributes of directory and file 110 checkAttributesOfDirectory(dir); 111 checkAttributesOfFile(dir, file); 112 113 // symbolic links may be supported 114 Path link = dir.resolve("link"); 115 try { 116 Files.createSymbolicLink(link, file); 117 } catch (UnsupportedOperationException x) { 118 return; 119 } catch (IOException x) { 120 return; 121 } 122 checkAttributesOfLink(link); 123 } 124 125 public static void main(String[] args) throws IOException { 126 // create temporary directory to run tests 127 Path dir = TestUtil.createTemporaryDirectory(); 128 try { 129 attributeReadWriteTests(dir); 130 } finally { 131 TestUtil.removeAll(dir); 132 } 133 } 134} 135