View Javadoc
1   /*
2    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  	 * Test for fast-forward remote update.
93  	 *
94  	 * @throws IOException
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 	 * Test for non fast-forward remote update, when remote object is not known
108 	 * to local repository.
109 	 *
110 	 * @throws IOException
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 	 * Test for non fast-forward remote update, when remote object is known to
124 	 * local repository, but it is not an ancestor of new object.
125 	 *
126 	 * @throws IOException
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 	 * Test for non fast-forward remote update, when force update flag is set.
140 	 *
141 	 * @throws IOException
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 	 * Test for remote ref creation.
155 	 *
156 	 * @throws IOException
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 	 * Test for remote ref deletion.
168 	 *
169 	 * @throws IOException
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 	 * Test for remote ref deletion (try), when that ref doesn't exist on remote
182 	 * repo.
183 	 *
184 	 * @throws IOException
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 	 * Test for remote ref update, when it is already up to date.
195 	 *
196 	 * @throws IOException
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 	 * Test for remote ref update with expected remote object.
210 	 *
211 	 * @throws IOException
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 	 * Test for remote ref update with expected old object set, when old object
226 	 * is not that expected one.
227 	 *
228 	 * @throws IOException
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 	 * Test for remote ref update with expected old object set, when old object
243 	 * is not that expected one and force update flag is set (which should have
244 	 * lower priority) - shouldn't change behavior.
245 	 *
246 	 * @throws IOException
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 	 * Test for remote ref update, when connection rejects update.
261 	 *
262 	 * @throws IOException
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 	 * Test for remote refs updates with mixed cases that shouldn't depend on
277 	 * each other.
278 	 *
279 	 * @throws IOException
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 	 * Test for local tracking ref update.
300 	 *
301 	 * @throws IOException
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 	 * Test for local tracking ref update disabled.
322 	 *
323 	 * @throws IOException
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 	 * Test for local tracking ref update when remote update has failed.
340 	 *
341 	 * @throws IOException
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 	 * Test for push operation result - that contains expected elements.
357 	 *
358 	 * @throws IOException
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 			// nothing here
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 			// nothing here
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 }