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.

DiffUtilsTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tests;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.util.List;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.revwalk.RevCommit;
  22. import org.junit.Test;
  23. import com.gitblit.models.AnnotatedLine;
  24. import com.gitblit.utils.DiffUtils;
  25. import com.gitblit.utils.DiffUtils.DiffOutputType;
  26. import com.gitblit.utils.JGitUtils;
  27. public class DiffUtilsTest {
  28. @Test
  29. public void testDiffOutputTypes() throws Exception {
  30. assertEquals(DiffOutputType.PLAIN, DiffOutputType.forName("plain"));
  31. assertEquals(DiffOutputType.GITWEB, DiffOutputType.forName("gitweb"));
  32. assertEquals(DiffOutputType.GITBLIT, DiffOutputType.forName("gitblit"));
  33. assertEquals(null, DiffOutputType.forName(null));
  34. }
  35. @Test
  36. public void testParentCommitDiff() throws Exception {
  37. Repository repository = GitBlitSuite.getHelloworldRepository();
  38. RevCommit commit = JGitUtils.getCommit(repository,
  39. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  40. String diff = DiffUtils.getCommitDiff(repository, commit, DiffOutputType.PLAIN);
  41. repository.close();
  42. assertTrue(diff != null && diff.length() > 0);
  43. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  44. assertTrue(diff.indexOf(expected) > -1);
  45. }
  46. @Test
  47. public void testArbitraryCommitDiff() throws Exception {
  48. Repository repository = GitBlitSuite.getHelloworldRepository();
  49. RevCommit baseCommit = JGitUtils.getCommit(repository,
  50. "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
  51. RevCommit commit = JGitUtils.getCommit(repository,
  52. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  53. String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffOutputType.PLAIN);
  54. repository.close();
  55. assertTrue(diff != null && diff.length() > 0);
  56. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  57. assertTrue(diff.indexOf(expected) > -1);
  58. }
  59. @Test
  60. public void testPlainFileDiff() throws Exception {
  61. Repository repository = GitBlitSuite.getHelloworldRepository();
  62. RevCommit commit = JGitUtils.getCommit(repository,
  63. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  64. String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffOutputType.PLAIN);
  65. repository.close();
  66. assertTrue(diff != null && diff.length() > 0);
  67. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  68. assertTrue(diff.indexOf(expected) > -1);
  69. }
  70. @Test
  71. public void testFilePatch() throws Exception {
  72. Repository repository = GitBlitSuite.getHelloworldRepository();
  73. RevCommit commit = JGitUtils.getCommit(repository,
  74. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  75. String patch = DiffUtils.getCommitPatch(repository, null, commit, "java.java");
  76. repository.close();
  77. assertTrue(patch != null && patch.length() > 0);
  78. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  79. assertTrue(patch.indexOf(expected) > -1);
  80. }
  81. @Test
  82. public void testArbitraryFilePatch() throws Exception {
  83. Repository repository = GitBlitSuite.getHelloworldRepository();
  84. RevCommit baseCommit = JGitUtils.getCommit(repository,
  85. "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
  86. RevCommit commit = JGitUtils.getCommit(repository,
  87. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  88. String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, "java.java");
  89. repository.close();
  90. assertTrue(patch != null && patch.length() > 0);
  91. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  92. assertTrue(patch.indexOf(expected) > -1);
  93. }
  94. @Test
  95. public void testArbitraryCommitPatch() throws Exception {
  96. Repository repository = GitBlitSuite.getHelloworldRepository();
  97. RevCommit baseCommit = JGitUtils.getCommit(repository,
  98. "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
  99. RevCommit commit = JGitUtils.getCommit(repository,
  100. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  101. String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, null);
  102. repository.close();
  103. assertTrue(patch != null && patch.length() > 0);
  104. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  105. assertTrue(patch.indexOf(expected) > -1);
  106. }
  107. @Test
  108. public void testBlame() throws Exception {
  109. Repository repository = GitBlitSuite.getHelloworldRepository();
  110. List<AnnotatedLine> lines = DiffUtils.blame(repository, "java.java",
  111. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  112. repository.close();
  113. assertTrue(lines.size() > 0);
  114. assertEquals("c6d31dccf5cc75e8e46299fc62d38f60ec6d41e0", lines.get(0).commitId);
  115. }
  116. }