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 java.io.ByteArrayInputStream;
48  import java.io.ByteArrayOutputStream;
49  import java.io.IOException;
50  import java.io.InputStream;
51  import java.io.OutputStream;
52  
53  import org.junit.Assert;
54  import org.junit.Test;
55  
56  public class AutoCRLFOutputStreamTest {
57  
58  	@Test
59  	public void test() throws IOException {
60  		assertNoCrLf("", "");
61  		assertNoCrLf("\r", "\r");
62  		assertNoCrLf("\r\n", "\n");
63  		assertNoCrLf("\r\n", "\r\n");
64  		assertNoCrLf("\r\r", "\r\r");
65  		assertNoCrLf("\r\n\r", "\n\r");
66  		assertNoCrLf("\r\n\r\r", "\r\n\r\r");
67  		assertNoCrLf("\r\n\r\n", "\r\n\r\n");
68  		assertNoCrLf("\r\n\r\n\r", "\n\r\n\r");
69  		assertNoCrLf("\0\n", "\0\n");
70  	}
71  
72  	@Test
73  	public void testBoundary() throws IOException {
74  		for (int i = AutoCRLFOutputStream.BUFFER_SIZE - 10; i < AutoCRLFOutputStream.BUFFER_SIZE + 10; i++) {
75  			String s1 = Strings.repeat("a", i);
76  			assertNoCrLf(s1, s1);
77  			String s2 = Strings.repeat("\0", i);
78  			assertNoCrLf(s2, s2);
79  		}
80  	}
81  
82  	private void assertNoCrLf(String string, String string2) throws IOException {
83  		assertNoCrLfHelper(string, string2);
84  		// \u00e5 = LATIN SMALL LETTER A WITH RING ABOVE
85  		// the byte value is negative
86  		assertNoCrLfHelper("\u00e5" + string, "\u00e5" + string2);
87  		assertNoCrLfHelper("\u00e5" + string + "\u00e5", "\u00e5" + string2
88  				+ "\u00e5");
89  		assertNoCrLfHelper(string + "\u00e5", string2 + "\u00e5");
90  	}
91  
92  	private void assertNoCrLfHelper(String expect, String input)
93  			throws IOException {
94  		byte[] inbytes = input.getBytes();
95  		byte[] expectBytes = expect.getBytes();
96  		for (int i = -4; i < 5; ++i) {
97  			int size = Math.abs(i);
98  			byte[] buf = new byte[size];
99  			InputStream in = new ByteArrayInputStream(inbytes);
100 			ByteArrayOutputStream bos = new ByteArrayOutputStream();
101 			OutputStream out = new AutoCRLFOutputStream(bos);
102 			if (i > 0) {
103 				int n;
104 				while ((n = in.read(buf)) >= 0) {
105 					out.write(buf, 0, n);
106 				}
107 			} else if (i < 0) {
108 				int n;
109 				while ((n = in.read(buf)) >= 0) {
110 					byte[] b = new byte[n];
111 					System.arraycopy(buf, 0, b, 0, n);
112 					out.write(b);
113 				}
114 			} else {
115 				int c;
116 				while ((c = in.read()) != -1)
117 					out.write(c);
118 			}
119 			out.flush();
120 			in.close();
121 			out.close();
122 			byte[] actualBytes = bos.toByteArray();
123 			Assert.assertEquals("bufsize=" + size, encode(expectBytes),
124 					encode(actualBytes));
125 		}
126 	}
127 
128 	String encode(byte[] in) {
129 		StringBuilder str = new StringBuilder();
130 		for (byte b : in) {
131 			if (b < 32)
132 				str.append(0xFF & b);
133 			else {
134 				str.append("'");
135 				str.append((char) b);
136 				str.append("'");
137 			}
138 			str.append(' ');
139 		}
140 		return str.toString();
141 	}
142 
143 }