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); 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; 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"); | 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 first, String... more) { 47 out.println(); 48 input = first; 49 try { 50 path = fs.getPath(first, more); 51 out.format("%s -> %s", first, path); 52 } catch (Exception x) { 53 exc = x; 54 out.format("%s -> %s", first, 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); 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 resolveSibling(String other, String expected) { 179 out.format("test resolveSibling %s\n", other); 180 checkPath(); 181 check(path.resolveSibling(other), expected); 182 return this; 183 } 184 185 PathOps relativize(String other, String expected) { 186 out.format("test relativize %s\n", other); 187 checkPath(); 188 Path that = fs.getPath(other); 189 check(path.relativize(that), expected); 190 return this; 191 } 192 193 PathOps normalize(String expected) { 194 out.println("check normalized path"); 195 checkPath(); 196 check(path.normalize(), expected); 197 return this; 198 } 199 200 PathOps string(String expected) { 201 out.println("check string representation"); 202 checkPath(); 203 check(path, expected); 204 return this; 210 checkPath(); 211 check(Files.isSameFile(path, test(target).path()), true); 212 } catch (IOException ioe) { 213 fail(); 214 } 215 return this; 216 } 217 218 PathOps invalid() { 219 if (!(exc instanceof InvalidPathException)) { 220 out.println("InvalidPathException not thrown as expected"); 221 fail(); 222 } 223 return this; 224 } 225 226 static PathOps test(String s) { 227 return new PathOps(s); 228 } 229 230 static PathOps test(String first, String... more) { 231 return new PathOps(first, more); 232 } 233 234 // -- PathOpss -- 235 236 static void header(String s) { 237 out.println(); 238 out.println(); 239 out.println("-- " + s + " --"); 240 } 241 242 static void doPathOpTests() { 243 header("Path operations"); 244 245 // construction 246 test("/") 247 .string("/"); 248 test("/", "") 249 .string("/"); 250 test("/", "foo") 251 .string("/foo"); 252 test("/", "/foo") 253 .string("/foo"); 254 test("/", "foo/") 255 .string("/foo"); 256 test("foo", "bar", "gus") 257 .string("foo/bar/gus"); 258 test("") 259 .string(""); 260 test("", "/") 261 .string("/"); 262 test("", "foo", "", "bar", "", "/gus") 263 .string("foo/bar/gus"); 264 265 // all components 266 test("/a/b/c") 267 .root("/") 268 .parent("/a/b") 269 .name("c"); 270 271 // root component only 272 test("/") 273 .root("/") 274 .parent(null) 275 .name(null); 276 277 // no root component 278 test("a/b") 279 .root(null) 280 .parent("a") 281 .name("b"); 282 283 // name component only 284 test("foo") 285 .root(null) 286 .parent(null) 287 .name("foo"); 288 test("") 289 .root(null) 290 .parent(null) 291 .name(""); 292 293 // startsWith 294 test("") 295 .starts("") 296 .notStarts("/"); 297 test("/") 298 .starts("/") 299 .notStarts("") 300 .notStarts("/foo"); 301 test("/foo") 302 .starts("/") 303 .starts("/foo") 304 .notStarts("/f") 305 .notStarts(""); 306 test("/foo/bar") 307 .starts("/") 308 .starts("/foo") 309 .starts("/foo/") 310 .starts("/foo/bar") 311 .notStarts("/f") 312 .notStarts("foo") 313 .notStarts("foo/bar") 314 .notStarts(""); 315 test("foo") 316 .starts("foo") 317 .notStarts("") 318 .notStarts("f"); 319 test("foo/bar") 320 .starts("foo") 321 .starts("foo/") 322 .starts("foo/bar") 323 .notStarts("f") 324 .notStarts("/foo") 325 .notStarts("/foo/bar"); 326 327 // endsWith 328 test("") 329 .ends("") 330 .notEnds("/"); 331 test("/") 332 .ends("/") 333 .notEnds("") 334 .notEnds("foo") 335 .notEnds("/foo"); 336 test("/foo") 337 .ends("foo") 338 .ends("/foo") 339 .notEnds("/") 340 .notEnds("fool"); 341 test("/foo/bar") 342 .ends("bar") 343 .ends("foo/bar") 344 .ends("foo/bar/") 345 .ends("/foo/bar") 346 .notEnds("/bar"); 347 test("/foo/bar/") 348 .ends("bar") 349 .ends("foo/bar") 350 .ends("foo/bar/") 351 .ends("/foo/bar") 352 .notEnds("/bar"); 353 test("foo") 354 .ends("foo") 355 .notEnds("") 356 .notEnds("oo") 357 .notEnds("oola"); 358 test("foo/bar") 359 .ends("bar") 360 .ends("bar/") 361 .ends("foo/bar/") 362 .ends("foo/bar") 363 .notEnds("r") 364 .notEnds("barmaid") 365 .notEnds("/bar") 366 .notEnds("ar") 367 .notEnds("barack") 368 .notEnds("/bar") 369 .notEnds("o/bar"); 370 test("foo/bar/gus") 371 .ends("gus") 372 .ends("bar/gus") 373 .ends("foo/bar/gus") 374 .notEnds("g") 375 .notEnds("/gus") 376 .notEnds("r/gus") 377 .notEnds("barack/gus") 378 .notEnds("bar/gust"); 379 380 // elements 381 test("a/b/c") 382 .element(0,"a") 383 .element(1,"b") 384 .element(2,"c"); 385 386 // isAbsolute 387 test("/") 388 .absolute(); 389 test("/tmp") 390 .absolute(); 391 test("tmp") 392 .notAbsolute(); 393 test("") 394 .notAbsolute(); 395 396 // resolve 397 test("/tmp") 398 .resolve("foo", "/tmp/foo") 399 .resolve("/foo", "/foo") 400 .resolve("", "/tmp"); 401 test("tmp") 402 .resolve("foo", "tmp/foo") 403 .resolve("/foo", "/foo") 404 .resolve("", "tmp"); 405 test("") 406 .resolve("", "") 407 .resolve("foo", "foo") 408 .resolve("/foo", "/foo"); 409 410 // resolveSibling 411 test("foo") 412 .resolveSibling("bar", "bar") 413 .resolveSibling("/bar", "/bar") 414 .resolveSibling("", ""); 415 test("foo/bar") 416 .resolveSibling("gus", "foo/gus") 417 .resolveSibling("/gus", "/gus") 418 .resolveSibling("", "foo"); 419 test("/foo") 420 .resolveSibling("gus", "/gus") 421 .resolveSibling("/gus", "/gus") 422 .resolveSibling("", "/"); 423 test("/foo/bar") 424 .resolveSibling("gus", "/foo/gus") 425 .resolveSibling("/gus", "/gus") 426 .resolveSibling("", "/foo"); 427 test("") 428 .resolveSibling("foo", "foo") 429 .resolveSibling("/foo", "/foo") 430 .resolve("", ""); 431 432 // relativize 433 test("/a/b/c") 434 .relativize("/a/b/c", "") 435 .relativize("/a/b/c/d/e", "d/e") 436 .relativize("/a/x", "../../x") 437 .relativize("/x", "../../../x"); 438 test("a/b/c") 439 .relativize("a/b/c/d", "d") 440 .relativize("a/x", "../../x") 441 .relativize("x", "../../../x") 442 .relativize("", "../../.."); 443 test("") 444 .relativize("a", "a") 445 .relativize("a/b/c", "a/b/c") 446 .relativize("", ""); 447 448 // normalize 449 test("/") 450 .normalize("/"); 451 test("foo") 452 .normalize("foo"); 453 test("/foo") 454 .normalize("/foo"); 455 test(".") 456 .normalize(""); 457 test("..") 458 .normalize(".."); 459 test("/..") 460 .normalize("/"); 461 test("/../..") 462 .normalize("/"); 463 test("foo/.") 464 .normalize("foo"); 465 test("./foo") 466 .normalize("foo"); |