View Javadoc
1   /*
2    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertNotNull;
46  import static org.junit.Assert.assertNull;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.api.errors.GitAPIException;
53  import org.eclipse.jgit.api.errors.JGitInternalException;
54  import org.eclipse.jgit.errors.NoWorkTreeException;
55  import org.eclipse.jgit.junit.MockSystemReader;
56  import org.eclipse.jgit.junit.RepositoryTestCase;
57  import org.eclipse.jgit.lib.Constants;
58  import org.eclipse.jgit.lib.Repository;
59  import org.eclipse.jgit.util.SystemReader;
60  import org.junit.Before;
61  import org.junit.Test;
62  
63  public class InitCommandTest extends RepositoryTestCase {
64  
65  	@Override
66  	@Before
67  	public void setUp() throws Exception {
68  		super.setUp();
69  	}
70  
71  	@Test
72  	public void testInitRepository() throws IOException, JGitInternalException,
73  			GitAPIException {
74  		File directory = createTempDirectory("testInitRepository");
75  		InitCommand command = new InitCommand();
76  		command.setDirectory(directory);
77  		Repository repository = command.call().getRepository();
78  		addRepoToClose(repository);
79  		assertNotNull(repository);
80  	}
81  
82  	@Test
83  	public void testInitNonEmptyRepository() throws IOException,
84  			JGitInternalException, GitAPIException {
85  		File directory = createTempDirectory("testInitRepository2");
86  		File someFile = new File(directory, "someFile");
87  		someFile.createNewFile();
88  		assertTrue(someFile.exists());
89  		assertTrue(directory.listFiles().length > 0);
90  		InitCommand command = new InitCommand();
91  		command.setDirectory(directory);
92  		Repository repository = command.call().getRepository();
93  		addRepoToClose(repository);
94  		assertNotNull(repository);
95  	}
96  
97  	@Test
98  	public void testInitBareRepository() throws IOException,
99  			JGitInternalException, GitAPIException {
100 		File directory = createTempDirectory("testInitBareRepository");
101 		InitCommand command = new InitCommand();
102 		command.setDirectory(directory);
103 		command.setBare(true);
104 		Repository repository = command.call().getRepository();
105 		addRepoToClose(repository);
106 		assertNotNull(repository);
107 		assertTrue(repository.isBare());
108 	}
109 
110 	// non-bare repos where gitDir and directory is set. Same as
111 	// "git init --separate-git-dir /tmp/a /tmp/b"
112 	@Test
113 	public void testInitWithExplicitGitDir() throws IOException,
114 			JGitInternalException, GitAPIException {
115 		File wt = createTempDirectory("testInitRepositoryWT");
116 		File gitDir = createTempDirectory("testInitRepositoryGIT");
117 		InitCommand command = new InitCommand();
118 		command.setDirectory(wt);
119 		command.setGitDir(gitDir);
120 		Repository repository = command.call().getRepository();
121 		addRepoToClose(repository);
122 		assertNotNull(repository);
123 		assertEqualsFile(wt, repository.getWorkTree());
124 		assertEqualsFile(gitDir, repository.getDirectory());
125 	}
126 
127 	// non-bare repos where only gitDir is set. Same as
128 	// "git init --separate-git-dir /tmp/a"
129 	@Test
130 	public void testInitWithOnlyExplicitGitDir() throws IOException,
131 			JGitInternalException, GitAPIException {
132 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
133 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
134 				.getAbsolutePath());
135 		File gitDir = createTempDirectory("testInitRepository/.git");
136 		InitCommand command = new InitCommand();
137 		command.setGitDir(gitDir);
138 		Repository repository = command.call().getRepository();
139 		addRepoToClose(repository);
140 		assertNotNull(repository);
141 		assertEqualsFile(gitDir, repository.getDirectory());
142 		assertEqualsFile(new File(reader.getProperty("user.dir")),
143 				repository.getWorkTree());
144 	}
145 
146 	// Bare repos where gitDir and directory is set will only work if gitDir and
147 	// directory is pointing to same dir. Same as
148 	// "git init --bare --separate-git-dir /tmp/a /tmp/b"
149 	// (works in native git but I guess that's more a bug)
150 	@Test(expected = IllegalStateException.class)
151 	public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
152 			JGitInternalException, GitAPIException {
153 		File gitDir = createTempDirectory("testInitRepository.git");
154 		InitCommand command = new InitCommand();
155 		command.setBare(true);
156 		command.setDirectory(gitDir);
157 		command.setGitDir(new File(gitDir, ".."));
158 		command.call();
159 	}
160 
161 	// If neither directory nor gitDir is set in a non-bare repo make sure
162 	// worktree and gitDir are set correctly. Standard case. Same as
163 	// "git init"
164 	@Test
165 	public void testInitWithDefaultsNonBare() throws JGitInternalException,
166 			GitAPIException, IOException {
167 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
168 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
169 				.getAbsolutePath());
170 		InitCommand command = new InitCommand();
171 		command.setBare(false);
172 		Repository repository = command.call().getRepository();
173 		addRepoToClose(repository);
174 		assertNotNull(repository);
175 		assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
176 				repository.getDirectory());
177 		assertEqualsFile(new File(reader.getProperty("user.dir")),
178 				repository.getWorkTree());
179 	}
180 
181 	// If neither directory nor gitDir is set in a bare repo make sure
182 	// worktree and gitDir are set correctly. Standard case. Same as
183 	// "git init --bare"
184 	@Test(expected = NoWorkTreeException.class)
185 	public void testInitWithDefaultsBare() throws JGitInternalException,
186 			GitAPIException, IOException {
187 		MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
188 		reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
189 				.getAbsolutePath());
190 		InitCommand command = new InitCommand();
191 		command.setBare(true);
192 		Repository repository = command.call().getRepository();
193 		addRepoToClose(repository);
194 		assertNotNull(repository);
195 		assertEqualsFile(new File(reader.getProperty("user.dir")),
196 				repository.getDirectory());
197 		assertNull(repository.getWorkTree());
198 	}
199 
200 	// In a non-bare repo when directory and gitDir is set then they shouldn't
201 	// point to the same dir. Same as
202 	// "git init --separate-git-dir /tmp/a /tmp/a"
203 	// (works in native git but I guess that's more a bug)
204 	@Test(expected = IllegalStateException.class)
205 	public void testInitNonBare_GitdirAndDirShouldntBeSame()
206 			throws JGitInternalException, GitAPIException, IOException {
207 		File gitDir = createTempDirectory("testInitRepository.git");
208 		InitCommand command = new InitCommand();
209 		command.setBare(false);
210 		command.setGitDir(gitDir);
211 		command.setDirectory(gitDir);
212 		command.call().getRepository();
213 	}
214 }