You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EolStreamTypeUtilTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2015, Ivan Motsch <ivan.motsch@bsiag.com>
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.api;
  43. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_CRLF;
  44. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_LF;
  45. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.DIRECT;
  46. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_CRLF;
  47. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_LF;
  48. import static org.junit.Assert.assertArrayEquals;
  49. import java.io.ByteArrayInputStream;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.InputStream;
  52. import java.io.OutputStream;
  53. import java.nio.charset.StandardCharsets;
  54. import java.util.Arrays;
  55. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  56. import org.eclipse.jgit.util.IO;
  57. import org.eclipse.jgit.util.io.EolStreamTypeUtil;
  58. import org.junit.Test;
  59. /**
  60. * Unit tests for end-of-line conversion streams
  61. */
  62. public class EolStreamTypeUtilTest {
  63. @Test
  64. public void testCheckoutDirect() throws Exception {
  65. testCheckout(DIRECT, DIRECT, "", "");
  66. testCheckout(DIRECT, DIRECT, "\r", "\r");
  67. testCheckout(DIRECT, DIRECT, "\n", "\n");
  68. testCheckout(DIRECT, DIRECT, "\r\n", "\r\n");
  69. testCheckout(DIRECT, DIRECT, "\n\r", "\n\r");
  70. testCheckout(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
  71. testCheckout(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
  72. testCheckout(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
  73. testCheckout(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
  74. testCheckout(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
  75. testCheckout(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
  76. }
  77. @Test
  78. public void testCheckoutLF() throws Exception {
  79. testCheckout(TEXT_LF, AUTO_LF, "", "");
  80. testCheckout(TEXT_LF, AUTO_LF, "\r", "\r");
  81. testCheckout(TEXT_LF, AUTO_LF, "\n", "\n");
  82. testCheckout(TEXT_LF, AUTO_LF, "\r\n", "\n");
  83. testCheckout(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
  84. testCheckout(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
  85. testCheckout(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
  86. testCheckout(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
  87. testCheckout(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
  88. testCheckout(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
  89. testCheckout(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
  90. }
  91. @Test
  92. public void testCheckoutCRLF() throws Exception {
  93. testCheckout(TEXT_CRLF, AUTO_CRLF, "", "");
  94. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
  95. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
  96. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
  97. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
  98. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
  99. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
  100. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
  101. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
  102. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
  103. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
  104. }
  105. /**
  106. * Test stream type detection based on stream content.
  107. * <p>
  108. * Tests three things with the output text:
  109. * <p>
  110. * 1) conversion if output was declared as text
  111. * <p>
  112. * 2) conversion if output was declared as potentially text (AUTO_...) and
  113. * is in fact text
  114. * <p>
  115. * 3) conversion if modified output (now with binary characters) was
  116. * declared as potentially text but now contains binary characters
  117. * <p>
  118. *
  119. * @param streamTypeText
  120. * is the enum meaning that the output is definitely text (no
  121. * binary check at all)
  122. * @param streamTypeWithBinaryCheck
  123. * is the enum meaning that the output may be text (binary check
  124. * is done)
  125. * @param output
  126. * is a text output without binary characters
  127. * @param expectedConversion
  128. * is the expected converted output without binary characters
  129. * @throws Exception
  130. */
  131. private void testCheckout(EolStreamType streamTypeText,
  132. EolStreamType streamTypeWithBinaryCheck, String output,
  133. String expectedConversion) throws Exception {
  134. ByteArrayOutputStream b;
  135. byte[] outputBytes = output.getBytes(StandardCharsets.UTF_8);
  136. byte[] expectedConversionBytes = expectedConversion
  137. .getBytes(StandardCharsets.UTF_8);
  138. // test using output text and assuming it was declared TEXT
  139. b = new ByteArrayOutputStream();
  140. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  141. streamTypeText)) {
  142. out.write(outputBytes);
  143. }
  144. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  145. // test using ouput text and assuming it was declared AUTO, using binary
  146. // detection
  147. b = new ByteArrayOutputStream();
  148. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  149. streamTypeWithBinaryCheck)) {
  150. out.write(outputBytes);
  151. }
  152. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  153. // now pollute output text with some binary bytes
  154. outputBytes = extendWithBinaryData(outputBytes);
  155. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  156. // again, test using output text and assuming it was declared TEXT
  157. b = new ByteArrayOutputStream();
  158. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  159. streamTypeText)) {
  160. out.write(outputBytes);
  161. }
  162. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  163. // again, test using ouput text and assuming it was declared AUTO, using
  164. // binary
  165. // detection
  166. b = new ByteArrayOutputStream();
  167. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  168. streamTypeWithBinaryCheck)) {
  169. out.write(outputBytes);
  170. }
  171. // expect no conversion
  172. assertArrayEquals(outputBytes, b.toByteArray());
  173. }
  174. @Test
  175. public void testCheckinDirect() throws Exception {
  176. testCheckin(DIRECT, DIRECT, "", "");
  177. testCheckin(DIRECT, DIRECT, "\r", "\r");
  178. testCheckin(DIRECT, DIRECT, "\n", "\n");
  179. testCheckin(DIRECT, DIRECT, "\r\n", "\r\n");
  180. testCheckin(DIRECT, DIRECT, "\n\r", "\n\r");
  181. testCheckin(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
  182. testCheckin(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
  183. testCheckin(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
  184. testCheckin(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
  185. testCheckin(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
  186. testCheckin(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
  187. }
  188. @Test
  189. public void testCheckinLF() throws Exception {
  190. testCheckin(TEXT_LF, AUTO_LF, "", "");
  191. testCheckin(TEXT_LF, AUTO_LF, "\r", "\r");
  192. testCheckin(TEXT_LF, AUTO_LF, "\n", "\n");
  193. testCheckin(TEXT_LF, AUTO_LF, "\r\n", "\n");
  194. testCheckin(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
  195. testCheckin(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
  196. testCheckin(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
  197. testCheckin(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
  198. testCheckin(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
  199. testCheckin(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
  200. testCheckin(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
  201. }
  202. @Test
  203. public void testCheckinCRLF() throws Exception {
  204. testCheckin(TEXT_CRLF, AUTO_CRLF, "", "");
  205. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
  206. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
  207. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
  208. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
  209. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
  210. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
  211. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
  212. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
  213. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
  214. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
  215. }
  216. /**
  217. * Test stream type detection based on stream content.
  218. * <p>
  219. * Tests three things with the input text:
  220. * <p>
  221. * 1) conversion if input was declared as text
  222. * <p>
  223. * 2) conversion if input was declared as potentially text (AUTO_...) and is
  224. * in fact text
  225. * <p>
  226. * 3) conversion if modified input (now with binary characters) was declared
  227. * as potentially text but now contains binary characters
  228. * <p>
  229. *
  230. * @param streamTypeText
  231. * is the enum meaning that the input is definitely text (no
  232. * binary check at all)
  233. * @param streamTypeWithBinaryCheck
  234. * is the enum meaning that the input may be text (binary check
  235. * is done)
  236. * @param input
  237. * is a text input without binary characters
  238. * @param expectedConversion
  239. * is the expected converted input without binary characters
  240. * @throws Exception
  241. */
  242. private void testCheckin(EolStreamType streamTypeText,
  243. EolStreamType streamTypeWithBinaryCheck, String input,
  244. String expectedConversion) throws Exception {
  245. byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
  246. byte[] expectedConversionBytes = expectedConversion
  247. .getBytes(StandardCharsets.UTF_8);
  248. // test using input text and assuming it was declared TEXT
  249. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  250. new ByteArrayInputStream(inputBytes),
  251. streamTypeText)) {
  252. byte[] b = new byte[1024];
  253. int len = IO.readFully(in, b, 0);
  254. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  255. }
  256. // test using input text and assuming it was declared AUTO, using binary
  257. // detection
  258. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  259. new ByteArrayInputStream(inputBytes),
  260. streamTypeWithBinaryCheck)) {
  261. byte[] b = new byte[1024];
  262. int len = IO.readFully(in, b, 0);
  263. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  264. }
  265. // now pollute input text with some binary bytes
  266. inputBytes = extendWithBinaryData(inputBytes);
  267. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  268. // again, test using input text and assuming it was declared TEXT
  269. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  270. new ByteArrayInputStream(inputBytes), streamTypeText)) {
  271. byte[] b = new byte[1024];
  272. int len = IO.readFully(in, b, 0);
  273. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  274. }
  275. // again, test using input text and assuming it was declared AUTO, using
  276. // binary
  277. // detection
  278. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  279. new ByteArrayInputStream(inputBytes),
  280. streamTypeWithBinaryCheck)) {
  281. byte[] b = new byte[1024];
  282. int len = IO.readFully(in, b, 0);
  283. // expect no conversion
  284. assertArrayEquals(inputBytes, Arrays.copyOf(b, len));
  285. }
  286. }
  287. private byte[] extendWithBinaryData(byte[] data) throws Exception {
  288. int n = 3;
  289. byte[] dataEx = new byte[data.length + n];
  290. System.arraycopy(data, 0, dataEx, 0, data.length);
  291. for (int i = 0; i < n; i++) {
  292. dataEx[data.length + i] = (byte) i;
  293. }
  294. return dataEx;
  295. }
  296. }