選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

JGitUtilsTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.gitblit.tests;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.util.Date;
  5. import java.util.List;
  6. import junit.framework.TestCase;
  7. import org.eclipse.jgit.lib.Constants;
  8. import org.eclipse.jgit.lib.Repository;
  9. import org.eclipse.jgit.revwalk.RevBlob;
  10. import org.eclipse.jgit.revwalk.RevCommit;
  11. import org.eclipse.jgit.revwalk.RevObject;
  12. import org.eclipse.jgit.revwalk.RevTree;
  13. import org.eclipse.jgit.storage.file.FileRepository;
  14. import com.gitblit.utils.JGitUtils;
  15. import com.gitblit.utils.JGitUtils.DiffOutputType;
  16. import com.gitblit.wicket.models.PathModel.PathChangeModel;
  17. import com.gitblit.wicket.models.RefModel;
  18. import com.gitblit.wicket.models.TicketModel;
  19. public class JGitUtilsTest extends TestCase {
  20. private File repositoriesFolder = new File("c:/projects/git");
  21. private boolean exportAll = true;
  22. private boolean readNested = true;
  23. private List<String> getRepositories() {
  24. return JGitUtils.getRepositoryList(repositoriesFolder, exportAll, readNested);
  25. }
  26. private Repository getRepository() throws Exception {
  27. return new FileRepository(new File(repositoriesFolder, getRepositories().get(0)) + "/" + Constants.DOT_GIT);
  28. }
  29. public void testFindRepositories() {
  30. List<String> list = getRepositories();
  31. assertTrue("No repositories found in " + repositoriesFolder, list.size() > 0);
  32. }
  33. public void testOpenRepository() throws Exception {
  34. Repository r = getRepository();
  35. r.close();
  36. assertTrue("Could not find repository!", r != null);
  37. }
  38. public void testLastChangeRepository() throws Exception {
  39. Repository r = getRepository();
  40. Date date = JGitUtils.getLastChange(r);
  41. r.close();
  42. assertTrue("Could not get last repository change date!", date != null);
  43. }
  44. public void testFirstCommit() throws Exception {
  45. Repository r = getRepository();
  46. RevCommit commit = JGitUtils.getFirstCommit(r, null);
  47. r.close();
  48. assertTrue("Could not get first commit!", commit != null);
  49. System.out.println(commit.getName() + " " + commit.getShortMessage());
  50. }
  51. public void testRetrieveRevObject() throws Exception {
  52. Repository r = getRepository();
  53. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  54. RevTree tree = commit.getTree();
  55. RevObject object = JGitUtils.getRevObject(r, tree, "AUTHORS");
  56. r.close();
  57. assertTrue("Object is null!", object != null);
  58. }
  59. public void testRetrieveStringContent() throws Exception {
  60. Repository r = getRepository();
  61. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  62. RevTree tree = commit.getTree();
  63. RevBlob blob = (RevBlob) JGitUtils.getRevObject(r, tree, "AUTHORS");
  64. String content = JGitUtils.getRawContentAsString(r, blob);
  65. r.close();
  66. assertTrue("Content is null!", content != null);
  67. }
  68. public void testTicGit() throws Exception {
  69. Repository r = new FileRepository(new File(repositoriesFolder, "ticgit") + "/" + Constants.DOT_GIT);
  70. RefModel ticgit = JGitUtils.getTicketsBranch(r);
  71. assertTrue("Ticgit branch does not exist!", ticgit != null);
  72. List<TicketModel> tickets = JGitUtils.getTickets(r);
  73. assertTrue("No tickets found!", tickets.size() > 0);
  74. r.close();
  75. }
  76. public void testFilesInCommit() throws Exception {
  77. Repository r = getRepository();
  78. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  79. List<PathChangeModel> paths = JGitUtils.getFilesInCommit(r, commit);
  80. r.close();
  81. assertTrue("No changed paths found!", paths.size() > 0);
  82. }
  83. public void testCommitDiff() throws Exception {
  84. Repository r = getRepository();
  85. RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
  86. String diff = JGitUtils.getCommitDiff(r, commit, DiffOutputType.PLAIN);
  87. r.close();
  88. System.out.println(diff);
  89. }
  90. public void testZip() throws Exception {
  91. Repository r = new FileRepository(new File(repositoriesFolder, "gitblit.git/" + Constants.DOT_GIT));
  92. FileOutputStream fos = null;
  93. try {
  94. File zipFile = new File("c:/output.zip");
  95. zipFile.delete();
  96. fos = new FileOutputStream(zipFile);
  97. if (JGitUtils.zip(r, "src", Constants.HEAD, fos)) {
  98. System.out.println("zip = " + zipFile.length() + " bytes");
  99. } else {
  100. System.err.println("failed to generate zip file?!");
  101. }
  102. } finally {
  103. if (fos != null) {
  104. try {
  105. fos.close();
  106. } catch (Throwable t) {
  107. }
  108. }
  109. }
  110. }
  111. }