1 /*
2  * Copyright (c) 2018, 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 /**
25  * @test
26  * @summary Basic lines functionality
27  * @bug 8200380
28  * @run main/othervm Lines
29  */
30 package test.java.lang.String;
31 
32 import java.util.Iterator;
33 import java.util.stream.Stream;
34 import java.io.BufferedReader;
35 import java.io.StringReader;
36 
37 import org.testng.annotations.Test;
38 import static org.testng.Assert.fail;
39 
40 public class Lines {
41     /*
42      * Test with strings
43      */
44     @Test
testLines()45     public void testLines() {
46         testString("");
47         testString(" ");
48         testString("\n");
49         testString("\n\n\n");
50         testString("\r\r\r");
51         testString("\r\n\r\n\r\n");
52         testString("\n\r\r\n");
53         testString("abc\ndef\nghi\n");
54         testString("abc\ndef\nghi");
55         testString("abc\rdef\rghi\r");
56         testString("abc\rdef\rghi");
57         testString("abc\r\ndef\r\nghi\r\n");
58         testString("abc\r\ndef\r\nghi");
59 
60         testString("\2022");
61         testString("\2022\n");
62         testString("\2022\n\2022\n\2022\n");
63         testString("\2022\r\2022\r\2022\r");
64         testString("\2022\r\n\2022\r\n\2022\r\n");
65         testString("\2022\n\2022\r\2022\r\n");
66         testString("abc\2022\ndef\2022\nghi\2022\n");
67         testString("abc\2022\ndef\2022\nghi\2022");
68         testString("abc\2022\rdef\2022\rghi\2022\r");
69         testString("abc\2022\rdef\2022\rghi\2022");
70         testString("abc\2022\r\ndef\2022\r\nghi\2022\r\n");
71         testString("abc\2022\r\ndef\2022\r\nghi\2022");
72         testString("\2022\n\n\n");
73     }
74 
testString(String string)75     static void testString(String string) {
76         Stream<String> lines = string.lines();
77         Stream<String> brLines = new BufferedReader(new StringReader(string)).lines();
78 
79         Iterator<String> iterator = lines.iterator();
80         Iterator<String> brIterator = brLines.iterator();
81         int count = 0;
82 
83         while (iterator.hasNext() && brIterator.hasNext()) {
84             count++;
85             String line = iterator.next();
86             String brLine = brIterator.next();
87 
88             if (!line.equals(brLine)) {
89                 String replace = string.replaceAll("\n", "\\n").replaceAll("\r", "\\r");
90                 fail(String.format("Mismatch at line %d of \"%s\"%n", count, replace));
91             }
92         }
93 
94         if (iterator.hasNext() || brIterator.hasNext()) {
95             fail(String.format("Mismatch after line %d of \"%s\"%n", count, string));
96         }
97     }
98 }
99