View Javadoc
1   /*
2    * Copyright (C) 2011, 2013 Robin Rosenberg
3    * Copyright (C) 2013 Robin Stocker
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.util.io;
46  
47  import static java.nio.charset.StandardCharsets.UTF_8;
48  
49  import java.io.ByteArrayInputStream;
50  import java.io.ByteArrayOutputStream;
51  import java.io.IOException;
52  import java.io.InputStream;
53  import java.io.OutputStream;
54  
55  import org.junit.Assert;
56  import org.junit.Test;
57  
58  public class AutoCRLFOutputStreamTest {
59  
60  	@Test
61  	public void test() throws IOException {
62  		assertNoCrLf("", "");
63  		assertNoCrLf("\r", "\r");
64  		assertNoCrLf("\r\n", "\n");
65  		assertNoCrLf("\r\n", "\r\n");
66  		assertNoCrLf("\r\r", "\r\r");
67  		assertNoCrLf("\r\n\r", "\n\r");
68  		assertNoCrLf("\r\n\r\r", "\r\n\r\r");
69  		assertNoCrLf("\r\n\r\n", "\r\n\r\n");
70  		assertNoCrLf("\r\n\r\n\r", "\n\r\n\r");
71  		assertNoCrLf("\0\n", "\0\n");
72  	}
73  
74  	@Test
75  	public void testBoundary() throws IOException {
76  		for (int i = AutoCRLFOutputStream.BUFFER_SIZE - 10; i < AutoCRLFOutputStream.BUFFER_SIZE + 10; i++) {
77  			String s1 = Strings.repeat("a", i);
78  			assertNoCrLf(s1, s1);
79  			String s2 = Strings.repeat("\0", i);
80  			assertNoCrLf(s2, s2);
81  		}
82  	}
83  
84  	private void assertNoCrLf(String string, String string2) throws IOException {
85  		assertNoCrLfHelper(string, string2);
86  		// \u00e5 = LATIN SMALL LETTER A WITH RING ABOVE
87  		// the byte value is negative
88  		assertNoCrLfHelper("\u00e5" + string, "\u00e5" + string2);
89  		assertNoCrLfHelper("\u00e5" + string + "\u00e5", "\u00e5" + string2
90  				+ "\u00e5");
91  		assertNoCrLfHelper(string + "\u00e5", string2 + "\u00e5");
92  	}
93  
94  	private void assertNoCrLfHelper(String expect, String input)
95  			throws IOException {
96  		byte[] inbytes = input.getBytes(UTF_8);
97  		byte[] expectBytes = expect.getBytes(UTF_8);
98  		for (int i = -4; i < 5; ++i) {
99  			int size = Math.abs(i);
100 			byte[] buf = new byte[size];
101 			try (InputStream in = new ByteArrayInputStream(inbytes);
102 					ByteArrayOutputStream bos = new ByteArrayOutputStream();
103 					OutputStream out = new AutoCRLFOutputStream(bos)) {
104 				if (i > 0) {
105 					int n;
106 					while ((n = in.read(buf)) >= 0) {
107 						out.write(buf, 0, n);
108 					}
109 				} else if (i < 0) {
110 					int n;
111 					while ((n = in.read(buf)) >= 0) {
112 						byte[] b = new byte[n];
113 						System.arraycopy(buf, 0, b, 0, n);
114 						out.write(b);
115 					}
116 				} else {
117 					int c;
118 					while ((c = in.read()) != -1)
119 						out.write(c);
120 				}
121 				out.flush();
122 				byte[] actualBytes = bos.toByteArray();
123 				Assert.assertEquals("bufsize=" + size, encode(expectBytes),
124 						encode(actualBytes));
125 			}
126 		}
127 	}
128 
129 	String encode(byte[] in) {
130 		StringBuilder str = new StringBuilder();
131 		for (byte b : in) {
132 			if (b < 32)
133 				str.append(0xFF & b);
134 			else {
135 				str.append("'");
136 				str.append((char) b);
137 				str.append("'");
138 			}
139 			str.append(' ');
140 		}
141 		return str.toString();
142 	}
143 
144 }