View Javadoc
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  
45  package org.eclipse.jgit.internal.storage.file;
46  
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertTrue;
50  import static org.junit.Assert.fail;
51  
52  import java.io.File;
53  import java.io.IOException;
54  
55  import org.eclipse.jgit.errors.ConfigInvalidException;
56  import org.eclipse.jgit.errors.NoWorkTreeException;
57  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
58  import org.eclipse.jgit.lib.ConfigConstants;
59  import org.eclipse.jgit.lib.Constants;
60  import org.eclipse.jgit.lib.Repository;
61  import org.eclipse.jgit.storage.file.FileBasedConfig;
62  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
63  import org.eclipse.jgit.util.FS;
64  import org.eclipse.jgit.util.FileUtils;
65  import org.junit.Test;
66  
67  /**
68   * Tests for setting up the working directory when creating a Repository
69   */
70  public class RepositorySetupWorkDirTest extends LocalDiskRepositoryTestCase {
71  
72  	@Test
73  	public void testIsBare_CreateRepositoryFromArbitraryGitDir()
74  			throws Exception {
75  		File gitDir = getFile("workdir");
76  		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
77  		assertTrue(repo.isBare());
78  	}
79  
80  	@Test
81  	public void testNotBare_CreateRepositoryFromDotGitGitDir() throws Exception {
82  		File gitDir = getFile("workdir", Constants.DOT_GIT);
83  		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
84  		assertFalse(repo.isBare());
85  		assertWorkdirPath(repo, "workdir");
86  		assertGitdirPath(repo, "workdir", Constants.DOT_GIT);
87  	}
88  
89  	@Test
90  	public void testWorkdirIsParentDir_CreateRepositoryFromDotGitGitDir()
91  			throws Exception {
92  		File gitDir = getFile("workdir", Constants.DOT_GIT);
93  		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
94  		String workdir = repo.getWorkTree().getName();
95  		assertEquals(workdir, "workdir");
96  	}
97  
98  	@Test
99  	public void testNotBare_CreateRepositoryFromWorkDirOnly() throws Exception {
100 		File workdir = getFile("workdir", "repo");
101 		Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
102 				.build();
103 		assertFalse(repo.isBare());
104 		assertWorkdirPath(repo, "workdir", "repo");
105 		assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
106 	}
107 
108 	@Test
109 	public void testWorkdirIsDotGit_CreateRepositoryFromWorkDirOnly()
110 			throws Exception {
111 		File workdir = getFile("workdir", "repo");
112 		Repository repo = new FileRepositoryBuilder().setWorkTree(workdir)
113 				.build();
114 		assertGitdirPath(repo, "workdir", "repo", Constants.DOT_GIT);
115 	}
116 
117 	@Test
118 	public void testNotBare_CreateRepositoryFromGitDirOnlyWithWorktreeConfig()
119 			throws Exception {
120 		File gitDir = getFile("workdir", "repoWithConfig");
121 		File workTree = getFile("workdir", "treeRoot");
122 		setWorkTree(gitDir, workTree);
123 		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
124 		assertFalse(repo.isBare());
125 		assertWorkdirPath(repo, "workdir", "treeRoot");
126 		assertGitdirPath(repo, "workdir", "repoWithConfig");
127 	}
128 
129 	@Test
130 	public void testBare_CreateRepositoryFromGitDirOnlyWithBareConfigTrue()
131 			throws Exception {
132 		File gitDir = getFile("workdir", "repoWithConfig");
133 		setBare(gitDir, true);
134 		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
135 		assertTrue(repo.isBare());
136 	}
137 
138 	@Test
139 	public void testWorkdirIsParent_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
140 			throws Exception {
141 		File gitDir = getFile("workdir", "repoWithBareConfigTrue", "child");
142 		setBare(gitDir, false);
143 		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
144 		assertWorkdirPath(repo, "workdir", "repoWithBareConfigTrue");
145 	}
146 
147 	@Test
148 	public void testNotBare_CreateRepositoryFromGitDirOnlyWithBareConfigFalse()
149 			throws Exception {
150 		File gitDir = getFile("workdir", "repoWithBareConfigFalse", "child");
151 		setBare(gitDir, false);
152 		Repository repo = new FileRepositoryBuilder().setGitDir(gitDir).build();
153 		assertFalse(repo.isBare());
154 		assertWorkdirPath(repo, "workdir", "repoWithBareConfigFalse");
155 		assertGitdirPath(repo, "workdir", "repoWithBareConfigFalse", "child");
156 	}
157 
158 	@Test
159 	public void testExceptionThrown_BareRepoGetWorkDir() throws Exception {
160 		File gitDir = getFile("workdir");
161 		try (Repository repo = new FileRepository(gitDir)) {
162 			repo.getWorkTree();
163 			fail("Expected NoWorkTreeException missing");
164 		} catch (NoWorkTreeException e) {
165 			// expected
166 		}
167 	}
168 
169 	@Test
170 	public void testExceptionThrown_BareRepoGetIndex() throws Exception {
171 		File gitDir = getFile("workdir");
172 		try (Repository repo = new FileRepository(gitDir)) {
173 			repo.readDirCache();
174 			fail("Expected NoWorkTreeException missing");
175 		} catch (NoWorkTreeException e) {
176 			// expected
177 		}
178 	}
179 
180 	@Test
181 	public void testExceptionThrown_BareRepoGetIndexFile() throws Exception {
182 		File gitDir = getFile("workdir");
183 		try (Repository repo = new FileRepository(gitDir)) {
184 			repo.getIndexFile();
185 			fail("Expected NoWorkTreeException missing");
186 		} catch (NoWorkTreeException e) {
187 			// expected
188 		}
189 	}
190 
191 	private File getFile(String... pathComponents) throws IOException {
192 		File dir = getTemporaryDirectory();
193 		for (String pathComponent : pathComponents)
194 			dir = new File(dir, pathComponent);
195 		FileUtils.mkdirs(dir, true);
196 		return dir;
197 	}
198 
199 	private void setBare(File gitDir, boolean bare) throws IOException,
200 			ConfigInvalidException {
201 		FileBasedConfig cfg = configFor(gitDir);
202 		cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
203 				ConfigConstants.CONFIG_KEY_BARE, bare);
204 		cfg.save();
205 	}
206 
207 	private void setWorkTree(File gitDir, File workTree)
208 			throws IOException,
209 			ConfigInvalidException {
210 		String path = workTree.getAbsolutePath();
211 		FileBasedConfig cfg = configFor(gitDir);
212 		cfg.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
213 				ConfigConstants.CONFIG_KEY_WORKTREE, path);
214 		cfg.save();
215 	}
216 
217 	private FileBasedConfig configFor(File gitDir) throws IOException,
218 			ConfigInvalidException {
219 		File configPath = new File(gitDir, Constants.CONFIG);
220 		FileBasedConfig cfg = new FileBasedConfig(configPath, FS.DETECTED);
221 		cfg.load();
222 		return cfg;
223 	}
224 
225 	private void assertGitdirPath(Repository repo, String... expected)
226 			throws IOException {
227 		File exp = getFile(expected).getCanonicalFile();
228 		File act = repo.getDirectory().getCanonicalFile();
229 		assertEquals("Wrong Git Directory", exp, act);
230 	}
231 
232 	private void assertWorkdirPath(Repository repo, String... expected)
233 			throws IOException {
234 		File exp = getFile(expected).getCanonicalFile();
235 		File act = repo.getWorkTree().getCanonicalFile();
236 		assertEquals("Wrong working Directory", exp, act);
237 	}
238 }