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.assertEquals;
46 import static org.junit.Assert.fail;
47
48 import java.io.File;
49 import java.io.IOException;
50
51 import org.eclipse.jgit.api.ListBranchCommand.ListMode;
52 import org.eclipse.jgit.api.errors.GitAPIException;
53 import org.eclipse.jgit.api.errors.JGitInternalException;
54 import org.eclipse.jgit.errors.RepositoryNotFoundException;
55 import org.eclipse.jgit.junit.RepositoryTestCase;
56 import org.eclipse.jgit.lib.Repository;
57 import org.eclipse.jgit.util.FileUtils;
58 import org.junit.After;
59 import org.junit.Before;
60 import org.junit.Test;
61
62 public class GitConstructionTest extends RepositoryTestCase {
63 private Repository bareRepo;
64
65 @Override
66 @Before
67 public void setUp() throws Exception {
68 super.setUp();
69 Git git = new Git(db);
70 git.commit().setMessage("initial commit").call();
71 writeTrashFile("Test.txt", "Hello world");
72 git.add().addFilepattern("Test.txt").call();
73 git.commit().setMessage("Initial commit").call();
74
75 bareRepo = Git.cloneRepository().setBare(true)
76 .setURI(db.getDirectory().toURI().toString())
77 .setDirectory(createUniqueTestGitDir(true)).call()
78 .getRepository();
79 addRepoToClose(bareRepo);
80 }
81
82 @Override
83 @After
84 public void tearDown() throws Exception {
85 db.close();
86 bareRepo.close();
87 super.tearDown();
88 }
89
90 @Test
91 public void testWrap() throws JGitInternalException, GitAPIException {
92 Git git = Git.wrap(db);
93 assertEquals(1, git.branchList().call().size());
94
95 git = Git.wrap(bareRepo);
96 assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
97 .size());
98
99 try {
100 Git.wrap(null);
101 fail("Expected exception has not been thrown");
102 } catch (NullPointerException e) {
103
104 }
105 }
106
107 @Test
108 public void testOpen() throws IOException, JGitInternalException,
109 GitAPIException {
110 Git git = Git.open(db.getDirectory());
111 assertEquals(1, git.branchList().call().size());
112
113 git = Git.open(bareRepo.getDirectory());
114 assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
115 .size());
116
117 git = Git.open(db.getWorkTree());
118 assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
119 .size());
120
121 try {
122 Git.open(db.getObjectsDirectory());
123 fail("Expected exception has not been thrown");
124 } catch (RepositoryNotFoundException e) {
125
126 }
127 }
128
129 @Test
130
131
132
133
134
135
136
137
138
139
140 public void testClose() throws IOException, JGitInternalException,
141 GitAPIException {
142 File workTree = db.getWorkTree();
143 db.close();
144 Git git = Git.open(workTree);
145 git.gc().setExpire(null).call();
146 git.checkout().setName(git.getRepository().resolve("HEAD^").getName())
147 .call();
148 try {
149 FileUtils.delete(workTree, FileUtils.RECURSIVE);
150 } catch (IOException e) {
151 git.close();
152 FileUtils.delete(workTree, FileUtils.RECURSIVE);
153 }
154 }
155 }