Path: blob/master/test/jdk/java/nio/file/attribute/DosFileAttributeView/Basic.java
41159 views
/*1* Copyright (c) 2008, 2011, 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.DosFileAttributeView26* @library ../..27*/2829import java.nio.file.*;30import static java.nio.file.LinkOption.*;31import java.nio.file.attribute.*;32import java.util.*;33import java.io.IOException;3435public class Basic {3637static void check(boolean okay) {38if (!okay)39throw new RuntimeException("Test failed");40}4142// exercise each setter/getter method, leaving all attributes unset43static void testAttributes(DosFileAttributeView view) throws IOException {44view.setReadOnly(true);45check(view.readAttributes().isReadOnly());46view.setReadOnly(false);47check(!view.readAttributes().isReadOnly());48view.setHidden(true);49check(view.readAttributes().isHidden());50view.setHidden(false);51check(!view.readAttributes().isHidden());52view.setArchive(true);53check(view.readAttributes().isArchive());54view.setArchive(false);55check(!view.readAttributes().isArchive());56view.setSystem(true);57check(view.readAttributes().isSystem());58view.setSystem(false);59check(!view.readAttributes().isSystem());60}6162// set the value of all attributes63static void setAll(DosFileAttributeView view, boolean value)64throws IOException65{66view.setReadOnly(value);67view.setHidden(value);68view.setArchive(value);69view.setSystem(value);70}7172// read and write FAT attributes73static void readWriteTests(Path dir) throws IOException {7475// create "foo" and test that we can read/write each FAT attribute76Path file = Files.createFile(dir.resolve("foo"));77try {78testAttributes(Files.getFileAttributeView(file, DosFileAttributeView.class));7980// Following tests use a symbolic link so skip if not supported81if (!TestUtil.supportsLinks(dir))82return;8384Path link = dir.resolve("link");85Files.createSymbolicLink(link, file);8687// test following links88testAttributes(Files.getFileAttributeView(link, DosFileAttributeView.class));8990// test not following links91try {92try {93testAttributes(Files94.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS));95} catch (IOException x) {96// access to link attributes not supported97return;98}99100// set all attributes on link101// run test on target of link (which leaves them all un-set)102// check that attributes of link remain all set103setAll(Files104.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS), true);105testAttributes(Files106.getFileAttributeView(link, DosFileAttributeView.class));107DosFileAttributes attrs =108Files.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS)109.readAttributes();110check(attrs.isReadOnly());111check(attrs.isHidden());112check(attrs.isArchive());113check(attrs.isSystem());114setAll(Files115.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS), false);116117// set all attributes on target118// run test on link (which leaves them all un-set)119// check that attributes of target remain all set120setAll(Files.getFileAttributeView(link, DosFileAttributeView.class), true);121testAttributes(Files122.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS));123attrs = Files.getFileAttributeView(link, DosFileAttributeView.class).readAttributes();124check(attrs.isReadOnly());125check(attrs.isHidden());126check(attrs.isArchive());127check(attrs.isSystem());128setAll(Files.getFileAttributeView(link, DosFileAttributeView.class), false);129} finally {130TestUtil.deleteUnchecked(link);131}132} finally {133TestUtil.deleteUnchecked(file);134}135}136137public static void main(String[] args) throws IOException {138// create temporary directory to run tests139Path dir = TestUtil.createTemporaryDirectory();140141try {142// skip test if DOS file attributes not supported143if (!Files.getFileStore(dir).supportsFileAttributeView("dos")) {144System.out.println("DOS file attribute not supported.");145return;146}147readWriteTests(dir);148} finally {149TestUtil.removeAll(dir);150}151}152}153154155