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 package org.eclipse.jgit.api;
44
45 import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_CRLF;
46 import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_LF;
47 import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.DIRECT;
48 import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_CRLF;
49 import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_LF;
50 import static org.junit.Assert.assertArrayEquals;
51
52 import java.io.ByteArrayInputStream;
53 import java.io.ByteArrayOutputStream;
54 import java.io.InputStream;
55 import java.io.OutputStream;
56 import java.nio.charset.StandardCharsets;
57 import java.util.Arrays;
58
59 import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
60 import org.eclipse.jgit.util.IO;
61 import org.eclipse.jgit.util.io.EolStreamTypeUtil;
62 import org.junit.Test;
63
64
65
66
67 public class EolStreamTypeUtilTest {
68
69 @Test
70 public void testCheckoutDirect() throws Exception {
71 testCheckout(DIRECT, DIRECT, "", "");
72 testCheckout(DIRECT, DIRECT, "\r", "\r");
73 testCheckout(DIRECT, DIRECT, "\n", "\n");
74
75 testCheckout(DIRECT, DIRECT, "\r\n", "\r\n");
76 testCheckout(DIRECT, DIRECT, "\n\r", "\n\r");
77
78 testCheckout(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
79 testCheckout(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
80
81 testCheckout(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
82 testCheckout(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
83 testCheckout(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
84 testCheckout(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
85 }
86
87 @Test
88 public void testCheckoutLF() throws Exception {
89 testCheckout(TEXT_LF, AUTO_LF, "", "");
90 testCheckout(TEXT_LF, AUTO_LF, "\r", "\r");
91 testCheckout(TEXT_LF, AUTO_LF, "\n", "\n");
92
93 testCheckout(TEXT_LF, AUTO_LF, "\r\n", "\n");
94 testCheckout(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
95
96 testCheckout(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
97 testCheckout(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
98
99 testCheckout(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
100 testCheckout(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
101 testCheckout(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
102 testCheckout(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
103 }
104
105 @Test
106 public void testCheckoutCRLF() throws Exception {
107 testCheckout(TEXT_CRLF, AUTO_CRLF, "", "");
108 testCheckout(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
109 testCheckout(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
110
111 testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
112 testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
113
114 testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
115 testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
116
117 testCheckout(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
118 testCheckout(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
119 testCheckout(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
120 testCheckout(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 private void testCheckout(EolStreamType streamTypeText,
150 EolStreamType streamTypeWithBinaryCheck, String output,
151 String expectedConversion) throws Exception {
152 ByteArrayOutputStream b;
153 byte[] outputBytes = output.getBytes(StandardCharsets.UTF_8);
154 byte[] expectedConversionBytes = expectedConversion
155 .getBytes(StandardCharsets.UTF_8);
156
157
158 b = new ByteArrayOutputStream();
159 try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
160 streamTypeText)) {
161 out.write(outputBytes);
162 }
163 assertArrayEquals(expectedConversionBytes, b.toByteArray());
164
165
166
167 b = new ByteArrayOutputStream();
168 try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
169 streamTypeWithBinaryCheck)) {
170 out.write(outputBytes);
171 }
172 assertArrayEquals(expectedConversionBytes, b.toByteArray());
173
174
175 outputBytes = extendWithBinaryData(outputBytes);
176 expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
177
178
179 b = new ByteArrayOutputStream();
180 try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
181 streamTypeText)) {
182 out.write(outputBytes);
183 }
184 assertArrayEquals(expectedConversionBytes, b.toByteArray());
185
186
187
188
189 b = new ByteArrayOutputStream();
190 try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
191 streamTypeWithBinaryCheck)) {
192 out.write(outputBytes);
193 }
194
195 assertArrayEquals(outputBytes, b.toByteArray());
196 }
197
198 @Test
199 public void testCheckinDirect() throws Exception {
200 testCheckin(DIRECT, DIRECT, "", "");
201 testCheckin(DIRECT, DIRECT, "\r", "\r");
202 testCheckin(DIRECT, DIRECT, "\n", "\n");
203
204 testCheckin(DIRECT, DIRECT, "\r\n", "\r\n");
205 testCheckin(DIRECT, DIRECT, "\n\r", "\n\r");
206
207 testCheckin(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
208 testCheckin(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
209
210 testCheckin(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
211 testCheckin(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
212 testCheckin(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
213 testCheckin(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
214 }
215
216 @Test
217 public void testCheckinLF() throws Exception {
218 testCheckin(TEXT_LF, AUTO_LF, "", "");
219 testCheckin(TEXT_LF, AUTO_LF, "\r", "\r");
220 testCheckin(TEXT_LF, AUTO_LF, "\n", "\n");
221
222 testCheckin(TEXT_LF, AUTO_LF, "\r\n", "\n");
223 testCheckin(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
224
225 testCheckin(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
226 testCheckin(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
227
228 testCheckin(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
229 testCheckin(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
230 testCheckin(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
231 testCheckin(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
232 }
233
234 @Test
235 public void testCheckinCRLF() throws Exception {
236 testCheckin(TEXT_CRLF, AUTO_CRLF, "", "");
237 testCheckin(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
238 testCheckin(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
239
240 testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
241 testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
242
243 testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
244 testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
245
246 testCheckin(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
247 testCheckin(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
248 testCheckin(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
249 testCheckin(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 private void testCheckin(EolStreamType streamTypeText,
279 EolStreamType streamTypeWithBinaryCheck, String input,
280 String expectedConversion) throws Exception {
281 byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
282 byte[] expectedConversionBytes = expectedConversion
283 .getBytes(StandardCharsets.UTF_8);
284
285
286 try (InputStream in = EolStreamTypeUtil.wrapInputStream(
287 new ByteArrayInputStream(inputBytes),
288 streamTypeText)) {
289 byte[] b = new byte[1024];
290 int len = IO.readFully(in, b, 0);
291 assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
292 }
293
294
295
296 try (InputStream in = EolStreamTypeUtil.wrapInputStream(
297 new ByteArrayInputStream(inputBytes),
298 streamTypeWithBinaryCheck)) {
299 byte[] b = new byte[1024];
300 int len = IO.readFully(in, b, 0);
301 assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
302 }
303
304
305 inputBytes = extendWithBinaryData(inputBytes);
306 expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
307
308
309 try (InputStream in = EolStreamTypeUtil.wrapInputStream(
310 new ByteArrayInputStream(inputBytes), streamTypeText)) {
311 byte[] b = new byte[1024];
312 int len = IO.readFully(in, b, 0);
313 assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
314 }
315
316
317
318
319 try (InputStream in = EolStreamTypeUtil.wrapInputStream(
320 new ByteArrayInputStream(inputBytes),
321 streamTypeWithBinaryCheck)) {
322 byte[] b = new byte[1024];
323 int len = IO.readFully(in, b, 0);
324
325 assertArrayEquals(inputBytes, Arrays.copyOf(b, len));
326 }
327 }
328
329 private byte[] extendWithBinaryData(byte[] data) throws Exception {
330 int n = 3;
331 byte[] dataEx = new byte[data.length + n];
332 System.arraycopy(data, 0, dataEx, 0, data.length);
333 for (int i = 0; i < n; i++) {
334 dataEx[data.length + i] = (byte) i;
335 }
336 return dataEx;
337 }
338
339 }