1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.File;
17 import java.io.IOException;
18
19 import org.eclipse.jgit.api.errors.GitAPIException;
20 import org.eclipse.jgit.api.errors.JGitInternalException;
21 import org.eclipse.jgit.errors.NoWorkTreeException;
22 import org.eclipse.jgit.junit.MockSystemReader;
23 import org.eclipse.jgit.junit.RepositoryTestCase;
24 import org.eclipse.jgit.lib.Constants;
25 import org.eclipse.jgit.lib.Repository;
26 import org.eclipse.jgit.util.SystemReader;
27 import org.junit.Before;
28 import org.junit.Test;
29
30 public class InitCommandTest extends RepositoryTestCase {
31
32 @Override
33 @Before
34 public void setUp() throws Exception {
35 super.setUp();
36 }
37
38 @Test
39 public void testInitRepository()
40 throws IOException, JGitInternalException, GitAPIException {
41 File directory = createTempDirectory("testInitRepository");
42 InitCommand command = new InitCommand();
43 command.setDirectory(directory);
44 try (Git git = command.call()) {
45 assertNotNull(git.getRepository());
46 }
47 }
48
49 @Test
50 public void testInitNonEmptyRepository() throws IOException,
51 JGitInternalException, GitAPIException {
52 File directory = createTempDirectory("testInitRepository2");
53 File someFile = new File(directory, "someFile");
54 someFile.createNewFile();
55 assertTrue(someFile.exists());
56 assertTrue(directory.listFiles().length > 0);
57 InitCommand command = new InitCommand();
58 command.setDirectory(directory);
59 try (Git git = command.call()) {
60 assertNotNull(git.getRepository());
61 }
62 }
63
64 @Test
65 public void testInitBareRepository() throws IOException,
66 JGitInternalException, GitAPIException {
67 File directory = createTempDirectory("testInitBareRepository");
68 InitCommand command = new InitCommand();
69 command.setDirectory(directory);
70 command.setBare(true);
71 try (Git git = command.call()) {
72 Repository repository = git.getRepository();
73 assertNotNull(repository);
74 assertTrue(repository.isBare());
75 }
76 }
77
78
79
80 @Test
81 public void testInitWithExplicitGitDir() throws IOException,
82 JGitInternalException, GitAPIException {
83 File wt = createTempDirectory("testInitRepositoryWT");
84 File gitDir = createTempDirectory("testInitRepositoryGIT");
85 InitCommand command = new InitCommand();
86 command.setDirectory(wt);
87 command.setGitDir(gitDir);
88 try (Git git = command.call()) {
89 Repository repository = git.getRepository();
90 assertNotNull(repository);
91 assertEqualsFile(wt, repository.getWorkTree());
92 assertEqualsFile(gitDir, repository.getDirectory());
93 }
94 }
95
96
97
98 @Test
99 public void testInitWithOnlyExplicitGitDir() throws IOException,
100 JGitInternalException, GitAPIException {
101 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
102 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
103 .getAbsolutePath());
104 File gitDir = createTempDirectory("testInitRepository/.git");
105 InitCommand command = new InitCommand();
106 command.setGitDir(gitDir);
107 try (Git git = command.call()) {
108 Repository repository = git.getRepository();
109 assertNotNull(repository);
110 assertEqualsFile(gitDir, repository.getDirectory());
111 assertEqualsFile(new File(reader.getProperty("user.dir")),
112 repository.getWorkTree());
113 }
114 }
115
116
117
118
119
120 @Test(expected = IllegalStateException.class)
121 public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
122 JGitInternalException, GitAPIException {
123 File gitDir = createTempDirectory("testInitRepository.git");
124 InitCommand command = new InitCommand();
125 command.setBare(true);
126 command.setDirectory(gitDir);
127 command.setGitDir(new File(gitDir, ".."));
128 command.call();
129 }
130
131
132
133
134 @Test
135 public void testInitWithDefaultsNonBare() throws JGitInternalException,
136 GitAPIException, IOException {
137 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
138 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
139 .getAbsolutePath());
140 InitCommand command = new InitCommand();
141 command.setBare(false);
142 try (Git git = command.call()) {
143 Repository repository = git.getRepository();
144 assertNotNull(repository);
145 assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
146 repository.getDirectory());
147 assertEqualsFile(new File(reader.getProperty("user.dir")),
148 repository.getWorkTree());
149 }
150 }
151
152
153
154
155 @Test(expected = NoWorkTreeException.class)
156 public void testInitWithDefaultsBare() throws JGitInternalException,
157 GitAPIException, IOException {
158 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
159 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
160 .getAbsolutePath());
161 InitCommand command = new InitCommand();
162 command.setBare(true);
163 try (Git git = command.call()) {
164 Repository repository = git.getRepository();
165 assertNotNull(repository);
166 assertEqualsFile(new File(reader.getProperty("user.dir")),
167 repository.getDirectory());
168 assertNull(repository.getWorkTree());
169 }
170 }
171
172
173
174
175
176 @Test(expected = IllegalStateException.class)
177 public void testInitNonBare_GitdirAndDirShouldntBeSame()
178 throws JGitInternalException, GitAPIException, IOException {
179 File gitDir = createTempDirectory("testInitRepository.git");
180 InitCommand command = new InitCommand();
181 command.setBare(false);
182 command.setGitDir(gitDir);
183 command.setDirectory(gitDir);
184 command.call().getRepository();
185 }
186 }