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