You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PushProcessTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.IOException;
  15. import java.io.OutputStream;
  16. import java.util.HashMap;
  17. import java.util.HashSet;
  18. import java.util.Map;
  19. import org.eclipse.jgit.errors.NotSupportedException;
  20. import org.eclipse.jgit.errors.TransportException;
  21. import org.eclipse.jgit.lib.ObjectId;
  22. import org.eclipse.jgit.lib.ObjectIdRef;
  23. import org.eclipse.jgit.lib.ProgressMonitor;
  24. import org.eclipse.jgit.lib.Ref;
  25. import org.eclipse.jgit.lib.RefUpdate.Result;
  26. import org.eclipse.jgit.lib.Repository;
  27. import org.eclipse.jgit.lib.TextProgressMonitor;
  28. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  29. import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. public class PushProcessTest extends SampleDataRepositoryTestCase {
  33. private PushProcess process;
  34. private MockTransport transport;
  35. private HashSet<RemoteRefUpdate> refUpdates;
  36. private HashSet<Ref> advertisedRefs;
  37. private Status connectionUpdateStatus;
  38. @Override
  39. @Before
  40. public void setUp() throws Exception {
  41. super.setUp();
  42. transport = new MockTransport(db, new URIish());
  43. refUpdates = new HashSet<>();
  44. advertisedRefs = new HashSet<>();
  45. connectionUpdateStatus = Status.OK;
  46. }
  47. /**
  48. * Test for fast-forward remote update.
  49. *
  50. * @throws IOException
  51. */
  52. @Test
  53. public void testUpdateFastForward() throws IOException {
  54. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  55. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  56. "refs/heads/master", false, null, null);
  57. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  58. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  59. testOneUpdateStatus(rru, ref, Status.OK, Boolean.TRUE);
  60. }
  61. /**
  62. * Test for non fast-forward remote update, when remote object is not known
  63. * to local repository.
  64. *
  65. * @throws IOException
  66. */
  67. @Test
  68. public void testUpdateNonFastForwardUnknownObject() throws IOException {
  69. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  70. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  71. "refs/heads/master", false, null, null);
  72. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  73. ObjectId.fromString("0000000000000000000000000000000000000001"));
  74. testOneUpdateStatus(rru, ref, Status.REJECTED_NONFASTFORWARD, null);
  75. }
  76. /**
  77. * Test for non fast-forward remote update, when remote object is known to
  78. * local repository, but it is not an ancestor of new object.
  79. *
  80. * @throws IOException
  81. */
  82. @Test
  83. public void testUpdateNonFastForward() throws IOException {
  84. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  85. "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
  86. "refs/heads/master", false, null, null);
  87. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  88. ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
  89. testOneUpdateStatus(rru, ref, Status.REJECTED_NONFASTFORWARD, null);
  90. }
  91. /**
  92. * Test for non fast-forward remote update, when force update flag is set.
  93. *
  94. * @throws IOException
  95. */
  96. @Test
  97. public void testUpdateNonFastForwardForced() throws IOException {
  98. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  99. "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
  100. "refs/heads/master", true, null, null);
  101. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  102. ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
  103. testOneUpdateStatus(rru, ref, Status.OK, Boolean.FALSE);
  104. }
  105. /**
  106. * Test for remote ref creation.
  107. *
  108. * @throws IOException
  109. */
  110. @Test
  111. public void testUpdateCreateRef() throws IOException {
  112. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  113. "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
  114. "refs/heads/master", false, null, null);
  115. testOneUpdateStatus(rru, null, Status.OK, Boolean.TRUE);
  116. }
  117. /**
  118. * Test for remote ref deletion.
  119. *
  120. * @throws IOException
  121. */
  122. @Test
  123. public void testUpdateDelete() throws IOException {
  124. final RemoteRefUpdate rru = new RemoteRefUpdate(db, (String) null,
  125. "refs/heads/master", false, null, null);
  126. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  127. ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
  128. testOneUpdateStatus(rru, ref, Status.OK, Boolean.TRUE);
  129. }
  130. /**
  131. * Test for remote ref deletion (try), when that ref doesn't exist on remote
  132. * repo.
  133. *
  134. * @throws IOException
  135. */
  136. @Test
  137. public void testUpdateDeleteNonExisting() throws IOException {
  138. final RemoteRefUpdate rru = new RemoteRefUpdate(db, (String) null,
  139. "refs/heads/master", false, null, null);
  140. testOneUpdateStatus(rru, null, Status.NON_EXISTING, null);
  141. }
  142. /**
  143. * Test for remote ref update, when it is already up to date.
  144. *
  145. * @throws IOException
  146. */
  147. @Test
  148. public void testUpdateUpToDate() throws IOException {
  149. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  150. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  151. "refs/heads/master", false, null, null);
  152. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  153. ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
  154. testOneUpdateStatus(rru, ref, Status.UP_TO_DATE, null);
  155. }
  156. /**
  157. * Test for remote ref update with expected remote object.
  158. *
  159. * @throws IOException
  160. */
  161. @Test
  162. public void testUpdateExpectedRemote() throws IOException {
  163. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  164. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  165. "refs/heads/master", false, null, ObjectId
  166. .fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  167. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  168. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  169. testOneUpdateStatus(rru, ref, Status.OK, Boolean.TRUE);
  170. }
  171. /**
  172. * Test for remote ref update with expected old object set, when old object
  173. * is not that expected one.
  174. *
  175. * @throws IOException
  176. */
  177. @Test
  178. public void testUpdateUnexpectedRemote() throws IOException {
  179. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  180. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  181. "refs/heads/master", false, null, ObjectId
  182. .fromString("0000000000000000000000000000000000000001"));
  183. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  184. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  185. testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null);
  186. }
  187. /**
  188. * Test for remote ref update with expected old object set, when old object
  189. * is not that expected one and force update flag is set (which should have
  190. * lower priority) - shouldn't change behavior.
  191. *
  192. * @throws IOException
  193. */
  194. @Test
  195. public void testUpdateUnexpectedRemoteVsForce() throws IOException {
  196. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  197. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  198. "refs/heads/master", true, null, ObjectId
  199. .fromString("0000000000000000000000000000000000000001"));
  200. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  201. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  202. testOneUpdateStatus(rru, ref, Status.REJECTED_REMOTE_CHANGED, null);
  203. }
  204. /**
  205. * Test for remote ref update, when connection rejects update.
  206. *
  207. * @throws IOException
  208. */
  209. @Test
  210. public void testUpdateRejectedByConnection() throws IOException {
  211. connectionUpdateStatus = Status.REJECTED_OTHER_REASON;
  212. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  213. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  214. "refs/heads/master", false, null, null);
  215. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  216. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  217. testOneUpdateStatus(rru, ref, Status.REJECTED_OTHER_REASON, null);
  218. }
  219. /**
  220. * Test for remote refs updates with mixed cases that shouldn't depend on
  221. * each other.
  222. *
  223. * @throws IOException
  224. */
  225. @Test
  226. public void testUpdateMixedCases() throws IOException {
  227. final RemoteRefUpdate rruOk = new RemoteRefUpdate(db, (String) null,
  228. "refs/heads/master", false, null, null);
  229. final Ref refToChange = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  230. ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
  231. final RemoteRefUpdate rruReject = new RemoteRefUpdate(db,
  232. (String) null, "refs/heads/nonexisting", false, null, null);
  233. refUpdates.add(rruOk);
  234. refUpdates.add(rruReject);
  235. advertisedRefs.add(refToChange);
  236. executePush();
  237. assertEquals(Status.OK, rruOk.getStatus());
  238. assertTrue(rruOk.isFastForward());
  239. assertEquals(Status.NON_EXISTING, rruReject.getStatus());
  240. }
  241. /**
  242. * Test for local tracking ref update.
  243. *
  244. * @throws IOException
  245. */
  246. @Test
  247. public void testTrackingRefUpdateEnabled() throws IOException {
  248. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  249. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  250. "refs/heads/master", false, "refs/remotes/test/master", null);
  251. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  252. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  253. refUpdates.add(rru);
  254. advertisedRefs.add(ref);
  255. final PushResult result = executePush();
  256. final TrackingRefUpdate tru = result
  257. .getTrackingRefUpdate("refs/remotes/test/master");
  258. assertNotNull(tru);
  259. assertEquals("refs/remotes/test/master", tru.getLocalName());
  260. assertEquals(Result.NEW, tru.getResult());
  261. }
  262. /**
  263. * Test for local tracking ref update disabled.
  264. *
  265. * @throws IOException
  266. */
  267. @Test
  268. public void testTrackingRefUpdateDisabled() throws IOException {
  269. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  270. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  271. "refs/heads/master", false, null, null);
  272. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  273. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  274. refUpdates.add(rru);
  275. advertisedRefs.add(ref);
  276. final PushResult result = executePush();
  277. assertTrue(result.getTrackingRefUpdates().isEmpty());
  278. }
  279. /**
  280. * Test for local tracking ref update when remote update has failed.
  281. *
  282. * @throws IOException
  283. */
  284. @Test
  285. public void testTrackingRefUpdateOnReject() throws IOException {
  286. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  287. "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
  288. "refs/heads/master", false, null, null);
  289. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  290. ObjectId.fromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
  291. final PushResult result = testOneUpdateStatus(rru, ref,
  292. Status.REJECTED_NONFASTFORWARD, null);
  293. assertTrue(result.getTrackingRefUpdates().isEmpty());
  294. }
  295. /**
  296. * Test for push operation result - that contains expected elements.
  297. *
  298. * @throws IOException
  299. */
  300. @Test
  301. public void testPushResult() throws IOException {
  302. final RemoteRefUpdate rru = new RemoteRefUpdate(db,
  303. "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
  304. "refs/heads/master", false, "refs/remotes/test/master", null);
  305. final Ref ref = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master",
  306. ObjectId.fromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
  307. refUpdates.add(rru);
  308. advertisedRefs.add(ref);
  309. final PushResult result = executePush();
  310. assertEquals(1, result.getTrackingRefUpdates().size());
  311. assertEquals(1, result.getAdvertisedRefs().size());
  312. assertEquals(1, result.getRemoteUpdates().size());
  313. assertNotNull(result.getTrackingRefUpdate("refs/remotes/test/master"));
  314. assertNotNull(result.getAdvertisedRef("refs/heads/master"));
  315. assertNotNull(result.getRemoteUpdate("refs/heads/master"));
  316. }
  317. private PushResult testOneUpdateStatus(final RemoteRefUpdate rru,
  318. final Ref advertisedRef, final Status expectedStatus,
  319. Boolean fastForward) throws NotSupportedException,
  320. TransportException {
  321. refUpdates.add(rru);
  322. if (advertisedRef != null)
  323. advertisedRefs.add(advertisedRef);
  324. final PushResult result = executePush();
  325. assertEquals(expectedStatus, rru.getStatus());
  326. if (fastForward != null)
  327. assertEquals(fastForward, Boolean.valueOf(rru.isFastForward()));
  328. return result;
  329. }
  330. private PushResult executePush() throws NotSupportedException,
  331. TransportException {
  332. process = new PushProcess(transport, refUpdates);
  333. return process.execute(new TextProgressMonitor());
  334. }
  335. private class MockTransport extends Transport {
  336. MockTransport(Repository local, URIish uri) {
  337. super(local, uri);
  338. }
  339. @Override
  340. public FetchConnection openFetch() throws NotSupportedException,
  341. TransportException {
  342. throw new NotSupportedException("mock");
  343. }
  344. @Override
  345. public PushConnection openPush() throws NotSupportedException,
  346. TransportException {
  347. return new MockPushConnection();
  348. }
  349. @Override
  350. public void close() {
  351. // nothing here
  352. }
  353. }
  354. private class MockPushConnection extends BaseConnection implements
  355. PushConnection {
  356. MockPushConnection() {
  357. final Map<String, Ref> refsMap = new HashMap<>();
  358. for (Ref r : advertisedRefs)
  359. refsMap.put(r.getName(), r);
  360. available(refsMap);
  361. }
  362. @Override
  363. public void close() {
  364. // nothing here
  365. }
  366. @Override
  367. public void push(ProgressMonitor monitor,
  368. Map<String, RemoteRefUpdate> refsToUpdate, OutputStream out)
  369. throws TransportException {
  370. push(monitor, refsToUpdate);
  371. }
  372. @Override
  373. public void push(ProgressMonitor monitor,
  374. Map<String, RemoteRefUpdate> refsToUpdate)
  375. throws TransportException {
  376. for (RemoteRefUpdate rru : refsToUpdate.values()) {
  377. assertEquals(Status.NOT_ATTEMPTED, rru.getStatus());
  378. rru.setStatus(connectionUpdateStatus);
  379. }
  380. }
  381. }
  382. }