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.

MergeAlgorithmTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.merge;
  44. import java.io.ByteArrayOutputStream;
  45. import java.io.IOException;
  46. import junit.framework.TestCase;
  47. import org.eclipse.jgit.diff.RawText;
  48. import org.eclipse.jgit.lib.Constants;
  49. public class MergeAlgorithmTest extends TestCase {
  50. MergeFormatter fmt=new MergeFormatter();
  51. // the texts which are used in this merge-tests are constructed by
  52. // concatenating fixed chunks of text defined by the String constants
  53. // A..Y. The common base text is always the text A+B+C+D+E+F+G+H+I+J.
  54. // The two texts being merged are constructed by deleting some chunks
  55. // or inserting new chunks. Some of the chunks are one-liners, others
  56. // contain more than one line.
  57. private static final String A = "aaa\n";
  58. private static final String B = "bbbbb\nbb\nbbb\n";
  59. private static final String C = "c\n";
  60. private static final String D = "dd\n";
  61. private static final String E = "ee\n";
  62. private static final String F = "fff\nff\n";
  63. private static final String G = "gg\n";
  64. private static final String H = "h\nhhh\nhh\n";
  65. private static final String I = "iiii\n";
  66. private static final String J = "jj\n";
  67. private static final String Z = "zzz\n";
  68. private static final String Y = "y\n";
  69. // constants which define how conflict-regions are expected to be reported.
  70. private static final String XXX_0 = "<<<<<<< O\n";
  71. private static final String XXX_1 = "=======\n";
  72. private static final String XXX_2 = ">>>>>>> T\n";
  73. // the common base from which all merges texts derive from
  74. String base=A+B+C+D+E+F+G+H+I+J;
  75. // the following constants define the merged texts. The name of the
  76. // constants describe how they are created out of the common base. E.g.
  77. // the constant named replace_XYZ_by_MNO stands for the text which is
  78. // created from common base by replacing first chunk X by chunk M, then
  79. // Y by N and then Z by O.
  80. String replace_C_by_Z=A+B+Z+D+E+F+G+H+I+J;
  81. String replace_A_by_Y=Y+B+C+D+E+F+G+H+I+J;
  82. String replace_A_by_Z=Z+B+C+D+E+F+G+H+I+J;
  83. String replace_J_by_Y=A+B+C+D+E+F+G+H+I+Y;
  84. String replace_J_by_Z=A+B+C+D+E+F+G+H+I+Z;
  85. String replace_BC_by_ZZ=A+Z+Z+D+E+F+G+H+I+J;
  86. String replace_BCD_by_ZZZ=A+Z+Z+Z+E+F+G+H+I+J;
  87. String replace_BD_by_ZZ=A+Z+C+Z+E+F+G+H+I+J;
  88. String replace_BCDEGI_by_ZZZZZZ=A+Z+Z+Z+Z+F+Z+H+Z+J;
  89. String replace_CEFGHJ_by_YYYYYY=A+B+Y+D+Y+Y+Y+Y+I+Y;
  90. String replace_BDE_by_ZZY=A+Z+C+Z+Y+F+G+H+I+J;
  91. /**
  92. * Check for a conflict where the second text was changed similar to the
  93. * first one, but the second texts modification covers one more line.
  94. *
  95. * @throws IOException
  96. */
  97. public void testTwoConflictingModifications() throws IOException {
  98. assertEquals(A + XXX_0 + B + Z + XXX_1 + Z + Z + XXX_2 + D + E + F + G
  99. + H + I + J,
  100. merge(base, replace_C_by_Z, replace_BC_by_ZZ));
  101. }
  102. /**
  103. * Test a case where we have three consecutive chunks. The first text
  104. * modifies all three chunks. The second text modifies the first and the
  105. * last chunk. This should be reported as one conflicting region.
  106. *
  107. * @throws IOException
  108. */
  109. public void testOneAgainstTwoConflictingModifications() throws IOException {
  110. assertEquals(A + XXX_0 + Z + Z + Z + XXX_1 + Z + C + Z + XXX_2 + E + F
  111. + G + H + I + J,
  112. merge(base, replace_BCD_by_ZZZ, replace_BD_by_ZZ));
  113. }
  114. /**
  115. * Test a merge where only the second text contains modifications. Expect as
  116. * merge result the second text.
  117. *
  118. * @throws IOException
  119. */
  120. public void testNoAgainstOneModification() throws IOException {
  121. assertEquals(replace_BD_by_ZZ.toString(),
  122. merge(base, base, replace_BD_by_ZZ));
  123. }
  124. /**
  125. * Both texts contain modifications but not on the same chunks. Expect a
  126. * non-conflict merge result.
  127. *
  128. * @throws IOException
  129. */
  130. public void testTwoNonConflictingModifications() throws IOException {
  131. assertEquals(Y + B + Z + D + E + F + G + H + I + J,
  132. merge(base, replace_C_by_Z, replace_A_by_Y));
  133. }
  134. /**
  135. * Merge two complicated modifications. The merge algorithm has to extend
  136. * and combine conflicting regions to get to the expected merge result.
  137. *
  138. * @throws IOException
  139. */
  140. public void testTwoComplicatedModifications() throws IOException {
  141. assertEquals(A + XXX_0 + Z + Z + Z + Z + F + Z + H + XXX_1 + B + Y + D
  142. + Y + Y + Y + Y + XXX_2 + Z + Y,
  143. merge(base,
  144. replace_BCDEGI_by_ZZZZZZ,
  145. replace_CEFGHJ_by_YYYYYY));
  146. }
  147. /**
  148. * Test a conflicting region at the very start of the text.
  149. *
  150. * @throws IOException
  151. */
  152. public void testConflictAtStart() throws IOException {
  153. assertEquals(XXX_0 + Z + XXX_1 + Y + XXX_2 + B + C + D + E + F + G + H
  154. + I + J, merge(base, replace_A_by_Z, replace_A_by_Y));
  155. }
  156. /**
  157. * Test a conflicting region at the very end of the text.
  158. *
  159. * @throws IOException
  160. */
  161. public void testConflictAtEnd() throws IOException {
  162. assertEquals(A+B+C+D+E+F+G+H+I+XXX_0+Z+XXX_1+Y+XXX_2, merge(base, replace_J_by_Z, replace_J_by_Y));
  163. }
  164. private String merge(String commonBase, String ours, String theirs) throws IOException {
  165. MergeResult r=MergeAlgorithm.merge(new RawText(Constants.encode(commonBase)), new RawText(Constants.encode(ours)), new RawText(Constants.encode(theirs)));
  166. ByteArrayOutputStream bo=new ByteArrayOutputStream(50);
  167. fmt.formatMerge(bo, r, "B", "O", "T", Constants.CHARACTER_ENCODING);
  168. return new String(bo.toByteArray(), Constants.CHARACTER_ENCODING);
  169. }
  170. }