1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util;
12
13 import static java.nio.charset.StandardCharsets.UTF_8;
14 import static org.junit.Assert.assertEquals;
15
16 import java.io.BufferedReader;
17 import java.io.ByteArrayInputStream;
18 import java.io.InputStreamReader;
19 import java.io.Reader;
20 import java.util.ArrayList;
21 import java.util.Collection;
22
23 import org.eclipse.jgit.lib.Constants;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.Parameterized;
27 import org.junit.runners.Parameterized.Parameter;
28 import org.junit.runners.Parameterized.Parameters;
29
30 @RunWith(Parameterized.class)
31 public class IOReadLineTest {
32 @Parameter(0)
33 public boolean buffered;
34
35 @Parameter(1)
36 public int sizeHint;
37
38 @SuppressWarnings("boxing")
39 @Parameters(name="buffered={0}, sizeHint={1}")
40 public static Collection<Object[]> getParameters() {
41 Boolean[] bv = {false, true};
42 Integer[] sv = {-1, 0, 1, 2, 3, 4, 64};
43 Collection<Object[]> params = new ArrayList<>(bv.length * sv.length);
44 for (boolean b : bv) {
45 for (Integer s : sv) {
46 params.add(new Object[]{b, s});
47 }
48 }
49 return params;
50 }
51
52 @Test
53 public void testReadLine() throws Exception {
54 Reader r = newReader("foo\nbar\nbaz\n");
55 assertEquals("foo\n", readLine(r));
56 assertEquals("bar\n", readLine(r));
57 assertEquals("baz\n", readLine(r));
58 assertEquals("", readLine(r));
59 }
60
61 @Test
62 public void testReadLineNoTrailingNewline() throws Exception {
63 Reader r = newReader("foo\nbar\nbaz");
64 assertEquals("foo\n", readLine(r));
65 assertEquals("bar\n", readLine(r));
66 assertEquals("baz", readLine(r));
67 assertEquals("", readLine(r));
68 }
69
70 private String readLine(Reader r) throws Exception {
71 return IO.readLine(r, sizeHint);
72 }
73
74 private Reader newReader(String in) {
75 Reader r = new InputStreamReader(
76 new ByteArrayInputStream(Constants.encode(in)), UTF_8);
77 if (buffered) {
78 r = new BufferedReader(r);
79 }
80 assertEquals(Boolean.valueOf(buffered),
81 Boolean.valueOf(r.markSupported()));
82 return r;
83 }
84 }