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()
73 throws IOException, JGitInternalException, GitAPIException {
74 File directory = createTempDirectory("testInitRepository");
75 InitCommand command = new InitCommand();
76 command.setDirectory(directory);
77 try (Git git = command.call()) {
78 assertNotNull(git.getRepository());
79 }
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 try (Git git = command.call()) {
93 assertNotNull(git.getRepository());
94 }
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 try (Git git = command.call()) {
105 Repository repository = git.getRepository();
106 assertNotNull(repository);
107 assertTrue(repository.isBare());
108 }
109 }
110
111
112
113 @Test
114 public void testInitWithExplicitGitDir() throws IOException,
115 JGitInternalException, GitAPIException {
116 File wt = createTempDirectory("testInitRepositoryWT");
117 File gitDir = createTempDirectory("testInitRepositoryGIT");
118 InitCommand command = new InitCommand();
119 command.setDirectory(wt);
120 command.setGitDir(gitDir);
121 try (Git git = command.call()) {
122 Repository repository = git.getRepository();
123 assertNotNull(repository);
124 assertEqualsFile(wt, repository.getWorkTree());
125 assertEqualsFile(gitDir, repository.getDirectory());
126 }
127 }
128
129
130
131 @Test
132 public void testInitWithOnlyExplicitGitDir() throws IOException,
133 JGitInternalException, GitAPIException {
134 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
135 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
136 .getAbsolutePath());
137 File gitDir = createTempDirectory("testInitRepository/.git");
138 InitCommand command = new InitCommand();
139 command.setGitDir(gitDir);
140 try (Git git = command.call()) {
141 Repository repository = git.getRepository();
142 assertNotNull(repository);
143 assertEqualsFile(gitDir, repository.getDirectory());
144 assertEqualsFile(new File(reader.getProperty("user.dir")),
145 repository.getWorkTree());
146 }
147 }
148
149
150
151
152
153 @Test(expected = IllegalStateException.class)
154 public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
155 JGitInternalException, GitAPIException {
156 File gitDir = createTempDirectory("testInitRepository.git");
157 InitCommand command = new InitCommand();
158 command.setBare(true);
159 command.setDirectory(gitDir);
160 command.setGitDir(new File(gitDir, ".."));
161 command.call();
162 }
163
164
165
166
167 @Test
168 public void testInitWithDefaultsNonBare() throws JGitInternalException,
169 GitAPIException, IOException {
170 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
171 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
172 .getAbsolutePath());
173 InitCommand command = new InitCommand();
174 command.setBare(false);
175 try (Git git = command.call()) {
176 Repository repository = git.getRepository();
177 assertNotNull(repository);
178 assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
179 repository.getDirectory());
180 assertEqualsFile(new File(reader.getProperty("user.dir")),
181 repository.getWorkTree());
182 }
183 }
184
185
186
187
188 @Test(expected = NoWorkTreeException.class)
189 public void testInitWithDefaultsBare() throws JGitInternalException,
190 GitAPIException, IOException {
191 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
192 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
193 .getAbsolutePath());
194 InitCommand command = new InitCommand();
195 command.setBare(true);
196 try (Git git = command.call()) {
197 Repository repository = git.getRepository();
198 assertNotNull(repository);
199 assertEqualsFile(new File(reader.getProperty("user.dir")),
200 repository.getDirectory());
201 assertNull(repository.getWorkTree());
202 }
203 }
204
205
206
207
208
209 @Test(expected = IllegalStateException.class)
210 public void testInitNonBare_GitdirAndDirShouldntBeSame()
211 throws JGitInternalException, GitAPIException, IOException {
212 File gitDir = createTempDirectory("testInitRepository.git");
213 InitCommand command = new InitCommand();
214 command.setBare(false);
215 command.setGitDir(gitDir);
216 command.setDirectory(gitDir);
217 command.call().getRepository();
218 }
219 }