commit | author | age
|
ec97f7
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
2a7306
|
16 |
package com.gitblit.tests;
|
JM |
17 |
|
|
18 |
import java.io.File;
|
8daefa
|
19 |
import java.lang.reflect.Field;
|
143fc9
|
20 |
import java.util.concurrent.Executors;
|
7e8873
|
21 |
import java.util.concurrent.atomic.AtomicBoolean;
|
8daefa
|
22 |
import java.util.concurrent.atomic.AtomicInteger;
|
2a7306
|
23 |
|
8daefa
|
24 |
import org.eclipse.jgit.api.Git;
|
2a7306
|
25 |
import org.eclipse.jgit.lib.Repository;
|
8daefa
|
26 |
import org.eclipse.jgit.lib.RepositoryCache;
|
JM |
27 |
import org.eclipse.jgit.lib.RepositoryCache.FileKey;
|
843c42
|
28 |
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
|
8daefa
|
29 |
import org.eclipse.jgit.util.FS;
|
7e8873
|
30 |
import org.junit.AfterClass;
|
JM |
31 |
import org.junit.BeforeClass;
|
|
32 |
import org.junit.runner.RunWith;
|
|
33 |
import org.junit.runners.Suite;
|
|
34 |
import org.junit.runners.Suite.SuiteClasses;
|
2a7306
|
35 |
|
28d6b2
|
36 |
import com.gitblit.GitBlit;
|
a125cf
|
37 |
import com.gitblit.GitBlitException;
|
143fc9
|
38 |
import com.gitblit.GitBlitServer;
|
a125cf
|
39 |
import com.gitblit.models.RepositoryModel;
|
168566
|
40 |
import com.gitblit.utils.JGitUtils;
|
28d6b2
|
41 |
|
7e8873
|
42 |
/**
|
JM |
43 |
* The GitBlitSuite uses test-gitblit.properties and test-users.conf. The suite
|
|
44 |
* is fairly comprehensive for all lower-level functionality. Wicket pages are
|
|
45 |
* currently not unit-tested.
|
5dd805
|
46 |
*
|
7e8873
|
47 |
* This suite starts a Gitblit server instance within the same JVM instance as
|
JM |
48 |
* the unit tests. This allows the unit tests to access the GitBlit static
|
|
49 |
* singleton while also being able to communicate with the instance via tcp/ip
|
|
50 |
* for testing rpc requests, federation requests, and git servlet operations.
|
5dd805
|
51 |
*
|
7e8873
|
52 |
* @author James Moger
|
5dd805
|
53 |
*
|
7e8873
|
54 |
*/
|
JM |
55 |
@RunWith(Suite.class)
|
c89745
|
56 |
@SuiteClasses({ ArrayUtilsTest.class, FileUtilsTest.class, TimeUtilsTest.class,
|
JM |
57 |
StringUtilsTest.class, Base64Test.class, JsonUtilsTest.class, ByteFormatTest.class,
|
20714a
|
58 |
ObjectCacheTest.class, PermissionsTest.class, UserServiceTest.class, LdapUserServiceTest.class,
|
98b4b9
|
59 |
MarkdownUtilsTest.class, JGitUtilsTest.class, SyndicationUtilsTest.class,
|
5dd805
|
60 |
DiffUtilsTest.class, MetricUtilsTest.class, X509UtilsTest.class,
|
75bca8
|
61 |
GitBlitTest.class, FederationTests.class, RpcTests.class, GitServletTest.class, GitDaemonTest.class,
|
5316d2
|
62 |
GroovyScriptTest.class, LuceneExecutorTest.class, IssuesTest.class, RepositoryModelTest.class,
|
bb6b14
|
63 |
FanoutServiceTest.class, Issue0259Test.class, Issue0271Test.class, HtpasswdUserServiceTest.class,
|
9ae71a
|
64 |
ModelUtilsTest.class, JnaUtilsTest.class })
|
7e8873
|
65 |
public class GitBlitSuite {
|
143fc9
|
66 |
|
93d506
|
67 |
public static final File REPOSITORIES = new File("data/git");
|
5dd805
|
68 |
|
06fa4b
|
69 |
public static final File SETTINGS = new File("src/test/config/test-gitblit.properties");
|
5dd805
|
70 |
|
06fa4b
|
71 |
public static final File USERSCONF = new File("src/test/config/test-users.conf");
|
143fc9
|
72 |
|
JM |
73 |
static int port = 8280;
|
75bca8
|
74 |
static int gitPort = 8300;
|
143fc9
|
75 |
static int shutdownPort = 8281;
|
JM |
76 |
|
|
77 |
public static String url = "http://localhost:" + port;
|
75bca8
|
78 |
public static String gitServletUrl = "http://localhost:" + port + "/git";
|
JM |
79 |
public static String gitDaemonUrl = "git://localhost:" + gitPort;
|
143fc9
|
80 |
public static String account = "admin";
|
JM |
81 |
public static String password = "admin";
|
2a7306
|
82 |
|
7e8873
|
83 |
private static AtomicBoolean started = new AtomicBoolean(false);
|
1f9dae
|
84 |
|
2a7306
|
85 |
public static Repository getHelloworldRepository() throws Exception {
|
843c42
|
86 |
return getRepository("helloworld.git");
|
2a7306
|
87 |
}
|
JM |
88 |
|
|
89 |
public static Repository getTicgitRepository() throws Exception {
|
843c42
|
90 |
return getRepository("ticgit.git");
|
2a7306
|
91 |
}
|
JM |
92 |
|
4ab184
|
93 |
public static Repository getJGitRepository() throws Exception {
|
843c42
|
94 |
return getRepository("test/jgit.git");
|
4ab184
|
95 |
}
|
JM |
96 |
|
11924d
|
97 |
public static Repository getAmbitionRepository() throws Exception {
|
843c42
|
98 |
return getRepository("test/ambition.git");
|
11924d
|
99 |
}
|
JM |
100 |
|
0f43a5
|
101 |
public static Repository getIssuesTestRepository() throws Exception {
|
d896e6
|
102 |
JGitUtils.createRepository(REPOSITORIES, "gb-issues.git").close();
|
843c42
|
103 |
return getRepository("gb-issues.git");
|
d3065f
|
104 |
}
|
5dd805
|
105 |
|
d3065f
|
106 |
public static Repository getGitectiveRepository() throws Exception {
|
843c42
|
107 |
return getRepository("test/gitective.git");
|
JM |
108 |
}
|
5dd805
|
109 |
|
843c42
|
110 |
private static Repository getRepository(String name) throws Exception {
|
JM |
111 |
File gitDir = FileKey.resolve(new File(REPOSITORIES, name), FS.DETECTED);
|
|
112 |
Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
|
|
113 |
return repository;
|
0f43a5
|
114 |
}
|
JM |
115 |
|
7e8873
|
116 |
public static boolean startGitblit() throws Exception {
|
JM |
117 |
if (started.get()) {
|
|
118 |
// already started
|
|
119 |
return false;
|
|
120 |
}
|
5dd805
|
121 |
|
f3ce6e
|
122 |
GitServletTest.deleteWorkingFolders();
|
5dd805
|
123 |
|
143fc9
|
124 |
// Start a Gitblit instance
|
JM |
125 |
Executors.newSingleThreadExecutor().execute(new Runnable() {
|
5dd805
|
126 |
@Override
|
143fc9
|
127 |
public void run() {
|
JM |
128 |
GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
|
75bca8
|
129 |
"" + shutdownPort, "--gitPort", "" + gitPort, "--repositoriesFolder",
|
143fc9
|
130 |
"\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
|
06fa4b
|
131 |
GitBlitSuite.USERSCONF.getAbsolutePath(), "--settings", GitBlitSuite.SETTINGS.getAbsolutePath(),
|
93d506
|
132 |
"--baseFolder", "data");
|
143fc9
|
133 |
}
|
JM |
134 |
});
|
|
135 |
|
|
136 |
// Wait a few seconds for it to be running
|
d1dc77
|
137 |
Thread.sleep(5000);
|
7e8873
|
138 |
|
JM |
139 |
started.set(true);
|
|
140 |
return true;
|
143fc9
|
141 |
}
|
JM |
142 |
|
|
143 |
public static void stopGitblit() throws Exception {
|
|
144 |
// Stop Gitblit
|
|
145 |
GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
|
|
146 |
|
|
147 |
// Wait a few seconds for it to be running
|
f3ce6e
|
148 |
Thread.sleep(5000);
|
4ab184
|
149 |
}
|
JM |
150 |
|
7e8873
|
151 |
@BeforeClass
|
JM |
152 |
public static void setUp() throws Exception {
|
|
153 |
startGitblit();
|
a125cf
|
154 |
|
JM |
155 |
if (REPOSITORIES.exists() || REPOSITORIES.mkdirs()) {
|
168566
|
156 |
cloneOrFetch("helloworld.git", "https://github.com/git/hello-world.git");
|
f3ce6e
|
157 |
cloneOrFetch("ticgit.git", "https://github.com/schacon/ticgit.git");
|
168566
|
158 |
cloneOrFetch("test/jgit.git", "https://github.com/eclipse/jgit.git");
|
JM |
159 |
cloneOrFetch("test/helloworld.git", "https://github.com/git/hello-world.git");
|
11924d
|
160 |
cloneOrFetch("test/ambition.git", "https://github.com/defunkt/ambition.git");
|
d3065f
|
161 |
cloneOrFetch("test/gitective.git", "https://github.com/kevinsawicki/gitective.git");
|
5dd805
|
162 |
|
a125cf
|
163 |
enableTickets("ticgit.git");
|
JM |
164 |
enableDocs("ticgit.git");
|
|
165 |
showRemoteBranches("ticgit.git");
|
0f47b2
|
166 |
automaticallyTagBranchTips("ticgit.git");
|
168566
|
167 |
showRemoteBranches("test/jgit.git");
|
5dd805
|
168 |
automaticallyTagBranchTips("test/jgit.git");
|
a125cf
|
169 |
}
|
143fc9
|
170 |
}
|
JM |
171 |
|
7e8873
|
172 |
@AfterClass
|
JM |
173 |
public static void tearDown() throws Exception {
|
143fc9
|
174 |
stopGitblit();
|
2a7306
|
175 |
}
|
JM |
176 |
|
7e8873
|
177 |
private static void cloneOrFetch(String name, String fromUrl) throws Exception {
|
168566
|
178 |
System.out.print("Fetching " + name + "... ");
|
75bca8
|
179 |
try {
|
JM |
180 |
JGitUtils.cloneRepository(REPOSITORIES, name, fromUrl);
|
|
181 |
} catch (Throwable t) {
|
|
182 |
System.out.println("Error: " + t.getMessage());
|
|
183 |
}
|
168566
|
184 |
System.out.println("done.");
|
4ab184
|
185 |
}
|
a125cf
|
186 |
|
7e8873
|
187 |
private static void enableTickets(String repositoryName) {
|
a125cf
|
188 |
try {
|
JM |
189 |
RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
|
|
190 |
model.useTickets = true;
|
892570
|
191 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
a125cf
|
192 |
} catch (GitBlitException g) {
|
JM |
193 |
g.printStackTrace();
|
|
194 |
}
|
|
195 |
}
|
168566
|
196 |
|
7e8873
|
197 |
private static void enableDocs(String repositoryName) {
|
a125cf
|
198 |
try {
|
JM |
199 |
RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
|
|
200 |
model.useDocs = true;
|
892570
|
201 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
a125cf
|
202 |
} catch (GitBlitException g) {
|
JM |
203 |
g.printStackTrace();
|
|
204 |
}
|
|
205 |
}
|
168566
|
206 |
|
7e8873
|
207 |
private static void showRemoteBranches(String repositoryName) {
|
a125cf
|
208 |
try {
|
JM |
209 |
RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
|
|
210 |
model.showRemoteBranches = true;
|
892570
|
211 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
a125cf
|
212 |
} catch (GitBlitException g) {
|
JM |
213 |
g.printStackTrace();
|
|
214 |
}
|
|
215 |
}
|
5dd805
|
216 |
|
0f47b2
|
217 |
private static void automaticallyTagBranchTips(String repositoryName) {
|
JM |
218 |
try {
|
|
219 |
RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
|
|
220 |
model.useIncrementalPushTags = true;
|
|
221 |
GitBlit.self().updateRepositoryModel(model.name, model, false);
|
|
222 |
} catch (GitBlitException g) {
|
|
223 |
g.printStackTrace();
|
|
224 |
}
|
|
225 |
}
|
5dd805
|
226 |
|
8daefa
|
227 |
public static void close(File repository) {
|
JM |
228 |
try {
|
|
229 |
File gitDir = FileKey.resolve(repository, FS.detect());
|
|
230 |
if (gitDir != null && gitDir.exists()) {
|
|
231 |
close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
|
|
232 |
}
|
|
233 |
} catch (Exception e) {
|
|
234 |
e.printStackTrace();
|
|
235 |
}
|
|
236 |
}
|
5dd805
|
237 |
|
8daefa
|
238 |
public static void close(Git git) {
|
JM |
239 |
close(git.getRepository());
|
|
240 |
}
|
5dd805
|
241 |
|
8daefa
|
242 |
public static void close(Repository r) {
|
JM |
243 |
RepositoryCache.close(r);
|
|
244 |
|
|
245 |
// assume 2 uses in case reflection fails
|
|
246 |
int uses = 2;
|
|
247 |
try {
|
|
248 |
Field useCnt = Repository.class.getDeclaredField("useCnt");
|
|
249 |
useCnt.setAccessible(true);
|
|
250 |
uses = ((AtomicInteger) useCnt.get(r)).get();
|
|
251 |
} catch (Exception e) {
|
|
252 |
e.printStackTrace();
|
|
253 |
}
|
|
254 |
for (int i = 0; i < uses; i++) {
|
|
255 |
r.close();
|
|
256 |
}
|
|
257 |
}
|
2a7306
|
258 |
}
|