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

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