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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.IOException;
  50. import java.io.UnsupportedEncodingException;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.util.RawParseUtils;
  53. import org.junit.Test;
  54. public class RawTextTest {
  55. @Test
  56. public void testEmpty() {
  57. final RawText r = new RawText(new byte[0]);
  58. assertEquals(0, r.size());
  59. }
  60. @Test
  61. public void testEquals() {
  62. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
  63. final RawText b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n"));
  64. RawTextComparator cmp = RawTextComparator.DEFAULT;
  65. assertEquals(2, a.size());
  66. assertEquals(2, b.size());
  67. // foo-a != foo-b
  68. assertFalse(cmp.equals(a, 0, b, 0));
  69. assertFalse(cmp.equals(b, 0, a, 0));
  70. // foo-b == foo-b
  71. assertTrue(cmp.equals(a, 1, b, 0));
  72. assertTrue(cmp.equals(b, 0, a, 1));
  73. }
  74. @Test
  75. public void testWriteLine1() throws IOException {
  76. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
  77. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  78. a.writeLine(o, 0);
  79. final byte[] r = o.toByteArray();
  80. assertEquals("foo-a", RawParseUtils.decode(r));
  81. }
  82. @Test
  83. public void testWriteLine2() throws IOException {
  84. final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b"));
  85. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  86. a.writeLine(o, 1);
  87. final byte[] r = o.toByteArray();
  88. assertEquals("foo-b", RawParseUtils.decode(r));
  89. }
  90. @Test
  91. public void testWriteLine3() throws IOException {
  92. final RawText a = new RawText(Constants.encodeASCII("a\n\nb\n"));
  93. final ByteArrayOutputStream o = new ByteArrayOutputStream();
  94. a.writeLine(o, 1);
  95. final byte[] r = o.toByteArray();
  96. assertEquals("", RawParseUtils.decode(r));
  97. }
  98. @Test
  99. public void testComparatorReduceCommonStartEnd()
  100. throws UnsupportedEncodingException {
  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. throws UnsupportedEncodingException {
  127. RawText a;
  128. RawText b;
  129. Edit e;
  130. a = new RawText("R\n y\n".getBytes("UTF-8"));
  131. b = new RawText("S\n\n y\n".getBytes("UTF-8"));
  132. e = new Edit(0, 2, 0, 3);
  133. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  134. assertEquals(new Edit(0, 1, 0, 2), e);
  135. a = new RawText("S\n\n y\n".getBytes("UTF-8"));
  136. b = new RawText("R\n y\n".getBytes("UTF-8"));
  137. e = new Edit(0, 3, 0, 2);
  138. e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
  139. assertEquals(new Edit(0, 2, 0, 1), e);
  140. }
  141. private static RawText t(String text) {
  142. StringBuilder r = new StringBuilder();
  143. for (int i = 0; i < text.length(); i++) {
  144. r.append(text.charAt(i));
  145. r.append('\n');
  146. }
  147. try {
  148. return new RawText(r.toString().getBytes("UTF-8"));
  149. } catch (UnsupportedEncodingException e) {
  150. throw new RuntimeException(e);
  151. }
  152. }
  153. }