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.

JGitUtilsTest.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.io.File;
  18. import java.io.FileOutputStream;
  19. import java.util.Date;
  20. import java.util.List;
  21. import junit.framework.TestCase;
  22. import org.eclipse.jgit.lib.Constants;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.revwalk.RevBlob;
  25. import org.eclipse.jgit.revwalk.RevCommit;
  26. import org.eclipse.jgit.revwalk.RevObject;
  27. import org.eclipse.jgit.revwalk.RevTree;
  28. import com.gitblit.GitBlit;
  29. import com.gitblit.models.Metric;
  30. import com.gitblit.models.PathModel.PathChangeModel;
  31. import com.gitblit.models.RefModel;
  32. import com.gitblit.models.TicketModel;
  33. import com.gitblit.models.TicketModel.Comment;
  34. import com.gitblit.utils.JGitUtils;
  35. import com.gitblit.utils.MetricUtils;
  36. public class JGitUtilsTest extends TestCase {
  37. public void testFindRepositories() {
  38. List<String> list = JGitUtils.getRepositoryList(null, true, true);
  39. assertTrue(list.size() == 0);
  40. list.addAll(JGitUtils.getRepositoryList(new File("DoesNotExist"), true, true));
  41. assertTrue(list.size() == 0);
  42. list.addAll(JGitUtils.getRepositoryList(GitBlitSuite.REPOSITORIES, true, true));
  43. assertTrue("No repositories found in " + GitBlitSuite.REPOSITORIES, list.size() > 0);
  44. }
  45. public void testOpenRepository() throws Exception {
  46. Repository repository = GitBlitSuite.getHelloworldRepository();
  47. repository.close();
  48. assertTrue("Could not find repository!", repository != null);
  49. }
  50. public void testFirstCommit() throws Exception {
  51. assertTrue(JGitUtils.getFirstChange(null, null).equals(new Date(0)));
  52. Repository repository = GitBlitSuite.getHelloworldRepository();
  53. RevCommit commit = JGitUtils.getFirstCommit(repository, null);
  54. Date firstChange = JGitUtils.getFirstChange(repository, null);
  55. repository.close();
  56. assertTrue("Could not get first commit!", commit != null);
  57. assertTrue("Incorrect first commit!",
  58. commit.getName().equals("f554664a346629dc2b839f7292d06bad2db4aece"));
  59. assertTrue(firstChange.equals(new Date(commit.getCommitTime() * 1000L)));
  60. }
  61. public void testLastCommit() throws Exception {
  62. assertTrue(JGitUtils.getLastChange(null).equals(new Date(0)));
  63. Repository repository = GitBlitSuite.getHelloworldRepository();
  64. assertTrue(JGitUtils.getCommit(repository, null) != null);
  65. Date date = JGitUtils.getLastChange(repository);
  66. repository.close();
  67. assertTrue("Could not get last repository change date!", date != null);
  68. }
  69. public void testCreateRepository() throws Exception {
  70. String[] repositories = { "NewTestRepository.git", "NewTestRepository" };
  71. for (String repositoryName : repositories) {
  72. boolean isBare = repositoryName.endsWith(".git");
  73. Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,
  74. repositoryName, isBare);
  75. File folder;
  76. if (isBare) {
  77. folder = new File(GitBlitSuite.REPOSITORIES, repositoryName);
  78. } else {
  79. folder = new File(GitBlitSuite.REPOSITORIES, repositoryName + "/.git");
  80. }
  81. assertTrue(repository != null);
  82. assertFalse(JGitUtils.hasCommits(repository));
  83. assertTrue(JGitUtils.getFirstCommit(repository, null) == null);
  84. assertTrue(JGitUtils.getFirstChange(repository, null).getTime() == folder
  85. .lastModified());
  86. assertTrue(JGitUtils.getLastChange(repository).getTime() == folder
  87. .lastModified());
  88. assertTrue(JGitUtils.getCommit(repository, null) == null);
  89. repository.close();
  90. assertTrue(GitBlit.self().deleteRepository(repositoryName));
  91. }
  92. }
  93. public void testRefs() throws Exception {
  94. Repository repository = GitBlitSuite.getTicgitRepository();
  95. for (RefModel model : JGitUtils.getLocalBranches(repository, -1)) {
  96. assertTrue(model.getName().startsWith(Constants.R_HEADS));
  97. assertTrue(model.equals(model));
  98. assertFalse(model.equals(""));
  99. assertTrue(model.hashCode() == model.getCommitId().hashCode()
  100. + model.getName().hashCode());
  101. assertTrue(model.getShortLog().equals(model.commit.getShortMessage()));
  102. }
  103. for (RefModel model : JGitUtils.getRemoteBranches(repository, -1)) {
  104. assertTrue(model.getName().startsWith(Constants.R_REMOTES));
  105. assertTrue(model.equals(model));
  106. assertFalse(model.equals(""));
  107. assertTrue(model.hashCode() == model.getCommitId().hashCode()
  108. + model.getName().hashCode());
  109. assertTrue(model.getShortLog().equals(model.commit.getShortMessage()));
  110. }
  111. for (RefModel model : JGitUtils.getTags(repository, -1)) {
  112. if (model.getObjectId().getName().equals("283035e4848054ff1803cb0e690270787dc92399")) {
  113. assertTrue("Not an annotated tag!", model.isAnnotatedTag());
  114. }
  115. assertTrue(model.getName().startsWith(Constants.R_TAGS));
  116. assertTrue(model.equals(model));
  117. assertFalse(model.equals(""));
  118. assertTrue(model.hashCode() == model.getCommitId().hashCode()
  119. + model.getName().hashCode());
  120. assertTrue(model.getShortLog().equals(model.commit.getShortMessage()));
  121. }
  122. repository.close();
  123. }
  124. public void testRetrieveRevObject() throws Exception {
  125. Repository repository = GitBlitSuite.getHelloworldRepository();
  126. RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
  127. RevTree tree = commit.getTree();
  128. RevObject object = JGitUtils.getRevObject(repository, tree, "java.java");
  129. repository.close();
  130. assertTrue("Object is null!", object != null);
  131. }
  132. public void testRetrieveStringContent() throws Exception {
  133. Repository repository = GitBlitSuite.getHelloworldRepository();
  134. RevCommit commit = JGitUtils.getCommit(repository, Constants.HEAD);
  135. RevTree tree = commit.getTree();
  136. RevBlob blob = (RevBlob) JGitUtils.getRevObject(repository, tree, "java.java");
  137. String content = JGitUtils.getRawContentAsString(repository, blob);
  138. repository.close();
  139. assertTrue("Content is null!", content != null && content.length() > 0);
  140. }
  141. public void testFilesInCommit() throws Exception {
  142. Repository repository = GitBlitSuite.getHelloworldRepository();
  143. RevCommit commit = JGitUtils.getCommit(repository,
  144. "1d0c2933a4ae69c362f76797d42d6bd182d05176");
  145. List<PathChangeModel> paths = JGitUtils.getFilesInCommit(repository, commit);
  146. repository.close();
  147. assertTrue("No changed paths found!", paths.size() == 1);
  148. for (PathChangeModel path : paths) {
  149. assertTrue("PathChangeModel hashcode incorrect!",
  150. path.hashCode() == (path.commitId.hashCode() + path.path.hashCode()));
  151. assertTrue("PathChangeModel equals itself failed!", path.equals(path));
  152. assertFalse("PathChangeModel equals string failed!", path.equals(""));
  153. }
  154. }
  155. public void testZip() throws Exception {
  156. Repository repository = GitBlitSuite.getHelloworldRepository();
  157. File zipFile = new File(GitBlitSuite.REPOSITORIES, "helloworld.zip");
  158. FileOutputStream fos = new FileOutputStream(zipFile);
  159. boolean success = JGitUtils.zip(repository, null, Constants.HEAD, fos);
  160. assertTrue("Failed to generate zip file!", success);
  161. assertTrue(zipFile.length() > 0);
  162. fos.close();
  163. zipFile.delete();
  164. repository.close();
  165. }
  166. public void testMetrics() throws Exception {
  167. Repository repository = GitBlitSuite.getHelloworldRepository();
  168. List<Metric> metrics = MetricUtils.getDateMetrics(repository, true);
  169. repository.close();
  170. assertTrue("No metrics found!", metrics.size() > 0);
  171. }
  172. public void testTicGit() throws Exception {
  173. Repository repository = GitBlitSuite.getTicgitRepository();
  174. RefModel branch = JGitUtils.getTicketsBranch(repository);
  175. assertTrue("Ticgit branch does not exist!", branch != null);
  176. List<TicketModel> ticketsA = JGitUtils.getTickets(repository);
  177. List<TicketModel> ticketsB = JGitUtils.getTickets(repository);
  178. repository.close();
  179. assertTrue("No tickets found!", ticketsA.size() > 0);
  180. for (int i = 0; i < ticketsA.size(); i++) {
  181. TicketModel ticketA = ticketsA.get(i);
  182. TicketModel ticketB = ticketsB.get(i);
  183. assertTrue("Tickets are not equal!", ticketA.equals(ticketB));
  184. assertFalse(ticketA.equals(""));
  185. assertTrue(ticketA.hashCode() == ticketA.id.hashCode());
  186. for (int j = 0; j < ticketA.comments.size(); j++) {
  187. Comment commentA = ticketA.comments.get(j);
  188. Comment commentB = ticketB.comments.get(j);
  189. assertTrue("Comments are not equal!", commentA.equals(commentB));
  190. assertFalse(commentA.equals(""));
  191. assertTrue(commentA.hashCode() == commentA.text.hashCode());
  192. }
  193. }
  194. }
  195. }