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
44 package org.eclipse.jgit.junit.http;
45
46 import static org.junit.Assert.fail;
47
48 import java.io.IOException;
49 import java.net.URI;
50 import java.net.URISyntaxException;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.HashSet;
54 import java.util.List;
55 import java.util.Set;
56
57 import org.eclipse.jetty.servlet.ServletContextHandler;
58 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
59 import org.eclipse.jgit.junit.TestRepository;
60 import org.eclipse.jgit.lib.AnyObjectId;
61 import org.eclipse.jgit.lib.Constants;
62 import org.eclipse.jgit.lib.ObjectId;
63 import org.eclipse.jgit.lib.Repository;
64 import org.eclipse.jgit.revwalk.RevCommit;
65 import org.eclipse.jgit.revwalk.RevObject;
66 import org.eclipse.jgit.transport.RefSpec;
67 import org.eclipse.jgit.transport.RemoteRefUpdate;
68 import org.eclipse.jgit.transport.URIish;
69
70
71
72
73 public abstract class HttpTestCase extends LocalDiskRepositoryTestCase {
74
75 protected static final String master = Constants.R_HEADS + Constants.MASTER;
76
77
78 protected AppServer server;
79
80
81 @Override
82 public void setUp() throws Exception {
83 super.setUp();
84 server = createServer();
85 }
86
87
88 @Override
89 public void tearDown() throws Exception {
90 server.tearDown();
91 super.tearDown();
92 }
93
94
95
96
97
98
99
100
101
102
103
104 protected AppServer createServer() {
105 return new AppServer();
106 }
107
108
109
110
111
112
113
114 protected TestRepository<Repository> createTestRepository()
115 throws IOException {
116 return new TestRepository<>(createBareRepository());
117 }
118
119
120
121
122
123
124
125
126 protected URIish toURIish(String path) throws URISyntaxException {
127 URI u = server.getURI().resolve(path);
128 return new URIish(u.toString());
129 }
130
131
132
133
134
135
136
137
138
139 protected URIish toURIish(ServletContextHandler app, String name)
140 throws URISyntaxException {
141 String p = app.getContextPath();
142 if (!p.endsWith("/") && !name.startsWith("/"))
143 p += "/";
144 p += name;
145 return toURIish(p);
146 }
147
148
149
150
151
152
153 protected List<AccessEvent> getRequests() {
154 return server.getRequests();
155 }
156
157
158
159
160
161
162
163
164
165 protected List<AccessEvent> getRequests(URIish base, String path) {
166 return server.getRequests(base, path);
167 }
168
169
170
171
172
173
174
175
176 protected List<AccessEvent> getRequests(String path) {
177 return server.getRequests(path);
178 }
179
180
181
182
183
184
185
186
187 protected static void fsck(Repository db, RevObject... tips)
188 throws Exception {
189 TestRepository<? extends Repository> tr =
190 new TestRepository<>(db);
191 tr.fsck(tips);
192 }
193
194
195
196
197
198
199
200 protected static Set<RefSpec> mirror(String... refs) {
201 HashSet<RefSpec> r = new HashSet<>();
202 for (String name : refs) {
203 RefSpec rs = new RefSpec(name);
204 rs = rs.setDestination(name);
205 rs = rs.setForceUpdate(true);
206 r.add(rs);
207 }
208 return r;
209 }
210
211
212
213
214
215
216
217
218
219 protected static Collection<RemoteRefUpdate> push(TestRepository from,
220 RevCommit q) throws IOException {
221 final Repository db = from.getRepository();
222 final String srcExpr = q.name();
223 final String dstName = master;
224 final boolean forceUpdate = true;
225 final String localName = null;
226 final ObjectId oldId = null;
227
228 RemoteRefUpdate u = new RemoteRefUpdate(db, srcExpr, dstName,
229 forceUpdate, localName, oldId);
230 return Collections.singleton(u);
231 }
232
233
234
235
236
237
238
239
240 public static String loose(URIish base, AnyObjectId id) {
241 final String objectName = id.name();
242 final String d = objectName.substring(0, 2);
243 final String f = objectName.substring(2);
244 return join(base, "objects/" + d + "/" + f);
245 }
246
247
248
249
250
251
252
253
254
255 public static String join(URIish base, String path) {
256 if (path.startsWith("/"))
257 fail("Cannot join absolute path " + path + " to URIish " + base);
258
259 String dir = base.getPath();
260 if (!dir.endsWith("/"))
261 dir += "/";
262 return dir + path;
263 }
264
265
266
267
268
269
270
271
272
273 protected static String rewriteUrl(String url, String newProtocol,
274 int newPort) {
275 String newUrl = url;
276 if (newProtocol != null && !newProtocol.isEmpty()) {
277 int schemeEnd = newUrl.indexOf("://");
278 if (schemeEnd >= 0) {
279 newUrl = newProtocol + newUrl.substring(schemeEnd);
280 }
281 }
282 if (newPort > 0) {
283 newUrl = newUrl.replaceFirst(":\\d+/", ":" + newPort + "/");
284 } else {
285
286 newUrl = newUrl.replaceFirst(":\\d+/", "/");
287 }
288 return newUrl;
289 }
290
291
292
293
294
295
296
297
298
299 protected static URIish extendPath(URIish uri, String pathComponents)
300 throws URISyntaxException {
301 String raw = uri.toString();
302 String newComponents = pathComponents;
303 if (!newComponents.startsWith("/")) {
304 newComponents = '/' + newComponents;
305 }
306 if (!newComponents.endsWith("/")) {
307 newComponents += '/';
308 }
309 int i = raw.lastIndexOf('/');
310 raw = raw.substring(0, i) + newComponents + raw.substring(i + 1);
311 return new URIish(raw);
312 }
313 }