Skip to content

Commit 6cd780a

Browse files
committed
Add tests and asserts
1 parent 7fd8d4c commit 6cd780a

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,40 @@
2727
public class UnixLineEndingInputStreamTest {
2828

2929
private String roundtrip(final String msg) throws IOException {
30-
return roundtrip(msg, true);
30+
return roundtrip(msg, true, 0);
3131
}
3232

33-
private String roundtrip(final String msg, final boolean ensure) throws IOException {
33+
private String roundtrip(final String msg, final boolean ensureLineFeedAtEndOfFile, final int minBufferLen) throws IOException {
34+
final String string;
35+
// read(byte[])
3436
try (final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
35-
final UnixLineEndingInputStream lf = new UnixLineEndingInputStream(baos, ensure)) {
36-
final byte[] buf = new byte[100];
37-
return new String(buf, 0, lf.read(buf), StandardCharsets.UTF_8);
37+
final UnixLineEndingInputStream in = new UnixLineEndingInputStream(baos, ensureLineFeedAtEndOfFile)) {
38+
// read into a buffer larger than the fixture.
39+
final byte[] buf = new byte[minBufferLen + msg.length() * 10];
40+
string = new String(buf, 0, in.read(buf), StandardCharsets.UTF_8);
3841
}
42+
// read(byte[], int, int)
43+
try (final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
44+
final UnixLineEndingInputStream in = new UnixLineEndingInputStream(baos, ensureLineFeedAtEndOfFile)) {
45+
// read into a buffer larger than the fixture.
46+
final byte[] buf = new byte[minBufferLen + msg.length() * 10];
47+
assertEquals(string, new String(buf, 0, in.read(buf, 0, buf.length), StandardCharsets.UTF_8));
48+
}
49+
// read
50+
try (final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
51+
final UnixLineEndingInputStream in = new UnixLineEndingInputStream(baos, ensureLineFeedAtEndOfFile)) {
52+
// read into a buffer larger than the fixture.
53+
final int[] buf = new int[minBufferLen + msg.length() * 10];
54+
if (buf.length > 0) {
55+
int b;
56+
int i = 0;
57+
while ((b = in.read()) != -1) {
58+
buf[i++] = b;
59+
}
60+
assertEquals(string, new String(buf, 0, i));
61+
}
62+
}
63+
return string;
3964
}
4065

4166
@Test
@@ -50,7 +75,12 @@ public void testCrOnlyEnsureAtEof() throws Exception {
5075

5176
@Test
5277
public void testCrOnlyNotAtEof() throws Exception {
53-
assertEquals("a\nb", roundtrip("a\rb", false));
78+
assertEquals("a\nb", roundtrip("a\rb", false, 0));
79+
}
80+
81+
@Test
82+
public void testEmpty() throws Exception {
83+
assertEquals("", roundtrip(""));
5484
}
5585

5686
@Test
@@ -65,8 +95,8 @@ public void testMultipleBlankLines() throws Exception {
6595

6696
@Test
6797
public void testRetainLineFeed() throws Exception {
68-
assertEquals("a\n\n", roundtrip("a\r\n\r\n", false));
69-
assertEquals("a", roundtrip("a", false));
98+
assertEquals("a\n\n", roundtrip("a\r\n\r\n", false, 0));
99+
assertEquals("a", roundtrip("a", false, 0));
70100
}
71101

72102
@Test

0 commit comments

Comments
 (0)