Misc.java revision 893:f06f30b29f36
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
26 * @summary Unit test for java.nio.file.Path for miscellenous methods not
27 *   covered by other tests
28 * @library ..
29 */
30
31import java.nio.file.*;
32import static java.nio.file.LinkOption.*;
33import java.nio.file.attribute.*;
34import java.io.*;
35import java.util.*;
36
37public class Misc {
38    static final boolean isWindows =
39        System.getProperty("os.name").startsWith("Windows");
40    static boolean supportsLinks;
41
42    public static void main(String[] args) throws IOException {
43        Path dir = TestUtil.createTemporaryDirectory();
44        try {
45            supportsLinks = TestUtil.supportsLinks(dir);
46
47            // equals and hashCode methods
48            equalsAndHashCode();
49
50            // checkAccess method
51            checkAccessTests(dir);
52
53            // getFileAttributeView methods
54            getFileAttributeViewTests(dir);
55
56            // toRealPath method
57            toRealPathTests(dir);
58
59            // isSameFile method
60            isSameFileTests(dir);
61
62            // isHidden method
63            isHiddenTests(dir);
64
65        } finally {
66            TestUtil.removeAll(dir);
67        }
68    }
69
70    /**
71     * Exercise equals and hashCode methods
72     */
73    static void equalsAndHashCode() {
74
75        Path thisFile = Paths.get("this");
76        Path thatFile = Paths.get("that");
77
78        assertTrue(thisFile.equals(thisFile));
79        assertTrue(!thisFile.equals(thatFile));
80
81        assertTrue(!thisFile.equals(null));
82        assertTrue(!thisFile.equals(new Object()));
83
84        Path likeThis = Paths.get("This");
85        if (isWindows) {
86            // case insensitive
87            assertTrue(thisFile.equals(likeThis));
88            assertTrue(thisFile.hashCode() == likeThis.hashCode());
89        } else {
90            // case senstive
91            assertTrue(!thisFile.equals(likeThis));
92        }
93    }
94
95    /**
96     * Exercise checkAccess method
97     */
98    static void checkAccessTests(Path dir) throws IOException {
99        final Path file = dir.resolve("foo").createFile();
100
101        /**
102         * Test: This directory should readable and writable
103         */
104        dir.checkAccess();
105        dir.checkAccess(AccessMode.READ);
106        dir.checkAccess(AccessMode.WRITE);
107        dir.checkAccess(AccessMode.READ, AccessMode.WRITE);
108
109        /**
110         * Test: File does not exist
111         */
112        Path doesNotExist = dir.resolve("thisDoesNotExists");
113        try {
114            doesNotExist.checkAccess();
115            throw new RuntimeException("NoSuchFileException expected");
116        } catch (NoSuchFileException x) {
117        }
118        try {
119            doesNotExist.checkAccess(AccessMode.READ);
120            throw new RuntimeException("NoSuchFileException expected");
121        } catch (NoSuchFileException x) {
122        }
123        try {
124            doesNotExist.checkAccess(AccessMode.WRITE);
125            throw new RuntimeException("NoSuchFileException expected");
126        } catch (NoSuchFileException x) {
127        }
128        try {
129            doesNotExist.checkAccess(AccessMode.EXECUTE);
130            throw new RuntimeException("NoSuchFileException expected");
131        } catch (NoSuchFileException x) {
132        }
133
134        /**
135         * Test: Edit ACL to deny WRITE and EXECUTE
136         */
137        AclFileAttributeView view = file
138            .getFileAttributeView(AclFileAttributeView.class);
139        if (view != null &&
140            file.getFileStore().supportsFileAttributeView("acl"))
141        {
142            UserPrincipal owner = view.getOwner();
143            List<AclEntry> acl = view.getAcl();
144
145            // Insert entry to deny WRITE and EXECUTE
146            AclEntry entry = AclEntry.newBuilder()
147                .setType(AclEntryType.DENY)
148                .setPrincipal(owner)
149                .setPermissions(AclEntryPermission.WRITE_DATA,
150                    AclEntryPermission.EXECUTE)
151                .build();
152            acl.add(0, entry);
153            view.setAcl(acl);
154
155            try {
156                file.checkAccess(AccessMode.WRITE);
157                throw new RuntimeException("AccessDeniedException expected");
158            } catch (AccessDeniedException x) {
159            }
160
161            try {
162                file.checkAccess(AccessMode.EXECUTE);
163                throw new RuntimeException("AccessDeniedException expected");
164            } catch (AccessDeniedException x) {
165            }
166
167
168            // Restore ACL
169            acl.remove(0);
170            view.setAcl(acl);
171        }
172
173        /**
174         * Test: Windows DOS read-only attribute
175         */
176        if (isWindows) {
177            DosFileAttributeView dview =
178                file.getFileAttributeView(DosFileAttributeView.class);
179            dview.setReadOnly(true);
180            try {
181                file.checkAccess(AccessMode.WRITE);
182                throw new RuntimeException("AccessDeniedException expected");
183            } catch (AccessDeniedException x) {
184            }
185            dview.setReadOnly(false);
186
187            // Read-only attribute does not make direcory read-only
188            dview = dir.getFileAttributeView(DosFileAttributeView.class);
189            boolean save = dview.readAttributes().isReadOnly();
190            dview.setReadOnly(true);
191            dir.checkAccess(AccessMode.WRITE);
192            dview.setReadOnly(save);
193        }
194
195        /**
196         * Test: null
197         */
198        try {
199            file.checkAccess((AccessMode)null);
200            throw new RuntimeException("NullPointerException expected");
201        } catch (NullPointerException ignore) { }
202
203        // clean-up
204        file.delete();
205    }
206
207    /**
208     * Exercise getFileAttributeFile methods
209     */
210    static void getFileAttributeViewTests(Path dir) {
211        assertTrue(dir.getFileAttributeView(BasicFileAttributeView.class)
212            instanceof BasicFileAttributeView);
213        assertTrue(dir.getFileAttributeView(BasicFileAttributeView.class, NOFOLLOW_LINKS)
214            instanceof BasicFileAttributeView);
215        assertTrue(dir.getFileAttributeView("basic")
216            instanceof BasicFileAttributeView);
217        assertTrue(dir.getFileAttributeView("basic", NOFOLLOW_LINKS)
218            instanceof BasicFileAttributeView);
219        assertTrue(dir.getFileAttributeView(BogusFileAttributeView.class) == null);
220        assertTrue(dir.getFileAttributeView("bogus") == null);
221        try {
222            dir.getFileAttributeView((Class<FileAttributeView>)null);
223        } catch (NullPointerException ignore) { }
224        try {
225            dir.getFileAttributeView(BasicFileAttributeView.class, (LinkOption[])null);
226        } catch (NullPointerException ignore) { }
227        try {
228            dir.getFileAttributeView(BasicFileAttributeView.class, (LinkOption)null);
229        } catch (NullPointerException ignore) { }
230        try {
231            dir.getFileAttributeView((String)null);
232        } catch (NullPointerException ignore) { }
233        try {
234            dir.getFileAttributeView("basic", (LinkOption[])null);
235        } catch (NullPointerException ignore) { }
236        try {
237            dir.getFileAttributeView("basic", (LinkOption)null);
238        } catch (NullPointerException ignore) { }
239
240    }
241    interface BogusFileAttributeView extends FileAttributeView { }
242
243    /**
244     * Exercise toRealPath method
245     */
246    static void toRealPathTests(Path dir) throws IOException {
247        final Path file = dir.resolve("foo").createFile();
248        final Path link = dir.resolve("link");
249
250        /**
251         * Test: toRealPath(true) will access same file as toRealPath(false)
252         */
253        assertTrue(file.toRealPath(true).isSameFile(file.toRealPath(false)));
254
255        /**
256         * Test: toRealPath(true) should resolve links
257         */
258        if (supportsLinks) {
259            link.createSymbolicLink(file.toAbsolutePath());
260            assertTrue(link.toRealPath(true).equals(file.toRealPath(true)));
261            link.delete();
262        }
263
264
265        /**
266         * Test: toRealPath(false) should not resolve links
267         */
268        if (supportsLinks) {
269            link.createSymbolicLink(file.toAbsolutePath());
270            assertTrue(link.toRealPath(false).getName().equals(link.getName()));
271            link.delete();
272        }
273
274        /**
275         * Test: toRealPath should eliminate "."
276         */
277        assertTrue(dir.resolve(".").toRealPath(true).equals(dir.toRealPath(true)));
278        assertTrue(dir.resolve(".").toRealPath(false).equals(dir.toRealPath(false)));
279
280        /**
281         * Test: toRealPath should eliminate ".." when it doesn't follow a
282         *       symbolic link
283         */
284        Path subdir = dir.resolve("subdir").createDirectory();
285        assertTrue(subdir.resolve("..").toRealPath(true).equals(dir.toRealPath(true)));
286        assertTrue(subdir.resolve("..").toRealPath(false).equals(dir.toRealPath(false)));
287        subdir.delete();
288
289        // clean-up
290        file.delete();
291    }
292
293    /**
294     * Exercise isSameFile method
295     */
296    static void isSameFileTests(Path dir) throws IOException {
297        Path thisFile = dir.resolve("thisFile");
298        Path thatFile = dir.resolve("thatFile");
299
300        /**
301         * Test: isSameFile for self and null
302         */
303        assertTrue(thisFile.isSameFile(thisFile));
304        assertTrue(!thisFile.isSameFile(null));
305
306        /**
307         * Test: Neither files exist
308         */
309        try {
310            thisFile.isSameFile(thatFile);
311            throw new RuntimeException("IOException not thrown");
312        } catch (IOException x) {
313        }
314        try {
315            thatFile.isSameFile(thisFile);
316            throw new RuntimeException("IOException not thrown");
317        } catch (IOException x) {
318        }
319
320        thisFile.createFile();
321        try {
322            /**
323             * Test: One file exists
324             */
325            try {
326                thisFile.isSameFile(thatFile);
327                throw new RuntimeException("IOException not thrown");
328            } catch (IOException x) {
329            }
330            try {
331                thatFile.isSameFile(thisFile);
332                throw new RuntimeException("IOException not thrown");
333            } catch (IOException x) {
334            }
335
336            thatFile.createFile();
337
338            /**
339             * Test: Both file exists
340             */
341            try {
342                assertTrue(!thisFile.isSameFile(thatFile));
343                assertTrue(!thatFile.isSameFile(thisFile));
344            } finally {
345                TestUtil.deleteUnchecked(thatFile);
346            }
347
348            /**
349             * Test: Symbolic links
350             */
351            if (supportsLinks) {
352                thatFile.createSymbolicLink(thisFile);
353                try {
354                    assertTrue(thisFile.isSameFile(thatFile));
355                    assertTrue(thatFile.isSameFile(thisFile));
356                } finally {
357                    TestUtil.deleteUnchecked(thatFile);
358                }
359            }
360        } finally {
361            thisFile.delete(false);
362        }
363    }
364
365    /**
366     * Exercise isHidden method
367     */
368    static void isHiddenTests(Path dir) throws IOException {
369        assertTrue(!dir.isHidden());
370
371        Path file = dir.resolve(".foo");
372        if (isWindows) {
373            file.createFile();
374            try {
375                Attributes.setAttribute(file, "dos:hidden", true);
376                assertTrue(file.isHidden());
377            } finally {
378                file.delete();
379            }
380        } else {
381            assertTrue(file.isHidden());
382        }
383    }
384
385    static void assertTrue(boolean okay) {
386        if (!okay)
387            throw new RuntimeException("Assertion Failed");
388    }
389}
390