Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/BufferedReader/Lines.java
41152 views
1
/*
2
* Copyright (c) 2012, 2013, 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
* @bug 8003258 8029434
27
* @run testng Lines
28
*/
29
30
import java.io.BufferedReader;
31
import java.io.Reader;
32
import java.io.StringReader;
33
import java.io.LineNumberReader;
34
import java.io.IOException;
35
import java.io.UncheckedIOException;
36
import java.util.HashMap;
37
import java.util.Map;
38
import java.util.ArrayList;
39
import java.util.Iterator;
40
import java.util.NoSuchElementException;
41
import java.util.Spliterator;
42
import java.util.stream.Stream;
43
import java.util.concurrent.atomic.AtomicInteger;
44
import org.testng.annotations.Test;
45
import static org.testng.Assert.*;
46
47
@Test(groups = "unit")
48
public class Lines {
49
private static final Map<String, Integer> cases = new HashMap<>();
50
51
static {
52
cases.put("", 0);
53
cases.put("Line 1", 1);
54
cases.put("Line 1\n", 1);
55
cases.put("Line 1\n\n\n", 3);
56
cases.put("Line 1\nLine 2\nLine 3", 3);
57
cases.put("Line 1\nLine 2\nLine 3\n", 3);
58
cases.put("Line 1\n\nLine 3\n\nLine5", 5);
59
}
60
61
/**
62
* Helper Reader class which generate specified number of lines contents
63
* with each line will be "<code>Line &lt;line_number&gt;</code>".
64
*
65
* <p>This class also support to simulate {@link IOException} when read pass
66
* a specified line number.
67
*/
68
private static class MockLineReader extends Reader {
69
final int line_count;
70
boolean closed = false;
71
int line_no = 0;
72
String line = null;
73
int pos = 0;
74
int inject_ioe_after_line;
75
76
MockLineReader(int cnt) {
77
this(cnt, cnt);
78
}
79
80
MockLineReader(int cnt, int inject_ioe) {
81
line_count = cnt;
82
inject_ioe_after_line = inject_ioe;
83
}
84
85
public void reset() {
86
synchronized(lock) {
87
line = null;
88
line_no = 0;
89
pos = 0;
90
closed = false;
91
}
92
}
93
94
public void inject_ioe() {
95
inject_ioe_after_line = line_no;
96
}
97
98
public int getLineNumber() {
99
synchronized(lock) {
100
return line_no;
101
}
102
}
103
104
@Override
105
public void close() {
106
closed = true;
107
}
108
109
@Override
110
public int read(char[] buf, int off, int len) throws IOException {
111
synchronized(lock) {
112
if (closed) {
113
throw new IOException("Stream is closed.");
114
}
115
116
if (line == null) {
117
if (line_count > line_no) {
118
line_no += 1;
119
if (line_no > inject_ioe_after_line) {
120
throw new IOException("Failed to read line " + line_no);
121
}
122
line = "Line " + line_no + "\n";
123
pos = 0;
124
} else {
125
return -1; // EOS reached
126
}
127
}
128
129
int cnt = line.length() - pos;
130
assert(cnt != 0);
131
// try to fill with remaining
132
if (cnt >= len) {
133
line.getChars(pos, pos + len, buf, off);
134
pos += len;
135
if (cnt == len) {
136
assert(pos == line.length());
137
line = null;
138
}
139
return len;
140
} else {
141
line.getChars(pos, pos + cnt, buf, off);
142
off += cnt;
143
len -= cnt;
144
line = null;
145
/* hold for next read, so we won't IOE during fill buffer
146
int more = read(buf, off, len);
147
return (more == -1) ? cnt : cnt + more;
148
*/
149
return cnt;
150
}
151
}
152
}
153
}
154
155
private static void verify(Map.Entry<String, Integer> e) {
156
final String data = e.getKey();
157
final int total_lines = e.getValue();
158
try (BufferedReader br = new BufferedReader(
159
new StringReader(data))) {
160
assertEquals(br.lines()
161
.mapToInt(l -> 1).reduce(0, (x, y) -> x + y),
162
total_lines,
163
data + " should produce " + total_lines + " lines.");
164
} catch (IOException ioe) {
165
fail("Should not have any exception.");
166
}
167
}
168
169
public void testLinesBasic() {
170
// Basic test cases
171
cases.entrySet().stream().forEach(Lines::verify);
172
// Similar test, also verify MockLineReader is correct
173
for (int i = 0; i < 10; i++) {
174
try (BufferedReader br = new BufferedReader(new MockLineReader(i))) {
175
assertEquals(br.lines()
176
.peek(l -> assertTrue(l.matches("^Line \\d+$")))
177
.mapToInt(l -> 1).reduce(0, (x, y) -> x + y),
178
i,
179
"MockLineReader(" + i + ") should produce " + i + " lines.");
180
} catch (IOException ioe) {
181
fail("Unexpected IOException.");
182
}
183
}
184
}
185
186
public void testUncheckedIOException() throws IOException {
187
MockLineReader r = new MockLineReader(10, 3);
188
ArrayList<String> ar = new ArrayList<>();
189
try (BufferedReader br = new BufferedReader(r)) {
190
br.lines().limit(3L).forEach(ar::add);
191
assertEquals(ar.size(), 3, "Should be able to read 3 lines.");
192
} catch (UncheckedIOException uioe) {
193
fail("Unexpected UncheckedIOException");
194
}
195
r.reset();
196
try (BufferedReader br = new BufferedReader(r)) {
197
br.lines().forEach(ar::add);
198
fail("Should had thrown UncheckedIOException.");
199
} catch (UncheckedIOException uioe) {
200
assertEquals(r.getLineNumber(), 4, "should fail to read 4th line");
201
assertEquals(ar.size(), 6, "3 + 3 lines read");
202
}
203
for (int i = 0; i < ar.size(); i++) {
204
assertEquals(ar.get(i), "Line " + (i % 3 + 1));
205
}
206
}
207
208
public void testIterator() throws IOException {
209
MockLineReader r = new MockLineReader(6);
210
BufferedReader br = new BufferedReader(r);
211
String line = br.readLine();
212
assertEquals(r.getLineNumber(), 1, "Read one line");
213
Stream<String> s = br.lines();
214
Iterator<String> it = s.iterator();
215
// Ensure iterate with only next works
216
for (int i = 0; i < 5; i++) {
217
String str = it.next();
218
assertEquals(str, "Line " + (i + 2), "Addtional five lines");
219
}
220
// NoSuchElementException
221
try {
222
it.next();
223
fail("Should have run out of lines.");
224
} catch (NoSuchElementException nsse) {}
225
}
226
227
public void testPartialReadAndLineNo() throws IOException {
228
MockLineReader r = new MockLineReader(5);
229
LineNumberReader lr = new LineNumberReader(r);
230
char[] buf = new char[5];
231
lr.read(buf, 0, 5);
232
assertEquals(0, lr.getLineNumber(), "LineNumberReader start with line 0");
233
assertEquals(1, r.getLineNumber(), "MockLineReader start with line 1");
234
assertEquals(new String(buf), "Line ");
235
String l1 = lr.readLine();
236
assertEquals(l1, "1", "Remaining of the first line");
237
assertEquals(1, lr.getLineNumber(), "Line 1 is read");
238
assertEquals(1, r.getLineNumber(), "MockLineReader not yet go next line");
239
lr.read(buf, 0, 4);
240
assertEquals(1, lr.getLineNumber(), "In the middle of line 2");
241
assertEquals(new String(buf, 0, 4), "Line");
242
ArrayList<String> ar = lr.lines()
243
.peek(l -> assertEquals(lr.getLineNumber(), r.getLineNumber()))
244
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
245
assertEquals(ar.get(0), " 2", "Remaining in the second line");
246
for (int i = 1; i < ar.size(); i++) {
247
assertEquals(ar.get(i), "Line " + (i + 2), "Rest are full lines");
248
}
249
}
250
251
public void testInterlacedRead() throws IOException {
252
MockLineReader r = new MockLineReader(10);
253
BufferedReader br = new BufferedReader(r);
254
char[] buf = new char[5];
255
Stream<String> s = br.lines();
256
Iterator<String> it = s.iterator();
257
258
br.read(buf);
259
assertEquals(new String(buf), "Line ");
260
assertEquals(it.next(), "1");
261
try {
262
s.iterator().next();
263
fail("Should failed on second attempt to get iterator from s");
264
} catch (IllegalStateException ise) {}
265
br.read(buf, 0, 2);
266
assertEquals(new String(buf, 0, 2), "Li");
267
// Get stream again should continue from where left
268
// Only read remaining of the line
269
br.lines().limit(1L).forEach(line -> assertEquals(line, "ne 2"));
270
br.read(buf, 0, 2);
271
assertEquals(new String(buf, 0, 2), "Li");
272
br.read(buf, 0, 2);
273
assertEquals(new String(buf, 0, 2), "ne");
274
assertEquals(it.next(), " 3");
275
// Line 4
276
br.readLine();
277
// interator pick
278
assertEquals(it.next(), "Line 5");
279
// Another stream instantiated by lines()
280
AtomicInteger line_no = new AtomicInteger(6);
281
br.lines().forEach(l -> assertEquals(l, "Line " + line_no.getAndIncrement()));
282
// Read after EOL
283
assertFalse(it.hasNext());
284
}
285
286
public void testCharacteristics() {
287
try (BufferedReader br = new BufferedReader(
288
new StringReader(""))) {
289
Spliterator<String> instance = br.lines().spliterator();
290
assertTrue(instance.hasCharacteristics(Spliterator.NONNULL));
291
assertTrue(instance.hasCharacteristics(Spliterator.ORDERED));
292
} catch (IOException ioe) {
293
fail("Should not have any exception.");
294
}
295
}
296
}
297
298