1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
111
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
128
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
147
148
149
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
162
163
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
182
183
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
201
202
203
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 }