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