PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/com/atlassian/bamboo/plugins/git/ObjectCacheLocalTest.java

https://bitbucket.org/atlassian/bamboo-git-plugin
Java | 145 lines | 114 code | 29 blank | 2 comment | 2 complexity | e8cb7ced4eeab409f54b2baf1a835bfe MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.bamboo.plugins.git;
  2. import com.atlassian.bamboo.repository.RepositoryException;
  3. import org.apache.commons.io.FileUtils;
  4. import org.eclipse.jgit.api.Git;
  5. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  6. import org.eclipse.jgit.api.errors.NoFilepatternException;
  7. import org.eclipse.jgit.api.errors.NoHeadException;
  8. import org.eclipse.jgit.api.errors.NoMessageException;
  9. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  10. import org.eclipse.jgit.lib.Constants;
  11. import org.eclipse.jgit.storage.file.FileRepository;
  12. import org.testng.Assert;
  13. import org.testng.annotations.Test;
  14. import java.io.File;
  15. import java.io.IOException;
  16. public class ObjectCacheLocalTest extends GitAbstractTest
  17. {
  18. @Test
  19. public void testCacheGetsReusedLocally() throws Exception
  20. {
  21. TestSetup t = new TestSetup().prepare();
  22. File targetDir = t.createDir("target");
  23. GitOperationHelper goh = createJGitOperationHelper(t.accessData);
  24. goh.checkout(t.cacheDir, targetDir, t.lastRevision, null);
  25. String contents = FileUtils.readFileToString(new File(targetDir, "file.txt"));
  26. Assert.assertEquals(contents, t.lastContents);
  27. RepositorySummary rs = new RepositorySummary(targetDir);
  28. Assert.assertTrue(rs.objects.isEmpty());
  29. Assert.assertTrue(rs.packs.isEmpty());
  30. }
  31. @Test
  32. public void testEmptyCache() throws Exception
  33. {
  34. TestSetup t = new TestSetup().prepare();
  35. File targetDir = t.createDir("target");
  36. GitOperationHelper goh = createJGitOperationHelper(t.accessData);
  37. goh.fetch(targetDir, "HEAD", false);
  38. goh.checkout(null, targetDir, t.lastRevision, null);
  39. String contents = FileUtils.readFileToString(new File(targetDir, "file.txt"));
  40. Assert.assertEquals(contents, t.lastContents);
  41. RepositorySummary rs = new RepositorySummary(targetDir);
  42. // no cache - something expected but we can't know whether objects or pack - depends
  43. Assert.assertFalse(rs.objects.isEmpty() && rs.packs.isEmpty());
  44. }
  45. @Test
  46. public void testDeleteCacheDirBeforeSourceCheckout() throws Exception
  47. {
  48. TestSetup t = new TestSetup().prepare();
  49. File targetDir = t.createDir("target");
  50. File emptyCache = new File(createTempDirectory(), "not_created");
  51. GitOperationHelper goh = createJGitOperationHelper(t.accessData);
  52. goh.fetch(targetDir, "HEAD", false);
  53. goh.checkout(emptyCache, targetDir, t.lastRevision, null);
  54. String contents = FileUtils.readFileToString(new File(targetDir, "file.txt"));
  55. Assert.assertEquals(contents, t.lastContents);
  56. }
  57. // this test is invalid
  58. public void testOldCacheGetsReusedLocally() throws Exception
  59. {
  60. final String asyncContents = "Async commit contents";
  61. TestSetup t = new TestSetup().prepare();
  62. String asyncRev = t.commitTestFileContents(asyncContents, "Async commit");
  63. File targetDir = t.createDir("target");
  64. GitOperationHelper goh = createJGitOperationHelper(t.accessData);
  65. goh.fetch(targetDir, "HEAD", false);
  66. goh.checkout(t.cacheDir, targetDir, asyncRev, null);
  67. String contents = FileUtils.readFileToString(new File(targetDir, "file.txt"));
  68. Assert.assertEquals(contents, asyncContents);
  69. }
  70. private class TestSetup
  71. {
  72. private File baseDir;
  73. private File cacheDir;
  74. private String lastRevision;
  75. private String lastContents;
  76. private GitRepositoryAccessData accessData;
  77. private File sourceRepositoryDir;
  78. private Git srcGit;
  79. private File srcFile;
  80. public TestSetup prepare()
  81. throws IOException, NoFilepatternException, NoHeadException, NoMessageException, ConcurrentRefUpdateException, WrongRepositoryStateException, RepositoryException
  82. {
  83. baseDir = createTempDirectory();
  84. sourceRepositoryDir = createDir("source");
  85. cacheDir = createDir("cache");
  86. FileRepository sourceRepository = new FileRepository(new File(sourceRepositoryDir, Constants.DOT_GIT));
  87. sourceRepository.create(false);
  88. srcGit = new Git(sourceRepository);
  89. srcFile = new File(sourceRepositoryDir, "file.txt");
  90. StringBuilder sb = new StringBuilder();
  91. for (int i = 0; i < 100; i++)
  92. {
  93. sb.append("Line ").append(i).append('\n');
  94. lastContents = sb.toString();
  95. lastRevision = commitTestFileContents(lastContents, "Commit " + i);
  96. }
  97. accessData = createAccessData(sourceRepositoryDir.getAbsolutePath());
  98. GitOperationHelper goh = createJGitOperationHelper(accessData);
  99. goh.fetch(cacheDir, "HEAD", false);
  100. return this;
  101. }
  102. private String commitTestFileContents(String contents, String comment)
  103. throws IOException, NoFilepatternException, NoHeadException, NoMessageException, ConcurrentRefUpdateException, WrongRepositoryStateException
  104. {
  105. FileUtils.writeStringToFile(srcFile, contents);
  106. srcGit.add().addFilepattern(".").call();
  107. return srcGit.commit().setMessage(comment).setCommitter("testUser", "testUser@testDomain").call().name();
  108. }
  109. private File createDir(String dirName)
  110. {
  111. File dir = new File(baseDir, dirName);
  112. Assert.assertTrue(dir.mkdir(), "Creating test directory " + dirName);
  113. return dir;
  114. }
  115. }
  116. }