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.

RenameDetectorTest.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2010, Google 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.diff;
  44. import java.util.List;
  45. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  46. import org.eclipse.jgit.junit.TestRepository;
  47. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  48. import org.eclipse.jgit.lib.FileMode;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.RepositoryTestCase;
  51. public class RenameDetectorTest extends RepositoryTestCase {
  52. private static final String PATH_A = "src/A";
  53. private static final String PATH_H = "src/H";
  54. private static final String PATH_Q = "src/Q";
  55. private RenameDetector rd;
  56. private TestRepository testDb;
  57. @Override
  58. public void setUp() throws Exception {
  59. super.setUp();
  60. testDb = new TestRepository(db);
  61. rd = new RenameDetector(db);
  62. }
  63. public void testExactRename_OneRename() throws Exception {
  64. ObjectId foo = blob("foo");
  65. DiffEntry a = DiffEntry.add(PATH_A, foo);
  66. DiffEntry b = DiffEntry.delete(PATH_Q, foo);
  67. rd.add(a);
  68. rd.add(b);
  69. List<DiffEntry> entries = rd.compute();
  70. assertEquals(1, entries.size());
  71. assertRename(b, a, 100, entries.get(0));
  72. }
  73. public void testExactRename_OneRenameOneModify() throws Exception {
  74. ObjectId foo = blob("foo");
  75. ObjectId bar = blob("bar");
  76. DiffEntry a = DiffEntry.add(PATH_A, foo);
  77. DiffEntry b = DiffEntry.delete(PATH_Q, foo);
  78. DiffEntry c = DiffEntry.modify(PATH_H);
  79. c.newId = c.oldId = AbbreviatedObjectId.fromObjectId(bar);
  80. rd.add(a);
  81. rd.add(b);
  82. rd.add(c);
  83. List<DiffEntry> entries = rd.compute();
  84. assertEquals(2, entries.size());
  85. assertRename(b, a, 100, entries.get(0));
  86. assertSame(c, entries.get(1));
  87. }
  88. public void testExactRename_ManyRenames() throws Exception {
  89. ObjectId foo = blob("foo");
  90. ObjectId bar = blob("bar");
  91. DiffEntry a = DiffEntry.add(PATH_A, foo);
  92. DiffEntry b = DiffEntry.delete(PATH_Q, foo);
  93. DiffEntry c = DiffEntry.add("README", bar);
  94. DiffEntry d = DiffEntry.delete("REEDME", bar);
  95. rd.add(a);
  96. rd.add(b);
  97. rd.add(c);
  98. rd.add(d);
  99. List<DiffEntry> entries = rd.compute();
  100. assertEquals(2, entries.size());
  101. assertRename(d, c, 100, entries.get(0));
  102. assertRename(b, a, 100, entries.get(1));
  103. }
  104. public void testInexactRename_OnePair() throws Exception {
  105. ObjectId aId = blob("foo\nbar\nbaz\n");
  106. ObjectId bId = blob("foo\nbar\nblah\n");
  107. DiffEntry a = DiffEntry.add(PATH_A, aId);
  108. DiffEntry b = DiffEntry.delete(PATH_Q, bId);
  109. rd.add(a);
  110. rd.add(b);
  111. List<DiffEntry> entries = rd.compute();
  112. assertEquals(1, entries.size());
  113. assertRename(b, a, 61, entries.get(0));
  114. }
  115. public void testInexactRename_OneRenameTwoUnrelatedFiles() throws Exception {
  116. ObjectId aId = blob("foo\nbar\nbaz\n");
  117. ObjectId bId = blob("foo\nbar\nblah\n");
  118. DiffEntry a = DiffEntry.add(PATH_A, aId);
  119. DiffEntry b = DiffEntry.delete(PATH_Q, bId);
  120. ObjectId cId = blob("some\nsort\nof\ntext\n");
  121. ObjectId dId = blob("completely\nunrelated\ntext\n");
  122. DiffEntry c = DiffEntry.add("c", cId);
  123. DiffEntry d = DiffEntry.delete("d", dId);
  124. rd.add(a);
  125. rd.add(b);
  126. rd.add(c);
  127. rd.add(d);
  128. List<DiffEntry> entries = rd.compute();
  129. assertEquals(3, entries.size());
  130. assertSame(c, entries.get(0));
  131. assertSame(d, entries.get(1));
  132. assertRename(b, a, 61, entries.get(2));
  133. }
  134. public void testInexactRename_LastByteDifferent() throws Exception {
  135. ObjectId aId = blob("foo\nbar\na");
  136. ObjectId bId = blob("foo\nbar\nb");
  137. DiffEntry a = DiffEntry.add(PATH_A, aId);
  138. DiffEntry b = DiffEntry.delete(PATH_Q, bId);
  139. rd.add(a);
  140. rd.add(b);
  141. List<DiffEntry> entries = rd.compute();
  142. assertEquals(1, entries.size());
  143. assertRename(b, a, 88, entries.get(0));
  144. }
  145. public void testInexactRenames_OnePair2() throws Exception {
  146. ObjectId aId = blob("ab\nab\nab\nac\nad\nae\n");
  147. ObjectId bId = blob("ac\nab\nab\nab\naa\na0\na1\n");
  148. DiffEntry a = DiffEntry.add(PATH_A, aId);
  149. DiffEntry b = DiffEntry.delete(PATH_Q, bId);
  150. rd.add(a);
  151. rd.add(b);
  152. rd.setRenameScore(50);
  153. List<DiffEntry> entries = rd.compute();
  154. assertEquals(1, entries.size());
  155. assertRename(b, a, 57, entries.get(0));
  156. }
  157. public void testNoRenames_SingleByteFiles() throws Exception {
  158. ObjectId aId = blob("a");
  159. ObjectId bId = blob("b");
  160. DiffEntry a = DiffEntry.add(PATH_A, aId);
  161. DiffEntry b = DiffEntry.delete(PATH_Q, bId);
  162. rd.add(a);
  163. rd.add(b);
  164. List<DiffEntry> entries = rd.compute();
  165. assertEquals(2, entries.size());
  166. assertSame(a, entries.get(0));
  167. assertSame(b, entries.get(1));
  168. }
  169. public void testNoRenames_EmptyFile1() throws Exception {
  170. ObjectId aId = blob("");
  171. DiffEntry a = DiffEntry.add(PATH_A, aId);
  172. rd.add(a);
  173. List<DiffEntry> entries = rd.compute();
  174. assertEquals(1, entries.size());
  175. assertSame(a, entries.get(0));
  176. }
  177. public void testNoRenames_EmptyFile2() throws Exception {
  178. ObjectId aId = blob("");
  179. ObjectId bId = blob("blah");
  180. DiffEntry a = DiffEntry.add(PATH_A, aId);
  181. DiffEntry b = DiffEntry.delete(PATH_Q, bId);
  182. rd.add(a);
  183. rd.add(b);
  184. List<DiffEntry> entries = rd.compute();
  185. assertEquals(2, entries.size());
  186. assertSame(a, entries.get(0));
  187. assertSame(b, entries.get(1));
  188. }
  189. public void testNoRenames_SymlinkAndFile() throws Exception {
  190. ObjectId aId = blob("src/dest");
  191. DiffEntry a = DiffEntry.add(PATH_A, aId);
  192. DiffEntry b = DiffEntry.delete(PATH_Q, aId);
  193. b.oldMode = FileMode.SYMLINK;
  194. rd.add(a);
  195. rd.add(b);
  196. List<DiffEntry> entries = rd.compute();
  197. assertEquals(2, entries.size());
  198. assertSame(a, entries.get(0));
  199. assertSame(b, entries.get(1));
  200. }
  201. public void testNoRenames_GitlinkAndFile() throws Exception {
  202. ObjectId aId = blob("src/dest");
  203. DiffEntry a = DiffEntry.add(PATH_A, aId);
  204. DiffEntry b = DiffEntry.delete(PATH_Q, aId);
  205. b.oldMode = FileMode.GITLINK;
  206. rd.add(a);
  207. rd.add(b);
  208. List<DiffEntry> entries = rd.compute();
  209. assertEquals(2, entries.size());
  210. assertSame(a, entries.get(0));
  211. assertSame(b, entries.get(1));
  212. }
  213. private ObjectId blob(String content) throws Exception {
  214. return testDb.blob(content).copy();
  215. }
  216. private static void assertRename(DiffEntry o, DiffEntry n, int score,
  217. DiffEntry rename) {
  218. assertEquals(ChangeType.RENAME, rename.getChangeType());
  219. assertEquals(o.getOldName(), rename.getOldName());
  220. assertEquals(n.getNewName(), rename.getNewName());
  221. assertEquals(o.getOldMode(), rename.getOldMode());
  222. assertEquals(n.getNewMode(), rename.getNewMode());
  223. assertEquals(o.getOldId(), rename.getOldId());
  224. assertEquals(n.getNewId(), rename.getNewId());
  225. assertEquals(score, rename.getScore());
  226. }
  227. }