View Javadoc
1   /*
2    * Copyright (C) 2011, 2013 Robin Rosenberg
3    * Copyright (C) 2013 Robin Stocker 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  
16  import java.io.ByteArrayInputStream;
17  import java.io.ByteArrayOutputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import org.eclipse.jgit.diff.RawText;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  public class AutoCRLFOutputStreamTest {
27  
28  	@Test
29  	public void test() throws IOException {
30  		assertNoCrLf("", "");
31  		assertNoCrLf("\r", "\r");
32  		assertNoCrLf("\r\n", "\n");
33  		assertNoCrLf("\r\n", "\r\n");
34  		assertNoCrLf("\r\r", "\r\r");
35  		assertNoCrLf("\n\r", "\n\r"); // Lone CR
36  		assertNoCrLf("\r\n\r\r", "\r\n\r\r");
37  		assertNoCrLf("\r\n\r\n", "\r\n\r\n");
38  		assertNoCrLf("\n\r\n\r", "\n\r\n\r");
39  		assertNoCrLf("\0\n", "\0\n");
40  	}
41  
42  	@Test
43  	public void testBoundary() throws IOException {
44  		int bufferSize = RawText.getBufferSize();
45  		for (int i = bufferSize - 10; i < bufferSize + 10; i++) {
46  			String s1 = Strings.repeat("a", i);
47  			assertNoCrLf(s1, s1);
48  			String s2 = Strings.repeat("\0", i);
49  			assertNoCrLf(s2, s2);
50  		}
51  	}
52  
53  	private void assertNoCrLf(String string, String string2) throws IOException {
54  		assertNoCrLfHelper(string, string2);
55  		// \u00e5 = LATIN SMALL LETTER A WITH RING ABOVE
56  		// the byte value is negative
57  		assertNoCrLfHelper("\u00e5" + string, "\u00e5" + string2);
58  		assertNoCrLfHelper("\u00e5" + string + "\u00e5", "\u00e5" + string2
59  				+ "\u00e5");
60  		assertNoCrLfHelper(string + "\u00e5", string2 + "\u00e5");
61  	}
62  
63  	private void assertNoCrLfHelper(String expect, String input)
64  			throws IOException {
65  		byte[] inbytes = input.getBytes(UTF_8);
66  		byte[] expectBytes = expect.getBytes(UTF_8);
67  		for (int i = -4; i < 5; ++i) {
68  			int size = Math.abs(i);
69  			byte[] buf = new byte[size];
70  			try (InputStream in = new ByteArrayInputStream(inbytes);
71  					ByteArrayOutputStream bos = new ByteArrayOutputStream();
72  					OutputStream out = new AutoCRLFOutputStream(bos)) {
73  				if (i > 0) {
74  					int n;
75  					while ((n = in.read(buf)) >= 0) {
76  						out.write(buf, 0, n);
77  					}
78  				} else if (i < 0) {
79  					int n;
80  					while ((n = in.read(buf)) >= 0) {
81  						byte[] b = new byte[n];
82  						System.arraycopy(buf, 0, b, 0, n);
83  						out.write(b);
84  					}
85  				} else {
86  					int c;
87  					while ((c = in.read()) != -1)
88  						out.write(c);
89  				}
90  				out.flush();
91  				byte[] actualBytes = bos.toByteArray();
92  				Assert.assertEquals("bufsize=" + size, encode(expectBytes),
93  						encode(actualBytes));
94  			}
95  		}
96  	}
97  
98  	String encode(byte[] in) {
99  		StringBuilder str = new StringBuilder();
100 		for (byte b : in) {
101 			if (b < 32)
102 				str.append(0xFF & b);
103 			else {
104 				str.append("'");
105 				str.append((char) b);
106 				str.append("'");
107 			}
108 			str.append(' ');
109 		}
110 		return str.toString();
111 	}
112 
113 }