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