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 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.ValueSource;
29
30 class WindowsLineEndingInputStreamTest {
31
32 private String roundtripReadByte(final String msg) throws IOException {
33 return roundtripReadByte(msg, true);
34 }
35
36 private String roundtripReadByte(final String msg, final boolean ensure) throws IOException {
37
38 try (WindowsLineEndingInputStream lf = new WindowsLineEndingInputStream(
39 CharSequenceInputStream.builder().setCharSequence(msg).setCharset(StandardCharsets.UTF_8).get(), ensure)) {
40 final byte[] buf = new byte[100];
41 int i = 0;
42 while (i < buf.length) {
43 final int read = lf.read();
44 if (read < 0) {
45 break;
46 }
47 buf[i++] = (byte) read;
48 }
49 return new String(buf, 0, i, StandardCharsets.UTF_8);
50 }
51 }
52
53 private String roundtripReadByteArray(final String msg) throws IOException {
54 return roundtripReadByteArray(msg, true);
55 }
56
57 private String roundtripReadByteArray(final String msg, final boolean ensure) throws IOException {
58
59 try (WindowsLineEndingInputStream lf = new WindowsLineEndingInputStream(
60 CharSequenceInputStream.builder().setCharSequence(msg).setCharset(StandardCharsets.UTF_8).get(), ensure)) {
61 final byte[] buf = new byte[100];
62 final int read = lf.read(buf);
63 return new String(buf, 0, read, StandardCharsets.UTF_8);
64 }
65 }
66
67 private String roundtripReadByteArrayIndex(final String msg) throws IOException {
68 return roundtripReadByteArrayIndex(msg, true);
69 }
70
71 private String roundtripReadByteArrayIndex(final String msg, final boolean ensure) throws IOException {
72
73 try (WindowsLineEndingInputStream lf = new WindowsLineEndingInputStream(
74 CharSequenceInputStream.builder().setCharSequence(msg).setCharset(StandardCharsets.UTF_8).get(), ensure)) {
75 final byte[] buf = new byte[100];
76 final int read = lf.read(buf, 0, 100);
77 return new String(buf, 0, read, StandardCharsets.UTF_8);
78 }
79 }
80
81 @Test
82 void testInTheMiddleOfTheLine_Byte() throws Exception {
83 assertEquals("a\r\nbc\r\n", roundtripReadByte("a\r\nbc"));
84 }
85
86 @Test
87 void testInTheMiddleOfTheLine_ByteArray() throws Exception {
88 assertEquals("a\r\nbc\r\n", roundtripReadByteArray("a\r\nbc"));
89 }
90
91 @Test
92 void testInTheMiddleOfTheLine_ByteArrayIndex() throws Exception {
93 assertEquals("a\r\nbc\r\n", roundtripReadByteArrayIndex("a\r\nbc"));
94 }
95
96 @Test
97 void testLinuxLineFeeds_Byte() throws Exception {
98 assertEquals("ab\r\nc", roundtripReadByte("ab\nc", false));
99 }
100
101 @Test
102 void testLinuxLineFeeds_ByteArray() throws Exception {
103 assertEquals("ab\r\nc", roundtripReadByteArray("ab\nc", false));
104 }
105
106 @Test
107 void testLinuxLineFeeds_ByteArrayIndex() throws Exception {
108 assertEquals("ab\r\nc", roundtripReadByteArrayIndex("ab\nc", false));
109 }
110
111 @Test
112 void testMalformed_Byte() throws Exception {
113 assertEquals("a\rbc", roundtripReadByte("a\rbc", false));
114 }
115
116 @Test
117 void testMalformed_ByteArray() throws Exception {
118 assertEquals("a\rbc", roundtripReadByteArray("a\rbc", false));
119 }
120
121 @Test
122 void testMalformed_ByteArrayIndex() throws Exception {
123 assertEquals("a\rbc", roundtripReadByteArrayIndex("a\rbc", false));
124 }
125
126 @ParameterizedTest
127 @ValueSource(booleans = { false, true })
128 void testMark(final boolean ensureLineFeedAtEndOfFile) {
129 assertThrows(UnsupportedOperationException.class, () -> new WindowsLineEndingInputStream(new NullInputStream(), true).mark(1));
130 }
131
132 @SuppressWarnings("resource")
133 @ParameterizedTest
134 @ValueSource(booleans = { false, true })
135 void testMarkSupported(final boolean ensureLineFeedAtEndOfFile) {
136 assertFalse(new WindowsLineEndingInputStream(new NullInputStream(), true).markSupported());
137 }
138
139 @Test
140 void testMultipleBlankLines_Byte() throws Exception {
141 assertEquals("a\r\n\r\nbc\r\n", roundtripReadByte("a\r\n\r\nbc"));
142 }
143
144 @Test
145 void testMultipleBlankLines_ByteArray() throws Exception {
146 assertEquals("a\r\n\r\nbc\r\n", roundtripReadByteArray("a\r\n\r\nbc"));
147 }
148
149 @Test
150 void testMultipleBlankLines_ByteArrayIndex() throws Exception {
151 assertEquals("a\r\n\r\nbc\r\n", roundtripReadByteArrayIndex("a\r\n\r\nbc"));
152 }
153
154 @Test
155 void testRetainLineFeed_Byte() throws Exception {
156 assertEquals("a\r\n\r\n", roundtripReadByte("a\r\n\r\n", false));
157 assertEquals("a", roundtripReadByte("a", false));
158 }
159
160 @Test
161 void testRetainLineFeed_ByteArray() throws Exception {
162 assertEquals("a\r\n\r\n", roundtripReadByteArray("a\r\n\r\n", false));
163 assertEquals("a", roundtripReadByteArray("a", false));
164 }
165
166 @Test
167 void testRetainLineFeed_ByteArrayIndex() throws Exception {
168 assertEquals("a\r\n\r\n", roundtripReadByteArray("a\r\n\r\n", false));
169 assertEquals("a", roundtripReadByteArrayIndex("a", false));
170 }
171
172 @Test
173 void testSimpleString_Byte() throws Exception {
174 assertEquals("abc\r\n", roundtripReadByte("abc"));
175 }
176
177 @Test
178 void testSimpleString_ByteArray() throws Exception {
179 assertEquals("abc\r\n", roundtripReadByteArray("abc"));
180 }
181
182 @Test
183 void testSimpleString_ByteArrayIndex() throws Exception {
184 assertEquals("abc\r\n", roundtripReadByteArrayIndex("abc"));
185 }
186
187 @Test
188 void testTwoLinesAtEnd_Byte() throws Exception {
189 assertEquals("a\r\n\r\n", roundtripReadByte("a\r\n\r\n"));
190 }
191
192 @Test
193 void testTwoLinesAtEnd_ByteArray() throws Exception {
194 assertEquals("a\r\n\r\n", roundtripReadByteArray("a\r\n\r\n"));
195 }
196
197 @Test
198 void testTwoLinesAtEnd_ByteArrayIndex() throws Exception {
199 assertEquals("a\r\n\r\n", roundtripReadByteArrayIndex("a\r\n\r\n"));
200 }
201 }