您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RepositorySetupWorkDirTest.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. *
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  48. /**
  49. * Tests for setting up the working directory when creating a Repository
  50. */
  51. public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
  52. public void testIsBare_CreateRepositoryFromArbitraryGitDir()
  53. throws Exception {
  54. File gitDir = getFile("workdir");
  55. assertTrue(new FileRepository(gitDir).isBare());
  56. }
  57. public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
  58. File gitDir = getFile("workdir", Constants.DOT_GIT);
  59. Repository repo = new FileRepository(gitDir);
  60. assertFalse(repo.isBare());
  61. assertWorkdirPath(repo, "workdir");
  62. assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
  63. }
  64. public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
  65. throws Exception {
  66. File gitDir = getFile("workdir", Constants.DOT_GIT);
  67. Repository repo = new FileRepository(gitDir);
  68. String workdir = repo.getWorkDir().getName();
  69. assertEquals(workdir, "workdir");
  70. }
  71. public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
  72. File workdir = getFile("workdir", "repo");
  73. Repository repo = new FileRepository(null, workdir);
  74. assertFalse(repo.isBare());
  75. assertWorkdirPath(repo, "workdir", "repo");
  76. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  77. }
  78. public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
  79. throws Exception {
  80. File workdir = getFile("workdir", "repo");
  81. Repository repo = new FileRepository(null, workdir);
  82. assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
  83. }
  84. public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
  85. throws Exception {
  86. File gitDir = getFile("workdir", "repoWithConfig");
  87. File workTree = getFile("workdir", "treeRoot");
  88. setWorkTree(gitDir, workTree);
  89. Repository repo = new FileRepository(gitDir, null);
  90. assertFalse(repo.isBare());
  91. assertWorkdirPath(repo, "workdir", "treeRoot");
  92. assertGitdirPath(repo, "workdir", "repoWithConfig");
  93. }
  94. public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
  95. throws Exception {
  96. File gitDir = getFile("workdir", "repoWithConfig");
  97. setBare(gitDir, true);
  98. Repository repo = new FileRepository(gitDir, null);
  99. assertTrue(repo.isBare());
  100. }
  101. public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  102. throws Exception {
  103. File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
  104. setBare(gitDir, false);
  105. Repository repo = new FileRepository(gitDir, null);
  106. assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
  107. }
  108. public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
  109. throws Exception {
  110. File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
  111. setBare(gitDir, false);
  112. Repository repo = new FileRepository(gitDir, null);
  113. assertFalse(repo.isBare());
  114. assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
  115. assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
  116. }
  117. public void testNotBare_MakeBareUnbareBySetWorkdir() throws Exception {
  118. File gitDir = getFile("gitDir");
  119. Repository repo = new FileRepository(gitDir);
  120. repo.setWorkDir(getFile("workingDir"));
  121. assertFalse(repo.isBare());
  122. assertWorkdirPath(repo, "workingDir");
  123. assertGitdirPath(repo, "gitDir");
  124. }
  125. public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
  126. File gitDir = getFile("workdir");
  127. try {
  128. new FileRepository(gitDir).getWorkDir();
  129. fail("Expected IllegalStateException missing");
  130. } catch (IllegalStateException e) {
  131. // expected
  132. }
  133. }
  134. public void testExceptionThrown_BareRepoGetIndex() throws Exception {
  135. File gitDir = getFile("workdir");
  136. try {
  137. new FileRepository(gitDir).getIndex();
  138. fail("Expected IllegalStateException missing");
  139. } catch (IllegalStateException e) {
  140. // expected
  141. }
  142. }
  143. public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
  144. File gitDir = getFile("workdir");
  145. try {
  146. new FileRepository(gitDir).getIndexFile();
  147. fail("Expected Exception missing");
  148. } catch (IllegalStateException e) {
  149. // expected
  150. }
  151. }
  152. private File getFile(String... pathComponents) {
  153. String rootPath = new File(new File("target"), "trash").getPath();
  154. for (String pathComponent : pathComponents)
  155. rootPath = rootPath + File.separatorChar + pathComponent;
  156. File result = new File(rootPath);
  157. result.mkdir();
  158. return result;
  159. }
  160. private void setBare(File gitDir, boolean bare) throws IOException {
  161. FileRepository repo = new FileRepository(gitDir, null);
  162. repo.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  163. ConfigConstants.CONFIG_KEY_BARE, bare);
  164. repo.getConfig().save();
  165. }
  166. private void setWorkTree(File gitDir, File workTree) throws IOException {
  167. FileRepository repo = new FileRepository(gitDir, null);
  168. repo.getConfig()
  169. .setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  170. ConfigConstants.CONFIG_KEY_WORKTREE,
  171. workTree.getAbsolutePath());
  172. repo.getConfig().save();
  173. }
  174. private void assertGitdirPath(Repository repo, String... expected)
  175. throws IOException {
  176. File exp = getFile(expected).getCanonicalFile();
  177. File act = repo.getDirectory().getCanonicalFile();
  178. assertEquals("Wrong Git Directory", exp, act);
  179. }
  180. private void assertWorkdirPath(Repository repo, String... expected)
  181. throws IOException {
  182. File exp = getFile(expected).getCanonicalFile();
  183. File act = repo.getWorkDir().getCanonicalFile();
  184. assertEquals("Wrong working Directory", exp, act);
  185. }
  186. }