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.DosFileAttributeView
893N/A * @library ../..
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport static java.nio.file.LinkOption.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.util.*;
893N/Aimport java.io.IOException;
893N/A
893N/Apublic class Basic {
893N/A
893N/A static void check(boolean okay) {
893N/A if (!okay)
893N/A throw new RuntimeException("Test failed");
893N/A }
893N/A
893N/A // exercise each setter/getter method, leaving all attributes unset
893N/A static void testAttributes(DosFileAttributeView view) throws IOException {
893N/A view.setReadOnly(true);
893N/A check(view.readAttributes().isReadOnly());
893N/A view.setReadOnly(false);
893N/A check(!view.readAttributes().isReadOnly());
893N/A view.setHidden(true);
893N/A check(view.readAttributes().isHidden());
893N/A view.setHidden(false);
893N/A check(!view.readAttributes().isHidden());
893N/A view.setArchive(true);
893N/A check(view.readAttributes().isArchive());
893N/A view.setArchive(false);
893N/A check(!view.readAttributes().isArchive());
893N/A view.setSystem(true);
893N/A check(view.readAttributes().isSystem());
893N/A view.setSystem(false);
893N/A check(!view.readAttributes().isSystem());
893N/A }
893N/A
893N/A // set the value of all attributes
893N/A static void setAll(DosFileAttributeView view, boolean value)
893N/A throws IOException
893N/A {
893N/A view.setReadOnly(value);
893N/A view.setHidden(value);
893N/A view.setArchive(value);
893N/A view.setSystem(value);
893N/A }
893N/A
893N/A // read and write FAT attributes
893N/A static void readWriteTests(Path dir) throws IOException {
893N/A
893N/A // create "foo" and test that we can read/write each FAT attribute
3471N/A Path file = Files.createFile(dir.resolve("foo"));
893N/A try {
3471N/A testAttributes(Files.getFileAttributeView(file, DosFileAttributeView.class));
893N/A
893N/A // Following tests use a symbolic link so skip if not supported
893N/A if (!TestUtil.supportsLinks(dir))
893N/A return;
893N/A
3471N/A Path link = dir.resolve("link");
3471N/A Files.createSymbolicLink(link, file);
893N/A
893N/A // test following links
3471N/A testAttributes(Files.getFileAttributeView(link, DosFileAttributeView.class));
893N/A
893N/A // test not following links
893N/A try {
893N/A try {
3471N/A testAttributes(Files
3471N/A .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS));
893N/A } catch (IOException x) {
893N/A // access to link attributes not supported
893N/A return;
893N/A }
893N/A
893N/A // set all attributes on link
893N/A // run test on target of link (which leaves them all un-set)
893N/A // check that attributes of link remain all set
3471N/A setAll(Files
3471N/A .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS), true);
3471N/A testAttributes(Files
3471N/A .getFileAttributeView(link, DosFileAttributeView.class));
3471N/A DosFileAttributes attrs =
3471N/A Files.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS)
3471N/A .readAttributes();
893N/A check(attrs.isReadOnly());
893N/A check(attrs.isHidden());
893N/A check(attrs.isArchive());
893N/A check(attrs.isSystem());
3471N/A setAll(Files
3471N/A .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS), false);
893N/A
893N/A // set all attributes on target
893N/A // run test on link (which leaves them all un-set)
893N/A // check that attributes of target remain all set
3471N/A setAll(Files.getFileAttributeView(link, DosFileAttributeView.class), true);
3471N/A testAttributes(Files
3471N/A .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS));
3471N/A attrs = Files.getFileAttributeView(link, DosFileAttributeView.class).readAttributes();
893N/A check(attrs.isReadOnly());
893N/A check(attrs.isHidden());
893N/A check(attrs.isArchive());
893N/A check(attrs.isSystem());
3471N/A setAll(Files.getFileAttributeView(link, DosFileAttributeView.class), false);
893N/A } finally {
893N/A TestUtil.deleteUnchecked(link);
893N/A }
893N/A } finally {
893N/A TestUtil.deleteUnchecked(file);
893N/A }
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
893N/A try {
893N/A // skip test if DOS file attributes not supported
3471N/A if (!Files.getFileStore(dir).supportsFileAttributeView("dos")) {
893N/A System.out.println("DOS file attribute not supported.");
893N/A return;
893N/A }
893N/A readWriteTests(dir);
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A}