1 /*
   2  * Copyright (c) 2008, 2017, 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 6873621 6979526 7006126 7020517
  26  * @summary Unit test for java.nio.file.FileStore
  27  * @key intermittent
  28  * @library .. /test/lib
  29  * @run main Basic
  30  */
  31 
  32 import java.nio.file.*;
  33 import java.nio.file.attribute.*;
  34 import java.io.File;
  35 import java.io.IOException;
  36 
  37 import jdk.test.lib.util.FileUtils;
  38 
  39 
  40 public class Basic {
  41 
  42     static final long G = 1024L * 1024L * 1024L;
  43 
  44     public static void main(String[] args) throws IOException {
  45         Path dir = TestUtil.createTemporaryDirectory();
  46         try {
  47             doTests(dir);
  48         } finally {
  49             TestUtil.removeAll(dir);
  50         }
  51     }
  52 
  53     static void assertTrue(boolean okay) {
  54         if (!okay)
  55             throw new RuntimeException("Assertion failed");
  56     }
  57 
  58     static void checkWithin1GB(long value1, long value2) {
  59         long diff = Math.abs(value1 - value2);
  60         if (diff > G)
  61             throw new RuntimeException("values differ by more than 1GB");
  62     }
  63 
  64     static void doTests(Path dir) throws IOException {
  65         /**
  66          * Test: Directory should be on FileStore that is writable
  67          */
  68         assertTrue(!Files.getFileStore(dir).isReadOnly());
  69 
  70         /**
  71          * Test: Two files should have the same FileStore
  72          */
  73         Path file1 = Files.createFile(dir.resolve("foo"));
  74         Path file2 = Files.createFile(dir.resolve("bar"));
  75         FileStore store1 = Files.getFileStore(file1);
  76         FileStore store2 = Files.getFileStore(file2);
  77         assertTrue(store1.equals(store2));
  78         assertTrue(store2.equals(store1));
  79         assertTrue(store1.hashCode() == store2.hashCode());
  80 
  81         /**
  82          * Test: File and FileStore attributes
  83          */
  84         assertTrue(store1.supportsFileAttributeView("basic"));
  85         assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class));
  86         assertTrue(store1.supportsFileAttributeView("posix") ==
  87             store1.supportsFileAttributeView(PosixFileAttributeView.class));
  88         assertTrue(store1.supportsFileAttributeView("dos") ==
  89             store1.supportsFileAttributeView(DosFileAttributeView.class));
  90         assertTrue(store1.supportsFileAttributeView("acl") ==
  91             store1.supportsFileAttributeView(AclFileAttributeView.class));
  92         assertTrue(store1.supportsFileAttributeView("user") ==
  93             store1.supportsFileAttributeView(UserDefinedFileAttributeView.class));
  94 
  95         /**
  96          * Test: Space atributes
  97          */
  98         File f = file1.toFile();
  99         long total = f.getTotalSpace();
 100         long free = f.getFreeSpace();
 101         long usable = f.getUsableSpace();
 102 
 103         // check values are "close"
 104         checkWithin1GB(total,  store1.getTotalSpace());
 105         checkWithin1GB(free,   store1.getUnallocatedSpace());
 106         checkWithin1GB(usable, store1.getUsableSpace());
 107 
 108         // get values by name
 109         checkWithin1GB(total,  (Long)store1.getAttribute("totalSpace"));
 110         checkWithin1GB(free,   (Long)store1.getAttribute("unallocatedSpace"));
 111         checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace"));
 112 
 113         /**
 114          * Test: Enumerate all FileStores
 115          */
 116         if (FileUtils.areFileSystemsAccessible()) {
 117             FileStore prev = null;
 118             for (FileStore store: FileSystems.getDefault().getFileStores()) {
 119                 System.out.format("%s (name=%s type=%s)\n", store, store.name(),
 120                     store.type());
 121 
 122                 // check space attributes are accessible
 123                 try {
 124                     store.getTotalSpace();
 125                     store.getUnallocatedSpace();
 126                     store.getUsableSpace();
 127                 } catch (NoSuchFileException nsfe) {
 128                     // ignore exception as the store could have been
 129                     // deleted since the iterator was instantiated
 130                     System.err.format("%s was not found\n", store);
 131                 } catch (AccessDeniedException ade) {
 132                     // ignore exception as the lack of ability to access the
 133                     // store due to lack of file permission or similar does not
 134                     // reflect whether the space attributes would be accessible
 135                     // were access to be permitted
 136                     System.err.format("%s is inaccessible\n", store);
 137                 }
 138 
 139                 // two distinct FileStores should not be equal
 140                 assertTrue(!store.equals(prev));
 141                 prev = store;
 142             }
 143         } else {
 144             System.err.println
 145                 ("Skipping FileStore check due to file system access failure");
 146         }
 147     }
 148 }