< prev index next >

test/jdk/jdk/nio/zipfs/Basic.java

Print this page


   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 


  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 


   1 /*
   2  * Copyright (c) 2009, 2019, 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.FileStore;
  27 import java.nio.file.FileSystem;
  28 import java.nio.file.FileSystems;
  29 import java.nio.file.FileVisitResult;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.nio.file.ProviderMismatchException;
  34 import java.nio.file.SimpleFileVisitor;
  35 import java.nio.file.StandardCopyOption;
  36 import java.nio.file.attribute.BasicFileAttributes;
  37 import java.nio.file.spi.FileSystemProvider;
  38 import java.net.URI;
  39 import java.io.IOException;
  40 import java.util.Collections;
  41 import java.util.Map;
  42 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
  43 /**
  44  * @test
  45  * @bug 8038500 8040059 8150366 8150496 8147539
  46  * @summary Basic test for zip provider
  47  *
  48  * @modules jdk.zipfs
  49  * @run main Basic
  50  * @run main/othervm/java.security.policy=test.policy Basic
  51  */
  52 
  53 public class Basic {
  54     public static void main(String[] args) throws Exception {
  55         // Test: zip should should be returned in provider list
  56         boolean found = false;
  57         for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
  58             if (provider.getScheme().equalsIgnoreCase("jar")) {
  59                 found = true;
  60                 break;
  61             }
  62         }
  63         if (!found)
  64             throw new RuntimeException("'jar' provider not installed");
  65 


  69 
  70         // Test: FileSystems#newFileSystem(Path)
  71         Map<String,?> env = Collections.emptyMap();
  72         FileSystems.newFileSystem(jarFile, null).close();
  73 
  74         // Test: FileSystems#newFileSystem(URI)
  75         URI uri = new URI("jar", jarFile.toUri().toString(), null);
  76         FileSystem fs = FileSystems.newFileSystem(uri, env, null);
  77 
  78         // Test: exercise toUri method
  79         String expected = uri.toString() + "!/foo";
  80         String actual = fs.getPath("/foo").toUri().toString();
  81         if (!actual.equals(expected)) {
  82             throw new RuntimeException("toUri returned '" + actual +
  83                 "', expected '" + expected + "'");
  84         }
  85 
  86         // Test: exercise directory iterator and retrieval of basic attributes
  87         Files.walkFileTree(fs.getPath("/"), new FileTreePrinter());
  88 


























  89         // Test: copy file from zip file to current (scratch) directory
  90         Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider");
  91         if (Files.exists(source)) {
  92             Path target = Paths.get(source.getFileName().toString());
  93             Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
  94             try {
  95                 long s1 = Files.readAttributes(source, BasicFileAttributes.class).size();
  96                 long s2 = Files.readAttributes(target, BasicFileAttributes.class).size();
  97                 if (s2 != s1)
  98                     throw new RuntimeException("target size != source size");
  99             } finally {
 100                 Files.delete(target);
 101             }
 102         }
 103 
 104         // Test: FileStore
 105         FileStore store = Files.getFileStore(fs.getPath("/"));
 106         if (!store.supportsFileAttributeView("basic"))
 107             throw new RuntimeException("BasicFileAttributeView should be supported");
 108 


< prev index next >