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