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 try (TestRepository<? extends Repository> tr =
190 new TestRepository<>(db)) {
191 tr.fsck(tips);
192 }
193 }
194
195
196
197
198
199
200
201 protected static Set<RefSpec> mirror(String... refs) {
202 HashSet<RefSpec> r = new HashSet<>();
203 for (String name : refs) {
204 RefSpec rs = new RefSpec(name);
205 rs = rs.setDestination(name);
206 rs = rs.setForceUpdate(true);
207 r.add(rs);
208 }
209 return r;
210 }
211
212
213
214
215
216
217
218
219
220 protected static Collection<RemoteRefUpdate> push(TestRepository from,
221 RevCommit q) throws IOException {
222 final Repository db = from.getRepository();
223 final String srcExpr = q.name();
224 final String dstName = master;
225 final boolean forceUpdate = true;
226 final String localName = null;
227 final ObjectId oldId = null;
228
229 RemoteRefUpdate u = new RemoteRefUpdate(db, srcExpr, dstName,
230 forceUpdate, localName, oldId);
231 return Collections.singleton(u);
232 }
233
234
235
236
237
238
239
240
241 public static String loose(URIish base, AnyObjectId id) {
242 final String objectName = id.name();
243 final String d = objectName.substring(0, 2);
244 final String f = objectName.substring(2);
245 return join(base, "objects/" + d + "/" + f);
246 }
247
248
249
250
251
252
253
254
255
256 public static String join(URIish base, String path) {
257 if (path.startsWith("/"))
258 fail("Cannot join absolute path " + path + " to URIish " + base);
259
260 String dir = base.getPath();
261 if (!dir.endsWith("/"))
262 dir += "/";
263 return dir + path;
264 }
265
266
267
268
269
270
271
272
273
274 protected static String rewriteUrl(String url, String newProtocol,
275 int newPort) {
276 String newUrl = url;
277 if (newProtocol != null && !newProtocol.isEmpty()) {
278 int schemeEnd = newUrl.indexOf("://");
279 if (schemeEnd >= 0) {
280 newUrl = newProtocol + newUrl.substring(schemeEnd);
281 }
282 }
283 if (newPort > 0) {
284 newUrl = newUrl.replaceFirst(":\\d+/", ":" + newPort + "/");
285 } else {
286
287 newUrl = newUrl.replaceFirst(":\\d+/", "/");
288 }
289 return newUrl;
290 }
291
292
293
294
295
296
297
298
299
300 protected static URIish./../../../../org/eclipse/jgit/transport/URIish.html#URIish">URIish extendPath(URIish uri, String pathComponents)
301 throws URISyntaxException {
302 String raw = uri.toString();
303 String newComponents = pathComponents;
304 if (!newComponents.startsWith("/")) {
305 newComponents = '/' + newComponents;
306 }
307 if (!newComponents.endsWith("/")) {
308 newComponents += '/';
309 }
310 int i = raw.lastIndexOf('/');
311 raw = raw.substring(0, i) + newComponents + raw.substring(i + 1);
312 return new URIish(raw);
313 }
314 }