View Javadoc
1   /*
2    * Copyright (C) 2010, Marc Strapetz <marc.strapetz@syntevo.com>
3    * Copyright (C) 2015, 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  
21  import org.junit.Test;
22  
23  public class AutoLFInputStreamTest {
24  
25  	@Test
26  	public void testLF() throws IOException {
27  		final byte[] bytes = asBytes("1\n2\n3");
28  		test(bytes, bytes, false);
29  	}
30  
31  	@Test
32  	public void testCR() throws IOException {
33  		final byte[] bytes = asBytes("1\r2\r3");
34  		test(bytes, bytes, false);
35  	}
36  
37  	@Test
38  	public void testCRLF() throws IOException {
39  		test(asBytes("1\r\n2\r\n3"), asBytes("1\n2\n3"), false);
40  	}
41  
42  	@Test
43  	public void testLFCR() throws IOException {
44  		final byte[] bytes = asBytes("1\n\r2\n\r3");
45  		test(bytes, bytes, false);
46  	}
47  
48  	@Test
49  	public void testEmpty() throws IOException {
50  		final byte[] bytes = asBytes("");
51  		test(bytes, bytes, false);
52  	}
53  
54  	@Test
55  	public void testBinaryDetect() throws IOException {
56  		final byte[] bytes = asBytes("1\r\n2\r\n3\0");
57  		test(bytes, bytes, true);
58  	}
59  
60  	@Test
61  	public void testBinaryDontDetect() throws IOException {
62  		test(asBytes("1\r\n2\r\n3\0"), asBytes("1\n2\n3\0"), false);
63  	}
64  
65  	private static void test(byte[] input, byte[] expected,
66  			boolean detectBinary) throws IOException {
67  		try (InputStream bis1 = new ByteArrayInputStream(input);
68  				InputStream cis1 = new AutoLFInputStream(bis1, detectBinary)) {
69  			int index1 = 0;
70  			for (int b = cis1.read(); b != -1; b = cis1.read()) {
71  				assertEquals(expected[index1], (byte) b);
72  				index1++;
73  			}
74  
75  			assertEquals(expected.length, index1);
76  
77  			for (int bufferSize = 1; bufferSize < 10; bufferSize++) {
78  				final byte[] buffer = new byte[bufferSize];
79  				try (InputStream bis2 = new ByteArrayInputStream(input);
80  						InputStream cis2 = new AutoLFInputStream(bis2,
81  								detectBinary)) {
82  
83  					int read = 0;
84  					for (int readNow = cis2.read(buffer, 0,
85  							buffer.length); readNow != -1
86  									&& read < expected.length; readNow = cis2
87  											.read(buffer, 0, buffer.length)) {
88  						for (int index2 = 0; index2 < readNow; index2++) {
89  							assertEquals(expected[read + index2],
90  									buffer[index2]);
91  						}
92  						read += readNow;
93  					}
94  
95  					assertEquals(expected.length, read);
96  				}
97  			}
98  		}
99  	}
100 
101 	private static byte[] asBytes(String in) {
102 		return in.getBytes(UTF_8);
103 	}
104 }