Basic.java revision 893
893N/A/*
893N/A * Copyright 2008-2009 Sun Microsystems, Inc. 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 *
893N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
893N/A * CA 95054 USA or visit www.sun.com if you need additional information or
893N/A * have any questions.
893N/A */
893N/A
893N/A/* @test
893N/A * @bug 4313887
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
893N/A Path file = dir.resolve("foo");
893N/A file.newOutputStream().close();
893N/A try {
893N/A testAttributes(file
893N/A .getFileAttributeView(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
893N/A Path link = dir.resolve("link").createSymbolicLink(file);
893N/A
893N/A // test following links
893N/A testAttributes(link
893N/A .getFileAttributeView(DosFileAttributeView.class));
893N/A
893N/A // test not following links
893N/A try {
893N/A try {
893N/A testAttributes(link
893N/A .getFileAttributeView(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
893N/A setAll(link
893N/A .getFileAttributeView(DosFileAttributeView.class, NOFOLLOW_LINKS), true);
893N/A testAttributes(link
893N/A .getFileAttributeView(DosFileAttributeView.class));
893N/A DosFileAttributes attrs = Attributes.readDosFileAttributes(link, NOFOLLOW_LINKS);
893N/A check(attrs.isReadOnly());
893N/A check(attrs.isHidden());
893N/A check(attrs.isArchive());
893N/A check(attrs.isSystem());
893N/A setAll(link
893N/A .getFileAttributeView(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
893N/A setAll(link
893N/A .getFileAttributeView(DosFileAttributeView.class), true);
893N/A testAttributes(link
893N/A .getFileAttributeView(DosFileAttributeView.class, NOFOLLOW_LINKS));
893N/A attrs = Attributes.readDosFileAttributes(link);
893N/A check(attrs.isReadOnly());
893N/A check(attrs.isHidden());
893N/A check(attrs.isArchive());
893N/A check(attrs.isSystem());
893N/A setAll(link
893N/A .getFileAttributeView(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
893N/A if (!dir.getFileStore().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}