893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
1319N/A * @bug 4313887 6838333
893N/A * @summary Unit test for java.nio.file.attribute.BasicFileAttributeView
893N/A * @library ../..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.util.*;
893N/Aimport java.util.concurrent.TimeUnit;
893N/Aimport java.io.*;
893N/A
893N/Apublic class Basic {
893N/A
893N/A static void check(boolean okay, String msg) {
893N/A if (!okay)
893N/A throw new RuntimeException(msg);
893N/A }
893N/A
893N/A static void checkAttributesOfDirectory(Path dir)
893N/A throws IOException
893N/A {
3471N/A BasicFileAttributes attrs = Files.readAttributes(dir, BasicFileAttributes.class);
893N/A check(attrs.isDirectory(), "is a directory");
893N/A check(!attrs.isRegularFile(), "is not a regular file");
893N/A check(!attrs.isSymbolicLink(), "is not a link");
893N/A check(!attrs.isOther(), "is not other");
893N/A
893N/A // last-modified-time should match java.io.File
1319N/A File f = new File(dir.toString());
1319N/A check(f.lastModified() == attrs.lastModifiedTime().toMillis(),
1319N/A "last-modified time should be the same");
893N/A }
893N/A
893N/A static void checkAttributesOfFile(Path dir, Path file)
893N/A throws IOException
893N/A {
3471N/A BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
893N/A check(attrs.isRegularFile(), "is a regular file");
893N/A check(!attrs.isDirectory(), "is not a directory");
893N/A check(!attrs.isSymbolicLink(), "is not a link");
893N/A check(!attrs.isOther(), "is not other");
893N/A
893N/A // size and last-modified-time should match java.io.File
893N/A File f = new File(file.toString());
893N/A check(f.length() == attrs.size(), "size should be the same");
1319N/A check(f.lastModified() == attrs.lastModifiedTime().toMillis(),
1319N/A "last-modified time should be the same");
893N/A
893N/A // copy last-modified time and file create time from directory to file,
893N/A // re-read attribtues, and check they match
893N/A BasicFileAttributeView view =
3471N/A Files.getFileAttributeView(file, BasicFileAttributeView.class);
3471N/A BasicFileAttributes dirAttrs = Files.readAttributes(dir, BasicFileAttributes.class);
1319N/A view.setTimes(dirAttrs.lastModifiedTime(), null, null);
1319N/A if (dirAttrs.creationTime() != null) {
1319N/A view.setTimes(null, null, dirAttrs.creationTime());
893N/A }
893N/A attrs = view.readAttributes();
1319N/A check(attrs.lastModifiedTime().equals(dirAttrs.lastModifiedTime()),
893N/A "last-modified time should be equal");
1319N/A if (dirAttrs.creationTime() != null) {
1319N/A check(attrs.creationTime().equals(dirAttrs.creationTime()),
893N/A "create time should be the same");
893N/A }
893N/A
893N/A // security tests
893N/A check (!(attrs instanceof PosixFileAttributes),
893N/A "should not be able to cast to PosixFileAttributes");
893N/A }
893N/A
893N/A static void checkAttributesOfLink(Path link)
893N/A throws IOException
893N/A {
3471N/A BasicFileAttributes attrs =
3471N/A Files.readAttributes(link, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
893N/A check(attrs.isSymbolicLink(), "is a link");
893N/A check(!attrs.isDirectory(), "is a directory");
893N/A check(!attrs.isRegularFile(), "is not a regular file");
893N/A check(!attrs.isOther(), "is not other");
893N/A }
893N/A
893N/A static void attributeReadWriteTests(Path dir)
893N/A throws IOException
893N/A {
893N/A // create file
893N/A Path file = dir.resolve("foo");
3471N/A try (OutputStream out = Files.newOutputStream(file)) {
893N/A out.write("this is not an empty file".getBytes("UTF-8"));
893N/A }
893N/A
893N/A // check attributes of directory and file
893N/A checkAttributesOfDirectory(dir);
893N/A checkAttributesOfFile(dir, file);
893N/A
893N/A // symbolic links may be supported
893N/A Path link = dir.resolve("link");
893N/A try {
3471N/A Files.createSymbolicLink(link, file);
893N/A } catch (UnsupportedOperationException x) {
893N/A return;
893N/A } catch (IOException x) {
893N/A return;
893N/A }
893N/A checkAttributesOfLink(link);
893N/A }
893N/A
893N/A public static void main(String[] args) throws IOException {
893N/A // create temporary directory to run tests
893N/A Path dir = TestUtil.createTemporaryDirectory();
893N/A try {
893N/A attributeReadWriteTests(dir);
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A}