Path: blob/master/test/jdk/java/nio/file/attribute/BasicFileAttributeView/Basic.java
41155 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 683833325* @summary Unit test for java.nio.file.attribute.BasicFileAttributeView26* @library ../..27*/2829import java.nio.file.*;30import java.nio.file.attribute.*;31import java.util.*;32import java.util.concurrent.TimeUnit;33import java.io.*;3435public class Basic {3637static void check(boolean okay, String msg) {38if (!okay)39throw new RuntimeException(msg);40}4142static void checkAttributesOfDirectory(Path dir)43throws IOException44{45BasicFileAttributes attrs = Files.readAttributes(dir, BasicFileAttributes.class);46check(attrs.isDirectory(), "is a directory");47check(!attrs.isRegularFile(), "is not a regular file");48check(!attrs.isSymbolicLink(), "is not a link");49check(!attrs.isOther(), "is not other");5051// last-modified-time should match java.io.File in seconds52File f = new File(dir.toString());53check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS),54"last-modified time should be the same");55}5657static void checkAttributesOfFile(Path dir, Path file)58throws IOException59{60BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);61check(attrs.isRegularFile(), "is a regular file");62check(!attrs.isDirectory(), "is not a directory");63check(!attrs.isSymbolicLink(), "is not a link");64check(!attrs.isOther(), "is not other");6566// size and last-modified-time should match java.io.File in seconds67File f = new File(file.toString());68check(f.length() == attrs.size(), "size should be the same");69check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS),70"last-modified time should be the same");7172// copy last-modified time from directory to file,73// re-read attribtues, and check they match74BasicFileAttributeView view =75Files.getFileAttributeView(file, BasicFileAttributeView.class);76BasicFileAttributes dirAttrs = Files.readAttributes(dir, BasicFileAttributes.class);77view.setTimes(dirAttrs.lastModifiedTime(), null, null);7879attrs = view.readAttributes();80check(attrs.lastModifiedTime().equals(dirAttrs.lastModifiedTime()),81"last-modified time should be equal");8283// security tests84check (!(attrs instanceof PosixFileAttributes),85"should not be able to cast to PosixFileAttributes");86}8788static void checkAttributesOfLink(Path link)89throws IOException90{91BasicFileAttributes attrs =92Files.readAttributes(link, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);93check(attrs.isSymbolicLink(), "is a link");94check(!attrs.isDirectory(), "is a directory");95check(!attrs.isRegularFile(), "is not a regular file");96check(!attrs.isOther(), "is not other");97}9899static void attributeReadWriteTests(Path dir)100throws IOException101{102// create file103Path file = dir.resolve("foo");104try (OutputStream out = Files.newOutputStream(file)) {105out.write("this is not an empty file".getBytes("UTF-8"));106}107108// check attributes of directory and file109checkAttributesOfDirectory(dir);110checkAttributesOfFile(dir, file);111112// symbolic links may be supported113Path link = dir.resolve("link");114try {115Files.createSymbolicLink(link, file);116} catch (UnsupportedOperationException x) {117return;118} catch (IOException x) {119return;120}121checkAttributesOfLink(link);122}123124public static void main(String[] args) throws IOException {125// create temporary directory to run tests126Path dir = TestUtil.createTemporaryDirectory();127try {128attributeReadWriteTests(dir);129} finally {130TestUtil.removeAll(dir);131}132}133}134135136