1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8072773 26 * @library /test/lib /lib/testlibrary/bootlib 27 * @build java.base/java.util.stream.OpTestCase 28 * jdk.test.lib.RandomFactory 29 * @run testng/othervm StreamLinesTest 30 * @summary Tests streams returned from Files.lines, primarily focused on 31 * testing the file-channel-based stream stream with supported 32 * character sets 33 * @key randomness 34 */ 35 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 39 import java.io.BufferedReader; 40 import java.io.BufferedWriter; 41 import java.io.IOException; 42 import java.nio.charset.Charset; 43 import java.nio.charset.StandardCharsets; 44 import java.nio.file.Files; 45 import java.nio.file.Path; 46 import java.nio.file.StandardOpenOption; 47 import java.util.ArrayList; 48 import java.util.Arrays; 49 import java.util.EnumSet; 50 import java.util.List; 51 import java.util.Random; 52 import java.util.function.IntFunction; 53 import java.util.function.Supplier; 54 import java.util.stream.OpTestCase; 55 import java.util.stream.Stream; 56 import java.util.stream.TestData; 57 import jdk.test.lib.RandomFactory; 58 59 public class StreamLinesTest extends OpTestCase { 60 61 enum LineSeparator { 62 NONE(""), 63 N("\n"), 64 R("\r"), 65 RN("\r\n"); 66 67 public final String value; 68 69 LineSeparator(String value) { 70 this.value = value; 71 } 72 73 public String toString() { 74 return name(); 75 } 76 } 77 78 static Path generateTempFileWithLines(IntFunction<String> lineGenerator, 79 IntFunction<LineSeparator> lineSeparatorGenerator, 80 int lines, Charset cs, boolean endLineSep) throws IOException { 81 Path p = Files.createTempFile("lines", null); 82 BufferedWriter bw = Files.newBufferedWriter(p, cs); 83 84 for (int i = 0; i < lines - 1; i++) { 85 bw.write(lineGenerator.apply(i)); 86 bw.write(lineSeparatorGenerator.apply(i).value); 87 } 88 if (lines > 0) { 89 bw.write(lineGenerator.apply(lines - 1)); 90 if (endLineSep) 91 bw.write(lineSeparatorGenerator.apply(lines - 1).value); 92 } 93 94 bw.flush(); 95 bw.close(); 96 p.toFile().deleteOnExit(); 97 98 return p; 99 } 100 101 static void writeLineSeparator(Path p, 102 IntFunction<LineSeparator> lineSeparatorGenerator, 103 int lines, Charset cs) throws IOException { 104 BufferedWriter bw = Files.newBufferedWriter(p, cs, StandardOpenOption.APPEND); 105 bw.write(lineSeparatorGenerator.apply(lines - 1).value); 106 bw.flush(); 107 bw.close(); 108 } 109 110 static List<String> readAllLines(Path path, Charset cs) throws IOException { 111 try (BufferedReader reader = Files.newBufferedReader(path, cs)) { 112 List<String> result = new ArrayList<>(); 113 for (; ; ) { 114 String line = reader.readLine(); 115 if (line == null) 116 break; 117 result.add(line); 118 } 119 return result; 120 } 121 } 122 123 static Object[] of(String description, IntFunction<String> lineGenerator, 124 IntFunction<LineSeparator> separatorGenerator, int n, Charset cs) { 125 return new Object[]{description, lineGenerator, separatorGenerator, n, cs}; 126 } 127 128 private static final Random random = RandomFactory.getRandom(); 129 130 @DataProvider 131 public static Object[][] lines() { 132 List<Object[]> l = new ArrayList<>(); 133 134 // Include the three supported optimal-line charsets and one 135 // which does not 136 List<Charset> charsets = Arrays.asList(StandardCharsets.UTF_8, 137 StandardCharsets.US_ASCII, 138 StandardCharsets.ISO_8859_1, 139 StandardCharsets.UTF_16); 140 String[] lines = {"", "A", "AB", "ABC", "ABCD"}; 141 int[] linesSizes = {0, 1, 2, 3, 4, 16, 256, 1024}; 142 143 for (Charset charset : charsets) { 144 for (int linesSize : linesSizes) { 145 if (linesSize > 0) { 146 for (String line : lines) { 147 for (LineSeparator ls : EnumSet.complementOf(EnumSet.of(LineSeparator.NONE))) { 148 String description = String.format("%d lines of \"%s\" with separator %s", linesSize, line, ls); 149 l.add(of(description, 150 i -> line, 151 i -> ls, 152 linesSize, charset)); 153 } 154 } 155 } else { 156 l.add(of("Empty file: 0 lines", 157 i -> "", 158 i -> LineSeparator.NONE, 159 0, charset)); 160 } 161 } 162 } 163 164 for (Charset charset : charsets) { 165 l.add(of("A maximum of 1024 random lines and separators", 166 i -> lines[1 + random.nextInt(lines.length - 1)], 167 i -> LineSeparator.values()[random.nextInt(LineSeparator.values().length)], 168 1024, charset)); 169 } 170 171 for (Charset charset : charsets) { 172 l.add(of("One large line with no separators", 173 i -> "ABCD", 174 i -> LineSeparator.NONE, 175 1024, charset)); 176 } 177 178 return l.toArray(new Object[][]{}); 179 } 180 181 @Test(dataProvider = "lines") 182 public void test(String description, 183 IntFunction<String> lineGenerator, IntFunction<LineSeparator> separatorGenerator, 184 int lines, Charset cs) throws IOException { 185 Path p = generateTempFileWithLines(lineGenerator, separatorGenerator, lines, cs, false); 186 187 Supplier<Stream<String>> ss = () -> { 188 try { 189 return Files.lines(p, cs); 190 } 191 catch (IOException e) { 192 throw new RuntimeException(e); 193 } 194 }; 195 196 // Test without a separator at the end 197 List<String> expected = readAllLines(p, cs); 198 withData(TestData.Factory.ofSupplier("Lines with no separator at end", ss)) 199 .stream(s -> s) 200 .expectedResult(expected) 201 .exercise(); 202 203 // Test with a separator at the end 204 writeLineSeparator(p, separatorGenerator, lines, cs); 205 expected = readAllLines(p, cs); 206 withData(TestData.Factory.ofSupplier("Lines with separator at end", ss)) 207 .stream(s -> s) 208 .expectedResult(expected) 209 .exercise(); 210 } 211 212 }