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