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 47 * @summary Basic test for zip provider 48 * 49 * @run main Basic 50 * @run main/othervm/java.security.policy=test.policy Basic 51 * @modules jdk.zipfs 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 try (DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("/"))) { 93 for (Path entry: stream) { 94 found = entry.toString().equals("/META-INF"); 95 if (found) break; 96 } 97 } 98 99 if (!found) 100 throw new RuntimeException("Expected file not found"); 101 102 // Test: copy file from zip file to current (scratch) directory 103 Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider"); 104 if (Files.exists(source)) { 105 Path target = Paths.get(source.getFileName().toString()); 106 Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); 107 try { 108 long s1 = Files.readAttributes(source, BasicFileAttributes.class).size(); 109 long s2 = Files.readAttributes(target, BasicFileAttributes.class).size(); 110 if (s2 != s1) 111 throw new RuntimeException("target size != source size"); 112 } finally { 113 Files.delete(target); 114 } 115 } 116 117 // Test: FileStore 118 FileStore store = Files.getFileStore(fs.getPath("/")); 119 if (!store.supportsFileAttributeView("basic")) 120 throw new RuntimeException("BasicFileAttributeView should be supported"); 121 122 // Test: watch register should throw PME 123 try { 124 fs.getPath("/") 125 .register(FileSystems.getDefault().newWatchService(), ENTRY_CREATE); 126 throw new RuntimeException("watch service is not supported"); 127 } catch (ProviderMismatchException x) { } 128 129 // Test: ClosedFileSystemException 130 fs.close(); 131 if (fs.isOpen()) 132 throw new RuntimeException("FileSystem should be closed"); 133 try { 134 fs.provider().checkAccess(fs.getPath("/missing"), AccessMode.READ); 135 } catch (ClosedFileSystemException x) { } 136 } 137 138 // FileVisitor that pretty prints a file tree 139 static class FileTreePrinter extends SimpleFileVisitor<Path> { 140 private int indent = 0; 141 142 private void indent() { 143 StringBuilder sb = new StringBuilder(indent); 144 for (int i=0; i<indent; i++) sb.append(" "); 145 System.out.print(sb); 146 } 147 148 @Override 149 public FileVisitResult preVisitDirectory(Path dir, 150 BasicFileAttributes attrs) 151 { 152 if (dir.getFileName() != null) { 153 indent(); 154 System.out.println(dir.getFileName() + "/"); 155 indent++; 156 } 157 return FileVisitResult.CONTINUE; 158 } 159 160 @Override 161 public FileVisitResult visitFile(Path file, 162 BasicFileAttributes attrs) 163 { 164 indent(); 165 System.out.print(file.getFileName()); 166 if (attrs.isRegularFile()) 167 System.out.format("%n%s%n", attrs); 168 169 System.out.println(); 170 return FileVisitResult.CONTINUE; 171 } 172 173 @Override 174 public FileVisitResult postVisitDirectory(Path dir, IOException exc) 175 throws IOException 176 { 177 if (exc != null) 178 super.postVisitDirectory(dir, exc); 179 if (dir.getFileName() != null) 180 indent--; 181 return FileVisitResult.CONTINUE; 182 } 183 } 184 }