jdk/test/java/nio/file/Files/StreamLinesTest.java
author alanb
Thu, 17 Mar 2016 19:04:16 +0000
changeset 36511 9d0388c6b336
parent 33830 9f4be4f1c8b6
child 45069 b13bb485359a
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     1
/*
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     4
 *
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     8
 *
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    13
 * accompanied this code).
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    14
 *
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    18
 *
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    21
 * questions.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    22
 */
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    23
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    24
/* @test
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    25
 * @bug 8072773
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 33830
diff changeset
    26
 * @library /lib/testlibrary/ ../../../util/stream/bootlib
9d0388c6b336 8142968: Module System implementation
alanb
parents: 33830
diff changeset
    27
 * @build java.base/java.util.stream.OpTestCase
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    28
 * @build jdk.testlibrary.RandomFactory
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    29
 * @run testng/othervm StreamLinesTest
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    30
 * @summary Tests streams returned from Files.lines, primarily focused on
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    31
 *          testing the file-channel-based stream stream with supported
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    32
 *          character sets
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    33
 * @key randomness
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    34
 */
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    35
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    36
import org.testng.annotations.DataProvider;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    37
import org.testng.annotations.Test;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    38
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    39
import java.io.BufferedReader;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    40
import java.io.BufferedWriter;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    41
import java.io.IOException;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    42
import java.nio.charset.Charset;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    43
import java.nio.charset.StandardCharsets;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    44
import java.nio.file.Files;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    45
import java.nio.file.Path;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    46
import java.nio.file.StandardOpenOption;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    47
import java.util.ArrayList;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    48
import java.util.Arrays;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    49
import java.util.EnumSet;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    50
import java.util.List;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    51
import java.util.Random;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    52
import java.util.function.IntFunction;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    53
import java.util.function.Supplier;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    54
import java.util.stream.OpTestCase;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    55
import java.util.stream.Stream;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    56
import java.util.stream.TestData;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    57
import jdk.testlibrary.RandomFactory;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    58
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    59
public class StreamLinesTest extends OpTestCase {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    60
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    61
    enum LineSeparator {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    62
        NONE(""),
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    63
        N("\n"),
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    64
        R("\r"),
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    65
        RN("\r\n");
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    66
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    67
        public final String value;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    68
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    69
        LineSeparator(String value) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    70
            this.value = value;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    71
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    72
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    73
        public String toString() {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    74
            return name();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    75
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    76
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    77
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    78
    static Path generateTempFileWithLines(IntFunction<String> lineGenerator,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    79
                                          IntFunction<LineSeparator> lineSeparatorGenerator,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    80
                                          int lines, Charset cs, boolean endLineSep) throws IOException {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    81
        Path p = Files.createTempFile("lines", null);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    82
        BufferedWriter bw = Files.newBufferedWriter(p, cs);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    83
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    84
        for (int i = 0; i < lines - 1; i++) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    85
            bw.write(lineGenerator.apply(i));
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    86
            bw.write(lineSeparatorGenerator.apply(i).value);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    87
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    88
        if (lines > 0) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    89
            bw.write(lineGenerator.apply(lines - 1));
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    90
            if (endLineSep)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    91
                bw.write(lineSeparatorGenerator.apply(lines - 1).value);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    92
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    93
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    94
        bw.flush();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    95
        bw.close();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    96
        p.toFile().deleteOnExit();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    97
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    98
        return p;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
    99
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   100
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   101
    static void writeLineSeparator(Path p,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   102
                                   IntFunction<LineSeparator> lineSeparatorGenerator,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   103
                                   int lines, Charset cs) throws IOException {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   104
        BufferedWriter bw = Files.newBufferedWriter(p, cs, StandardOpenOption.APPEND);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   105
        bw.write(lineSeparatorGenerator.apply(lines - 1).value);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   106
        bw.flush();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   107
        bw.close();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   108
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   109
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   110
    static List<String> readAllLines(Path path, Charset cs) throws IOException {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   111
        try (BufferedReader reader = Files.newBufferedReader(path, cs)) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   112
            List<String> result = new ArrayList<>();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   113
            for (; ; ) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   114
                String line = reader.readLine();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   115
                if (line == null)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   116
                    break;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   117
                result.add(line);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   118
            }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   119
            return result;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   120
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   121
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   122
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   123
    static Object[] of(String description, IntFunction<String> lineGenerator,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   124
                       IntFunction<LineSeparator> separatorGenerator, int n, Charset cs) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   125
        return new Object[]{description, lineGenerator, separatorGenerator, n, cs};
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   126
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   127
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   128
    private static final Random random = RandomFactory.getRandom();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   129
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   130
    @DataProvider
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   131
    public static Object[][] lines() {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   132
        List<Object[]> l = new ArrayList<>();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   133
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   134
        // Include the three supported optimal-line charsets and one
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   135
        // which does not
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   136
        List<Charset> charsets = Arrays.asList(StandardCharsets.UTF_8,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   137
                                               StandardCharsets.US_ASCII,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   138
                                               StandardCharsets.ISO_8859_1,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   139
                                               StandardCharsets.UTF_16);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   140
        String[] lines = {"", "A", "AB", "ABC", "ABCD"};
32476
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   141
        int[] linesSizes = {0, 1, 2, 3, 4, 16, 256, 1024};
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   142
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   143
        for (Charset charset : charsets) {
32476
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   144
            for (int linesSize : linesSizes) {
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   145
                if (linesSize > 0) {
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   146
                    for (String line : lines) {
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   147
                        for (LineSeparator ls : EnumSet.complementOf(EnumSet.of(LineSeparator.NONE))) {
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   148
                            String description = String.format("%d lines of \"%s\" with separator %s", linesSize, line, ls);
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   149
                            l.add(of(description,
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   150
                                    i -> line,
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   151
                                    i -> ls,
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   152
                                    linesSize, charset));
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   153
                        }
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   154
                    }
32476
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   155
                } else {
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   156
                    l.add(of("Empty file: 0 lines",
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   157
                            i -> "",
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   158
                            i -> LineSeparator.NONE,
568d6c299cad 8135091: (fs) java/nio/file/Files/StreamLinesTest.java should test empty files
bpb
parents: 31258
diff changeset
   159
                            0, charset));
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   160
                }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   161
            }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   162
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   163
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   164
        for (Charset charset : charsets) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   165
            l.add(of("A maximum of 1024 random lines and separators",
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   166
                     i -> lines[1 + random.nextInt(lines.length - 1)],
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   167
                     i -> LineSeparator.values()[random.nextInt(LineSeparator.values().length)],
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   168
                     1024, charset));
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   169
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   170
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   171
        for (Charset charset : charsets) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   172
            l.add(of("One large line with no separators",
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   173
                     i -> "ABCD",
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   174
                     i -> LineSeparator.NONE,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   175
                     1024, charset));
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   176
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   177
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   178
        return l.toArray(new Object[][]{});
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   179
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   180
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   181
    @Test(dataProvider = "lines")
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   182
    public void test(String description,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   183
                     IntFunction<String> lineGenerator, IntFunction<LineSeparator> separatorGenerator,
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   184
                     int lines, Charset cs) throws IOException {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   185
        Path p = generateTempFileWithLines(lineGenerator, separatorGenerator, lines, cs, false);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   186
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   187
        Supplier<Stream<String>> ss = () -> {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   188
            try {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   189
                return Files.lines(p, cs);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   190
            }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   191
            catch (IOException e) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   192
                throw new RuntimeException(e);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   193
            }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   194
        };
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   195
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   196
        // Test without a separator at the end
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   197
        List<String> expected = readAllLines(p, cs);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   198
        withData(TestData.Factory.ofSupplier("Lines with no separator at end", ss))
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   199
                .stream(s -> s)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   200
                .expectedResult(expected)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   201
                .exercise();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   202
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   203
        // Test with a separator at the end
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   204
        writeLineSeparator(p, separatorGenerator, lines, cs);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   205
        expected = readAllLines(p, cs);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   206
        withData(TestData.Factory.ofSupplier("Lines with separator at end", ss))
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   207
                .stream(s -> s)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   208
                .expectedResult(expected)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   209
                .exercise();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   210
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   211
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents:
diff changeset
   212
}