1 /*
   2  * Copyright (c) 2008, 2020, 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 8242292
  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     static void checkIAE() throws IOException {
  95         URI absoluteUri = Path.of("foo.bar").toUri();
  96         URI relativeUri = URI.create(absoluteUri.getSchemeSpecificPart());
  97         System.out.println(relativeUri);
  98         try {
  99             FileSystem fs = FileSystems.getFileSystem(relativeUri);
 100             throw new RuntimeException("IllegalArgumentException expected");
 101         } catch (IllegalArgumentException iae) {
 102             System.out.println("Expected IllegalArgumentException caught: "
 103                 + "\"" + iae.getMessage() + "\"");
 104         } catch (Exception e) {
 105             throw new RuntimeException("IllegalArgumentException expected", e);
 106         }
 107     }
 108 
 109     public static void main(String[] args)
 110         throws IOException, URISyntaxException {
 111         String os = System.getProperty("os.name");
 112         FileSystem fs = FileSystems.getDefault();
 113 
 114         // close should throw UOE
 115         try {
 116             fs.close();
 117             throw new RuntimeException("UnsupportedOperationException expected");
 118         } catch (UnsupportedOperationException e) { }
 119         check(fs.isOpen(), "should be open");
 120 
 121         check(!fs.isReadOnly(), "should provide read-write access");
 122 
 123         check(fs.provider().getScheme().equals("file"),
 124             "should use 'file' scheme");
 125 
 126         // sanity check FileStores
 127         checkFileStores(fs);
 128 
 129         // sanity check supportedFileAttributeViews
 130         checkSupported(fs, "basic");
 131         if (os.equals("Linux"))
 132             checkSupported(fs, "posix", "unix", "owner", "dos", "user");
 133         if (os.contains("OS X"))
 134             checkSupported(fs, "posix", "unix", "owner");
 135         if (os.equals("Windows"))
 136             checkSupported(fs, "owner", "dos", "acl", "user");
 137 
 138         // sanity check throwing of IllegalArgumentException by
 139         // FileSystems.getFileSystem(URI) if the URI scheme is null
 140         checkIAE();
 141 
 142         // sanity check non-throwing of UnsupportedOperationException by
 143         // FileSystems.newFileSystem(URI, ..)
 144         checkNoUOE();
 145     }
 146 }