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