1 /*
   2  * Copyright (c) 2008, 2009, 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.FileStoreAttributeView
  27  * @library ../..
  28  */
  29 
  30 import java.nio.file.*;
  31 import java.nio.file.attribute.*;
  32 import java.io.File;
  33 import java.io.IOException;
  34 import java.util.*;
  35 
  36 /**
  37  * Simple unit test for FileStoreAttributeView that checks that the disk space
  38  * attribtues are "close" to the equivalent values reported by java.io.File.
  39  */
  40 
  41 public class Basic {
  42 
  43     static final long K = 1024L;
  44     static final long G = 1024L * 1024L * 1024L;
  45 
  46     /**
  47      * Print out the disk space information for the given file system
  48      */
  49     static void printFileStore(FileStore fs) throws IOException {
  50         FileStoreSpaceAttributeView view =
  51             fs.getFileStoreAttributeView(FileStoreSpaceAttributeView.class);
  52         FileStoreSpaceAttributes attrs = view.readAttributes();
  53 
  54         long total = attrs.totalSpace() / K;
  55         long used = (attrs.totalSpace() - attrs.unallocatedSpace()) / K;
  56         long avail = attrs.usableSpace() / K;
  57 
  58         String s = fs.toString();
  59         if (s.length() > 20) {
  60             System.out.println(s);
  61             s = "";
  62         }
  63         System.out.format("%-20s %12d %12d %12d\n", s, total, used, avail);
  64     }
  65 
  66     /**
  67      * Check that two values are within 1GB of each other
  68      */
  69     static void checkWithin1GB(long value1, long value2) {
  70         long diff = Math.abs(value1 - value2);
  71         if (diff > G)
  72             throw new RuntimeException("values differ by more than 1GB");
  73     }
  74 
  75     /**
  76      * Check disk space on the file system of the given file
  77      */
  78     static void checkSpace(Path file) throws IOException {
  79         System.out.println(" -- check space -- ");
  80         System.out.println(file);
  81 
  82         FileStore fs = file.getFileStore();
  83         System.out.format("Filesystem: %s\n", fs);
  84 
  85         // get values reported by java.io.File
  86         File f = new File(file.toString());
  87         long total = f.getTotalSpace();
  88         long free = f.getFreeSpace();
  89         long usable = f.getUsableSpace();
  90         System.out.println("java.io.File");
  91         System.out.format("    Total: %d\n", total);
  92         System.out.format("     Free: %d\n", free);
  93         System.out.format("   Usable: %d\n", usable);
  94 
  95         // get values reported by the FileStoreSpaceAttributeView
  96         FileStoreSpaceAttributes attrs = fs
  97             .getFileStoreAttributeView(FileStoreSpaceAttributeView.class)
  98             .readAttributes();
  99         System.out.println("java.nio.file.FileStoreSpaceAttributeView:");
 100         System.out.format("    Total: %d\n", attrs.totalSpace());
 101         System.out.format("     Free: %d\n", attrs.unallocatedSpace());
 102         System.out.format("   Usable: %d\n", attrs.usableSpace());
 103 
 104         // check values are "close"
 105         checkWithin1GB(total, attrs.totalSpace());
 106         checkWithin1GB(free, attrs.unallocatedSpace());
 107         checkWithin1GB(usable, attrs.usableSpace());
 108 
 109         // get values by name
 110         checkWithin1GB(total, (Long)fs.getAttribute("space:totalSpace"));
 111         checkWithin1GB(free, (Long)fs.getAttribute("space:unallocatedSpace"));
 112         checkWithin1GB(usable, (Long)fs.getAttribute("space:usableSpace"));
 113     }
 114 
 115     public static void main(String[] args) throws IOException {
 116         // print out the disk space information for all file systems
 117         FileSystem fs = FileSystems.getDefault();
 118         for (FileStore store: fs.getFileStores()) {
 119             printFileStore(store);
 120         }
 121 
 122         Path dir = TestUtil.createTemporaryDirectory();
 123         try {
 124             // check space using directory
 125             checkSpace(dir);
 126 
 127             // check space using file
 128             Path file = dir.resolve("foo").createFile();
 129             checkSpace(file);
 130 
 131         } finally {
 132             TestUtil.removeAll(dir);
 133         }
 134     }
 135 }