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.transport;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNotNull;
48 import static org.junit.Assert.assertTrue;
49
50 import java.io.IOException;
51 import java.io.OutputStream;
52 import java.util.HashMap;
53 import java.util.HashSet;
54 import java.util.Map;
55
56 import org.eclipse.jgit.errors.NotSupportedException;
57 import org.eclipse.jgit.errors.TransportException;
58 import org.eclipse.jgit.lib.ObjectId;
59 import org.eclipse.jgit.lib.ObjectIdRef;
60 import org.eclipse.jgit.lib.ProgressMonitor;
61 import org.eclipse.jgit.lib.Ref;
62 import org.eclipse.jgit.lib.RefUpdate.Result;
63 import org.eclipse.jgit.lib.Repository;
64 import org.eclipse.jgit.lib.TextProgressMonitor;
65 import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
66 import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
67 import org.junit.Before;
68 import org.junit.Test;
69
70 public class PushProcessTest extends SampleDataRepositoryTestCase {
71 private PushProcess process;
72
73 private MockTransport transport;
74
75 private HashSet<RemoteRefUpdate> refUpdates;
76
77 private HashSet<Ref> advertisedRefs;
78
79 private Status connectionUpdateStatus;
80
81 @Override
82 @Before
83 public void setUp() throws Exception {
84 super.setUp();
85 transport = new MockTransport(db, new URIish());
86 refUpdates = new HashSet<>();
87 advertisedRefs = new HashSet<>();
88 connectionUpdateStatus = Status.OK;
89 }
90
91
92
93
94
95
96 @Test
97 public void testUpdateFastForward() throws IOException {
98 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
99 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
100 "refs/heads/master", false, null, null);
101 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
102 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
103 testOneUpdateStatus(rru, ref, Status.OK, Boolean.TRUE);
104 }
105
106
107
108
109
110
111
112 @Test
113 public void testUpdateNonFastForwardUnknownObject() throws IOException {
114 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
115 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
116 "refs/heads/master", false, null, null);
117 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
118 ObjectId.fromString("0000000000000000000000000000000000000001"));
119 testOneUpdateStatus(rru, ref, Status.REJECTED_NONFASTFORWARD, null);
120 }
121
122
123
124
125
126
127
128 @Test
129 public void testUpdateNonFastForward() throws IOException {
130 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
131 "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
132 "refs/heads/master", false, null, null);
133 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
134 ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
135 testOneUpdateStatus(rru, ref, Status.REJECTED_NONFASTFORWARD, null);
136 }
137
138
139
140
141
142
143 @Test
144 public void testUpdateNonFastForwardForced() throws IOException {
145 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
146 "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
147 "refs/heads/master", true, null, null);
148 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
149 ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
150 testOneUpdateStatus(rru, ref, Status.OK, Boolean.FALSE);
151 }
152
153
154
155
156
157
158 @Test
159 public void testUpdateCreateRef() throws IOException {
160 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
161 "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
162 "refs/heads/master", false, null, null);
163 testOneUpdateStatus(rru, null, Status.OK, Boolean.TRUE);
164 }
165
166
167
168
169
170
171 @Test
172 public void testUpdateDelete() throws IOException {
173 final RemoteRefUpdate rru = new RemoteRefUpdate(db, (String) null,
174 "refs/heads/master", false, null, null);
175 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
176 ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
177 testOneUpdateStatus(rru, ref, Status.OK, Boolean.TRUE);
178 }
179
180
181
182
183
184
185
186 @Test
187 public void testUpdateDeleteNonExisting() throws IOException {
188 final RemoteRefUpdate rru = new RemoteRefUpdate(db, (String) null,
189 "refs/heads/master", false, null, null);
190 testOneUpdateStatus(rru, null, Status.NON_EXISTING, null);
191 }
192
193
194
195
196
197
198 @Test
199 public void testUpdateUpToDate() throws IOException {
200 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
201 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
202 "refs/heads/master", false, null, null);
203 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
204 ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
205 testOneUpdateStatus(rru, ref, Status.UP_TO_DATE, null);
206 }
207
208
209
210
211
212
213 @Test
214 public void testUpdateExpectedRemote() throws IOException {
215 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
216 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
217 "refs/heads/master", false, null, ObjectId
218 .fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
219 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
220 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
221 testOneUpdateStatus(rru, ref, Status.OK, Boolean.TRUE);
222 }
223
224
225
226
227
228
229
230 @Test
231 public void testUpdateUnexpectedRemote() throws IOException {
232 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
233 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
234 "refs/heads/master", false, null, ObjectId
235 .fromString("0000000000000000000000000000000000000001"));
236 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
237 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
238 testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null);
239 }
240
241
242
243
244
245
246
247
248 @Test
249 public void testUpdateUnexpectedRemoteVsForce() throws IOException {
250 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
251 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
252 "refs/heads/master", true, null, ObjectId
253 .fromString("0000000000000000000000000000000000000001"));
254 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
255 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
256 testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null);
257 }
258
259
260
261
262
263
264 @Test
265 public void testUpdateRejectedByConnection() throws IOException {
266 connectionUpdateStatus = Status.REJECTED_OTHER_REASON;
267 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
268 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
269 "refs/heads/master", false, null, null);
270 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
271 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
272 testOneUpdateStatus(rru, ref, Status.REJECTED_OTHER_REASON, null);
273 }
274
275
276
277
278
279
280
281 @Test
282 public void testUpdateMixedCases() throws IOException {
283 final RemoteRefUpdate rruOk = new RemoteRefUpdate(db, (String) null,
284 "refs/heads/master", false, null, null);
285 final Ref refToChange = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
286 ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
287 final RemoteRefUpdate rruReject = new RemoteRefUpdate(db,
288 (String) null, "refs/heads/nonexisting", false, null, null);
289 refUpdates.add(rruOk);
290 refUpdates.add(rruReject);
291 advertisedRefs.add(refToChange);
292 executePush();
293 assertEquals(Status.OK, rruOk.getStatus());
294 assertTrue(rruOk.isFastForward());
295 assertEquals(Status.NON_EXISTING, rruReject.getStatus());
296 }
297
298
299
300
301
302
303 @Test
304 public void testTrackingRefUpdateEnabled() throws IOException {
305 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
306 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
307 "refs/heads/master", false, "refs/remotes/test/master", null);
308 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
309 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
310 refUpdates.add(rru);
311 advertisedRefs.add(ref);
312 final PushResult result = executePush();
313 final TrackingRefUpdate tru = result
314 .getTrackingRefUpdate("refs/remotes/test/master");
315 assertNotNull(tru);
316 assertEquals("refs/remotes/test/master", tru.getLocalName());
317 assertEquals(Result.NEW, tru.getResult());
318 }
319
320
321
322
323
324
325 @Test
326 public void testTrackingRefUpdateDisabled() throws IOException {
327 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
328 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
329 "refs/heads/master", false, null, null);
330 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
331 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
332 refUpdates.add(rru);
333 advertisedRefs.add(ref);
334 final PushResult result = executePush();
335 assertTrue(result.getTrackingRefUpdates().isEmpty());
336 }
337
338
339
340
341
342
343 @Test
344 public void testTrackingRefUpdateOnReject() throws IOException {
345 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
346 "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
347 "refs/heads/master", false, null, null);
348 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
349 ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
350 final PushResult result = testOneUpdateStatus(rru, ref,
351 Status.REJECTED_NONFASTFORWARD, null);
352 assertTrue(result.getTrackingRefUpdates().isEmpty());
353 }
354
355
356
357
358
359
360 @Test
361 public void testPushResult() throws IOException {
362 final RemoteRefUpdate rru = new RemoteRefUpdate(db,
363 "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
364 "refs/heads/master", false, "refs/remotes/test/master", null);
365 final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
366 ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
367 refUpdates.add(rru);
368 advertisedRefs.add(ref);
369 final PushResult result = executePush();
370 assertEquals(1, result.getTrackingRefUpdates().size());
371 assertEquals(1, result.getAdvertisedRefs().size());
372 assertEquals(1, result.getRemoteUpdates().size());
373 assertNotNull(result.getTrackingRefUpdate("refs/remotes/test/master"));
374 assertNotNull(result.getAdvertisedRef("refs/heads/master"));
375 assertNotNull(result.getRemoteUpdate("refs/heads/master"));
376 }
377
378 private PushResult testOneUpdateStatus(final RemoteRefUpdate rru,
379 final Ref advertisedRef, final Status expectedStatus,
380 Boolean fastForward) throws NotSupportedException,
381 TransportException {
382 refUpdates.add(rru);
383 if (advertisedRef != null)
384 advertisedRefs.add(advertisedRef);
385 final PushResult result = executePush();
386 assertEquals(expectedStatus, rru.getStatus());
387 if (fastForward != null)
388 assertEquals(fastForward, Boolean.valueOf(rru.isFastForward()));
389 return result;
390 }
391
392 private PushResult executePush() throws NotSupportedException,
393 TransportException {
394 process = new PushProcess(transport, refUpdates);
395 return process.execute(new TextProgressMonitor());
396 }
397
398 private class MockTransport extends Transport {
399 MockTransport(Repository local, URIish uri) {
400 super(local, uri);
401 }
402
403 @Override
404 public FetchConnection openFetch() throws NotSupportedException,
405 TransportException {
406 throw new NotSupportedException("mock");
407 }
408
409 @Override
410 public PushConnection openPush() throws NotSupportedException,
411 TransportException {
412 return new MockPushConnection();
413 }
414
415 @Override
416 public void close() {
417
418 }
419 }
420
421 private class MockPushConnection extends BaseConnection implements
422 PushConnection {
423 MockPushConnection() {
424 final Map<String, Ref> refsMap = new HashMap<>();
425 for (Ref r : advertisedRefs)
426 refsMap.put(r.getName(), r);
427 available(refsMap);
428 }
429
430 @Override
431 public void close() {
432
433 }
434
435 @Override
436 public void push(ProgressMonitor monitor,
437 Map<String, RemoteRefUpdate> refsToUpdate, OutputStream out)
438 throws TransportException {
439 push(monitor, refsToUpdate);
440 }
441
442 @Override
443 public void push(ProgressMonitor monitor,
444 Map<String, RemoteRefUpdate> refsToUpdate)
445 throws TransportException {
446 for (RemoteRefUpdate rru : refsToUpdate.values()) {
447 assertEquals(Status.NOT_ATTEMPTED, rru.getStatus());
448 rru.setStatus(connectionUpdateStatus);
449 }
450 }
451 }
452 }