Basic.java revision 1319:5208d0c90d73
1/*
2 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4313887 6838333
26 * @summary Unit test for java.nio.file.attribute.DosFileAttributeView
27 * @library ../..
28 */
29
30import java.nio.file.*;
31import static java.nio.file.LinkOption.*;
32import java.nio.file.attribute.*;
33import java.util.*;
34import java.io.IOException;
35
36public class Basic {
37
38    static void check(boolean okay) {
39        if (!okay)
40            throw new RuntimeException("Test failed");
41    }
42
43    // exercise each setter/getter method, leaving all attributes unset
44    static void testAttributes(DosFileAttributeView view) throws IOException {
45        view.setReadOnly(true);
46        check(view.readAttributes().isReadOnly());
47        view.setReadOnly(false);
48        check(!view.readAttributes().isReadOnly());
49        view.setHidden(true);
50        check(view.readAttributes().isHidden());
51        view.setHidden(false);
52        check(!view.readAttributes().isHidden());
53        view.setArchive(true);
54        check(view.readAttributes().isArchive());
55        view.setArchive(false);
56        check(!view.readAttributes().isArchive());
57        view.setSystem(true);
58        check(view.readAttributes().isSystem());
59        view.setSystem(false);
60        check(!view.readAttributes().isSystem());
61    }
62
63    // set the value of all attributes
64    static void setAll(DosFileAttributeView view, boolean value)
65        throws IOException
66    {
67        view.setReadOnly(value);
68        view.setHidden(value);
69        view.setArchive(value);
70        view.setSystem(value);
71    }
72
73    // read and write FAT attributes
74    static void readWriteTests(Path dir) throws IOException {
75
76        // create "foo" and test that we can read/write each FAT attribute
77        Path file = dir.resolve("foo");
78        file.createFile();
79        try {
80            testAttributes(file
81                .getFileAttributeView(DosFileAttributeView.class));
82
83            // Following tests use a symbolic link so skip if not supported
84            if (!TestUtil.supportsLinks(dir))
85                return;
86
87            Path link = dir.resolve("link").createSymbolicLink(file);
88
89            // test following links
90            testAttributes(link
91                .getFileAttributeView(DosFileAttributeView.class));
92
93            // test not following links
94            try {
95                try {
96                    testAttributes(link
97                        .getFileAttributeView(DosFileAttributeView.class, NOFOLLOW_LINKS));
98                } catch (IOException x) {
99                    // access to link attributes not supported
100                    return;
101                }
102
103                // set all attributes on link
104                // run test on target of link (which leaves them all un-set)
105                // check that attributes of link remain all set
106                setAll(link
107                    .getFileAttributeView(DosFileAttributeView.class, NOFOLLOW_LINKS), true);
108                testAttributes(link
109                    .getFileAttributeView(DosFileAttributeView.class));
110                DosFileAttributes attrs = Attributes.readDosFileAttributes(link, NOFOLLOW_LINKS);
111                check(attrs.isReadOnly());
112                check(attrs.isHidden());
113                check(attrs.isArchive());
114                check(attrs.isSystem());
115                setAll(link
116                    .getFileAttributeView(DosFileAttributeView.class, NOFOLLOW_LINKS), false);
117
118                // set all attributes on target
119                // run test on link (which leaves them all un-set)
120                // check that attributes of target remain all set
121                setAll(link
122                    .getFileAttributeView(DosFileAttributeView.class), true);
123                testAttributes(link
124                    .getFileAttributeView(DosFileAttributeView.class, NOFOLLOW_LINKS));
125                attrs = Attributes.readDosFileAttributes(link);
126                check(attrs.isReadOnly());
127                check(attrs.isHidden());
128                check(attrs.isArchive());
129                check(attrs.isSystem());
130                setAll(link
131                    .getFileAttributeView(DosFileAttributeView.class), false);
132            } finally {
133                TestUtil.deleteUnchecked(link);
134            }
135        } finally {
136            TestUtil.deleteUnchecked(file);
137        }
138    }
139
140    public static void main(String[] args) throws IOException {
141        // create temporary directory to run tests
142        Path dir = TestUtil.createTemporaryDirectory();
143
144        try {
145            // skip test if DOS file attributes not supported
146            if (!dir.getFileStore().supportsFileAttributeView("dos")) {
147                System.out.println("DOS file attribute not supported.");
148                return;
149            }
150            readWriteTests(dir);
151        } finally {
152            TestUtil.removeAll(dir);
153        }
154    }
155}
156