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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.diff;
  45. import static java.nio.charset.StandardCharsets.UTF_8;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertNull;
  49. import static org.junit.Assert.assertTrue;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.IOException;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.util.RawParseUtils;
  54. import org.junit.Test;
  55. public class RawTextTest {
  56. @Test
  57. public void testEmpty() {
  58. final RawText r = new RawText(new byte[0]);
  59. assertEquals(0, r.size());
  60. }
  61. @Test
  62. public void testEquals() {
  63. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
  64. final RawText b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n"));
  65. RawTextComparator cmp = RawTextComparator.DEFAULT;
  66. assertEquals(2, a.size());
  67. assertEquals(2, b.size());
  68. // foo-a != foo-b
  69. assertFalse(cmp.equals(a, 0, b, 0));
  70. assertFalse(cmp.equals(b, 0, a, 0));
  71. // foo-b == foo-b
  72. assertTrue(cmp.equals(a, 1, b, 0));
  73. assertTrue(cmp.equals(b, 0, a, 1));
  74. }
  75. @Test
  76. public void testWriteLine1() throws IOException {
  77. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
  78. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  79. a.writeLine(o, 0);
  80. final byte[] r = o.toByteArray();
  81. assertEquals("foo-a", RawParseUtils.decode(r));
  82. }
  83. @Test
  84. public void testWriteLine2() throws IOException {
  85. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b"));
  86. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  87. a.writeLine(o, 1);
  88. final byte[] r = o.toByteArray();
  89. assertEquals("foo-b", RawParseUtils.decode(r));
  90. }
  91. @Test
  92. public void testWriteLine3() throws IOException {
  93. final RawText a = new RawText(Constants.encodeASCII("a\n\nb\n"));
  94. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  95. a.writeLine(o, 1);
  96. final byte[] r = o.toByteArray();
  97. assertEquals("", RawParseUtils.decode(r));
  98. }
  99. @Test
  100. public void testComparatorReduceCommonStartEnd() {
  101. final RawTextComparator c = RawTextComparator.DEFAULT;
  102. Edit e;
  103. e = c.reduceCommonStartEnd(t(""), t(""), new Edit(0, 0, 0, 0));
  104. assertEquals(new Edit(0, 0, 0, 0), e);
  105. e = c.reduceCommonStartEnd(t("a"), t("b"), new Edit(0, 1, 0, 1));
  106. assertEquals(new Edit(0, 1, 0, 1), e);
  107. e = c.reduceCommonStartEnd(t("a"), t("a"), new Edit(0, 1, 0, 1));
  108. assertEquals(new Edit(1, 1, 1, 1), e);
  109. e = c.reduceCommonStartEnd(t("axB"), t("axC"), new Edit(0, 3, 0, 3));
  110. assertEquals(new Edit(2, 3, 2, 3), e);
  111. e = c.reduceCommonStartEnd(t("Bxy"), t("Cxy"), new Edit(0, 3, 0, 3));
  112. assertEquals(new Edit(0, 1, 0, 1), e);
  113. e = c.reduceCommonStartEnd(t("bc"), t("Abc"), new Edit(0, 2, 0, 3));
  114. assertEquals(new Edit(0, 0, 0, 1), e);
  115. e = new Edit(0, 5, 0, 5);
  116. e = c.reduceCommonStartEnd(t("abQxy"), t("abRxy"), e);
  117. assertEquals(new Edit(2, 3, 2, 3), e);
  118. RawText a = new RawText("p\na b\nQ\nc d\n".getBytes(UTF_8));
  119. RawText b = new RawText("p\na b \nR\n c d \n".getBytes(UTF_8));
  120. e = new Edit(0, 4, 0, 4);
  121. e = RawTextComparator.WS_IGNORE_ALL.reduceCommonStartEnd(a, b, e);
  122. assertEquals(new Edit(2, 3, 2, 3), e);
  123. }
  124. @Test
  125. public void testComparatorReduceCommonStartEnd_EmptyLine() {
  126. RawText a;
  127. RawText b;
  128. Edit e;
  129. a = new RawText("R\n y\n".getBytes(UTF_8));
  130. b = new RawText("S\n\n y\n".getBytes(UTF_8));
  131. e = new Edit(0, 2, 0, 3);
  132. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  133. assertEquals(new Edit(0, 1, 0, 2), e);
  134. a = new RawText("S\n\n y\n".getBytes(UTF_8));
  135. b = new RawText("R\n y\n".getBytes(UTF_8));
  136. e = new Edit(0, 3, 0, 2);
  137. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  138. assertEquals(new Edit(0, 2, 0, 1), e);
  139. }
  140. @Test
  141. public void testComparatorReduceCommonStartButLastLineNoEol() {
  142. RawText a;
  143. RawText b;
  144. Edit e;
  145. a = new RawText("start".getBytes(UTF_8));
  146. b = new RawText("start of line".getBytes(UTF_8));
  147. e = new Edit(0, 1, 0, 1);
  148. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  149. assertEquals(new Edit(0, 1, 0, 1), e);
  150. }
  151. @Test
  152. public void testComparatorReduceCommonStartButLastLineNoEol_2() {
  153. RawText a;
  154. RawText b;
  155. Edit e;
  156. a = new RawText("start".getBytes(UTF_8));
  157. b = new RawText("start of\nlastline".getBytes(UTF_8));
  158. e = new Edit(0, 1, 0, 2);
  159. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  160. assertEquals(new Edit(0, 1, 0, 2), e);
  161. }
  162. @Test
  163. public void testLineDelimiter() throws Exception {
  164. RawText rt = new RawText(Constants.encodeASCII("foo\n"));
  165. assertEquals("\n", rt.getLineDelimiter());
  166. assertFalse(rt.isMissingNewlineAtEnd());
  167. rt = new RawText(Constants.encodeASCII("foo\r\n"));
  168. assertEquals("\r\n", rt.getLineDelimiter());
  169. assertFalse(rt.isMissingNewlineAtEnd());
  170. rt = new RawText(Constants.encodeASCII("foo\nbar"));
  171. assertEquals("\n", rt.getLineDelimiter());
  172. assertTrue(rt.isMissingNewlineAtEnd());
  173. rt = new RawText(Constants.encodeASCII("foo\r\nbar"));
  174. assertEquals("\r\n", rt.getLineDelimiter());
  175. assertTrue(rt.isMissingNewlineAtEnd());
  176. rt = new RawText(Constants.encodeASCII("foo\nbar\r\n"));
  177. assertEquals("\n", rt.getLineDelimiter());
  178. assertFalse(rt.isMissingNewlineAtEnd());
  179. rt = new RawText(Constants.encodeASCII("foo\r\nbar\n"));
  180. assertEquals("\r\n", rt.getLineDelimiter());
  181. assertFalse(rt.isMissingNewlineAtEnd());
  182. rt = new RawText(Constants.encodeASCII("foo"));
  183. assertNull(rt.getLineDelimiter());
  184. assertTrue(rt.isMissingNewlineAtEnd());
  185. rt = new RawText(Constants.encodeASCII(""));
  186. assertNull(rt.getLineDelimiter());
  187. assertTrue(rt.isMissingNewlineAtEnd());
  188. rt = new RawText(Constants.encodeASCII("\n"));
  189. assertEquals("\n", rt.getLineDelimiter());
  190. assertFalse(rt.isMissingNewlineAtEnd());
  191. rt = new RawText(Constants.encodeASCII("\r\n"));
  192. assertEquals("\r\n", rt.getLineDelimiter());
  193. assertFalse(rt.isMissingNewlineAtEnd());
  194. }
  195. @Test
  196. public void testLineDelimiter2() throws Exception {
  197. RawText rt = new RawText(Constants.encodeASCII("\nfoo"));
  198. assertEquals("\n", rt.getLineDelimiter());
  199. assertTrue(rt.isMissingNewlineAtEnd());
  200. }
  201. private static RawText t(String text) {
  202. StringBuilder r = new StringBuilder();
  203. for (int i = 0; i < text.length(); i++) {
  204. r.append(text.charAt(i));
  205. r.append('\n');
  206. }
  207. return new RawText(r.toString().getBytes(UTF_8));
  208. }
  209. }