1 /*
   2  * Copyright (c) 2008, 2015, 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 ..
  28  */
  29 
  30 import java.io.File;
  31 import java.nio.file.*;
  32 import java.io.IOException;
  33 import java.net.URI;
  34 import java.net.URISyntaxException;
  35 import java.util.HashMap;
  36 
  37 /**
  38  * Simple sanity checks for java.nio.file.FileSystem
  39  */
  40 public class Basic {
  41 
  42     static void check(boolean okay, String msg) {
  43         if (!okay)
  44             throw new RuntimeException(msg);
  45     }
  46 
  47     static void checkSupported(FileSystem fs, String... views) {
  48         for (String view: views) {
  49             check(fs.supportedFileAttributeViews().contains(view),
  50                 "support for '" + view + "' expected");
  51         }
  52     }
  53 
  54     static void checkNoUOE() throws IOException, URISyntaxException {
  55         String dir = System.getProperty("test.dir", ".");
  56         String fileName = dir + File.separator + "foo.bar";
  57         Path path = Paths.get(fileName);
  58         Path file = Files.createFile(path);
  59         try {
  60             URI uri = new URI("jar", file.toUri().toString(), null);
  61             System.out.println(uri);
  62             FileSystem fs = FileSystems.newFileSystem(uri, new HashMap());
  63             fs.close();
  64         } catch (ProviderNotFoundException pnfe) {
  65             System.out.println("Expected ProviderNotFoundException caught: "
  66                 + "\"" + pnfe.getMessage() + "\"");
  67         } finally {
  68             Files.delete(path);
  69         }
  70     }
  71 
  72     public static void main(String[] args) throws IOException, URISyntaxException {
  73         FileSystem fs = FileSystems.getDefault();
  74 
  75         // close should throw UOE
  76         try {
  77             fs.close();
  78             throw new RuntimeException("UnsupportedOperationException expected");
  79         } catch (UnsupportedOperationException e) { }
  80         check(fs.isOpen(), "should be open");
  81 
  82         check(!fs.isReadOnly(), "should provide read-write access");
  83 
  84         check(fs.provider().getScheme().equals("file"),
  85             "should use 'file' scheme");
  86 
  87         // santity check method - need to re-visit this in future as I/O errors
  88         // are possible
  89         for (FileStore store: fs.getFileStores()) {
  90             System.out.println(store);
  91         }
  92 
  93         // sanity check supportedFileAttributeViews
  94         checkSupported(fs, "basic");
  95         String os = System.getProperty("os.name");
  96         if (os.equals("SunOS"))
  97             checkSupported(fs, "posix", "unix", "owner", "acl", "user");
  98         if (os.equals("Linux"))
  99             checkSupported(fs, "posix", "unix", "owner", "dos", "user");
 100         if (os.contains("OS X"))
 101             checkSupported(fs, "posix", "unix", "owner");
 102         if (os.equals("Windows"))
 103             checkSupported(fs, "owner", "dos", "acl", "user");
 104 
 105         // sanity check non-throwing of UnsupportedOperationException by
 106         // FileSystems.newFileSystem(URI, ..)
 107         checkNoUOE();
 108     }
 109 }