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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 java.util.List;
  18. import org.eclipse.jgit.lib.Repository;
  19. import org.eclipse.jgit.revwalk.RevCommit;
  20. import org.junit.Test;
  21. import com.gitblit.models.AnnotatedLine;
  22. import com.gitblit.utils.DiffUtils;
  23. import com.gitblit.utils.DiffUtils.DiffOutputType;
  24. import com.gitblit.utils.JGitUtils;
  25. public class DiffUtilsTest extends GitblitUnitTest {
  26. @Test
  27. public void testDiffOutputTypes() throws Exception {
  28. assertEquals(DiffOutputType.PLAIN, DiffOutputType.forName("plain"));
  29. assertEquals(DiffOutputType.HTML, DiffOutputType.forName("html"));
  30. assertEquals(null, DiffOutputType.forName(null));
  31. }
  32. @Test
  33. public void testParentCommitDiff() throws Exception {
  34. Repository repository = GitBlitSuite.getHelloworldRepository();
  35. RevCommit commit = JGitUtils.getCommit(repository,
  36. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  37. String diff = DiffUtils.getCommitDiff(repository, commit, DiffOutputType.PLAIN).content;
  38. repository.close();
  39. assertTrue(diff != null && diff.length() > 0);
  40. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  41. assertTrue(diff.indexOf(expected) > -1);
  42. }
  43. @Test
  44. public void testArbitraryCommitDiff() throws Exception {
  45. Repository repository = GitBlitSuite.getHelloworldRepository();
  46. RevCommit baseCommit = JGitUtils.getCommit(repository,
  47. "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
  48. RevCommit commit = JGitUtils.getCommit(repository,
  49. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  50. String diff = DiffUtils.getDiff(repository, baseCommit, commit, DiffOutputType.PLAIN).content;
  51. repository.close();
  52. assertTrue(diff != null && diff.length() > 0);
  53. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  54. assertTrue(diff.indexOf(expected) > -1);
  55. }
  56. @Test
  57. public void testPlainFileDiff() throws Exception {
  58. Repository repository = GitBlitSuite.getHelloworldRepository();
  59. RevCommit commit = JGitUtils.getCommit(repository,
  60. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  61. String diff = DiffUtils.getDiff(repository, commit, "java.java", DiffOutputType.PLAIN).content;
  62. repository.close();
  63. assertTrue(diff != null && diff.length() > 0);
  64. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  65. assertTrue(diff.indexOf(expected) > -1);
  66. }
  67. @Test
  68. public void testFilePatch() throws Exception {
  69. Repository repository = GitBlitSuite.getHelloworldRepository();
  70. RevCommit commit = JGitUtils.getCommit(repository,
  71. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  72. String patch = DiffUtils.getCommitPatch(repository, null, commit, "java.java");
  73. repository.close();
  74. assertTrue(patch != null && patch.length() > 0);
  75. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  76. assertTrue(patch.indexOf(expected) > -1);
  77. }
  78. @Test
  79. public void testArbitraryFilePatch() throws Exception {
  80. Repository repository = GitBlitSuite.getHelloworldRepository();
  81. RevCommit baseCommit = JGitUtils.getCommit(repository,
  82. "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
  83. RevCommit commit = JGitUtils.getCommit(repository,
  84. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  85. String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, "java.java");
  86. repository.close();
  87. assertTrue(patch != null && patch.length() > 0);
  88. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  89. assertTrue(patch.indexOf(expected) > -1);
  90. }
  91. @Test
  92. public void testArbitraryCommitPatch() throws Exception {
  93. Repository repository = GitBlitSuite.getHelloworldRepository();
  94. RevCommit baseCommit = JGitUtils.getCommit(repository,
  95. "8baf6a833b5579384d9b9ceb8a16b5d0ea2ec4ca");
  96. RevCommit commit = JGitUtils.getCommit(repository,
  97. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  98. String patch = DiffUtils.getCommitPatch(repository, baseCommit, commit, null);
  99. repository.close();
  100. assertTrue(patch != null && patch.length() > 0);
  101. String expected = "- system.out.println(\"Hello World\");\n+ System.out.println(\"Hello World\"";
  102. assertTrue(patch.indexOf(expected) > -1);
  103. }
  104. @Test
  105. public void testBlame() throws Exception {
  106. Repository repository = GitBlitSuite.getHelloworldRepository();
  107. List<AnnotatedLine> lines = DiffUtils.blame(repository, "java.java",
  108. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  109. repository.close();
  110. assertTrue(lines.size() > 0);
  111. assertEquals("c6d31dccf5cc75e8e46299fc62d38f60ec6d41e0", lines.get(0).commitId);
  112. }
  113. }