FileAttributes.java revision 3471
1319N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
1319N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1319N/A *
1319N/A * This code is free software; you can redistribute it and/or modify it
1319N/A * under the terms of the GNU General Public License version 2 only, as
1319N/A * published by the Free Software Foundation.
1319N/A *
1319N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1319N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1319N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1319N/A * version 2 for more details (a copy is included in the LICENSE file that
1319N/A * accompanied this code).
1319N/A *
1319N/A * You should have received a copy of the GNU General Public License version
1319N/A * 2 along with this work; if not, write to the Free Software Foundation,
1319N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1319N/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.
1319N/A */
1319N/A
1319N/A/* @test
1319N/A * @bug 4313887 6838333
3471N/A * @summary Unit test for java.nio.file.Files
1319N/A * @library ..
1319N/A */
1319N/A
1319N/Aimport java.nio.file.*;
1319N/Aimport java.nio.file.attribute.*;
1319N/Aimport java.io.IOException;
1319N/Aimport java.util.*;
1319N/Aimport java.util.concurrent.TimeUnit;
1319N/A
1319N/A/**
1319N/A * Exercises getAttribute/setAttribute/readAttributes methods.
1319N/A */
1319N/A
1319N/Apublic class FileAttributes {
1319N/A
1319N/A static void assertTrue(boolean okay) {
1319N/A if (!okay)
1319N/A throw new RuntimeException("Assertion Failed");
1319N/A }
1319N/A
1319N/A static void checkEqual(Object o1, Object o2) {
1319N/A if (o1 == null) {
1319N/A assertTrue(o2 == null);
1319N/A } else {
1319N/A assertTrue (o1.equals(o2));
1319N/A }
1319N/A }
1319N/A
1319N/A // checks that two time values are within 1s of each other
1319N/A static void checkNearEqual(FileTime t1, FileTime t2) {
1319N/A long diff = Math.abs(t1.toMillis() - t2.toMillis());
1319N/A assertTrue(diff <= 1000);
1319N/A }
1319N/A
1319N/A // Exercise getAttribute/setAttribute/readAttributes on basic attributes
3471N/A static void checkBasicAttributes(Path file, BasicFileAttributes attrs)
1319N/A throws IOException
1319N/A {
1319N/A // getAttribute
3471N/A checkEqual(attrs.size(), Files.getAttribute(file, "size"));
3471N/A checkEqual(attrs.lastModifiedTime(), Files.getAttribute(file, "basic:lastModifiedTime"));
3471N/A checkEqual(attrs.lastAccessTime(), Files.getAttribute(file, "lastAccessTime"));
3471N/A checkEqual(attrs.creationTime(), Files.getAttribute(file, "basic:creationTime"));
3471N/A assertTrue((Boolean)Files.getAttribute(file, "isRegularFile"));
3471N/A assertTrue(!(Boolean)Files.getAttribute(file, "basic:isDirectory"));
3471N/A assertTrue(!(Boolean)Files.getAttribute(file, "isSymbolicLink"));
3471N/A assertTrue(!(Boolean)Files.getAttribute(file, "basic:isOther"));
3471N/A checkEqual(attrs.fileKey(), Files.getAttribute(file, "basic:fileKey"));
1319N/A
1319N/A // setAttribute
1319N/A FileTime modTime = attrs.lastModifiedTime();
3471N/A Files.setAttribute(file, "basic:lastModifiedTime", FileTime.fromMillis(0L));
3471N/A checkEqual(Files.getLastModifiedTime(file),
1319N/A FileTime.fromMillis(0L));
3471N/A Files.setAttribute(file, "lastModifiedTime", modTime);
3471N/A checkEqual(Files.getLastModifiedTime(file), modTime);
1319N/A
3471N/A Map<String,Object> map;
3471N/A map = Files.readAttributes(file, "*");
1319N/A assertTrue(map.size() >= 9);
1319N/A checkEqual(attrs.isRegularFile(), map.get("isRegularFile")); // check one
1319N/A
3471N/A map = Files.readAttributes(file, "basic:*");
1319N/A assertTrue(map.size() >= 9);
1319N/A checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime")); // check one
1319N/A
3471N/A map = Files.readAttributes(file, "size,lastModifiedTime");
1319N/A assertTrue(map.size() == 2);
1319N/A checkEqual(attrs.size(), map.get("size"));
1319N/A checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime"));
1319N/A
3471N/A map = Files.readAttributes(file,
1319N/A "basic:lastModifiedTime,lastAccessTime,ShouldNotExist");
1319N/A assertTrue(map.size() == 2);
1319N/A checkEqual(attrs.lastModifiedTime(), map.get("lastModifiedTime"));
1319N/A checkEqual(attrs.lastAccessTime(), map.get("lastAccessTime"));
1319N/A }
1319N/A
1319N/A // Exercise getAttribute/setAttribute/readAttributes on posix attributes
3471N/A static void checkPosixAttributes(Path file, PosixFileAttributes attrs)
1319N/A throws IOException
1319N/A {
1319N/A checkBasicAttributes(file, attrs);
1319N/A
1319N/A // getAttribute
3471N/A checkEqual(attrs.permissions(), Files.getAttribute(file, "posix:permissions"));
3471N/A checkEqual(attrs.owner(), Files.getAttribute(file, "posix:owner"));
3471N/A checkEqual(attrs.group(), Files.getAttribute(file, "posix:group"));
1319N/A
1319N/A // setAttribute
1319N/A Set<PosixFilePermission> orig = attrs.permissions();
3471N/A Set<PosixFilePermission> newPerms = new HashSet<>(orig);
1319N/A newPerms.remove(PosixFilePermission.OTHERS_READ);
1319N/A newPerms.remove(PosixFilePermission.OTHERS_WRITE);
1319N/A newPerms.remove(PosixFilePermission.OTHERS_EXECUTE);
3471N/A Files.setAttribute(file, "posix:permissions", newPerms);
3471N/A checkEqual(Files.getPosixFilePermissions(file), newPerms);
3471N/A Files.setAttribute(file, "posix:permissions", orig);
3471N/A checkEqual(Files.getPosixFilePermissions(file), orig);
3471N/A Files.setAttribute(file, "posix:owner", attrs.owner());
3471N/A Files.setAttribute(file, "posix:group", attrs.group());
1319N/A
1319N/A // readAttributes
3471N/A Map<String,Object> map;
3471N/A map = Files.readAttributes(file, "posix:*");
1319N/A assertTrue(map.size() >= 12);
1319N/A checkEqual(attrs.permissions(), map.get("permissions")); // check one
1319N/A
3471N/A map = Files.readAttributes(file, "posix:size,owner,ShouldNotExist");
1319N/A assertTrue(map.size() == 2);
1319N/A checkEqual(attrs.size(), map.get("size"));
1319N/A checkEqual(attrs.owner(), map.get("owner"));
1319N/A }
1319N/A
1319N/A // Exercise getAttribute/readAttributes on unix attributes
3471N/A static void checkUnixAttributes(Path file) throws IOException {
1319N/A // getAttribute
3471N/A int mode = (Integer)Files.getAttribute(file, "unix:mode");
3471N/A long ino = (Long)Files.getAttribute(file, "unix:ino");
3471N/A long dev = (Long)Files.getAttribute(file, "unix:dev");
3471N/A long rdev = (Long)Files.getAttribute(file, "unix:rdev");
3471N/A int nlink = (Integer)Files.getAttribute(file, "unix:nlink");
3471N/A int uid = (Integer)Files.getAttribute(file, "unix:uid");
3471N/A int gid = (Integer)Files.getAttribute(file, "unix:gid");
3471N/A FileTime ctime = (FileTime)Files.getAttribute(file, "unix:ctime");
1319N/A
1319N/A // readAttributes
3471N/A Map<String,Object> map;
3471N/A map = Files.readAttributes(file, "unix:*");
1319N/A assertTrue(map.size() >= 20);
1319N/A
3471N/A map = Files.readAttributes(file, "unix:size,uid,gid,ShouldNotExist");
1319N/A assertTrue(map.size() == 3);
1319N/A checkEqual(map.get("size"),
3471N/A Files.readAttributes(file, BasicFileAttributes.class).size());
1319N/A }
1319N/A
1319N/A // Exercise getAttribute/setAttribute on dos attributes
3471N/A static void checkDosAttributes(Path file, DosFileAttributes attrs)
1319N/A throws IOException
1319N/A {
1319N/A checkBasicAttributes(file, attrs);
1319N/A
1319N/A // getAttribute
3471N/A checkEqual(attrs.isReadOnly(), Files.getAttribute(file, "dos:readonly"));
3471N/A checkEqual(attrs.isHidden(), Files.getAttribute(file, "dos:hidden"));
3471N/A checkEqual(attrs.isSystem(), Files.getAttribute(file, "dos:system"));
3471N/A checkEqual(attrs.isArchive(), Files.getAttribute(file, "dos:archive"));
1319N/A
1319N/A // setAttribute
1319N/A boolean value;
1319N/A
1319N/A value = attrs.isReadOnly();
3471N/A Files.setAttribute(file, "dos:readonly", !value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isReadOnly(), !value);
3471N/A Files.setAttribute(file, "dos:readonly", value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isReadOnly(), value);
1319N/A
1319N/A value = attrs.isHidden();
3471N/A Files.setAttribute(file, "dos:hidden", !value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isHidden(), !value);
3471N/A Files.setAttribute(file, "dos:hidden", value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isHidden(), value);
1319N/A
1319N/A value = attrs.isSystem();
3471N/A Files.setAttribute(file, "dos:system", !value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isSystem(), !value);
3471N/A Files.setAttribute(file, "dos:system", value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isSystem(), value);
1319N/A
1319N/A value = attrs.isArchive();
3471N/A Files.setAttribute(file, "dos:archive", !value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isArchive(), !value);
3471N/A Files.setAttribute(file, "dos:archive", value);
3471N/A checkEqual(Files.readAttributes(file, DosFileAttributes.class).isArchive(), value);
1319N/A
1319N/A // readAttributes
3471N/A Map<String,Object> map;
3471N/A map = Files.readAttributes(file, "dos:*");
1319N/A assertTrue(map.size() >= 13);
1319N/A checkEqual(attrs.isReadOnly(), map.get("readonly")); // check one
1319N/A
3471N/A map = Files.readAttributes(file, "dos:size,hidden,ShouldNotExist");
1319N/A assertTrue(map.size() == 2);
1319N/A checkEqual(attrs.size(), map.get("size"));
1319N/A checkEqual(attrs.isHidden(), map.get("hidden"));
1319N/A }
1319N/A
1319N/A static void miscTests(Path file) throws IOException {
1319N/A // NPE tests
1319N/A try {
3471N/A Files.getAttribute(file, null);
1319N/A throw new RuntimeException("NullPointerException expected");
1319N/A } catch (NullPointerException npe) { }
1319N/A try {
3471N/A Files.getAttribute(file, "isRegularFile", (LinkOption[])null);
1319N/A throw new RuntimeException("NullPointerException expected");
1319N/A } catch (NullPointerException npe) { }
1319N/A try {
3471N/A Files.setAttribute(file, null, 0L);
1319N/A throw new RuntimeException("NullPointerException expected");
1319N/A } catch (NullPointerException npe) { }
1319N/A }
1319N/A
1319N/A static void doTests(Path dir) throws IOException {
3471N/A Path file = dir.resolve("foo");
3471N/A Files.createFile(file);
3471N/A FileStore store = Files.getFileStore(file);
1319N/A try {
1319N/A checkBasicAttributes(file,
3471N/A Files.readAttributes(file, BasicFileAttributes.class));
1319N/A
1319N/A if (store.supportsFileAttributeView("posix"))
1319N/A checkPosixAttributes(file,
3471N/A Files.readAttributes(file, PosixFileAttributes.class));
1319N/A
1319N/A if (store.supportsFileAttributeView("unix"))
1319N/A checkUnixAttributes(file);
1319N/A
1319N/A if (store.supportsFileAttributeView("dos"))
1319N/A checkDosAttributes(file,
3471N/A Files.readAttributes(file, DosFileAttributes.class));
1319N/A
1319N/A miscTests(file);
1319N/A } finally {
3471N/A Files.delete(file);
1319N/A }
1319N/A }
1319N/A
1319N/A
1319N/A public static void main(String[] args) throws IOException {
1319N/A Path dir = TestUtil.createTemporaryDirectory();
1319N/A try {
1319N/A doTests(dir);
1319N/A } finally {
1319N/A TestUtil.removeAll(dir);
1319N/A }
1319N/A }
1319N/A}