Path: blob/master/test/jdk/java/nio/file/FileSystem/Basic.java
41155 views
/*1* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4313887 6838333 8132497 824229225* @summary Unit test for java.nio.file.FileSystem26* @library .. /test/lib27* @build jdk.test.lib.Platform28* jdk.test.lib.util.FileUtils29* @run main/othervm Basic30*/3132import java.io.File;33import java.io.IOException;34import java.net.URI;35import java.net.URISyntaxException;36import java.nio.file.Files;37import java.nio.file.FileStore;38import java.nio.file.FileSystem;39import java.nio.file.FileSystems;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.nio.file.ProviderNotFoundException;43import java.util.HashMap;44import jdk.test.lib.util.FileUtils;4546/**47* Simple sanity checks for java.nio.file.FileSystem48*/49public class Basic {5051static void check(boolean okay, String msg) {52if (!okay)53throw new RuntimeException(msg);54}5556static void checkFileStores(FileSystem fs) throws IOException {57// sanity check method58if (FileUtils.areFileSystemsAccessible()) {59System.out.println("\n--- Begin FileStores ---");60for (FileStore store: fs.getFileStores()) {61System.out.println(store);62}63System.out.println("--- EndFileStores ---\n");64} else {65System.err.println66("Skipping FileStore check due to file system access failure");67}68}6970static void checkSupported(FileSystem fs, String... views) {71for (String view: views) {72check(fs.supportedFileAttributeViews().contains(view),73"support for '" + view + "' expected");74}75}7677static void checkNoUOE() throws IOException, URISyntaxException {78String dir = System.getProperty("test.dir", ".");79String fileName = dir + File.separator + "foo.bar";80Path path = Paths.get(fileName);81Path file = Files.createFile(path);82try {83URI uri = new URI("jar", file.toUri().toString(), null);84System.out.println(uri);85FileSystem fs = FileSystems.newFileSystem(uri, new HashMap());86fs.close();87} catch (ProviderNotFoundException pnfe) {88System.out.println("Expected ProviderNotFoundException caught: "89+ "\"" + pnfe.getMessage() + "\"");90}91}9293static void checkIAE() throws IOException {94URI absoluteUri = Path.of("foo.bar").toUri();95URI relativeUri = URI.create(absoluteUri.getSchemeSpecificPart());96System.out.println(relativeUri);97try {98FileSystem fs = FileSystems.getFileSystem(relativeUri);99throw new RuntimeException("IllegalArgumentException expected");100} catch (IllegalArgumentException iae) {101System.out.println("Expected IllegalArgumentException caught: "102+ "\"" + iae.getMessage() + "\"");103} catch (Exception e) {104throw new RuntimeException("IllegalArgumentException expected", e);105}106}107108public static void main(String[] args)109throws IOException, URISyntaxException {110String os = System.getProperty("os.name");111FileSystem fs = FileSystems.getDefault();112113// close should throw UOE114try {115fs.close();116throw new RuntimeException("UnsupportedOperationException expected");117} catch (UnsupportedOperationException e) { }118check(fs.isOpen(), "should be open");119120check(!fs.isReadOnly(), "should provide read-write access");121122check(fs.provider().getScheme().equals("file"),123"should use 'file' scheme");124125// sanity check FileStores126checkFileStores(fs);127128// sanity check supportedFileAttributeViews129checkSupported(fs, "basic");130if (os.equals("Linux"))131checkSupported(fs, "posix", "unix", "owner", "dos", "user");132if (os.contains("OS X"))133checkSupported(fs, "posix", "unix", "owner");134if (os.equals("Windows"))135checkSupported(fs, "owner", "dos", "acl", "user");136137// sanity check throwing of IllegalArgumentException by138// FileSystems.getFileSystem(URI) if the URI scheme is null139checkIAE();140141// sanity check non-throwing of UnsupportedOperationException by142// FileSystems.newFileSystem(URI, ..)143checkNoUOE();144}145}146147148