jdk/test/java/nio/file/FileStore/Basic.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8537 045f48a970d8
child 29528 d1a052f7ccef
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8537
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3721
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3721
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3721
diff changeset
    21
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    22
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    23
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
/* @test
8537
045f48a970d8 7020517: (fs) FileStore.equals returns true if both volumes have the same serial number
alanb
parents: 8158
diff changeset
    25
 * @bug 4313887 6873621 6979526 7006126 7020517
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    26
 * @summary Unit test for java.nio.file.FileStore
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
 * @library ..
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    28
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    29
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    30
import java.nio.file.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    31
import java.nio.file.attribute.*;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    32
import java.io.File;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    33
import java.io.IOException;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    34
import java.util.*;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    35
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    36
public class Basic {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    37
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    38
    static final long G = 1024L * 1024L * 1024L;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    39
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    40
    public static void main(String[] args) throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    41
        Path dir = TestUtil.createTemporaryDirectory();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    42
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    43
            doTests(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    44
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    45
            TestUtil.removeAll(dir);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    46
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    47
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    48
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    49
    static void assertTrue(boolean okay) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    50
        if (!okay)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    51
            throw new RuntimeException("Assertion failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    52
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    53
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    54
    static void checkWithin1GB(long value1, long value2) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    55
        long diff = Math.abs(value1 - value2);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    56
        if (diff > G)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    57
            throw new RuntimeException("values differ by more than 1GB");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    58
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    59
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    60
    static void doTests(Path dir) throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    61
        /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    62
         * Test: Directory should be on FileStore that is writable
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    63
         */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    64
        assertTrue(!Files.getFileStore(dir).isReadOnly());
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    65
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    66
        /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    67
         * Test: Two files should have the same FileStore
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    68
         */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    69
        Path file1 = Files.createFile(dir.resolve("foo"));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    70
        Path file2 = Files.createFile(dir.resolve("bar"));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    71
        FileStore store1 = Files.getFileStore(file1);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    72
        FileStore store2 = Files.getFileStore(file2);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    73
        assertTrue(store1.equals(store2));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    74
        assertTrue(store2.equals(store1));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    75
        assertTrue(store1.hashCode() == store2.hashCode());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    76
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    77
        /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    78
         * Test: File and FileStore attributes
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    79
         */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    80
        assertTrue(store1.supportsFileAttributeView("basic"));
3721
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    81
        assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    82
        assertTrue(store1.supportsFileAttributeView("posix") ==
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    83
            store1.supportsFileAttributeView(PosixFileAttributeView.class));
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    84
        assertTrue(store1.supportsFileAttributeView("dos") ==
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    85
            store1.supportsFileAttributeView(DosFileAttributeView.class));
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    86
        assertTrue(store1.supportsFileAttributeView("acl") ==
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    87
            store1.supportsFileAttributeView(AclFileAttributeView.class));
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    88
        assertTrue(store1.supportsFileAttributeView("user") ==
1adb60cc9f39 6873621: (file) FileStore.supportsFileAttributeView(Class<FileAttributeView> type) returns wrong result
alanb
parents: 2057
diff changeset
    89
            store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    90
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    91
        /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    92
         * Test: Space atributes
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    93
         */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    94
        File f = file1.toFile();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    95
        long total = f.getTotalSpace();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    96
        long free = f.getFreeSpace();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    97
        long usable = f.getUsableSpace();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    98
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    99
        // check values are "close"
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   100
        checkWithin1GB(total,  store1.getTotalSpace());
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   101
        checkWithin1GB(free,   store1.getUnallocatedSpace());
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   102
        checkWithin1GB(usable, store1.getUsableSpace());
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   103
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   104
        // get values by name
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   105
        checkWithin1GB(total,  (Long)store1.getAttribute("totalSpace"));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   106
        checkWithin1GB(free,   (Long)store1.getAttribute("unallocatedSpace"));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   107
        checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   108
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   109
        /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   110
         * Test: Enumerate all FileStores
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   111
         */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   112
        FileStore prev = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   113
        for (FileStore store: FileSystems.getDefault().getFileStores()) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   114
            System.out.format("%s (name=%s type=%s)\n", store, store.name(),
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   115
                store.type());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   116
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   117
            // check space attributes are accessible
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   118
            store.getTotalSpace();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   119
            store.getUnallocatedSpace();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   120
            store.getUsableSpace();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   121
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   122
            // two distinct FileStores should not be equal
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   123
            assertTrue(!store.equals(prev));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   124
            prev = store;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   125
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   126
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
   127
}