Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BlameGeneratorTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2011, GitHub Inc.
  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.api.blame;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import org.eclipse.jgit.api.Git;
  48. import org.eclipse.jgit.blame.BlameGenerator;
  49. import org.eclipse.jgit.blame.BlameResult;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.revwalk.RevCommit;
  53. import org.junit.Test;
  54. /** Unit tests of {@link BlameGenerator}. */
  55. public class BlameGeneratorTest extends RepositoryTestCase {
  56. @Test
  57. public void testBoundLineDelete() throws Exception {
  58. Git git = new Git(db);
  59. String[] content1 = new String[] { "first", "second" };
  60. writeTrashFile("file.txt", join(content1));
  61. git.add().addFilepattern("file.txt").call();
  62. RevCommit c1 = git.commit().setMessage("create file").call();
  63. String[] content2 = new String[] { "third", "first", "second" };
  64. writeTrashFile("file.txt", join(content2));
  65. git.add().addFilepattern("file.txt").call();
  66. RevCommit c2 = git.commit().setMessage("create file").call();
  67. try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
  68. generator.push(null, db.resolve(Constants.HEAD));
  69. assertEquals(3, generator.getResultContents().size());
  70. assertTrue(generator.next());
  71. assertEquals(c2, generator.getSourceCommit());
  72. assertEquals(1, generator.getRegionLength());
  73. assertEquals(0, generator.getResultStart());
  74. assertEquals(1, generator.getResultEnd());
  75. assertEquals(0, generator.getSourceStart());
  76. assertEquals(1, generator.getSourceEnd());
  77. assertEquals("file.txt", generator.getSourcePath());
  78. assertTrue(generator.next());
  79. assertEquals(c1, generator.getSourceCommit());
  80. assertEquals(2, generator.getRegionLength());
  81. assertEquals(1, generator.getResultStart());
  82. assertEquals(3, generator.getResultEnd());
  83. assertEquals(0, generator.getSourceStart());
  84. assertEquals(2, generator.getSourceEnd());
  85. assertEquals("file.txt", generator.getSourcePath());
  86. assertFalse(generator.next());
  87. }
  88. }
  89. @Test
  90. public void testRenamedBoundLineDelete() throws Exception {
  91. Git git = new Git(db);
  92. final String FILENAME_1 = "subdir/file1.txt";
  93. final String FILENAME_2 = "subdir/file2.txt";
  94. String[] content1 = new String[] { "first", "second" };
  95. writeTrashFile(FILENAME_1, join(content1));
  96. git.add().addFilepattern(FILENAME_1).call();
  97. RevCommit c1 = git.commit().setMessage("create file1").call();
  98. // rename it
  99. writeTrashFile(FILENAME_2, join(content1));
  100. git.add().addFilepattern(FILENAME_2).call();
  101. deleteTrashFile(FILENAME_1);
  102. git.rm().addFilepattern(FILENAME_1).call();
  103. git.commit().setMessage("rename file1.txt to file2.txt").call();
  104. // and change the new file
  105. String[] content2 = new String[] { "third", "first", "second" };
  106. writeTrashFile(FILENAME_2, join(content2));
  107. git.add().addFilepattern(FILENAME_2).call();
  108. RevCommit c2 = git.commit().setMessage("change file2").call();
  109. try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
  110. generator.push(null, db.resolve(Constants.HEAD));
  111. assertEquals(3, generator.getResultContents().size());
  112. assertTrue(generator.next());
  113. assertEquals(c2, generator.getSourceCommit());
  114. assertEquals(1, generator.getRegionLength());
  115. assertEquals(0, generator.getResultStart());
  116. assertEquals(1, generator.getResultEnd());
  117. assertEquals(0, generator.getSourceStart());
  118. assertEquals(1, generator.getSourceEnd());
  119. assertEquals(FILENAME_2, generator.getSourcePath());
  120. assertTrue(generator.next());
  121. assertEquals(c1, generator.getSourceCommit());
  122. assertEquals(2, generator.getRegionLength());
  123. assertEquals(1, generator.getResultStart());
  124. assertEquals(3, generator.getResultEnd());
  125. assertEquals(0, generator.getSourceStart());
  126. assertEquals(2, generator.getSourceEnd());
  127. assertEquals(FILENAME_1, generator.getSourcePath());
  128. assertFalse(generator.next());
  129. }
  130. // and test again with other BlameGenerator API:
  131. try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
  132. generator.push(null, db.resolve(Constants.HEAD));
  133. BlameResult result = generator.computeBlameResult();
  134. assertEquals(3, result.getResultContents().size());
  135. assertEquals(c2, result.getSourceCommit(0));
  136. assertEquals(FILENAME_2, result.getSourcePath(0));
  137. assertEquals(c1, result.getSourceCommit(1));
  138. assertEquals(FILENAME_1, result.getSourcePath(1));
  139. assertEquals(c1, result.getSourceCommit(2));
  140. assertEquals(FILENAME_1, result.getSourcePath(2));
  141. }
  142. }
  143. @Test
  144. public void testLinesAllDeletedShortenedWalk() throws Exception {
  145. Git git = new Git(db);
  146. String[] content1 = new String[] { "first", "second", "third" };
  147. writeTrashFile("file.txt", join(content1));
  148. git.add().addFilepattern("file.txt").call();
  149. git.commit().setMessage("create file").call();
  150. String[] content2 = new String[] { "" };
  151. writeTrashFile("file.txt", join(content2));
  152. git.add().addFilepattern("file.txt").call();
  153. git.commit().setMessage("create file").call();
  154. writeTrashFile("file.txt", join(content1));
  155. git.add().addFilepattern("file.txt").call();
  156. RevCommit c3 = git.commit().setMessage("create file").call();
  157. try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
  158. generator.push(null, db.resolve(Constants.HEAD));
  159. assertEquals(3, generator.getResultContents().size());
  160. assertTrue(generator.next());
  161. assertEquals(c3, generator.getSourceCommit());
  162. assertEquals(0, generator.getResultStart());
  163. assertEquals(3, generator.getResultEnd());
  164. assertFalse(generator.next());
  165. }
  166. }
  167. private static String join(String... lines) {
  168. StringBuilder joined = new StringBuilder();
  169. for (String line : lines)
  170. joined.append(line).append('\n');
  171. return joined.toString();
  172. }
  173. }