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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.junit.Assert.assertArrayEquals;
  44. import static org.eclipse.jgit.lib.CoreConfig.EolStreamType.*;
  45. import java.io.ByteArrayInputStream;
  46. import java.io.ByteArrayOutputStream;
  47. import java.io.InputStream;
  48. import java.io.OutputStream;
  49. import java.nio.charset.StandardCharsets;
  50. import java.util.Arrays;
  51. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  52. import org.eclipse.jgit.util.IO;
  53. import org.eclipse.jgit.util.io.EolStreamTypeUtil;
  54. import org.junit.Test;
  55. /**
  56. * Unit tests for end-of-line conversion streams
  57. */
  58. public class EolStreamTypeUtilTest {
  59. @Test
  60. public void testCheckoutDirect() throws Exception {
  61. testCheckout(DIRECT, DIRECT, "", "");
  62. testCheckout(DIRECT, DIRECT, "\r", "\r");
  63. testCheckout(DIRECT, DIRECT, "\n", "\n");
  64. testCheckout(DIRECT, DIRECT, "\r\n", "\r\n");
  65. testCheckout(DIRECT, DIRECT, "\n\r", "\n\r");
  66. testCheckout(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
  67. testCheckout(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
  68. testCheckout(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
  69. testCheckout(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
  70. testCheckout(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
  71. testCheckout(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
  72. }
  73. @Test
  74. public void testCheckoutLF() throws Exception {
  75. testCheckout(TEXT_LF, AUTO_LF, "", "");
  76. testCheckout(TEXT_LF, AUTO_LF, "\r", "\r");
  77. testCheckout(TEXT_LF, AUTO_LF, "\n", "\n");
  78. testCheckout(TEXT_LF, AUTO_LF, "\r\n", "\n");
  79. testCheckout(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
  80. testCheckout(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
  81. testCheckout(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
  82. testCheckout(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
  83. testCheckout(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
  84. testCheckout(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
  85. testCheckout(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
  86. }
  87. @Test
  88. public void testCheckoutCRLF() throws Exception {
  89. testCheckout(TEXT_CRLF, AUTO_CRLF, "", "");
  90. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
  91. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
  92. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
  93. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
  94. testCheckout(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
  95. testCheckout(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
  96. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
  97. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
  98. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
  99. testCheckout(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
  100. }
  101. /**
  102. * Test stream type detection based on stream content.
  103. * <p>
  104. * Tests three things with the output text:
  105. * <p>
  106. * 1) conversion if output was declared as text
  107. * <p>
  108. * 2) conversion if output was declared as potentially text (AUTO_...) and
  109. * is in fact text
  110. * <p>
  111. * 3) conversion if modified output (now with binary characters) was
  112. * declared as potentially text but now contains binary characters
  113. * <p>
  114. *
  115. * @param streamTypeText
  116. * is the enum meaning that the output is definitely text (no
  117. * binary check at all)
  118. * @param streamTypeWithBinaryCheck
  119. * is the enum meaning that the output may be text (binary check
  120. * is done)
  121. * @param output
  122. * is a text output without binary characters
  123. * @param expectedConversion
  124. * is the expected converted output without binary characters
  125. * @throws Exception
  126. */
  127. private void testCheckout(EolStreamType streamTypeText,
  128. EolStreamType streamTypeWithBinaryCheck, String output,
  129. String expectedConversion) throws Exception {
  130. ByteArrayOutputStream b;
  131. byte[] outputBytes = output.getBytes(StandardCharsets.UTF_8);
  132. byte[] expectedConversionBytes = expectedConversion
  133. .getBytes(StandardCharsets.UTF_8);
  134. // test using output text and assuming it was declared TEXT
  135. b = new ByteArrayOutputStream();
  136. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  137. streamTypeText)) {
  138. out.write(outputBytes);
  139. }
  140. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  141. // test using ouput text and assuming it was declared AUTO, using binary
  142. // detection
  143. b = new ByteArrayOutputStream();
  144. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  145. streamTypeWithBinaryCheck)) {
  146. out.write(outputBytes);
  147. }
  148. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  149. // now pollute output text with some binary bytes
  150. outputBytes = extendWithBinaryData(outputBytes);
  151. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  152. // again, test using output text and assuming it was declared TEXT
  153. b = new ByteArrayOutputStream();
  154. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  155. streamTypeText)) {
  156. out.write(outputBytes);
  157. }
  158. assertArrayEquals(expectedConversionBytes, b.toByteArray());
  159. // again, test using ouput text and assuming it was declared AUTO, using
  160. // binary
  161. // detection
  162. b = new ByteArrayOutputStream();
  163. try (OutputStream out = EolStreamTypeUtil.wrapOutputStream(b,
  164. streamTypeWithBinaryCheck)) {
  165. out.write(outputBytes);
  166. }
  167. // expect no conversion
  168. assertArrayEquals(outputBytes, b.toByteArray());
  169. }
  170. @Test
  171. public void testCheckinDirect() throws Exception {
  172. testCheckin(DIRECT, DIRECT, "", "");
  173. testCheckin(DIRECT, DIRECT, "\r", "\r");
  174. testCheckin(DIRECT, DIRECT, "\n", "\n");
  175. testCheckin(DIRECT, DIRECT, "\r\n", "\r\n");
  176. testCheckin(DIRECT, DIRECT, "\n\r", "\n\r");
  177. testCheckin(DIRECT, DIRECT, "\n\r\n", "\n\r\n");
  178. testCheckin(DIRECT, DIRECT, "\r\n\r", "\r\n\r");
  179. testCheckin(DIRECT, DIRECT, "a\nb\n", "a\nb\n");
  180. testCheckin(DIRECT, DIRECT, "a\rb\r", "a\rb\r");
  181. testCheckin(DIRECT, DIRECT, "a\n\rb\n\r", "a\n\rb\n\r");
  182. testCheckin(DIRECT, DIRECT, "a\r\nb\r\n", "a\r\nb\r\n");
  183. }
  184. @Test
  185. public void testCheckinLF() throws Exception {
  186. testCheckin(TEXT_LF, AUTO_LF, "", "");
  187. testCheckin(TEXT_LF, AUTO_LF, "\r", "\r");
  188. testCheckin(TEXT_LF, AUTO_LF, "\n", "\n");
  189. testCheckin(TEXT_LF, AUTO_LF, "\r\n", "\n");
  190. testCheckin(TEXT_LF, AUTO_LF, "\n\r", "\n\r");
  191. testCheckin(TEXT_LF, AUTO_LF, "\n\r\n", "\n\n");
  192. testCheckin(TEXT_LF, AUTO_LF, "\r\n\r", "\n\r");
  193. testCheckin(TEXT_LF, AUTO_LF, "a\nb\n", "a\nb\n");
  194. testCheckin(TEXT_LF, AUTO_LF, "a\rb\r", "a\rb\r");
  195. testCheckin(TEXT_LF, AUTO_LF, "a\n\rb\n\r", "a\n\rb\n\r");
  196. testCheckin(TEXT_LF, AUTO_LF, "a\r\nb\r\n", "a\nb\n");
  197. }
  198. @Test
  199. public void testCheckinCRLF() throws Exception {
  200. testCheckin(TEXT_CRLF, AUTO_CRLF, "", "");
  201. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r", "\r");
  202. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n", "\r\n");
  203. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n", "\r\n");
  204. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r", "\r\n\r");
  205. testCheckin(TEXT_CRLF, AUTO_CRLF, "\n\r\n", "\r\n\r\n");
  206. testCheckin(TEXT_CRLF, AUTO_CRLF, "\r\n\r", "\r\n\r");
  207. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\nb\n", "a\r\nb\r\n");
  208. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\rb\r", "a\rb\r");
  209. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\n\rb\n\r", "a\r\n\rb\r\n\r");
  210. testCheckin(TEXT_CRLF, AUTO_CRLF, "a\r\nb\r\n", "a\r\nb\r\n");
  211. }
  212. /**
  213. * Test stream type detection based on stream content.
  214. * <p>
  215. * Tests three things with the input text:
  216. * <p>
  217. * 1) conversion if input was declared as text
  218. * <p>
  219. * 2) conversion if input was declared as potentially text (AUTO_...) and is
  220. * in fact text
  221. * <p>
  222. * 3) conversion if modified input (now with binary characters) was declared
  223. * as potentially text but now contains binary characters
  224. * <p>
  225. *
  226. * @param streamTypeText
  227. * is the enum meaning that the input is definitely text (no
  228. * binary check at all)
  229. * @param streamTypeWithBinaryCheck
  230. * is the enum meaning that the input may be text (binary check
  231. * is done)
  232. * @param input
  233. * is a text input without binary characters
  234. * @param expectedConversion
  235. * is the expected converted input without binary characters
  236. * @throws Exception
  237. */
  238. private void testCheckin(EolStreamType streamTypeText,
  239. EolStreamType streamTypeWithBinaryCheck, String input,
  240. String expectedConversion) throws Exception {
  241. byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
  242. byte[] expectedConversionBytes = expectedConversion
  243. .getBytes(StandardCharsets.UTF_8);
  244. // test using input text and assuming it was declared TEXT
  245. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  246. new ByteArrayInputStream(inputBytes),
  247. streamTypeText)) {
  248. byte[] b = new byte[1024];
  249. int len = IO.readFully(in, b, 0);
  250. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  251. }
  252. // test using input text and assuming it was declared AUTO, using binary
  253. // detection
  254. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  255. new ByteArrayInputStream(inputBytes),
  256. streamTypeWithBinaryCheck)) {
  257. byte[] b = new byte[1024];
  258. int len = IO.readFully(in, b, 0);
  259. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  260. }
  261. // now pollute input text with some binary bytes
  262. inputBytes = extendWithBinaryData(inputBytes);
  263. expectedConversionBytes = extendWithBinaryData(expectedConversionBytes);
  264. // again, test using input text and assuming it was declared TEXT
  265. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  266. new ByteArrayInputStream(inputBytes), streamTypeText)) {
  267. byte[] b = new byte[1024];
  268. int len = IO.readFully(in, b, 0);
  269. assertArrayEquals(expectedConversionBytes, Arrays.copyOf(b, len));
  270. }
  271. // again, test using input text and assuming it was declared AUTO, using
  272. // binary
  273. // detection
  274. try (InputStream in = EolStreamTypeUtil.wrapInputStream(
  275. new ByteArrayInputStream(inputBytes),
  276. streamTypeWithBinaryCheck)) {
  277. byte[] b = new byte[1024];
  278. int len = IO.readFully(in, b, 0);
  279. // expect no conversion
  280. assertArrayEquals(inputBytes, Arrays.copyOf(b, len));
  281. }
  282. }
  283. private byte[] extendWithBinaryData(byte[] data) throws Exception {
  284. int n = 3;
  285. byte[] dataEx = new byte[data.length + n];
  286. System.arraycopy(data, 0, dataEx, 0, data.length);
  287. for (int i = 0; i < n; i++) {
  288. dataEx[data.length + i] = (byte) i;
  289. }
  290. return dataEx;
  291. }
  292. }