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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 java.nio.charset.StandardCharsets.UTF_8;
  44. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_CRLF;
  45. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.AUTO_LF;
  46. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.DIRECT;
  47. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_CRLF;
  48. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.TEXT_LF;
  49. import static org.junit.Assert.assertArrayEquals;
  50. import java.io.ByteArrayInputStream;
  51. import java.io.ByteArrayOutputStream;
  52. import java.io.InputStream;
  53. import java.io.OutputStream;
  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(UTF_8);
  136. byte[] expectedConversionBytes = expectedConversion.getBytes(UTF_8);
  137. // test using output text and assuming it was declared TEXT
  138. b = new ByteArrayOutputStream();
  139. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  140. streamTypeText)) {
  141. out.write(outputBytes);
  142. }
  143. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  144. // test using ouput text and assuming it was declared AUTO, using binary
  145. // detection
  146. b = new ByteArrayOutputStream();
  147. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  148. streamTypeWithBinaryCheck)) {
  149. out.write(outputBytes);
  150. }
  151. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  152. // now pollute output text with some binary bytes
  153. outputBytes = extendWithBinaryData(outputBytes);
  154. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  155. // again, test using output text and assuming it was declared TEXT
  156. b = new ByteArrayOutputStream();
  157. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  158. streamTypeText)) {
  159. out.write(outputBytes);
  160. }
  161. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  162. // again, test using ouput text and assuming it was declared AUTO, using
  163. // binary
  164. // detection
  165. b = new ByteArrayOutputStream();
  166. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  167. streamTypeWithBinaryCheck)) {
  168. out.write(outputBytes);
  169. }
  170. // expect no conversion
  171. assertArrayEquals(outputBytes, b.toByteArray());
  172. }
  173. @Test
  174. public void testCheckinDirect() throws Exception {
  175. testCheckin(DIRECT, DIRECT, "", "");
  176. testCheckin(DIRECT, DIRECT, "\r", "\r");
  177. testCheckin(DIRECT, DIRECT, "\n", "\n");
  178. testCheckin(DIRECT, DIRECT, "\r\n", "\r\n");
  179. testCheckin(DIRECT, DIRECT, "\n\r", "\n\r");
  180. testCheckin(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
  181. testCheckin(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
  182. testCheckin(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
  183. testCheckin(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
  184. testCheckin(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
  185. testCheckin(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
  186. }
  187. @Test
  188. public void testCheckinLF() throws Exception {
  189. testCheckin(TEXT_LF, AUTO_LF, "", "");
  190. testCheckin(TEXT_LF, AUTO_LF, "\r", "\r");
  191. testCheckin(TEXT_LF, AUTO_LF, "\n", "\n");
  192. testCheckin(TEXT_LF, AUTO_LF, "\r\n", "\n");
  193. testCheckin(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
  194. testCheckin(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
  195. testCheckin(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
  196. testCheckin(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
  197. testCheckin(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
  198. testCheckin(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
  199. testCheckin(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
  200. }
  201. @Test
  202. public void testCheckinCRLF() throws Exception {
  203. testCheckin(TEXT_CRLF, AUTO_CRLF, "", "");
  204. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
  205. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
  206. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
  207. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
  208. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
  209. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
  210. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
  211. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
  212. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
  213. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
  214. }
  215. /**
  216. * Test stream type detection based on stream content.
  217. * <p>
  218. * Tests three things with the input text:
  219. * <p>
  220. * 1) conversion if input was declared as text
  221. * <p>
  222. * 2) conversion if input was declared as potentially text (AUTO_...) and is
  223. * in fact text
  224. * <p>
  225. * 3) conversion if modified input (now with binary characters) was declared
  226. * as potentially text but now contains binary characters
  227. * <p>
  228. *
  229. * @param streamTypeText
  230. * is the enum meaning that the input is definitely text (no
  231. * binary check at all)
  232. * @param streamTypeWithBinaryCheck
  233. * is the enum meaning that the input may be text (binary check
  234. * is done)
  235. * @param input
  236. * is a text input without binary characters
  237. * @param expectedConversion
  238. * is the expected converted input without binary characters
  239. * @throws Exception
  240. */
  241. private void testCheckin(EolStreamType streamTypeText,
  242. EolStreamType streamTypeWithBinaryCheck, String input,
  243. String expectedConversion) throws Exception {
  244. byte[] inputBytes = input.getBytes(UTF_8);
  245. byte[] expectedConversionBytes = expectedConversion.getBytes(UTF_8);
  246. // test using input text and assuming it was declared TEXT
  247. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  248. new ByteArrayInputStream(inputBytes),
  249. streamTypeText)) {
  250. byte[] b = new byte[1024];
  251. int len = IO.readFully(in, b, 0);
  252. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  253. }
  254. // test using input text and assuming it was declared AUTO, using binary
  255. // detection
  256. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  257. new ByteArrayInputStream(inputBytes),
  258. streamTypeWithBinaryCheck)) {
  259. byte[] b = new byte[1024];
  260. int len = IO.readFully(in, b, 0);
  261. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  262. }
  263. // now pollute input text with some binary bytes
  264. inputBytes = extendWithBinaryData(inputBytes);
  265. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  266. // again, test using input text and assuming it was declared TEXT
  267. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  268. new ByteArrayInputStream(inputBytes), streamTypeText)) {
  269. byte[] b = new byte[1024];
  270. int len = IO.readFully(in, b, 0);
  271. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  272. }
  273. // again, test using input text and assuming it was declared AUTO, using
  274. // binary
  275. // detection
  276. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  277. new ByteArrayInputStream(inputBytes),
  278. streamTypeWithBinaryCheck)) {
  279. byte[] b = new byte[1024];
  280. int len = IO.readFully(in, b, 0);
  281. // expect no conversion
  282. assertArrayEquals(inputBytes, Arrays.copyOf(b, len));
  283. }
  284. }
  285. private byte[] extendWithBinaryData(byte[] data) throws Exception {
  286. int n = 3;
  287. byte[] dataEx = new byte[data.length + n];
  288. System.arraycopy(data, 0, dataEx, 0, data.length);
  289. for (int i = 0; i < n; i++) {
  290. dataEx[data.length + i] = (byte) i;
  291. }
  292. return dataEx;
  293. }
  294. }