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