You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/07/14 18:39:22 UTC
(commons-io) branch master updated: Add tests and asserts
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new 6cd780ae1 Add tests and asserts
6cd780ae1 is described below
commit 6cd780ae1a81beff8c32c5ac2ca2d3ff35a5e2f5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 14:39:17 2024 -0400
Add tests and asserts
---
.../io/input/UnixLineEndingInputStreamTest.java | 46 ++++++++++++++++++----
1 file changed, 38 insertions(+), 8 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java b/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
index caa0ebabe..cd799faab 100644
--- a/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/UnixLineEndingInputStreamTest.java
@@ -27,15 +27,40 @@ import org.junit.jupiter.api.Test;
public class UnixLineEndingInputStreamTest {
private String roundtrip(final String msg) throws IOException {
- return roundtrip(msg, true);
+ return roundtrip(msg, true, 0);
}
- private String roundtrip(final String msg, final boolean ensure) throws IOException {
+ private String roundtrip(final String msg, final boolean ensureLineFeedAtEndOfFile, final int minBufferLen) throws IOException {
+ final String string;
+ // read(byte[])
try (final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
- final UnixLineEndingInputStream lf = new UnixLineEndingInputStream(baos, ensure)) {
- final byte[] buf = new byte[100];
- return new String(buf, 0, lf.read(buf), StandardCharsets.UTF_8);
+ final UnixLineEndingInputStream in = new UnixLineEndingInputStream(baos, ensureLineFeedAtEndOfFile)) {
+ // read into a buffer larger than the fixture.
+ final byte[] buf = new byte[minBufferLen + msg.length() * 10];
+ string = new String(buf, 0, in.read(buf), StandardCharsets.UTF_8);
}
+ // read(byte[], int, int)
+ try (final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
+ final UnixLineEndingInputStream in = new UnixLineEndingInputStream(baos, ensureLineFeedAtEndOfFile)) {
+ // read into a buffer larger than the fixture.
+ final byte[] buf = new byte[minBufferLen + msg.length() * 10];
+ assertEquals(string, new String(buf, 0, in.read(buf, 0, buf.length), StandardCharsets.UTF_8));
+ }
+ // read
+ try (final ByteArrayInputStream baos = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
+ final UnixLineEndingInputStream in = new UnixLineEndingInputStream(baos, ensureLineFeedAtEndOfFile)) {
+ // read into a buffer larger than the fixture.
+ final int[] buf = new int[minBufferLen + msg.length() * 10];
+ if (buf.length > 0) {
+ int b;
+ int i = 0;
+ while ((b = in.read()) != -1) {
+ buf[i++] = b;
+ }
+ assertEquals(string, new String(buf, 0, i));
+ }
+ }
+ return string;
}
@Test
@@ -50,7 +75,12 @@ public class UnixLineEndingInputStreamTest {
@Test
public void testCrOnlyNotAtEof() throws Exception {
- assertEquals("a\nb", roundtrip("a\rb", false));
+ assertEquals("a\nb", roundtrip("a\rb", false, 0));
+ }
+
+ @Test
+ public void testEmpty() throws Exception {
+ assertEquals("", roundtrip(""));
}
@Test
@@ -65,8 +95,8 @@ public class UnixLineEndingInputStreamTest {
@Test
public void testRetainLineFeed() throws Exception {
- assertEquals("a\n\n", roundtrip("a\r\n\r\n", false));
- assertEquals("a", roundtrip("a", false));
+ assertEquals("a\n\n", roundtrip("a\r\n\r\n", false, 0));
+ assertEquals("a", roundtrip("a", false, 0));
}
@Test