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.

RawTextTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.diff;
  12. import static java.nio.charset.StandardCharsets.UTF_8;
  13. import static org.junit.Assert.assertArrayEquals;
  14. import static org.junit.Assert.assertEquals;
  15. import static org.junit.Assert.assertFalse;
  16. import static org.junit.Assert.assertNull;
  17. import static org.junit.Assert.assertTrue;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.util.RawParseUtils;
  22. import org.junit.Test;
  23. public class RawTextTest {
  24. @Test
  25. public void testEmpty() {
  26. final RawText r = new RawText(new byte[0]);
  27. assertEquals(0, r.size());
  28. }
  29. @Test
  30. public void testNul() {
  31. String input = "foo-a\nf\0o-b\n";
  32. byte[] data = Constants.encodeASCII(input);
  33. final RawText a = new RawText(data);
  34. assertArrayEquals(a.content, data);
  35. assertEquals(2, a.size());
  36. assertEquals("foo-a\n", a.getString(0, 1, false));
  37. assertEquals("f\0o-b\n", a.getString(1, 2, false));
  38. assertEquals("foo-a", a.getString(0, 1, true));
  39. assertEquals("f\0o-b", a.getString(1, 2, true));
  40. }
  41. @Test
  42. public void testCrLfTextYes() {
  43. assertTrue(RawText
  44. .isCrLfText(Constants.encodeASCII("line 1\r\nline 2\r\n")));
  45. }
  46. @Test
  47. public void testCrLfTextNo() {
  48. assertFalse(
  49. RawText.isCrLfText(Constants.encodeASCII("line 1\nline 2\n")));
  50. }
  51. @Test
  52. public void testCrLfTextBinary() {
  53. assertFalse(RawText
  54. .isCrLfText(Constants.encodeASCII("line 1\r\nline\0 2\r\n")));
  55. }
  56. @Test
  57. public void testCrLfTextMixed() {
  58. assertTrue(RawText
  59. .isCrLfText(Constants.encodeASCII("line 1\nline 2\r\n")));
  60. }
  61. @Test
  62. public void testCrLfTextCutShort() {
  63. assertFalse(
  64. RawText.isCrLfText(Constants.encodeASCII("line 1\nline 2\r")));
  65. }
  66. @Test
  67. public void testEquals() {
  68. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
  69. final RawText b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n"));
  70. RawTextComparator cmp = RawTextComparator.DEFAULT;
  71. assertEquals(2, a.size());
  72. assertEquals(2, b.size());
  73. // foo-a != foo-b
  74. assertFalse(cmp.equals(a, 0, b, 0));
  75. assertFalse(cmp.equals(b, 0, a, 0));
  76. // foo-b == foo-b
  77. assertTrue(cmp.equals(a, 1, b, 0));
  78. assertTrue(cmp.equals(b, 0, a, 1));
  79. }
  80. @Test
  81. public void testWriteLine1() throws IOException {
  82. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
  83. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  84. a.writeLine(o, 0);
  85. final byte[] r = o.toByteArray();
  86. assertEquals("foo-a", RawParseUtils.decode(r));
  87. }
  88. @Test
  89. public void testWriteLine2() throws IOException {
  90. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b"));
  91. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  92. a.writeLine(o, 1);
  93. final byte[] r = o.toByteArray();
  94. assertEquals("foo-b", RawParseUtils.decode(r));
  95. }
  96. @Test
  97. public void testWriteLine3() throws IOException {
  98. final RawText a = new RawText(Constants.encodeASCII("a\n\nb\n"));
  99. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  100. a.writeLine(o, 1);
  101. final byte[] r = o.toByteArray();
  102. assertEquals("", RawParseUtils.decode(r));
  103. }
  104. @Test
  105. public void testComparatorReduceCommonStartEnd() {
  106. final RawTextComparator c = RawTextComparator.DEFAULT;
  107. Edit e;
  108. e = c.reduceCommonStartEnd(t(""), t(""), new Edit(0, 0, 0, 0));
  109. assertEquals(new Edit(0, 0, 0, 0), e);
  110. e = c.reduceCommonStartEnd(t("a"), t("b"), new Edit(0, 1, 0, 1));
  111. assertEquals(new Edit(0, 1, 0, 1), e);
  112. e = c.reduceCommonStartEnd(t("a"), t("a"), new Edit(0, 1, 0, 1));
  113. assertEquals(new Edit(1, 1, 1, 1), e);
  114. e = c.reduceCommonStartEnd(t("axB"), t("axC"), new Edit(0, 3, 0, 3));
  115. assertEquals(new Edit(2, 3, 2, 3), e);
  116. e = c.reduceCommonStartEnd(t("Bxy"), t("Cxy"), new Edit(0, 3, 0, 3));
  117. assertEquals(new Edit(0, 1, 0, 1), e);
  118. e = c.reduceCommonStartEnd(t("bc"), t("Abc"), new Edit(0, 2, 0, 3));
  119. assertEquals(new Edit(0, 0, 0, 1), e);
  120. e = new Edit(0, 5, 0, 5);
  121. e = c.reduceCommonStartEnd(t("abQxy"), t("abRxy"), e);
  122. assertEquals(new Edit(2, 3, 2, 3), e);
  123. RawText a = new RawText("p\na b\nQ\nc d\n".getBytes(UTF_8));
  124. RawText b = new RawText("p\na b \nR\n c d \n".getBytes(UTF_8));
  125. e = new Edit(0, 4, 0, 4);
  126. e = RawTextComparator.WS_IGNORE_ALL.reduceCommonStartEnd(a, b, e);
  127. assertEquals(new Edit(2, 3, 2, 3), e);
  128. }
  129. @Test
  130. public void testComparatorReduceCommonStartEnd_EmptyLine() {
  131. RawText a;
  132. RawText b;
  133. Edit e;
  134. a = new RawText("R\n y\n".getBytes(UTF_8));
  135. b = new RawText("S\n\n y\n".getBytes(UTF_8));
  136. e = new Edit(0, 2, 0, 3);
  137. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  138. assertEquals(new Edit(0, 1, 0, 2), e);
  139. a = new RawText("S\n\n y\n".getBytes(UTF_8));
  140. b = new RawText("R\n y\n".getBytes(UTF_8));
  141. e = new Edit(0, 3, 0, 2);
  142. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  143. assertEquals(new Edit(0, 2, 0, 1), e);
  144. }
  145. @Test
  146. public void testComparatorReduceCommonStartButLastLineNoEol() {
  147. RawText a;
  148. RawText b;
  149. Edit e;
  150. a = new RawText("start".getBytes(UTF_8));
  151. b = new RawText("start of line".getBytes(UTF_8));
  152. e = new Edit(0, 1, 0, 1);
  153. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  154. assertEquals(new Edit(0, 1, 0, 1), e);
  155. }
  156. @Test
  157. public void testComparatorReduceCommonStartButLastLineNoEol_2() {
  158. RawText a;
  159. RawText b;
  160. Edit e;
  161. a = new RawText("start".getBytes(UTF_8));
  162. b = new RawText("start of\nlastline".getBytes(UTF_8));
  163. e = new Edit(0, 1, 0, 2);
  164. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  165. assertEquals(new Edit(0, 1, 0, 2), e);
  166. }
  167. @Test
  168. public void testLineDelimiter() throws Exception {
  169. RawText rt = new RawText(Constants.encodeASCII("foo\n"));
  170. assertEquals("\n", rt.getLineDelimiter());
  171. assertFalse(rt.isMissingNewlineAtEnd());
  172. rt = new RawText(Constants.encodeASCII("foo\r\n"));
  173. assertEquals("\r\n", rt.getLineDelimiter());
  174. assertFalse(rt.isMissingNewlineAtEnd());
  175. rt = new RawText(Constants.encodeASCII("foo\nbar"));
  176. assertEquals("\n", rt.getLineDelimiter());
  177. assertTrue(rt.isMissingNewlineAtEnd());
  178. rt = new RawText(Constants.encodeASCII("foo\r\nbar"));
  179. assertEquals("\r\n", rt.getLineDelimiter());
  180. assertTrue(rt.isMissingNewlineAtEnd());
  181. rt = new RawText(Constants.encodeASCII("foo\nbar\r\n"));
  182. assertEquals("\n", rt.getLineDelimiter());
  183. assertFalse(rt.isMissingNewlineAtEnd());
  184. rt = new RawText(Constants.encodeASCII("foo\r\nbar\n"));
  185. assertEquals("\r\n", rt.getLineDelimiter());
  186. assertFalse(rt.isMissingNewlineAtEnd());
  187. rt = new RawText(Constants.encodeASCII("foo"));
  188. assertNull(rt.getLineDelimiter());
  189. assertTrue(rt.isMissingNewlineAtEnd());
  190. rt = new RawText(Constants.encodeASCII(""));
  191. assertNull(rt.getLineDelimiter());
  192. assertTrue(rt.isMissingNewlineAtEnd());
  193. rt = new RawText(Constants.encodeASCII("\n"));
  194. assertEquals("\n", rt.getLineDelimiter());
  195. assertFalse(rt.isMissingNewlineAtEnd());
  196. rt = new RawText(Constants.encodeASCII("\r\n"));
  197. assertEquals("\r\n", rt.getLineDelimiter());
  198. assertFalse(rt.isMissingNewlineAtEnd());
  199. }
  200. @Test
  201. public void testLineDelimiter2() throws Exception {
  202. RawText rt = new RawText(Constants.encodeASCII("\nfoo"));
  203. assertEquals("\n", rt.getLineDelimiter());
  204. assertTrue(rt.isMissingNewlineAtEnd());
  205. }
  206. private static RawText t(String text) {
  207. StringBuilder r = new StringBuilder();
  208. for (int i = 0; i < text.length(); i++) {
  209. r.append(text.charAt(i));
  210. r.append('\n');
  211. }
  212. return new RawText(r.toString().getBytes(UTF_8));
  213. }
  214. }