1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.input;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.nio.charset.StandardCharsets;
24
25 import org.junit.jupiter.api.Test;
26
27 public class UnixLineEndingInputStreamTest {
28
29 private String roundtrip(final String msg) throws IOException {
30 return roundtrip(msg, true);
31 }
32
33 private String roundtrip(final String msg, final boolean ensure) throws IOException {
34 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);
38 }
39 }
40
41 @Test
42 public void testCrAtEnd() throws Exception {
43 assertEquals("a\n", roundtrip("a\r"));
44 }
45
46 @Test
47 public void testCrOnlyEnsureAtEof() throws Exception {
48 assertEquals("a\nb\n", roundtrip("a\rb"));
49 }
50
51 @Test
52 public void testCrOnlyNotAtEof() throws Exception {
53 assertEquals("a\nb", roundtrip("a\rb", false));
54 }
55
56 @Test
57 public void testInTheMiddleOfTheLine() throws Exception {
58 assertEquals("a\nbc\n", roundtrip("a\r\nbc"));
59 }
60
61 @Test
62 public void testMultipleBlankLines() throws Exception {
63 assertEquals("a\n\nbc\n", roundtrip("a\r\n\r\nbc"));
64 }
65
66 @Test
67 public void testRetainLineFeed() throws Exception {
68 assertEquals("a\n\n", roundtrip("a\r\n\r\n", false));
69 assertEquals("a", roundtrip("a", false));
70 }
71
72 @Test
73 public void testSimpleString() throws Exception {
74 assertEquals("abc\n", roundtrip("abc"));
75 }
76
77 @Test
78 public void testTwoLinesAtEnd() throws Exception {
79 assertEquals("a\n\n", roundtrip("a\r\n\r\n"));
80 }
81
82 }