test/java/nio/file/Files/StreamTest.java

Print this page
rev 7302 : 8009736: Comparator API cleanup
Reviewed-by:
Contributed-by: henry.jen@oracle.com


  26  * @summary Unit test for java.nio.file.Files
  27  * @library ..
  28  * @build PassThroughFileSystem FaultyFileSystem
  29  * @run testng StreamTest
  30  */
  31 
  32 import java.io.IOException;
  33 import java.io.UncheckedIOException;
  34 import java.nio.charset.Charset;
  35 import java.nio.charset.MalformedInputException;
  36 import java.nio.file.DirectoryIteratorException;
  37 import java.nio.file.DirectoryStream;
  38 import java.nio.file.FileSystemLoopException;
  39 import java.nio.file.FileVisitOption;
  40 import java.nio.file.Files;
  41 import java.nio.file.NoSuchFileException;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.nio.file.attribute.BasicFileAttributes;
  45 import java.util.Arrays;
  46 import java.util.Comparators;
  47 import java.util.Iterator;
  48 import java.util.List;
  49 import java.util.Objects;
  50 import java.util.Set;
  51 import java.util.TreeSet;
  52 import java.util.function.BiPredicate;
  53 import java.util.stream.CloseableStream;
  54 import java.util.stream.Collectors;
  55 import org.testng.annotations.AfterClass;
  56 import org.testng.annotations.BeforeClass;
  57 import org.testng.annotations.Test;
  58 import static org.testng.Assert.*;
  59 
  60 @Test(groups = "unit")
  61 public class StreamTest {
  62     /**
  63      * Default test folder
  64      * testFolder - empty
  65      *            - file
  66      *            - dir - d1


 122         all = set.toArray(new Path[0]);
 123 
 124         // Follow links
 125         if (supportsLinks) {
 126             tmp = testFolder.resolve("linkDir");
 127             set.add(tmp.resolve("d1"));
 128             set.add(tmp.resolve("f1"));
 129             tmp = tmp.resolve("lnDir2");
 130             set.add(tmp);
 131         }
 132         all_folowLinks = set.toArray(new Path[0]);
 133     }
 134 
 135     @AfterClass
 136     void cleanupTestFolder() throws IOException {
 137         TestUtil.removeAll(testFolder);
 138     }
 139 
 140     public void testBasic() {
 141         try (CloseableStream<Path> s = Files.list(testFolder)) {
 142             Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
 143             assertEquals(actual, level1);
 144         } catch (IOException ioe) {
 145             fail("Unexpected IOException");
 146         }
 147 
 148         try (CloseableStream<Path> s = Files.list(testFolder.resolve("empty"))) {
 149             int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
 150             assertEquals(count, 0, "Expect empty stream.");
 151         } catch (IOException ioe) {
 152             fail("Unexpected IOException");
 153         }
 154     }
 155 
 156     public void testWalk() {
 157         try (CloseableStream<Path> s = Files.walk(testFolder)) {
 158             Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
 159             assertEquals(actual, all);
 160         } catch (IOException ioe) {
 161             fail("Unexpected IOException");
 162         }
 163     }
 164 
 165     public void testWalkOneLevel() {
 166         try (CloseableStream<Path> s = Files.walk(testFolder, 1)) {
 167             Object[] actual = s.filter(path -> ! path.equals(testFolder))
 168                                .sorted(Comparators.naturalOrder())
 169                                .toArray();
 170             assertEquals(actual, level1);
 171         } catch (IOException ioe) {
 172             fail("Unexpected IOException");
 173         }
 174     }
 175 
 176     public void testWalkFollowLink() {
 177         // If link is not supported, the directory structure won't have link.
 178         // We still want to test the behavior with FOLLOW_LINKS option.
 179         try (CloseableStream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) {
 180             Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
 181             assertEquals(actual, all_folowLinks);
 182         } catch (IOException ioe) {
 183             fail("Unexpected IOException");
 184         }
 185     }
 186 
 187     private void validateFileSystemLoopException(Path start, Path... causes) {
 188         try (CloseableStream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
 189             try {
 190                 int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
 191                 fail("Should got FileSystemLoopException, but got " + count + "elements.");
 192             } catch (UncheckedIOException uioe) {
 193                 IOException ioe = uioe.getCause();
 194                 if (ioe instanceof FileSystemLoopException) {
 195                     FileSystemLoopException fsle = (FileSystemLoopException) ioe;
 196                     boolean match = false;
 197                     for (Path cause: causes) {
 198                         if (fsle.getFile().equals(cause.toString())) {
 199                             match = true;
 200                             break;


 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(Comparators.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(Comparators.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,
 653                     (p, attr) -> true)) {
 654             s.close();
 655             Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
 656             fail("Operate on closed stream should throw IllegalStateException");
 657         } catch (IllegalStateException ex) {
 658             // expected
 659         }
 660     }
 661 }


  26  * @summary Unit test for java.nio.file.Files
  27  * @library ..
  28  * @build PassThroughFileSystem FaultyFileSystem
  29  * @run testng StreamTest
  30  */
  31 
  32 import java.io.IOException;
  33 import java.io.UncheckedIOException;
  34 import java.nio.charset.Charset;
  35 import java.nio.charset.MalformedInputException;
  36 import java.nio.file.DirectoryIteratorException;
  37 import java.nio.file.DirectoryStream;
  38 import java.nio.file.FileSystemLoopException;
  39 import java.nio.file.FileVisitOption;
  40 import java.nio.file.Files;
  41 import java.nio.file.NoSuchFileException;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.nio.file.attribute.BasicFileAttributes;
  45 import java.util.Arrays;
  46 import java.util.Comparator;
  47 import java.util.Iterator;
  48 import java.util.List;
  49 import java.util.Objects;
  50 import java.util.Set;
  51 import java.util.TreeSet;
  52 import java.util.function.BiPredicate;
  53 import java.util.stream.CloseableStream;
  54 import java.util.stream.Collectors;
  55 import org.testng.annotations.AfterClass;
  56 import org.testng.annotations.BeforeClass;
  57 import org.testng.annotations.Test;
  58 import static org.testng.Assert.*;
  59 
  60 @Test(groups = "unit")
  61 public class StreamTest {
  62     /**
  63      * Default test folder
  64      * testFolder - empty
  65      *            - file
  66      *            - dir - d1


 122         all = set.toArray(new Path[0]);
 123 
 124         // Follow links
 125         if (supportsLinks) {
 126             tmp = testFolder.resolve("linkDir");
 127             set.add(tmp.resolve("d1"));
 128             set.add(tmp.resolve("f1"));
 129             tmp = tmp.resolve("lnDir2");
 130             set.add(tmp);
 131         }
 132         all_folowLinks = set.toArray(new Path[0]);
 133     }
 134 
 135     @AfterClass
 136     void cleanupTestFolder() throws IOException {
 137         TestUtil.removeAll(testFolder);
 138     }
 139 
 140     public void testBasic() {
 141         try (CloseableStream<Path> s = Files.list(testFolder)) {
 142             Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
 143             assertEquals(actual, level1);
 144         } catch (IOException ioe) {
 145             fail("Unexpected IOException");
 146         }
 147 
 148         try (CloseableStream<Path> s = Files.list(testFolder.resolve("empty"))) {
 149             int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
 150             assertEquals(count, 0, "Expect empty stream.");
 151         } catch (IOException ioe) {
 152             fail("Unexpected IOException");
 153         }
 154     }
 155 
 156     public void testWalk() {
 157         try (CloseableStream<Path> s = Files.walk(testFolder)) {
 158             Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
 159             assertEquals(actual, all);
 160         } catch (IOException ioe) {
 161             fail("Unexpected IOException");
 162         }
 163     }
 164 
 165     public void testWalkOneLevel() {
 166         try (CloseableStream<Path> s = Files.walk(testFolder, 1)) {
 167             Object[] actual = s.filter(path -> ! path.equals(testFolder))
 168                                .sorted(Comparator.naturalOrder())
 169                                .toArray();
 170             assertEquals(actual, level1);
 171         } catch (IOException ioe) {
 172             fail("Unexpected IOException");
 173         }
 174     }
 175 
 176     public void testWalkFollowLink() {
 177         // If link is not supported, the directory structure won't have link.
 178         // We still want to test the behavior with FOLLOW_LINKS option.
 179         try (CloseableStream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) {
 180             Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
 181             assertEquals(actual, all_folowLinks);
 182         } catch (IOException ioe) {
 183             fail("Unexpected IOException");
 184         }
 185     }
 186 
 187     private void validateFileSystemLoopException(Path start, Path... causes) {
 188         try (CloseableStream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
 189             try {
 190                 int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
 191                 fail("Should got FileSystemLoopException, but got " + count + "elements.");
 192             } catch (UncheckedIOException uioe) {
 193                 IOException ioe = uioe.getCause();
 194                 if (ioe instanceof FileSystemLoopException) {
 195                     FileSystemLoopException fsle = (FileSystemLoopException) ioe;
 196                     boolean match = false;
 197                     for (Path cause: causes) {
 198                         if (fsle.getFile().equals(cause.toString())) {
 199                             match = true;
 200                             break;


 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,
 653                     (p, attr) -> true)) {
 654             s.close();
 655             Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
 656             fail("Operate on closed stream should throw IllegalStateException");
 657         } catch (IllegalStateException ex) {
 658             // expected
 659         }
 660     }
 661 }