1 /*
   2  * Copyright (c) 2009, 2011, 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 import java.nio.file.AccessMode;
  25 import java.nio.file.ClosedFileSystemException;
  26 import java.nio.file.DirectoryStream;
  27 import java.nio.file.FileStore;
  28 import java.nio.file.FileSystem;
  29 import java.nio.file.FileSystems;
  30 import java.nio.file.FileVisitResult;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.nio.file.ProviderMismatchException;
  35 import java.nio.file.SimpleFileVisitor;
  36 import java.nio.file.StandardCopyOption;
  37 import java.nio.file.attribute.BasicFileAttributes;
  38 import java.nio.file.spi.FileSystemProvider;
  39 import java.net.URI;
  40 import java.io.IOException;
  41 import java.util.Collections;
  42 import java.util.Map;
  43 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
  44 /**
  45  * @test
  46  * @bug 8038500 8040059 8150366 8150496 8147539 8211385
  47  * @summary Basic test for zip provider
  48  *
  49  * @modules jdk.zipfs
  50  * @run main Basic
  51  * @run main/othervm/java.security.policy=test.policy Basic
  52  */
  53 
  54 public class Basic {
  55     public static void main(String[] args) throws Exception {
  56         // Test: zip should should be returned in provider list
  57         boolean found = false;
  58         for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
  59             if (provider.getScheme().equalsIgnoreCase("jar")) {
  60                 found = true;
  61                 break;
  62             }
  63         }
  64         if (!found)
  65             throw new RuntimeException("'jar' provider not installed");
  66 
  67         // create JAR file for test
  68         Path jarFile = Utils.createJarFile("basic.jar",
  69                 "META-INF/services/java.nio.file.spi.FileSystemProvider");
  70 
  71         // Test: FileSystems#newFileSystem(Path)
  72         Map<String,?> env = Collections.emptyMap();
  73         FileSystems.newFileSystem(jarFile, null).close();
  74 
  75         // Test: FileSystems#newFileSystem(URI)
  76         URI uri = new URI("jar", jarFile.toUri().toString(), null);
  77         FileSystem fs = FileSystems.newFileSystem(uri, env, null);
  78 
  79         // Test: exercise toUri method
  80         String expected = uri.toString() + "!/foo";
  81         String actual = fs.getPath("/foo").toUri().toString();
  82         if (!actual.equals(expected)) {
  83             throw new RuntimeException("toUri returned '" + actual +
  84                 "', expected '" + expected + "'");
  85         }
  86 
  87         // Test: exercise directory iterator and retrieval of basic attributes
  88         Files.walkFileTree(fs.getPath("/"), new FileTreePrinter());
  89 
  90         // Test: DirectoryStream
  91         found = false;
  92 
  93         try (DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("/"))) {
  94             for (Path entry: stream) {
  95                 found = entry.toString().equals("/META-INF");
  96                 if (found) break;
  97             }
  98         }
  99         if (!found)
 100             throw new RuntimeException("Expected file not found");
 101 
 102         try (DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("META-INF"))) {
 103             for (Path entry: stream) {
 104                 if (entry.toString().equals("/META-INF/services"))
 105                     throw new RuntimeException("child path should be relative");
 106             }
 107         }
 108 
 109         try (DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("/META-INF"))) {
 110             for (Path entry: stream) {
 111                 if (entry.toString().equals("META-INF/services"))
 112                     throw new RuntimeException("child path should be absolute");
 113             }
 114         }
 115 
 116         // Test: copy file from zip file to current (scratch) directory
 117         Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider");
 118         if (Files.exists(source)) {
 119             Path target = Paths.get(source.getFileName().toString());
 120             Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
 121             try {
 122                 long s1 = Files.readAttributes(source, BasicFileAttributes.class).size();
 123                 long s2 = Files.readAttributes(target, BasicFileAttributes.class).size();
 124                 if (s2 != s1)
 125                     throw new RuntimeException("target size != source size");
 126             } finally {
 127                 Files.delete(target);
 128             }
 129         }
 130 
 131         // Test: FileStore
 132         FileStore store = Files.getFileStore(fs.getPath("/"));
 133         if (!store.supportsFileAttributeView("basic"))
 134             throw new RuntimeException("BasicFileAttributeView should be supported");
 135 
 136         // Test: watch register should throw PME
 137         try {
 138             fs.getPath("/")
 139               .register(FileSystems.getDefault().newWatchService(), ENTRY_CREATE);
 140             throw new RuntimeException("watch service is not supported");
 141         } catch (ProviderMismatchException x) { }
 142 
 143         // Test: ClosedFileSystemException
 144         fs.close();
 145         if (fs.isOpen())
 146             throw new RuntimeException("FileSystem should be closed");
 147         try {
 148             fs.provider().checkAccess(fs.getPath("/missing"), AccessMode.READ);
 149         } catch (ClosedFileSystemException x) { }
 150 
 151         Files.deleteIfExists(jarFile);
 152     }
 153 
 154     // FileVisitor that pretty prints a file tree
 155     static class FileTreePrinter extends SimpleFileVisitor<Path> {
 156         private int indent = 0;
 157 
 158         private void indent() {
 159             StringBuilder sb = new StringBuilder(indent);
 160             for (int i=0; i<indent; i++) sb.append(" ");
 161             System.out.print(sb);
 162         }
 163 
 164         @Override
 165         public FileVisitResult preVisitDirectory(Path dir,
 166                                                  BasicFileAttributes attrs)
 167         {
 168             if (dir.getFileName() != null) {
 169                 indent();
 170                 System.out.println(dir.getFileName() + "/");
 171                 indent++;
 172             }
 173             return FileVisitResult.CONTINUE;
 174         }
 175 
 176         @Override
 177         public FileVisitResult visitFile(Path file,
 178                                          BasicFileAttributes attrs)
 179         {
 180             indent();
 181             System.out.print(file.getFileName());
 182             if (attrs.isRegularFile())
 183                 System.out.format("%n%s%n", attrs);
 184 
 185             System.out.println();
 186             return FileVisitResult.CONTINUE;
 187         }
 188 
 189         @Override
 190         public FileVisitResult postVisitDirectory(Path dir, IOException exc)
 191             throws IOException
 192         {
 193             if (exc != null)
 194                 super.postVisitDirectory(dir, exc);
 195             if (dir.getFileName() != null)
 196                 indent--;
 197             return FileVisitResult.CONTINUE;
 198         }
 199     }
 200 }