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 15KB

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