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

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