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 6838333 8132497
  26  * @summary Unit test for java.nio.file.FileSystem
  27  * @library .. /test/lib
  28  * @run main/othervm Basic
  29  */
  30 
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.net.URI;
  34 import java.net.URISyntaxException;
  35 import java.nio.file.Files;
  36 import java.nio.file.FileStore;
  37 import java.nio.file.FileSystem;
  38 import java.nio.file.FileSystems;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.nio.file.ProviderNotFoundException;
  42 import java.util.HashMap;
  43 import jdk.test.lib.util.FileUtils;
  44 
  45 /**
  46  * Simple sanity checks for java.nio.file.FileSystem
  47  */
  48 public class Basic {
  49 
  50     static void check(boolean okay, String msg) {
  51         if (!okay)
  52             throw new RuntimeException(msg);
  53     }
  54 
  55     static void checkFileStores(FileSystem fs) throws IOException {
  56         // sanity check method
  57         if (FileUtils.areFileSystemsAccessible()) {
  58             System.out.println("\n--- Begin FileStores ---");
  59             for (FileStore store: fs.getFileStores()) {
  60                 System.out.println(store);
  61             }
  62             System.out.println("--- EndFileStores ---\n");
  63         } else {
  64             System.err.println
  65                 ("Skipping FileStore check due to file system access failure");
  66         }
  67     }
  68 
  69     static void checkSupported(FileSystem fs, String... views) {
  70         for (String view: views) {
  71             check(fs.supportedFileAttributeViews().contains(view),
  72                 "support for '" + view + "' expected");
  73         }
  74     }
  75 
  76     static void checkNoUOE() throws IOException, URISyntaxException {
  77         String dir = System.getProperty("test.dir", ".");
  78         String fileName = dir + File.separator + "foo.bar";
  79         Path path = Paths.get(fileName);
  80         Path file = Files.createFile(path);
  81         try {
  82             URI uri = new URI("jar", file.toUri().toString(), null);
  83             System.out.println(uri);
  84             FileSystem fs = FileSystems.newFileSystem(uri, new HashMap());
  85             fs.close();
  86         } catch (ProviderNotFoundException pnfe) {
  87             System.out.println("Expected ProviderNotFoundException caught: "
  88                 + "\"" + pnfe.getMessage() + "\"");
  89         }
  90     }
  91 
  92     public static void main(String[] args)
  93         throws IOException, URISyntaxException {
  94         String os = System.getProperty("os.name");
  95         FileSystem fs = FileSystems.getDefault();
  96 
  97         // close should throw UOE
  98         try {
  99             fs.close();
 100             throw new RuntimeException("UnsupportedOperationException expected");
 101         } catch (UnsupportedOperationException e) { }
 102         check(fs.isOpen(), "should be open");
 103 
 104         check(!fs.isReadOnly(), "should provide read-write access");
 105 
 106         check(fs.provider().getScheme().equals("file"),
 107             "should use 'file' scheme");
 108 
 109         // sanity check FileStores
 110         checkFileStores(fs);
 111 
 112         // sanity check supportedFileAttributeViews
 113         checkSupported(fs, "basic");
 114         if (os.equals("SunOS"))
 115             checkSupported(fs, "posix", "unix", "owner", "acl", "user");
 116         if (os.equals("Linux"))
 117             checkSupported(fs, "posix", "unix", "owner", "dos", "user");
 118         if (os.contains("OS X"))
 119             checkSupported(fs, "posix", "unix", "owner");
 120         if (os.equals("Windows"))
 121             checkSupported(fs, "owner", "dos", "acl", "user");
 122 
 123         // sanity check non-throwing of UnsupportedOperationException by
 124         // FileSystems.newFileSystem(URI, ..)
 125         checkNoUOE();
 126     }
 127 }