|
1 /* |
|
2 * Copyright (c) 2014, 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.FileSystem; |
|
25 import java.nio.file.FileSystems; |
|
26 import java.nio.file.Files; |
|
27 import java.nio.file.Path; |
|
28 import java.nio.file.InvalidPathException; |
|
29 import java.net.URI; |
|
30 import java.io.IOException; |
|
31 |
|
32 /** |
|
33 * @test |
|
34 * @summary Tests jrt path operations |
|
35 */ |
|
36 |
|
37 public class PathOps { |
|
38 |
|
39 static final java.io.PrintStream out = System.out; |
|
40 static FileSystem fs; |
|
41 |
|
42 private String input; |
|
43 private Path path; |
|
44 private Exception exc; |
|
45 |
|
46 private PathOps(String s) { |
|
47 out.println(); |
|
48 input = s; |
|
49 try { |
|
50 path = fs.getPath(s); |
|
51 out.format("%s -> %s", s, path); |
|
52 } catch (Exception x) { |
|
53 exc = x; |
|
54 out.format("%s -> %s", s, x); |
|
55 } |
|
56 out.println(); |
|
57 } |
|
58 |
|
59 Path path() { |
|
60 return path; |
|
61 } |
|
62 |
|
63 void fail() { |
|
64 throw new RuntimeException("PathOps failed"); |
|
65 } |
|
66 |
|
67 void checkPath() { |
|
68 if (path == null) { |
|
69 throw new InternalError("path is null"); |
|
70 } |
|
71 } |
|
72 |
|
73 void check(Object result, String expected) { |
|
74 out.format("\tExpected: %s\n", expected); |
|
75 out.format("\tActual: %s\n", result); |
|
76 if (result == null) { |
|
77 if (expected == null) return; |
|
78 } else { |
|
79 // compare string representations |
|
80 if (expected != null && result.toString().equals(expected.toString())) |
|
81 return; |
|
82 } |
|
83 fail(); |
|
84 } |
|
85 |
|
86 void check(Object result, boolean expected) { |
|
87 check(result, Boolean.toString(expected)); |
|
88 } |
|
89 |
|
90 PathOps root(String expected) { |
|
91 out.println("check root"); |
|
92 checkPath(); |
|
93 check(path.getRoot(), expected); |
|
94 return this; |
|
95 } |
|
96 |
|
97 PathOps parent(String expected) { |
|
98 out.println("check parent"); |
|
99 checkPath(); |
|
100 check(path.getParent(), expected); |
|
101 return this; |
|
102 } |
|
103 |
|
104 PathOps name(String expected) { |
|
105 out.println("check name"); |
|
106 checkPath(); |
|
107 check(path.getFileName(), expected); |
|
108 return this; |
|
109 } |
|
110 |
|
111 PathOps element(int index, String expected) { |
|
112 out.format("check element %d\n", index); |
|
113 checkPath(); |
|
114 check(path.getName(index), expected); |
|
115 return this; |
|
116 } |
|
117 |
|
118 PathOps subpath(int startIndex, int endIndex, String expected) { |
|
119 out.format("test subpath(%d,%d)\n", startIndex, endIndex); |
|
120 checkPath(); |
|
121 check(path.subpath(startIndex, endIndex), expected); |
|
122 return this; |
|
123 } |
|
124 |
|
125 PathOps starts(String prefix) { |
|
126 out.format("test startsWith with %s\n", prefix); |
|
127 checkPath(); |
|
128 Path s = fs.getPath(prefix); |
|
129 check(path.startsWith(s), true); |
|
130 return this; |
|
131 } |
|
132 |
|
133 PathOps notStarts(String prefix) { |
|
134 out.format("test not startsWith with %s\n", prefix); |
|
135 checkPath(); |
|
136 Path s = fs.getPath(prefix); |
|
137 check(path.startsWith(s), false); |
|
138 return this; |
|
139 } |
|
140 |
|
141 PathOps ends(String suffix) { |
|
142 out.format("test endsWith %s\n", suffix); |
|
143 checkPath(); |
|
144 Path s = fs.getPath(suffix); |
|
145 check(path.endsWith(s), true); |
|
146 return this; |
|
147 } |
|
148 |
|
149 PathOps notEnds(String suffix) { |
|
150 out.format("test not endsWith %s\n", suffix); |
|
151 checkPath(); |
|
152 Path s = fs.getPath(suffix); |
|
153 check(path.endsWith(s), false); |
|
154 return this; |
|
155 } |
|
156 |
|
157 PathOps absolute() { |
|
158 out.println("check path is absolute"); |
|
159 checkPath(); |
|
160 check(path.isAbsolute(), true); |
|
161 return this; |
|
162 } |
|
163 |
|
164 PathOps notAbsolute() { |
|
165 out.println("check path is not absolute"); |
|
166 checkPath(); |
|
167 check(path.isAbsolute(), false); |
|
168 return this; |
|
169 } |
|
170 |
|
171 PathOps resolve(String other, String expected) { |
|
172 out.format("test resolve %s\n", other); |
|
173 checkPath(); |
|
174 check(path.resolve(other), expected); |
|
175 return this; |
|
176 } |
|
177 |
|
178 PathOps relativize(String other, String expected) { |
|
179 out.format("test relativize %s\n", other); |
|
180 checkPath(); |
|
181 Path that = fs.getPath(other); |
|
182 check(path.relativize(that), expected); |
|
183 return this; |
|
184 } |
|
185 |
|
186 PathOps normalize(String expected) { |
|
187 out.println("check normalized path"); |
|
188 checkPath(); |
|
189 check(path.normalize(), expected); |
|
190 return this; |
|
191 } |
|
192 |
|
193 PathOps string(String expected) { |
|
194 out.println("check string representation"); |
|
195 checkPath(); |
|
196 check(path, expected); |
|
197 return this; |
|
198 } |
|
199 |
|
200 PathOps isSameFile(String target) { |
|
201 try { |
|
202 out.println("check two paths are same"); |
|
203 checkPath(); |
|
204 check(Files.isSameFile(path, test(target).path()), true); |
|
205 } catch (IOException ioe) { |
|
206 fail(); |
|
207 } |
|
208 return this; |
|
209 } |
|
210 |
|
211 PathOps invalid() { |
|
212 if (!(exc instanceof InvalidPathException)) { |
|
213 out.println("InvalidPathException not thrown as expected"); |
|
214 fail(); |
|
215 } |
|
216 return this; |
|
217 } |
|
218 |
|
219 static PathOps test(String s) { |
|
220 return new PathOps(s); |
|
221 } |
|
222 |
|
223 // -- PathOpss -- |
|
224 |
|
225 static void header(String s) { |
|
226 out.println(); |
|
227 out.println(); |
|
228 out.println("-- " + s + " --"); |
|
229 } |
|
230 |
|
231 static void doPathOpTests() { |
|
232 header("Path operations"); |
|
233 |
|
234 // all components |
|
235 test("/a/b/c") |
|
236 .root("/") |
|
237 .parent("/a/b") |
|
238 .name("c"); |
|
239 |
|
240 // root component only |
|
241 test("/") |
|
242 .root("/") |
|
243 .parent(null) |
|
244 .name(null); |
|
245 |
|
246 // no root component |
|
247 test("a/b") |
|
248 .root(null) |
|
249 .parent("a") |
|
250 .name("b"); |
|
251 |
|
252 // name component only |
|
253 test("foo") |
|
254 .root(null) |
|
255 .parent(null) |
|
256 .name("foo"); |
|
257 |
|
258 // startsWith |
|
259 test("") |
|
260 .starts("") |
|
261 .notStarts("/"); |
|
262 test("/") |
|
263 .starts("/") |
|
264 .notStarts("/foo"); |
|
265 test("/foo") |
|
266 .starts("/") |
|
267 .starts("/foo") |
|
268 .notStarts("/f") |
|
269 .notStarts(""); |
|
270 test("/foo/bar") |
|
271 .starts("/") |
|
272 .starts("/foo") |
|
273 .starts("/foo/") |
|
274 .starts("/foo/bar") |
|
275 .notStarts("/f") |
|
276 .notStarts("foo") |
|
277 .notStarts("foo/bar") |
|
278 .notStarts(""); |
|
279 test("foo") |
|
280 .starts("foo") |
|
281 .notStarts("f"); |
|
282 test("foo/bar") |
|
283 .starts("foo") |
|
284 .starts("foo/") |
|
285 .starts("foo/bar") |
|
286 .notStarts("f") |
|
287 .notStarts("/foo") |
|
288 .notStarts("/foo/bar"); |
|
289 |
|
290 // endsWith |
|
291 test("") |
|
292 .ends("") |
|
293 .notEnds("/"); |
|
294 test("/") |
|
295 .ends("/") |
|
296 .notEnds("foo") |
|
297 .notEnds("/foo"); |
|
298 test("/foo") |
|
299 .ends("foo") |
|
300 .ends("/foo") |
|
301 .notEnds("/"); |
|
302 test("/foo/bar") |
|
303 .ends("bar") |
|
304 .ends("foo/bar") |
|
305 .ends("foo/bar/") |
|
306 .ends("/foo/bar") |
|
307 .notEnds("/bar"); |
|
308 test("/foo/bar/") |
|
309 .ends("bar") |
|
310 .ends("foo/bar") |
|
311 .ends("foo/bar/") |
|
312 .ends("/foo/bar") |
|
313 .notEnds("/bar"); |
|
314 test("foo") |
|
315 .ends("foo"); |
|
316 test("foo/bar") |
|
317 .ends("bar") |
|
318 .ends("bar/") |
|
319 .ends("foo/bar/") |
|
320 .ends("foo/bar"); |
|
321 |
|
322 |
|
323 // elements |
|
324 test("a/b/c") |
|
325 .element(0,"a") |
|
326 .element(1,"b") |
|
327 .element(2,"c"); |
|
328 |
|
329 // isAbsolute |
|
330 test("/") |
|
331 .absolute(); |
|
332 test("/tmp") |
|
333 .absolute(); |
|
334 test("tmp") |
|
335 .notAbsolute(); |
|
336 test("") |
|
337 .notAbsolute(); |
|
338 |
|
339 // resolve |
|
340 test("/tmp") |
|
341 .resolve("foo", "/tmp/foo") |
|
342 .resolve("/foo", "/foo"); |
|
343 test("tmp") |
|
344 .resolve("foo", "tmp/foo") |
|
345 .resolve("/foo", "/foo"); |
|
346 |
|
347 // relativize |
|
348 test("/a/b/c") |
|
349 .relativize("/a/b/c", "") |
|
350 .relativize("/a/b/c/d/e", "d/e") |
|
351 .relativize("/a/x", "../../x"); |
|
352 |
|
353 // normalize |
|
354 test("/") |
|
355 .normalize("/"); |
|
356 test("foo") |
|
357 .normalize("foo"); |
|
358 test("/foo") |
|
359 .normalize("/foo"); |
|
360 test(".") |
|
361 .normalize(""); |
|
362 test("..") |
|
363 .normalize(".."); |
|
364 test("/..") |
|
365 .normalize("/"); |
|
366 test("/../..") |
|
367 .normalize("/"); |
|
368 test("foo/.") |
|
369 .normalize("foo"); |
|
370 test("./foo") |
|
371 .normalize("foo"); |
|
372 test("foo/..") |
|
373 .normalize(""); |
|
374 test("../foo") |
|
375 .normalize("../foo"); |
|
376 test("../../foo") |
|
377 .normalize("../../foo"); |
|
378 test("foo/bar/..") |
|
379 .normalize("foo"); |
|
380 test("foo/bar/gus/../..") |
|
381 .normalize("foo"); |
|
382 test("/foo/bar/gus/../..") |
|
383 .normalize("/foo"); |
|
384 test("/./.") |
|
385 .normalize("/"); |
|
386 test("/.") |
|
387 .normalize("/"); |
|
388 test("/./abc") |
|
389 .normalize("/abc"); |
|
390 // invalid |
|
391 test("foo\u0000bar") |
|
392 .invalid(); |
|
393 test("\u0000foo") |
|
394 .invalid(); |
|
395 test("bar\u0000") |
|
396 .invalid(); |
|
397 test("//foo\u0000bar") |
|
398 .invalid(); |
|
399 test("//\u0000foo") |
|
400 .invalid(); |
|
401 test("//bar\u0000") |
|
402 .invalid(); |
|
403 |
|
404 // normalization |
|
405 test("//foo//bar") |
|
406 .string("/foo/bar") |
|
407 .root("/") |
|
408 .parent("/foo") |
|
409 .name("bar"); |
|
410 |
|
411 // isSameFile |
|
412 test("/fileDoesNotExist") |
|
413 .isSameFile("/fileDoesNotExist"); |
|
414 } |
|
415 |
|
416 static void npes() { |
|
417 header("NullPointerException"); |
|
418 |
|
419 Path path = fs.getPath("foo"); |
|
420 |
|
421 try { |
|
422 path.resolve((String)null); |
|
423 throw new RuntimeException("NullPointerException not thrown"); |
|
424 } catch (NullPointerException npe) { |
|
425 } |
|
426 |
|
427 try { |
|
428 path.relativize(null); |
|
429 throw new RuntimeException("NullPointerException not thrown"); |
|
430 } catch (NullPointerException npe) { |
|
431 } |
|
432 |
|
433 try { |
|
434 path.compareTo(null); |
|
435 throw new RuntimeException("NullPointerException not thrown"); |
|
436 } catch (NullPointerException npe) { |
|
437 } |
|
438 |
|
439 try { |
|
440 path.startsWith((Path)null); |
|
441 throw new RuntimeException("NullPointerException not thrown"); |
|
442 } catch (NullPointerException npe) { |
|
443 } |
|
444 |
|
445 try { |
|
446 path.endsWith((Path)null); |
|
447 throw new RuntimeException("NullPointerException not thrown"); |
|
448 } catch (NullPointerException npe) { |
|
449 } |
|
450 |
|
451 } |
|
452 |
|
453 public static void main(String[] args) throws Throwable { |
|
454 fs = FileSystems.getFileSystem(URI.create("jrt:/")); |
|
455 npes(); |
|
456 doPathOpTests(); |
|
457 } |
|
458 } |