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