jdk/test/java/nio/file/Files/StreamTest.java
author henryjen
Thu, 09 May 2013 14:44:56 -0700
changeset 17696 23a863fbb6c3
child 17943 a8293d390273
permissions -rw-r--r--
8006884: (fs) Add Files.list, lines and find Reviewed-by: briangoetz, mduigou Contributed-by: alan.bateman@oracle.com, henry.jen@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     1
/*
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     4
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     7
 * published by the Free Software Foundation.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     8
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    13
 * accompanied this code).
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    14
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    18
 *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    21
 * questions.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    22
 */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    23
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    24
/* @test
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    25
 * @bug 8006884
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    26
 * @summary Unit test for java.nio.file.Files
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    27
 * @library ..
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    28
 * @build PassThroughFileSystem FaultyFileSystem
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    29
 * @run testng StreamTest
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    30
 */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    31
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    32
import java.io.IOException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    33
import java.io.UncheckedIOException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    34
import java.nio.charset.Charset;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    35
import java.nio.charset.MalformedInputException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    36
import java.nio.file.DirectoryIteratorException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    37
import java.nio.file.DirectoryStream;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    38
import java.nio.file.FileSystemLoopException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    39
import java.nio.file.FileVisitOption;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    40
import java.nio.file.Files;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    41
import java.nio.file.NoSuchFileException;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    42
import java.nio.file.Path;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    43
import java.nio.file.Paths;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    44
import java.nio.file.attribute.BasicFileAttributes;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    45
import java.util.Arrays;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    46
import java.util.Comparators;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    47
import java.util.Iterator;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    48
import java.util.List;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    49
import java.util.Objects;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    50
import java.util.Set;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    51
import java.util.TreeSet;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    52
import java.util.function.BiPredicate;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    53
import java.util.stream.CloseableStream;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    54
import java.util.stream.Collectors;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    55
import org.testng.annotations.AfterClass;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    56
import org.testng.annotations.BeforeClass;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    57
import org.testng.annotations.Test;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    58
import static org.testng.Assert.*;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    59
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    60
@Test(groups = "unit")
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    61
public class StreamTest {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    62
    /**
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    63
     * Default test folder
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    64
     * testFolder - empty
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    65
     *            - file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    66
     *            - dir - d1
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    67
     *                  - f1
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    68
     *                  - lnDir2 (../dir2)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    69
     *            - dir2
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    70
     *            - linkDir (./dir)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    71
     *            - linkFile(./file)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    72
     */
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    73
    static Path testFolder;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    74
    static boolean supportsLinks;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    75
    static Path[] level1;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    76
    static Path[] all;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    77
    static Path[] all_folowLinks;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    78
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    79
    @BeforeClass
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    80
    void setupTestFolder() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    81
        testFolder = TestUtil.createTemporaryDirectory();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    82
        supportsLinks = TestUtil.supportsLinks(testFolder);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    83
        TreeSet<Path> set = new TreeSet<>();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    84
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    85
        // Level 1
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    86
        Path empty = testFolder.resolve("empty");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    87
        Path file = testFolder.resolve("file");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    88
        Path dir = testFolder.resolve("dir");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    89
        Path dir2 = testFolder.resolve("dir2");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    90
        Files.createDirectory(empty);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    91
        Files.createFile(file);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    92
        Files.createDirectory(dir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    93
        Files.createDirectory(dir2);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    94
        set.add(empty);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    95
        set.add(file);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    96
        set.add(dir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    97
        set.add(dir2);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    98
        if (supportsLinks) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
    99
            Path tmp = testFolder.resolve("linkDir");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   100
            Files.createSymbolicLink(tmp, dir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   101
            set.add(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   102
            tmp = testFolder.resolve("linkFile");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   103
            Files.createSymbolicLink(tmp, file);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   104
            set.add(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   105
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   106
        level1 = set.toArray(new Path[0]);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   107
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   108
        // Level 2
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   109
        Path tmp = dir.resolve("d1");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   110
        Files.createDirectory(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   111
        set.add(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   112
        tmp = dir.resolve("f1");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   113
        Files.createFile(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   114
        set.add(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   115
        if (supportsLinks) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   116
            tmp = dir.resolve("lnDir2");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   117
            Files.createSymbolicLink(tmp, dir2);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   118
            set.add(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   119
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   120
        // walk include starting folder
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   121
        set.add(testFolder);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   122
        all = set.toArray(new Path[0]);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   123
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   124
        // Follow links
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   125
        if (supportsLinks) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   126
            tmp = testFolder.resolve("linkDir");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   127
            set.add(tmp.resolve("d1"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   128
            set.add(tmp.resolve("f1"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   129
            tmp = tmp.resolve("lnDir2");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   130
            set.add(tmp);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   131
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   132
        all_folowLinks = set.toArray(new Path[0]);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   133
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   134
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   135
    @AfterClass
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   136
    void cleanupTestFolder() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   137
        TestUtil.removeAll(testFolder);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   138
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   139
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   140
    public void testBasic() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   141
        try (CloseableStream<Path> s = Files.list(testFolder)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   142
            Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   143
            assertEquals(actual, level1);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   144
        } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   145
            fail("Unexpected IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   146
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   147
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   148
        try (CloseableStream<Path> s = Files.list(testFolder.resolve("empty"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   149
            int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   150
            assertEquals(count, 0, "Expect empty stream.");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   151
        } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   152
            fail("Unexpected IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   153
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   154
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   155
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   156
    public void testWalk() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   157
        try (CloseableStream<Path> s = Files.walk(testFolder)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   158
            Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   159
            assertEquals(actual, all);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   160
        } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   161
            fail("Unexpected IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   162
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   163
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   164
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   165
    public void testWalkOneLevel() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   166
        try (CloseableStream<Path> s = Files.walk(testFolder, 1)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   167
            Object[] actual = s.filter(path -> ! path.equals(testFolder))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   168
                               .sorted(Comparators.naturalOrder())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   169
                               .toArray();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   170
            assertEquals(actual, level1);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   171
        } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   172
            fail("Unexpected IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   173
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   174
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   175
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   176
    public void testWalkFollowLink() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   177
        // If link is not supported, the directory structure won't have link.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   178
        // We still want to test the behavior with FOLLOW_LINKS option.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   179
        try (CloseableStream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   180
            Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   181
            assertEquals(actual, all_folowLinks);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   182
        } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   183
            fail("Unexpected IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   184
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   185
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   186
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   187
    private void validateFileSystemLoopException(Path start, Path... causes) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   188
        try (CloseableStream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   189
            try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   190
                int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   191
                fail("Should got FileSystemLoopException, but got " + count + "elements.");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   192
            } catch (UncheckedIOException uioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   193
                IOException ioe = uioe.getCause();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   194
                if (ioe instanceof FileSystemLoopException) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   195
                    FileSystemLoopException fsle = (FileSystemLoopException) ioe;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   196
                    boolean match = false;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   197
                    for (Path cause: causes) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   198
                        if (fsle.getFile().equals(cause.toString())) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   199
                            match = true;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   200
                            break;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   201
                        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   202
                    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   203
                    assertTrue(match);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   204
                } else {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   205
                    fail("Unexpected UncheckedIOException cause " + ioe.toString());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   206
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   207
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   208
        } catch(IOException ex) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   209
            fail("Unexpected IOException " + ex);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   210
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   211
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   212
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   213
    public void testWalkFollowLinkLoop() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   214
        if (!supportsLinks) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   215
            return;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   216
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   217
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   218
        // Loops.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   219
        try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   220
            Path dir = testFolder.resolve("dir");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   221
            Path linkdir = testFolder.resolve("linkDir");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   222
            Path d1 = dir.resolve("d1");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   223
            Path cause = d1.resolve("lnSelf");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   224
            Files.createSymbolicLink(cause, d1);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   225
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   226
            // loop in descendant.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   227
            validateFileSystemLoopException(dir, cause);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   228
            // loop in self
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   229
            validateFileSystemLoopException(d1, cause);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   230
            // start from other place via link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   231
            validateFileSystemLoopException(linkdir,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   232
                    linkdir.resolve(Paths.get("d1", "lnSelf")));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   233
            Files.delete(cause);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   234
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   235
            // loop to parent.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   236
            cause = d1.resolve("lnParent");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   237
            Files.createSymbolicLink(cause, dir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   238
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   239
            // loop should be detected at test/dir/d1/lnParent/d1
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   240
            validateFileSystemLoopException(d1, cause.resolve("d1"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   241
            // loop should be detected at link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   242
            validateFileSystemLoopException(dir, cause);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   243
            // loop should be detected at test/linkdir/d1/lnParent
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   244
            // which is test/dir we have visited via test/linkdir
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   245
            validateFileSystemLoopException(linkdir,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   246
                    linkdir.resolve(Paths.get("d1", "lnParent")));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   247
            Files.delete(cause);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   248
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   249
            // cross loop
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   250
            Path dir2 = testFolder.resolve("dir2");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   251
            cause = dir2.resolve("lnDir");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   252
            Files.createSymbolicLink(cause, dir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   253
            validateFileSystemLoopException(dir,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   254
                    dir.resolve(Paths.get("lnDir2", "lnDir")));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   255
            validateFileSystemLoopException(dir2,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   256
                    dir2.resolve(Paths.get("lnDir", "lnDir2")));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   257
            validateFileSystemLoopException(linkdir,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   258
                    linkdir.resolve(Paths.get("lnDir2", "lnDir")));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   259
        } catch(IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   260
            fail("Unexpected IOException " + ioe);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   261
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   262
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   263
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   264
    private static class PathBiPredicate implements BiPredicate<Path, BasicFileAttributes> {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   265
        private final BiPredicate<Path, BasicFileAttributes> pred;
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   266
        private final Set<Path> visited = new TreeSet<Path>();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   267
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   268
        PathBiPredicate(BiPredicate<Path, BasicFileAttributes> pred) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   269
            this.pred = Objects.requireNonNull(pred);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   270
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   271
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   272
        public boolean test(Path path, BasicFileAttributes attrs) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   273
            visited.add(path);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   274
            return pred.test(path, attrs);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   275
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   276
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   277
        public Path[] visited() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   278
            return visited.toArray(new Path[0]);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   279
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   280
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   281
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   282
    public void testFind() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   283
        PathBiPredicate pred = new PathBiPredicate((path, attrs) -> true);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   284
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   285
        try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   286
            Set<Path> result = s.collect(Collectors.toCollection(TreeSet::new));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   287
            assertEquals(pred.visited(), all);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   288
            assertEquals(result.toArray(new Path[0]), pred.visited());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   289
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   290
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   291
        pred = new PathBiPredicate((path, attrs) -> attrs.isSymbolicLink());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   292
        try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   293
            s.forEach(path -> assertTrue(Files.isSymbolicLink(path)));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   294
            assertEquals(pred.visited(), all);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   295
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   296
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   297
        pred = new PathBiPredicate((path, attrs) ->
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   298
            path.getFileName().toString().startsWith("e"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   299
        try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   300
            s.forEach(path -> assertEquals(path.getFileName().toString(), "empty"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   301
            assertEquals(pred.visited(), all);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   302
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   303
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   304
        pred = new PathBiPredicate((path, attrs) ->
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   305
            path.getFileName().toString().startsWith("l") && attrs.isRegularFile());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   306
        try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   307
            s.forEach(path -> fail("Expect empty stream"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   308
            assertEquals(pred.visited(), all);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   309
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   310
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   311
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   312
    // Test borrowed from BytesAndLines
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   313
    public void testLines() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   314
        final Charset US_ASCII = Charset.forName("US-ASCII");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   315
        Path tmpfile = Files.createTempFile("blah", "txt");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   316
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   317
        try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   318
            // zero lines
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   319
            assertTrue(Files.size(tmpfile) == 0, "File should be empty");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   320
            try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   321
                assertEquals(s.mapToInt(l -> 1).reduce(0, Integer::sum), 0, "No line expected");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   322
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   323
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   324
            // one line
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   325
            byte[] hi = { (byte)'h', (byte)'i' };
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   326
            Files.write(tmpfile, hi);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   327
            try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   328
                List<String> lines = s.collect(Collectors.toList());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   329
                assertTrue(lines.size() == 1, "One line expected");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   330
                assertTrue(lines.get(0).equals("hi"), "'Hi' expected");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   331
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   332
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   333
            // two lines using platform's line separator
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   334
            List<String> expected = Arrays.asList("hi", "there");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   335
            Files.write(tmpfile, expected, US_ASCII);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   336
            assertTrue(Files.size(tmpfile) > 0, "File is empty");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   337
            try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   338
                List<String> lines = s.collect(Collectors.toList());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   339
                assertTrue(lines.equals(expected), "Unexpected lines");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   340
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   341
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   342
            // MalformedInputException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   343
            byte[] bad = { (byte)0xff, (byte)0xff };
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   344
            Files.write(tmpfile, bad);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   345
            try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   346
                try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   347
                    List<String> lines = s.collect(Collectors.toList());
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   348
                    throw new RuntimeException("UncheckedIOException expected");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   349
                } catch (UncheckedIOException ex) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   350
                    assertTrue(ex.getCause() instanceof MalformedInputException,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   351
                               "MalformedInputException expected");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   352
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   353
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   354
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   355
            // NullPointerException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   356
            try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   357
                Files.lines(null, US_ASCII);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   358
                throw new RuntimeException("NullPointerException expected");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   359
            } catch (NullPointerException ignore) { }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   360
            try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   361
                Files.lines(tmpfile, null);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   362
                throw new RuntimeException("NullPointerException expected");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   363
            } catch (NullPointerException ignore) { }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   364
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   365
        } finally {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   366
            Files.delete(tmpfile);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   367
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   368
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   369
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   370
    public void testDirectoryIteratorException() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   371
        Path dir = testFolder.resolve("dir2");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   372
        Path trigger = dir.resolve("DirectoryIteratorException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   373
        Files.createFile(trigger);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   374
        FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   375
        FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(dir, null);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   376
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   377
        try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   378
            fsp.setFaultyMode(false);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   379
            Path fakeRoot = fs.getRoot();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   380
            try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   381
                try (CloseableStream<Path> s = Files.list(fakeRoot)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   382
                    s.forEach(path -> assertEquals(path.getFileName().toString(), "DirectoryIteratorException"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   383
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   384
            } catch (UncheckedIOException uioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   385
                fail("Unexpected exception.");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   386
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   387
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   388
            fsp.setFaultyMode(true);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   389
            try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   390
                try (DirectoryStream<Path> ds = Files.newDirectoryStream(fakeRoot)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   391
                    Iterator<Path> itor = ds.iterator();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   392
                    while (itor.hasNext()) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   393
                        itor.next();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   394
                    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   395
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   396
                fail("Shoule throw DirectoryIteratorException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   397
            } catch (DirectoryIteratorException die) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   398
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   399
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   400
            try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   401
                try (CloseableStream<Path> s = Files.list(fakeRoot)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   402
                    s.forEach(path -> fail("should not get here"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   403
                }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   404
            } catch (UncheckedIOException uioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   405
                assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   406
            } catch (DirectoryIteratorException die) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   407
                fail("Should have been converted into UncheckedIOException.");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   408
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   409
        } finally {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   410
            // Cleanup
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   411
            if (fs != null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   412
                fs.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   413
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   414
            Files.delete(trigger);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   415
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   416
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   417
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   418
    public void testUncheckedIOException() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   419
        Path triggerFile = testFolder.resolve(Paths.get("dir2", "IOException"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   420
        Files.createFile(triggerFile);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   421
        Path triggerDir = testFolder.resolve(Paths.get("empty", "IOException"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   422
        Files.createDirectories(triggerDir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   423
        Files.createFile(triggerDir.resolve("file"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   424
        FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   425
        FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   426
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   427
        try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   428
            fsp.setFaultyMode(false);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   429
            Path fakeRoot = fs.getRoot();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   430
            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   431
                // only one file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   432
                s.forEach(path -> assertEquals(path.getFileName().toString(), "IOException"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   433
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   434
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   435
            try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   436
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   437
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   438
                // ordered as depth-first
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   439
                assertEquals(result, new String[] { "empty", "IOException", "file"});
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   440
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   441
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   442
            fsp.setFaultyMode(true);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   443
            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   444
                s.forEach(path -> fail("should have caused exception"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   445
            } catch (UncheckedIOException uioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   446
                assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   447
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   448
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   449
            try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   450
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   451
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   452
                fail("should not reach here due to IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   453
            } catch (UncheckedIOException uioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   454
                assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   455
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   456
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   457
            try (CloseableStream<Path> s = Files.walk(
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   458
                fakeRoot.resolve("empty").resolve("IOException")))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   459
            {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   460
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   461
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   462
                fail("should not reach here due to IOException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   463
            } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   464
                assertTrue(ioe instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   465
            } catch (UncheckedIOException ex) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   466
                fail("Top level should be repored as is");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   467
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   468
         } finally {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   469
            // Cleanup
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   470
            if (fs != null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   471
                fs.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   472
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   473
            Files.delete(triggerFile);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   474
            TestUtil.removeAll(triggerDir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   475
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   476
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   477
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   478
    public void testSecurityException() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   479
        Path triggerFile = testFolder.resolve(Paths.get("dir", "SecurityException"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   480
        Files.createFile(triggerFile);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   481
        Path sampleFile = testFolder.resolve(Paths.get("dir", "sample"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   482
        Files.createFile(sampleFile);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   483
        Path triggerDir = testFolder.resolve(Paths.get("dir2", "SecurityException"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   484
        Files.createDirectories(triggerDir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   485
        Files.createFile(triggerDir.resolve("fileInSE"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   486
        Path sample = testFolder.resolve(Paths.get("dir2", "file"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   487
        Files.createFile(sample);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   488
        FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   489
        FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   490
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   491
        try {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   492
            fsp.setFaultyMode(false);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   493
            Path fakeRoot = fs.getRoot();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   494
            // validate setting
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   495
            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   496
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   497
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   498
                assertEqualsNoOrder(result, new String[] { "d1","f1", "lnDir2", "SecurityException", "sample" });
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   499
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   500
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   501
            try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   502
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   503
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   504
                assertEqualsNoOrder(result, new String[] { "dir2", "SecurityException", "fileInSE", "file" });
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   505
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   506
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   507
            // execute test
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   508
            fsp.setFaultyMode(true);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   509
            // ignore file cause SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   510
            try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   511
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   512
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   513
                assertEqualsNoOrder(result, new String[] { "dir", "d1","f1", "lnDir2", "sample" });
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   514
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   515
            // skip folder cause SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   516
            try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   517
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   518
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   519
                assertEqualsNoOrder(result, new String[] { "dir2", "file" });
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   520
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   521
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   522
            // list instead of walk
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   523
            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   524
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   525
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   526
                assertEqualsNoOrder(result, new String[] { "d1","f1", "lnDir2", "sample" });
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   527
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   528
            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   529
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   530
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   531
                assertEqualsNoOrder(result, new String[] { "file" });
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   532
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   533
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   534
            // root cause SecurityException should be reported
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   535
            try (CloseableStream<Path> s = Files.walk(
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   536
                fakeRoot.resolve("dir2").resolve("SecurityException")))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   537
            {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   538
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   539
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   540
                fail("should not reach here due to SecurityException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   541
            } catch (SecurityException se) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   542
                assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   543
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   544
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   545
            // Walk a file cause SecurityException, we should get SE
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   546
            try (CloseableStream<Path> s = Files.walk(
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   547
                fakeRoot.resolve("dir").resolve("SecurityException")))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   548
            {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   549
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   550
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   551
                fail("should not reach here due to SecurityException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   552
            } catch (SecurityException se) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   553
                assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   554
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   555
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   556
            // List a file cause SecurityException, we should get SE as cannot read attribute
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   557
            try (CloseableStream<Path> s = Files.list(
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   558
                fakeRoot.resolve("dir2").resolve("SecurityException")))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   559
            {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   560
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   561
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   562
                fail("should not reach here due to SecurityException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   563
            } catch (SecurityException se) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   564
                assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   565
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   566
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   567
            try (CloseableStream<Path> s = Files.list(
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   568
                fakeRoot.resolve("dir").resolve("SecurityException")))
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   569
            {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   570
                String[] result = s.map(path -> path.getFileName().toString())
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   571
                                   .toArray(String[]::new);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   572
                fail("should not reach here due to SecurityException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   573
            } catch (SecurityException se) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   574
                assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   575
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   576
         } finally {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   577
            // Cleanup
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   578
            if (fs != null) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   579
                fs.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   580
            }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   581
            Files.delete(triggerFile);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   582
            Files.delete(sampleFile);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   583
            Files.delete(sample);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   584
            TestUtil.removeAll(triggerDir);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   585
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   586
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   587
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   588
    public void testConstructException() {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   589
        try (CloseableStream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   590
            s.forEach(l -> fail("File is not even exist!"));
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   591
        } catch (IOException ioe) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   592
            ioe.printStackTrace(System.err);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   593
            assertTrue(ioe instanceof NoSuchFileException);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   594
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   595
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   596
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   597
    public void testClosedStream() throws IOException {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   598
        try (CloseableStream<Path> s = Files.list(testFolder)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   599
            s.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   600
            Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   601
            assertTrue(actual.length <= level1.length);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   602
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   603
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   604
        try (CloseableStream<Path> s = Files.walk(testFolder)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   605
            s.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   606
            Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   607
            fail("Operate on closed stream should throw IllegalStateException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   608
        } catch (IllegalStateException ex) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   609
            // expected
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   610
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   611
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   612
        try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   613
                    (p, attr) -> true)) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   614
            s.close();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   615
            Object[] actual = s.sorted(Comparators.naturalOrder()).toArray();
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   616
            fail("Operate on closed stream should throw IllegalStateException");
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   617
        } catch (IllegalStateException ex) {
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   618
            // expected
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   619
        }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   620
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents:
diff changeset
   621
}