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 6873621 6979526 7006126 7020517 26 * @summary Unit test for java.nio.file.FileStore 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 public class Basic { 37 38 static final long G = 1024L * 1024L * 1024L; 39 40 public static void main(String[] args) throws IOException { 41 Path dir = TestUtil.createTemporaryDirectory(); 42 try { 43 doTests(dir); 44 } finally { 45 TestUtil.removeAll(dir); 46 } 47 } 48 49 static void assertTrue(boolean okay) { 50 if (!okay) 51 throw new RuntimeException("Assertion failed"); 52 } 53 54 static void checkWithin1GB(long value1, long value2) { 55 long diff = Math.abs(value1 - value2); 56 if (diff > G) 57 throw new RuntimeException("values differ by more than 1GB"); 58 } 59 60 static void doTests(Path dir) throws IOException { 61 /** 62 * Test: Directory should be on FileStore that is writable 63 */ 64 assertTrue(!Files.getFileStore(dir).isReadOnly()); 65 66 /** 67 * Test: Two files should have the same FileStore 68 */ 69 Path file1 = Files.createFile(dir.resolve("foo")); 70 Path file2 = Files.createFile(dir.resolve("bar")); 71 FileStore store1 = Files.getFileStore(file1); 72 FileStore store2 = Files.getFileStore(file2); 73 assertTrue(store1.equals(store2)); 74 assertTrue(store2.equals(store1)); 75 assertTrue(store1.hashCode() == store2.hashCode()); 76 77 /** 78 * Test: File and FileStore attributes 79 */ 80 assertTrue(store1.supportsFileAttributeView("basic")); 81 assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class)); 82 assertTrue(store1.supportsFileAttributeView("posix") == 83 store1.supportsFileAttributeView(PosixFileAttributeView.class)); 84 assertTrue(store1.supportsFileAttributeView("dos") == 85 store1.supportsFileAttributeView(DosFileAttributeView.class)); 86 assertTrue(store1.supportsFileAttributeView("acl") == 87 store1.supportsFileAttributeView(AclFileAttributeView.class)); 88 assertTrue(store1.supportsFileAttributeView("user") == 89 store1.supportsFileAttributeView(UserDefinedFileAttributeView.class)); 90 91 /** 92 * Test: Space atributes 93 */ 94 File f = file1.toFile(); 95 long total = f.getTotalSpace(); 96 long free = f.getFreeSpace(); 97 long usable = f.getUsableSpace(); 98 99 // check values are "close" 100 checkWithin1GB(total, store1.getTotalSpace()); 101 checkWithin1GB(free, store1.getUnallocatedSpace()); 102 checkWithin1GB(usable, store1.getUsableSpace()); 103 104 // get values by name 105 checkWithin1GB(total, (Long)store1.getAttribute("totalSpace")); 106 checkWithin1GB(free, (Long)store1.getAttribute("unallocatedSpace")); 107 checkWithin1GB(usable, (Long)store1.getAttribute("usableSpace")); 108 109 /** 110 * Test: Enumerate all FileStores 111 */ 112 FileStore prev = null; 113 for (FileStore store: FileSystems.getDefault().getFileStores()) { 114 System.out.format("%s (name=%s type=%s)\n", store, store.name(), 115 store.type()); 116 117 // check space attributes are accessible 118 store.getTotalSpace(); 119 store.getUnallocatedSpace(); 120 store.getUsableSpace(); 121 122 // two distinct FileStores should not be equal 123 assertTrue(!store.equals(prev)); 124 prev = store; 125 } 126 } 127 }