1 /*
2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /* @test
25  * @bug 4313887 6838333
26  * @summary Unit test for java.nio.file.attribute.DosFileAttributeView
27  * @library ../..
28  */
29 
30 import java.nio.file.*;
31 import static java.nio.file.LinkOption.*;
32 import java.nio.file.attribute.*;
33 import java.util.*;
34 import java.io.IOException;
35 
36 public class Basic {
37 
check(boolean okay)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
testAttributes(DosFileAttributeView view)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
setAll(DosFileAttributeView view, boolean value)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
readWriteTests(Path dir)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 = Files.createFile(dir.resolve("foo"));
78         try {
79             testAttributes(Files.getFileAttributeView(file, DosFileAttributeView.class));
80 
81             // Following tests use a symbolic link so skip if not supported
82             if (!TestUtil.supportsLinks(dir))
83                 return;
84 
85             Path link = dir.resolve("link");
86             Files.createSymbolicLink(link, file);
87 
88             // test following links
89             testAttributes(Files.getFileAttributeView(link, DosFileAttributeView.class));
90 
91             // test not following links
92             try {
93                 try {
94                     testAttributes(Files
95                         .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS));
96                 } catch (IOException x) {
97                     // access to link attributes not supported
98                     return;
99                 }
100 
101                 // set all attributes on link
102                 // run test on target of link (which leaves them all un-set)
103                 // check that attributes of link remain all set
104                 setAll(Files
105                     .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS), true);
106                 testAttributes(Files
107                     .getFileAttributeView(link, DosFileAttributeView.class));
108                 DosFileAttributes attrs =
109                     Files.getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS)
110                          .readAttributes();
111                 check(attrs.isReadOnly());
112                 check(attrs.isHidden());
113                 check(attrs.isArchive());
114                 check(attrs.isSystem());
115                 setAll(Files
116                     .getFileAttributeView(link, 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(Files.getFileAttributeView(link, DosFileAttributeView.class), true);
122                 testAttributes(Files
123                     .getFileAttributeView(link, DosFileAttributeView.class, NOFOLLOW_LINKS));
124                 attrs = Files.getFileAttributeView(link, DosFileAttributeView.class).readAttributes();
125                 check(attrs.isReadOnly());
126                 check(attrs.isHidden());
127                 check(attrs.isArchive());
128                 check(attrs.isSystem());
129                 setAll(Files.getFileAttributeView(link, DosFileAttributeView.class), false);
130             } finally {
131                 TestUtil.deleteUnchecked(link);
132             }
133         } finally {
134             TestUtil.deleteUnchecked(file);
135         }
136     }
137 
main(String[] args)138     public static void main(String[] args) throws IOException {
139         // create temporary directory to run tests
140         Path dir = TestUtil.createTemporaryDirectory();
141 
142         try {
143             // skip test if DOS file attributes not supported
144             if (!Files.getFileStore(dir).supportsFileAttributeView("dos")) {
145                 System.out.println("DOS file attribute not supported.");
146                 return;
147             }
148             readWriteTests(dir);
149         } finally {
150             TestUtil.removeAll(dir);
151         }
152     }
153 }
154