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 try (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
76 bareRepo = Git.cloneRepository().setBare(true)
77 .setURI(db.getDirectory().toURI().toString())
78 .setDirectory(createUniqueTestGitDir(true)).call()
79 .getRepository();
80 addRepoToClose(bareRepo);
81 }
82
83 @Override
84 @After
85 public void tearDown() throws Exception {
86 db.close();
87 bareRepo.close();
88 super.tearDown();
89 }
90
91 @Test
92 public void testWrap() throws JGitInternalException, GitAPIException {
93 Git git = Git.wrap(db);
94 assertEquals(1, git.branchList().call().size());
95
96 git = Git.wrap(bareRepo);
97 assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
98 .size());
99
100 try {
101 Git.wrap(null);
102 fail("Expected exception has not been thrown");
103 } catch (NullPointerException e) {
104
105 }
106 }
107
108 @Test
109 public void testOpen() throws IOException, JGitInternalException,
110 GitAPIException {
111 Git git = Git.open(db.getDirectory());
112 assertEquals(1, git.branchList().call().size());
113
114 git = Git.open(bareRepo.getDirectory());
115 assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
116 .size());
117
118 git = Git.open(db.getWorkTree());
119 assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
120 .size());
121
122 try {
123 Git.open(db.getObjectsDirectory());
124 fail("Expected exception has not been thrown");
125 } catch (RepositoryNotFoundException e) {
126
127 }
128 }
129
130 @Test
131
132
133
134
135
136
137
138
139
140
141 public void testClose() throws IOException, JGitInternalException,
142 GitAPIException {
143 File workTree = db.getWorkTree();
144 db.close();
145 Git git = Git.open(workTree);
146 git.gc().setExpire(null).call();
147 git.checkout().setName(git.getRepository().resolve("HEAD^").getName())
148 .call();
149 try {
150 FileUtils.delete(workTree, FileUtils.RECURSIVE);
151 } catch (IOException e) {
152 git.close();
153 FileUtils.delete(workTree, FileUtils.RECURSIVE);
154 }
155 }
156 }