Path: blob/master/test/jdk/java/nio/file/Files/StreamLinesTest.java
41153 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 807277325* @library /test/lib /lib/testlibrary/bootlib26* @build java.base/java.util.stream.OpTestCase27* jdk.test.lib.RandomFactory28* @run testng/othervm StreamLinesTest29* @summary Tests streams returned from Files.lines, primarily focused on30* testing the file-channel-based stream stream with supported31* character sets32* @key randomness33*/3435import org.testng.annotations.DataProvider;36import org.testng.annotations.Test;3738import java.io.BufferedReader;39import java.io.BufferedWriter;40import java.io.IOException;41import java.nio.charset.Charset;42import java.nio.charset.StandardCharsets;43import java.nio.file.Files;44import java.nio.file.Path;45import java.nio.file.StandardOpenOption;46import java.util.ArrayList;47import java.util.Arrays;48import java.util.EnumSet;49import java.util.List;50import java.util.Random;51import java.util.function.IntFunction;52import java.util.function.Supplier;53import java.util.stream.OpTestCase;54import java.util.stream.Stream;55import java.util.stream.TestData;56import jdk.test.lib.RandomFactory;5758public class StreamLinesTest extends OpTestCase {5960enum LineSeparator {61NONE(""),62N("\n"),63R("\r"),64RN("\r\n");6566public final String value;6768LineSeparator(String value) {69this.value = value;70}7172public String toString() {73return name();74}75}7677static Path generateTempFileWithLines(IntFunction<String> lineGenerator,78IntFunction<LineSeparator> lineSeparatorGenerator,79int lines, Charset cs, boolean endLineSep) throws IOException {80Path p = Files.createTempFile("lines", null);81BufferedWriter bw = Files.newBufferedWriter(p, cs);8283for (int i = 0; i < lines - 1; i++) {84bw.write(lineGenerator.apply(i));85bw.write(lineSeparatorGenerator.apply(i).value);86}87if (lines > 0) {88bw.write(lineGenerator.apply(lines - 1));89if (endLineSep)90bw.write(lineSeparatorGenerator.apply(lines - 1).value);91}9293bw.flush();94bw.close();95p.toFile().deleteOnExit();9697return p;98}99100static void writeLineSeparator(Path p,101IntFunction<LineSeparator> lineSeparatorGenerator,102int lines, Charset cs) throws IOException {103BufferedWriter bw = Files.newBufferedWriter(p, cs, StandardOpenOption.APPEND);104bw.write(lineSeparatorGenerator.apply(lines - 1).value);105bw.flush();106bw.close();107}108109static List<String> readAllLines(Path path, Charset cs) throws IOException {110try (BufferedReader reader = Files.newBufferedReader(path, cs)) {111List<String> result = new ArrayList<>();112for (; ; ) {113String line = reader.readLine();114if (line == null)115break;116result.add(line);117}118return result;119}120}121122static Object[] of(String description, IntFunction<String> lineGenerator,123IntFunction<LineSeparator> separatorGenerator, int n, Charset cs) {124return new Object[]{description, lineGenerator, separatorGenerator, n, cs};125}126127private static final Random random = RandomFactory.getRandom();128129@DataProvider130public static Object[][] lines() {131List<Object[]> l = new ArrayList<>();132133// Include the three supported optimal-line charsets and one134// which does not135List<Charset> charsets = Arrays.asList(StandardCharsets.UTF_8,136StandardCharsets.US_ASCII,137StandardCharsets.ISO_8859_1,138StandardCharsets.UTF_16);139String[] lines = {"", "A", "AB", "ABC", "ABCD"};140int[] linesSizes = {0, 1, 2, 3, 4, 16, 256, 1024};141142for (Charset charset : charsets) {143for (int linesSize : linesSizes) {144if (linesSize > 0) {145for (String line : lines) {146for (LineSeparator ls : EnumSet.complementOf(EnumSet.of(LineSeparator.NONE))) {147String description = String.format("%d lines of \"%s\" with separator %s", linesSize, line, ls);148l.add(of(description,149i -> line,150i -> ls,151linesSize, charset));152}153}154} else {155l.add(of("Empty file: 0 lines",156i -> "",157i -> LineSeparator.NONE,1580, charset));159}160}161}162163for (Charset charset : charsets) {164l.add(of("A maximum of 1024 random lines and separators",165i -> lines[1 + random.nextInt(lines.length - 1)],166i -> LineSeparator.values()[random.nextInt(LineSeparator.values().length)],1671024, charset));168}169170for (Charset charset : charsets) {171l.add(of("One large line with no separators",172i -> "ABCD",173i -> LineSeparator.NONE,1741024, charset));175}176177return l.toArray(new Object[][]{});178}179180@Test(dataProvider = "lines")181public void test(String description,182IntFunction<String> lineGenerator, IntFunction<LineSeparator> separatorGenerator,183int lines, Charset cs) throws IOException {184Path p = generateTempFileWithLines(lineGenerator, separatorGenerator, lines, cs, false);185186Supplier<Stream<String>> ss = () -> {187try {188return Files.lines(p, cs);189}190catch (IOException e) {191throw new RuntimeException(e);192}193};194195// Test without a separator at the end196List<String> expected = readAllLines(p, cs);197withData(TestData.Factory.ofSupplier("Lines with no separator at end", ss))198.stream(s -> s)199.expectedResult(expected)200.exercise();201202// Test with a separator at the end203writeLineSeparator(p, separatorGenerator, lines, cs);204expected = readAllLines(p, cs);205withData(TestData.Factory.ofSupplier("Lines with separator at end", ss))206.stream(s -> s)207.expectedResult(expected)208.exercise();209}210211}212213214