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