459 {
460 String[] result = s.map(path -> path.getFileName().toString())
461 .toArray(String[]::new);
462 fail("should not reach here due to IOException");
463 } catch (IOException ioe) {
464 assertTrue(ioe instanceof FaultyFileSystem.FaultyException);
465 } catch (UncheckedIOException ex) {
466 fail("Top level should be repored as is");
467 }
468 } finally {
469 // Cleanup
470 if (fs != null) {
471 fs.close();
472 }
473 Files.delete(triggerFile);
474 TestUtil.removeAll(triggerDir);
475 }
476 }
477
478 public void testSecurityException() throws IOException {
479 Path triggerFile = testFolder.resolve(Paths.get("dir", "SecurityException"));
480 Files.createFile(triggerFile);
481 Path sampleFile = testFolder.resolve(Paths.get("dir", "sample"));
482 Files.createFile(sampleFile);
483 Path triggerDir = testFolder.resolve(Paths.get("dir2", "SecurityException"));
484 Files.createDirectories(triggerDir);
485 Files.createFile(triggerDir.resolve("fileInSE"));
486 Path sample = testFolder.resolve(Paths.get("dir2", "file"));
487 Files.createFile(sample);
488 FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
489 FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);
490
491 try {
492 fsp.setFaultyMode(false);
493 Path fakeRoot = fs.getRoot();
494 // validate setting
495 try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
496 String[] result = s.map(path -> path.getFileName().toString())
497 .toArray(String[]::new);
498 assertEqualsNoOrder(result, new String[] { "d1","f1", "lnDir2", "SecurityException", "sample" });
499 }
500
501 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
502 String[] result = s.map(path -> path.getFileName().toString())
503 .toArray(String[]::new);
504 assertEqualsNoOrder(result, new String[] { "dir2", "SecurityException", "fileInSE", "file" });
505 }
506
507 // execute test
508 fsp.setFaultyMode(true);
509 // ignore file cause SecurityException
510 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir"))) {
511 String[] result = s.map(path -> path.getFileName().toString())
512 .toArray(String[]::new);
513 assertEqualsNoOrder(result, new String[] { "dir", "d1","f1", "lnDir2", "sample" });
514 }
515 // skip folder cause SecurityException
516 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
517 String[] result = s.map(path -> path.getFileName().toString())
518 .toArray(String[]::new);
519 assertEqualsNoOrder(result, new String[] { "dir2", "file" });
520 }
521
522 // list instead of walk
523 try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
524 String[] result = s.map(path -> path.getFileName().toString())
525 .toArray(String[]::new);
526 assertEqualsNoOrder(result, new String[] { "d1","f1", "lnDir2", "sample" });
527 }
528 try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
529 String[] result = s.map(path -> path.getFileName().toString())
530 .toArray(String[]::new);
531 assertEqualsNoOrder(result, new String[] { "file" });
532 }
533
534 // root cause SecurityException should be reported
535 try (CloseableStream<Path> s = Files.walk(
536 fakeRoot.resolve("dir2").resolve("SecurityException")))
537 {
538 String[] result = s.map(path -> path.getFileName().toString())
539 .toArray(String[]::new);
540 fail("should not reach here due to SecurityException");
541 } catch (SecurityException se) {
542 assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
543 }
544
545 // Walk a file cause SecurityException, we should get SE
546 try (CloseableStream<Path> s = Files.walk(
561 .toArray(String[]::new);
562 fail("should not reach here due to SecurityException");
563 } catch (SecurityException se) {
564 assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
565 }
566
567 try (CloseableStream<Path> s = Files.list(
568 fakeRoot.resolve("dir").resolve("SecurityException")))
569 {
570 String[] result = s.map(path -> path.getFileName().toString())
571 .toArray(String[]::new);
572 fail("should not reach here due to SecurityException");
573 } catch (SecurityException se) {
574 assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
575 }
576 } finally {
577 // Cleanup
578 if (fs != null) {
579 fs.close();
580 }
581 Files.delete(triggerFile);
582 Files.delete(sampleFile);
583 Files.delete(sample);
584 TestUtil.removeAll(triggerDir);
585 }
586 }
587
588 public void testConstructException() {
589 try (CloseableStream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
590 s.forEach(l -> fail("File is not even exist!"));
591 } catch (IOException ioe) {
592 ioe.printStackTrace(System.err);
593 assertTrue(ioe instanceof NoSuchFileException);
594 }
595 }
596
597 public void testClosedStream() throws IOException {
598 try (CloseableStream<Path> s = Files.list(testFolder)) {
599 s.close();
600 Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
601 assertTrue(actual.length <= level1.length);
602 }
603
604 try (CloseableStream<Path> s = Files.walk(testFolder)) {
605 s.close();
606 Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
607 fail("Operate on closed stream should throw IllegalStateException");
608 } catch (IllegalStateException ex) {
609 // expected
610 }
611
612 try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE,
|
459 {
460 String[] result = s.map(path -> path.getFileName().toString())
461 .toArray(String[]::new);
462 fail("should not reach here due to IOException");
463 } catch (IOException ioe) {
464 assertTrue(ioe instanceof FaultyFileSystem.FaultyException);
465 } catch (UncheckedIOException ex) {
466 fail("Top level should be repored as is");
467 }
468 } finally {
469 // Cleanup
470 if (fs != null) {
471 fs.close();
472 }
473 Files.delete(triggerFile);
474 TestUtil.removeAll(triggerDir);
475 }
476 }
477
478 public void testSecurityException() throws IOException {
479 Path empty = testFolder.resolve("empty");
480 Path triggerFile = Files.createFile(empty.resolve("SecurityException"));
481 Path sampleFile = Files.createDirectories(empty.resolve("sample"));
482
483 Path dir2 = testFolder.resolve("dir2");
484 Path triggerDir = Files.createDirectories(dir2.resolve("SecurityException"));
485 Files.createFile(triggerDir.resolve("fileInSE"));
486 Path sample = Files.createFile(dir2.resolve("file"));
487
488 Path triggerLink = null;
489 Path linkTriggerDir = null;
490 Path linkTriggerFile = null;
491 if (supportsLinks) {
492 Path dir = testFolder.resolve("dir");
493 triggerLink = Files.createSymbolicLink(dir.resolve("SecurityException"), empty);
494 linkTriggerDir = Files.createSymbolicLink(dir.resolve("lnDirSE"), triggerDir);
495 linkTriggerFile = Files.createSymbolicLink(dir.resolve("lnFileSE"), triggerFile);
496 }
497
498 FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
499 FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);
500
501 try {
502 fsp.setFaultyMode(false);
503 Path fakeRoot = fs.getRoot();
504 // validate setting
505 try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
506 String[] result = s.map(path -> path.getFileName().toString())
507 .toArray(String[]::new);
508 assertEqualsNoOrder(result, new String[] { "SecurityException", "sample" });
509 }
510
511 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
512 String[] result = s.map(path -> path.getFileName().toString())
513 .toArray(String[]::new);
514 assertEqualsNoOrder(result, new String[] { "dir2", "SecurityException", "fileInSE", "file" });
515 }
516
517 if (supportsLinks) {
518 try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
519 String[] result = s.map(path -> path.getFileName().toString())
520 .toArray(String[]::new);
521 assertEqualsNoOrder(result, new String[] { "d1", "f1", "lnDir2", "SecurityException", "lnDirSE", "lnFileSE" });
522 }
523 }
524
525 // execute test
526 fsp.setFaultyMode(true);
527 // ignore file cause SecurityException
528 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
529 String[] result = s.map(path -> path.getFileName().toString())
530 .toArray(String[]::new);
531 assertEqualsNoOrder(result, new String[] { "empty", "sample" });
532 }
533 // skip folder cause SecurityException
534 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
535 String[] result = s.map(path -> path.getFileName().toString())
536 .toArray(String[]::new);
537 assertEqualsNoOrder(result, new String[] { "dir2", "file" });
538 }
539
540 if (supportsLinks) {
541 // not following links
542 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir"))) {
543 String[] result = s.map(path -> path.getFileName().toString())
544 .toArray(String[]::new);
545 assertEqualsNoOrder(result, new String[] { "dir", "d1", "f1", "lnDir2", "lnDirSE", "lnFileSE" });
546 }
547
548 // following links
549 try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir"), FileVisitOption.FOLLOW_LINKS)) {
550 String[] result = s.map(path -> path.getFileName().toString())
551 .toArray(String[]::new);
552 // ?? Should fileInSE show up?
553 // With FaultyFS, it does as no exception thrown for link to "SecurityException" with read on "lnXxxSE"
554 assertEqualsNoOrder(result, new String[] { "dir", "d1", "f1", "lnDir2", "file", "lnDirSE", "lnFileSE", "fileInSE" });
555 }
556 }
557
558 // list instead of walk
559 try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
560 String[] result = s.map(path -> path.getFileName().toString())
561 .toArray(String[]::new);
562 assertEqualsNoOrder(result, new String[] { "sample" });
563 }
564 try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
565 String[] result = s.map(path -> path.getFileName().toString())
566 .toArray(String[]::new);
567 assertEqualsNoOrder(result, new String[] { "file" });
568 }
569
570 // root cause SecurityException should be reported
571 try (CloseableStream<Path> s = Files.walk(
572 fakeRoot.resolve("dir2").resolve("SecurityException")))
573 {
574 String[] result = s.map(path -> path.getFileName().toString())
575 .toArray(String[]::new);
576 fail("should not reach here due to SecurityException");
577 } catch (SecurityException se) {
578 assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
579 }
580
581 // Walk a file cause SecurityException, we should get SE
582 try (CloseableStream<Path> s = Files.walk(
597 .toArray(String[]::new);
598 fail("should not reach here due to SecurityException");
599 } catch (SecurityException se) {
600 assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
601 }
602
603 try (CloseableStream<Path> s = Files.list(
604 fakeRoot.resolve("dir").resolve("SecurityException")))
605 {
606 String[] result = s.map(path -> path.getFileName().toString())
607 .toArray(String[]::new);
608 fail("should not reach here due to SecurityException");
609 } catch (SecurityException se) {
610 assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
611 }
612 } finally {
613 // Cleanup
614 if (fs != null) {
615 fs.close();
616 }
617 if (supportsLinks) {
618 Files.delete(triggerLink);
619 Files.delete(linkTriggerDir);
620 Files.delete(linkTriggerFile);
621 }
622 Files.delete(triggerFile);
623 Files.delete(sampleFile);
624 Files.delete(sample);
625 TestUtil.removeAll(triggerDir);
626 }
627 }
628
629 public void testConstructException() {
630 try (CloseableStream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
631 s.forEach(l -> fail("File is not even exist!"));
632 } catch (IOException ioe) {
633 assertTrue(ioe instanceof NoSuchFileException);
634 }
635 }
636
637 public void testClosedStream() throws IOException {
638 try (CloseableStream<Path> s = Files.list(testFolder)) {
639 s.close();
640 Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
641 assertTrue(actual.length <= level1.length);
642 }
643
644 try (CloseableStream<Path> s = Files.walk(testFolder)) {
645 s.close();
646 Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
647 fail("Operate on closed stream should throw IllegalStateException");
648 } catch (IllegalStateException ex) {
649 // expected
650 }
651
652 try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE,
|