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  * @run testng/othervm StreamLinesTest
  29  * @summary Tests streams returned from Files.lines, primarily focused on
  30  *          testing the file-channel-based stream stream with supported
  31  *          character sets
  32  * @key randomness
  33  */
  34 
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 
  38 import java.io.BufferedReader;
  39 import java.io.BufferedWriter;
  40 import java.io.IOException;
  41 import java.nio.charset.Charset;
  42 import java.nio.charset.StandardCharsets;
  43 import java.nio.file.Files;
  44 import java.nio.file.Path;
  45 import java.nio.file.StandardOpenOption;
  46 import java.util.ArrayList;
  47 import java.util.Arrays;
  48 import java.util.EnumSet;
  49 import java.util.List;
  50 import java.util.Random;
  51 import java.util.function.IntFunction;
  52 import java.util.function.Supplier;
  53 import java.util.stream.OpTestCase;
  54 import java.util.stream.Stream;
  55 import java.util.stream.TestData;
  56 import jdk.test.lib.RandomFactory;
  57 
  58 public class StreamLinesTest extends OpTestCase {
  59 
  60     enum LineSeparator {
  61         NONE(""),
  62         N("\n"),
  63         R("\r"),
  64         RN("\r\n");
  65 
  66         public final String value;
  67 
  68         LineSeparator(String value) {
  69             this.value = value;
  70         }
  71 
  72         public String toString() {
  73             return name();
  74         }
  75     }
  76 
  77     static Path generateTempFileWithLines(IntFunction<String> lineGenerator,
  78                                           IntFunction<LineSeparator> lineSeparatorGenerator,
  79                                           int lines, Charset cs, boolean endLineSep) throws IOException {
  80         Path p = Files.createTempFile("lines", null);
  81         BufferedWriter bw = Files.newBufferedWriter(p, cs);
  82 
  83         for (int i = 0; i < lines - 1; i++) {
  84             bw.write(lineGenerator.apply(i));
  85             bw.write(lineSeparatorGenerator.apply(i).value);
  86         }
  87         if (lines > 0) {
  88             bw.write(lineGenerator.apply(lines - 1));
  89             if (endLineSep)
  90                 bw.write(lineSeparatorGenerator.apply(lines - 1).value);
  91         }
  92 
  93         bw.flush();
  94         bw.close();
  95         p.toFile().deleteOnExit();
  96 
  97         return p;
  98     }
  99 
 100     static void writeLineSeparator(Path p,
 101                                    IntFunction<LineSeparator> lineSeparatorGenerator,
 102                                    int lines, Charset cs) throws IOException {
 103         BufferedWriter bw = Files.newBufferedWriter(p, cs, StandardOpenOption.APPEND);
 104         bw.write(lineSeparatorGenerator.apply(lines - 1).value);
 105         bw.flush();
 106         bw.close();
 107     }
 108 
 109     static List<String> readAllLines(Path path, Charset cs) throws IOException {
 110         try (BufferedReader reader = Files.newBufferedReader(path, cs)) {
 111             List<String> result = new ArrayList<>();
 112             for (; ; ) {
 113                 String line = reader.readLine();
 114                 if (line == null)
 115                     break;
 116                 result.add(line);
 117             }
 118             return result;
 119         }
 120     }
 121 
 122     static Object[] of(String description, IntFunction<String> lineGenerator,
 123                        IntFunction<LineSeparator> separatorGenerator, int n, Charset cs) {
 124         return new Object[]{description, lineGenerator, separatorGenerator, n, cs};
 125     }
 126 
 127     private static final Random random = RandomFactory.getRandom();
 128 
 129     @DataProvider
 130     public static Object[][] lines() {
 131         List<Object[]> l = new ArrayList<>();
 132 
 133         // Include the three supported optimal-line charsets and one
 134         // which does not
 135         List<Charset> charsets = Arrays.asList(StandardCharsets.UTF_8,
 136                                                StandardCharsets.US_ASCII,
 137                                                StandardCharsets.ISO_8859_1,
 138                                                StandardCharsets.UTF_16);
 139         String[] lines = {"", "A", "AB", "ABC", "ABCD"};
 140         int[] linesSizes = {0, 1, 2, 3, 4, 16, 256, 1024};
 141 
 142         for (Charset charset : charsets) {
 143             for (int linesSize : linesSizes) {
 144                 if (linesSize > 0) {
 145                     for (String line : lines) {
 146                         for (LineSeparator ls : EnumSet.complementOf(EnumSet.of(LineSeparator.NONE))) {
 147                             String description = String.format("%d lines of \"%s\" with separator %s", linesSize, line, ls);
 148                             l.add(of(description,
 149                                     i -> line,
 150                                     i -> ls,
 151                                     linesSize, charset));
 152                         }
 153                     }
 154                 } else {
 155                     l.add(of("Empty file: 0 lines",
 156                             i -> "",
 157                             i -> LineSeparator.NONE,
 158                             0, charset));
 159                 }
 160             }
 161         }
 162 
 163         for (Charset charset : charsets) {
 164             l.add(of("A maximum of 1024 random lines and separators",
 165                      i -> lines[1 + random.nextInt(lines.length - 1)],
 166                      i -> LineSeparator.values()[random.nextInt(LineSeparator.values().length)],
 167                      1024, charset));
 168         }
 169 
 170         for (Charset charset : charsets) {
 171             l.add(of("One large line with no separators",
 172                      i -> "ABCD",
 173                      i -> LineSeparator.NONE,
 174                      1024, charset));
 175         }
 176 
 177         return l.toArray(new Object[][]{});
 178     }
 179 
 180     @Test(dataProvider = "lines")
 181     public void test(String description,
 182                      IntFunction<String> lineGenerator, IntFunction<LineSeparator> separatorGenerator,
 183                      int lines, Charset cs) throws IOException {
 184         Path p = generateTempFileWithLines(lineGenerator, separatorGenerator, lines, cs, false);
 185 
 186         Supplier<Stream<String>> ss = () -> {
 187             try {
 188                 return Files.lines(p, cs);
 189             }
 190             catch (IOException e) {
 191                 throw new RuntimeException(e);
 192             }
 193         };
 194 
 195         // Test without a separator at the end
 196         List<String> expected = readAllLines(p, cs);
 197         withData(TestData.Factory.ofSupplier("Lines with no separator at end", ss))
 198                 .stream(s -> s)
 199                 .expectedResult(expected)
 200                 .exercise();
 201 
 202         // Test with a separator at the end
 203         writeLineSeparator(p, separatorGenerator, lines, cs);
 204         expected = readAllLines(p, cs);
 205         withData(TestData.Factory.ofSupplier("Lines with separator at end", ss))
 206                 .stream(s -> s)
 207                 .expectedResult(expected)
 208                 .exercise();
 209     }
 210 
 211 }