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

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