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 66 // create JAR file for test 67 Path jarFile = Utils.createJarFile("basic.jar", 68 "META-INF/services/java.nio.file.spi.FileSystemProvider"); 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 109 // Test: watch register should throw PME 110 try { 111 fs.getPath("/") 112 .register(FileSystems.getDefault().newWatchService(), ENTRY_CREATE); 113 throw new RuntimeException("watch service is not supported"); 114 } catch (ProviderMismatchException x) { } 115 116 // Test: ClosedFileSystemException 117 fs.close(); 118 if (fs.isOpen()) 119 throw new RuntimeException("FileSystem should be closed"); 120 try { 121 fs.provider().checkAccess(fs.getPath("/missing"), AccessMode.READ); 122 } catch (ClosedFileSystemException x) { } 123 124 Files.deleteIfExists(jarFile); 125 } 126 127 // FileVisitor that pretty prints a file tree 128 static class FileTreePrinter extends SimpleFileVisitor<Path> { 129 private int indent = 0; 130 131 private void indent() { 132 StringBuilder sb = new StringBuilder(indent); 133 for (int i=0; i<indent; i++) sb.append(" "); 134 System.out.print(sb); 135 } 136 137 @Override 138 public FileVisitResult preVisitDirectory(Path dir, 139 BasicFileAttributes attrs) 140 { 141 if (dir.getFileName() != null) { 142 indent(); 143 System.out.println(dir.getFileName() + "/"); 144 indent++; 145 } 146 return FileVisitResult.CONTINUE; 147 } 148 149 @Override 150 public FileVisitResult visitFile(Path file, 151 BasicFileAttributes attrs) 152 { 153 indent(); 154 System.out.print(file.getFileName()); 155 if (attrs.isRegularFile()) 156 System.out.format("%n%s%n", attrs); 157 158 System.out.println(); 159 return FileVisitResult.CONTINUE; 160 } 161 162 @Override 163 public FileVisitResult postVisitDirectory(Path dir, IOException exc) 164 throws IOException 165 { 166 if (exc != null) 167 super.postVisitDirectory(dir, exc); 168 if (dir.getFileName() != null) 169 indent--; 170 return FileVisitResult.CONTINUE; 171 } 172 } 173 }