View Javadoc
1   /*
2    * Copyright (C) 2010, Marc Strapetz <marc.strapetz@syntevo.com>
3    * Copyright (C) 2015, 2020 Ivan Motsch <ivan.motsch@bsiag.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.util.io;
13  
14  import static java.nio.charset.StandardCharsets.UTF_8;
15  import static org.junit.Assert.assertEquals;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.function.Function;
21  
22  import org.eclipse.jgit.util.io.AutoLFInputStream.StreamFlag;
23  import org.junit.Test;
24  
25  public class AutoLFInputStreamTest {
26  
27  	@Test
28  	public void testLF() throws IOException {
29  		final byte[] bytes = asBytes("1\n2\n3");
30  		test(bytes, bytes);
31  	}
32  
33  	@Test
34  	public void testCR() throws IOException {
35  		final byte[] bytes = asBytes("1\r2\r3");
36  		test(bytes, bytes);
37  	}
38  
39  	@Test
40  	public void testCRLF() throws IOException {
41  		test(asBytes("1\r\n2\r\n3"), asBytes("1\n2\n3"));
42  	}
43  
44  	@Test
45  	public void testLFCR() throws IOException {
46  		final byte[] bytes = asBytes("1\n\r2\n\r3");
47  		test(bytes, bytes);
48  	}
49  
50  	@Test
51  	public void testEmpty() throws IOException {
52  		final byte[] bytes = asBytes("");
53  		test(bytes, bytes);
54  	}
55  
56  	@Test
57  	public void testBinaryDetect() throws IOException {
58  		final byte[] bytes = asBytes("1\r\n2\r\n3\0");
59  		test(bytes, bytes, StreamFlag.DETECT_BINARY);
60  	}
61  
62  	@Test
63  	public void testBinaryDontDetect() throws IOException {
64  		test(asBytes("1\r\n2\r\n3\0"), asBytes("1\n2\n3\0"));
65  	}
66  
67  	@Test
68  	public void testCrLf() throws IOException {
69  		byte[] bytes = asBytes("1\r\n2\n3\r\n\r");
70  		test(bytes, bytes, in -> AutoLFInputStream.create(in,
71  				StreamFlag.DETECT_BINARY, StreamFlag.FOR_CHECKOUT));
72  	}
73  
74  	@Test
75  	public void testCrLfDontDetect() throws IOException {
76  		test(asBytes("1\r\n2\r\n"), asBytes("1\n2\n"),
77  				in -> AutoLFInputStream.create(in, StreamFlag.DETECT_BINARY));
78  	}
79  
80  	private static void test(byte[] input, byte[] expected, StreamFlag... flags)
81  			throws IOException {
82  		test(input, expected, in -> AutoLFInputStream.create(in, flags));
83  	}
84  
85  	private static void test(byte[] input, byte[] expected,
86  			Function<InputStream, InputStream> factory) throws IOException {
87  		try (InputStream bis1 = new ByteArrayInputStream(input);
88  				InputStream cis1 = factory.apply(bis1)) {
89  			int index1 = 0;
90  			for (int b = cis1.read(); b != -1; b = cis1.read()) {
91  				assertEquals(expected[index1], (byte) b);
92  				index1++;
93  			}
94  
95  			assertEquals(expected.length, index1);
96  
97  			for (int bufferSize = 1; bufferSize < 10; bufferSize++) {
98  				final byte[] buffer = new byte[bufferSize];
99  				try (InputStream bis2 = new ByteArrayInputStream(input);
100 						InputStream cis2 = factory.apply(bis2)) {
101 
102 					int read = 0;
103 					for (int readNow = cis2.read(buffer, 0,
104 							buffer.length); readNow != -1
105 									&& read < expected.length; readNow = cis2
106 											.read(buffer, 0, buffer.length)) {
107 						for (int index2 = 0; index2 < readNow; index2++) {
108 							assertEquals(expected[read + index2],
109 									buffer[index2]);
110 						}
111 						read += readNow;
112 					}
113 
114 					assertEquals(expected.length, read);
115 				}
116 			}
117 		}
118 	}
119 
120 	private static byte[] asBytes(String in) {
121 		return in.getBytes(UTF_8);
122 	}
123 }