Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

EolStreamTypeUtilTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (C) 2015, 2020 Ivan Motsch <ivan.motsch@bsiag.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_CRLF;
  13. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_LF;
  14. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.DIRECT;
  15. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_CRLF;
  16. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_LF;
  17. import static org.junit.Assert.assertArrayEquals;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.util.Arrays;
  23. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  24. import org.eclipse.jgit.util.IO;
  25. import org.eclipse.jgit.util.io.EolStreamTypeUtil;
  26. import org.junit.Test;
  27. /**
  28. * Unit tests for end-of-line conversion streams
  29. */
  30. public class EolStreamTypeUtilTest {
  31. @Test
  32. public void testCheckoutDirect() throws Exception {
  33. testCheckout(DIRECT, DIRECT, "", "");
  34. testCheckout(DIRECT, DIRECT, "\r", "\r");
  35. testCheckout(DIRECT, DIRECT, "\n", "\n");
  36. testCheckout(DIRECT, DIRECT, "\r\n", "\r\n");
  37. testCheckout(DIRECT, DIRECT, "\n\r", "\n\r");
  38. testCheckout(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
  39. testCheckout(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
  40. testCheckout(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
  41. testCheckout(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
  42. testCheckout(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
  43. testCheckout(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
  44. }
  45. @Test
  46. public void testCheckoutLF() throws Exception {
  47. testCheckout(TEXT_LF, AUTO_LF, "", "");
  48. testCheckout(TEXT_LF, AUTO_LF, "\r", "\r");
  49. testCheckout(TEXT_LF, AUTO_LF, "\n", "\n");
  50. testCheckout(TEXT_LF, null, "\r\n", "\n");
  51. testCheckout(null, AUTO_LF, "\r\n", "\r\n");
  52. testCheckout(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
  53. testCheckout(TEXT_LF, null, "\n\r\n", "\n\n");
  54. testCheckout(null, AUTO_LF, "\n\r\n", "\n\r\n");
  55. testCheckout(TEXT_LF, null, "\r\n\r", "\n\r");
  56. testCheckout(null, AUTO_LF, "\r\n\r", "\r\n\r");
  57. testCheckout(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
  58. testCheckout(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
  59. testCheckout(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
  60. testCheckout(TEXT_LF, null, "a\r\nb\r\n", "a\nb\n");
  61. testCheckout(null, AUTO_LF, "a\r\nb\r\n", "a\r\nb\r\n");
  62. }
  63. @Test
  64. public void testCheckoutCRLF() throws Exception {
  65. testCheckout(TEXT_CRLF, AUTO_CRLF, "", "");
  66. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
  67. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
  68. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
  69. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
  70. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
  71. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
  72. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
  73. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
  74. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
  75. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
  76. }
  77. /**
  78. * Test stream type detection based on stream content.
  79. * <p>
  80. * Tests three things with the output text:
  81. * <p>
  82. * 1) conversion if output was declared as text
  83. * <p>
  84. * 2) conversion if output was declared as potentially text (AUTO_...) and
  85. * is in fact text
  86. * <p>
  87. * 3) conversion if modified output (now with binary characters) was
  88. * declared as potentially text but now contains binary characters
  89. * <p>
  90. *
  91. * @param streamTypeText
  92. * is the enum meaning that the output is definitely text (no
  93. * binary check at all)
  94. * @param streamTypeWithBinaryCheck
  95. * is the enum meaning that the output may be text (binary check
  96. * is done)
  97. * @param output
  98. * is a text output without binary characters
  99. * @param expectedConversion
  100. * is the expected converted output without binary characters
  101. * @throws Exception
  102. */
  103. private void testCheckout(EolStreamType streamTypeText,
  104. EolStreamType streamTypeWithBinaryCheck, String output,
  105. String expectedConversion) throws Exception {
  106. ByteArrayOutputStream b;
  107. byte[] outputBytes = output.getBytes(UTF_8);
  108. byte[] expectedConversionBytes = expectedConversion.getBytes(UTF_8);
  109. if (streamTypeText != null) {
  110. // test using output text and assuming it was declared TEXT
  111. b = new ByteArrayOutputStream();
  112. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  113. streamTypeText)) {
  114. out.write(outputBytes);
  115. }
  116. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  117. }
  118. if (streamTypeWithBinaryCheck != null) {
  119. // test using output text and assuming it was declared AUTO, using
  120. // binary detection
  121. b = new ByteArrayOutputStream();
  122. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  123. streamTypeWithBinaryCheck)) {
  124. out.write(outputBytes);
  125. }
  126. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  127. }
  128. // now pollute output text with some binary bytes
  129. outputBytes = extendWithBinaryData(outputBytes);
  130. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  131. if (streamTypeText != null) {
  132. // again, test using output text and assuming it was declared TEXT
  133. b = new ByteArrayOutputStream();
  134. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  135. streamTypeText)) {
  136. out.write(outputBytes);
  137. }
  138. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  139. }
  140. if (streamTypeWithBinaryCheck != null) {
  141. // again, test using output text and assuming it was declared AUTO,
  142. // using binary detection
  143. b = new ByteArrayOutputStream();
  144. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  145. streamTypeWithBinaryCheck)) {
  146. out.write(outputBytes);
  147. }
  148. // expect no conversion
  149. assertArrayEquals(outputBytes, b.toByteArray());
  150. }
  151. }
  152. @Test
  153. public void testCheckinDirect() throws Exception {
  154. testCheckin(DIRECT, DIRECT, "", "");
  155. testCheckin(DIRECT, DIRECT, "\r", "\r");
  156. testCheckin(DIRECT, DIRECT, "\n", "\n");
  157. testCheckin(DIRECT, DIRECT, "\r\n", "\r\n");
  158. testCheckin(DIRECT, DIRECT, "\n\r", "\n\r");
  159. testCheckin(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
  160. testCheckin(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
  161. testCheckin(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
  162. testCheckin(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
  163. testCheckin(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
  164. testCheckin(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
  165. }
  166. @Test
  167. public void testCheckinLF() throws Exception {
  168. testCheckin(TEXT_LF, AUTO_LF, "", "");
  169. testCheckin(TEXT_LF, AUTO_LF, "\r", "\r");
  170. testCheckin(TEXT_LF, AUTO_LF, "\n", "\n");
  171. testCheckin(TEXT_LF, AUTO_LF, "\r\n", "\n");
  172. testCheckin(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
  173. testCheckin(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
  174. testCheckin(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
  175. testCheckin(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
  176. testCheckin(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
  177. testCheckin(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
  178. testCheckin(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
  179. }
  180. @Test
  181. public void testCheckinCRLF() throws Exception {
  182. testCheckin(TEXT_CRLF, AUTO_CRLF, "", "");
  183. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
  184. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
  185. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
  186. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
  187. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
  188. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
  189. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
  190. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
  191. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
  192. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
  193. }
  194. /**
  195. * Test stream type detection based on stream content.
  196. * <p>
  197. * Tests three things with the input text:
  198. * <p>
  199. * 1) conversion if input was declared as text
  200. * <p>
  201. * 2) conversion if input was declared as potentially text (AUTO_...) and is
  202. * in fact text
  203. * <p>
  204. * 3) conversion if modified input (now with binary characters) was declared
  205. * as potentially text but now contains binary characters
  206. * <p>
  207. *
  208. * @param streamTypeText
  209. * is the enum meaning that the input is definitely text (no
  210. * binary check at all)
  211. * @param streamTypeWithBinaryCheck
  212. * is the enum meaning that the input may be text (binary check
  213. * is done)
  214. * @param input
  215. * is a text input without binary characters
  216. * @param expectedConversion
  217. * is the expected converted input without binary characters
  218. * @throws Exception
  219. */
  220. private void testCheckin(EolStreamType streamTypeText,
  221. EolStreamType streamTypeWithBinaryCheck, String input,
  222. String expectedConversion) throws Exception {
  223. byte[] inputBytes = input.getBytes(UTF_8);
  224. byte[] expectedConversionBytes = expectedConversion.getBytes(UTF_8);
  225. // test using input text and assuming it was declared TEXT
  226. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  227. new ByteArrayInputStream(inputBytes),
  228. streamTypeText)) {
  229. byte[] b = new byte[1024];
  230. int len = IO.readFully(in, b, 0);
  231. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  232. }
  233. // test using input text and assuming it was declared AUTO, using binary
  234. // detection
  235. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  236. new ByteArrayInputStream(inputBytes),
  237. streamTypeWithBinaryCheck)) {
  238. byte[] b = new byte[1024];
  239. int len = IO.readFully(in, b, 0);
  240. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  241. }
  242. // now pollute input text with some binary bytes
  243. inputBytes = extendWithBinaryData(inputBytes);
  244. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  245. // again, test using input text and assuming it was declared TEXT
  246. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  247. new ByteArrayInputStream(inputBytes), streamTypeText)) {
  248. byte[] b = new byte[1024];
  249. int len = IO.readFully(in, b, 0);
  250. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  251. }
  252. // again, test using input text and assuming it was declared AUTO, using
  253. // binary
  254. // detection
  255. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  256. new ByteArrayInputStream(inputBytes),
  257. streamTypeWithBinaryCheck)) {
  258. byte[] b = new byte[1024];
  259. int len = IO.readFully(in, b, 0);
  260. // expect no conversion
  261. assertArrayEquals(inputBytes, Arrays.copyOf(b, len));
  262. }
  263. }
  264. private byte[] extendWithBinaryData(byte[] data) throws Exception {
  265. int n = 3;
  266. byte[] dataEx = new byte[data.length + n];
  267. System.arraycopy(data, 0, dataEx, 0, data.length);
  268. for (int i = 0; i < n; i++) {
  269. dataEx[data.length + i] = (byte) i;
  270. }
  271. return dataEx;
  272. }
  273. }