Basic.java revision 2824:f439d8b1e84a
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 6873621 6979526 26 * @summary Unit test for java.nio.file.FileStore 27 * @library .. 28 */ 29 30import java.nio.file.*; 31import java.nio.file.attribute.*; 32import java.io.IOException; 33import java.util.*; 34 35public class Basic { 36 37 public static void main(String[] args) throws IOException { 38 Path dir = TestUtil.createTemporaryDirectory(); 39 try { 40 doTests(dir); 41 } finally { 42 TestUtil.removeAll(dir); 43 } 44 } 45 46 static void assertTrue(boolean okay) { 47 if (!okay) 48 throw new RuntimeException("Assertion failed"); 49 } 50 51 static void doTests(Path dir) throws IOException { 52 /** 53 * Test: Directory should be on FileStore that is writable 54 */ 55 assertTrue(!dir.getFileStore().isReadOnly()); 56 57 /** 58 * Test: Two files should have the same FileStore 59 */ 60 FileStore store1 = dir.resolve("foo").createFile().getFileStore(); 61 FileStore store2 = dir.resolve("bar").createFile().getFileStore(); 62 assertTrue(store1.equals(store2)); 63 assertTrue(store2.equals(store1)); 64 assertTrue(store1.hashCode() == store2.hashCode()); 65 66 /** 67 * Test: File and FileStore attributes 68 */ 69 assertTrue(store1.supportsFileAttributeView("basic")); 70 assertTrue(store1.supportsFileAttributeView(BasicFileAttributeView.class)); 71 assertTrue(store1.supportsFileAttributeView("posix") == 72 store1.supportsFileAttributeView(PosixFileAttributeView.class)); 73 assertTrue(store1.supportsFileAttributeView("dos") == 74 store1.supportsFileAttributeView(DosFileAttributeView.class)); 75 assertTrue(store1.supportsFileAttributeView("acl") == 76 store1.supportsFileAttributeView(AclFileAttributeView.class)); 77 assertTrue(store1.supportsFileAttributeView("user") == 78 store1.supportsFileAttributeView(UserDefinedFileAttributeView.class)); 79 80 /** 81 * Test: Enumerate all FileStores 82 */ 83 FileStore prev = null; 84 for (FileStore store: FileSystems.getDefault().getFileStores()) { 85 System.out.format("%s (name=%s type=%s)\n", store, store.name(), 86 store.type()); 87 88 // check space attributes 89 Attributes.readFileStoreSpaceAttributes(store); 90 91 // two distinct FileStores should not be equal 92 assertTrue(!store.equals(prev)); 93 prev = store; 94 } 95 } 96} 97