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

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