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 * @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
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++;
|
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
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
136 // Test: watch register should throw PME
137 try {
138 fs.getPath("/")
139 .register(FileSystems.getDefault().newWatchService(), ENTRY_CREATE);
140 throw new RuntimeException("watch service is not supported");
141 } catch (ProviderMismatchException x) { }
142
143 // Test: ClosedFileSystemException
144 fs.close();
145 if (fs.isOpen())
146 throw new RuntimeException("FileSystem should be closed");
147 try {
148 fs.provider().checkAccess(fs.getPath("/missing"), AccessMode.READ);
149 } catch (ClosedFileSystemException x) { }
150
151 Files.deleteIfExists(jarFile);
152 }
153
154 // FileVisitor that pretty prints a file tree
155 static class FileTreePrinter extends SimpleFileVisitor<Path> {
156 private int indent = 0;
157
158 private void indent() {
159 StringBuilder sb = new StringBuilder(indent);
160 for (int i=0; i<indent; i++) sb.append(" ");
161 System.out.print(sb);
162 }
163
164 @Override
165 public FileVisitResult preVisitDirectory(Path dir,
166 BasicFileAttributes attrs)
167 {
168 if (dir.getFileName() != null) {
169 indent();
170 System.out.println(dir.getFileName() + "/");
171 indent++;
|