Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TransportTest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertNull;
  15. import static org.junit.Assert.assertTrue;
  16. import java.io.IOException;
  17. import java.util.Arrays;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.eclipse.jgit.lib.Config;
  24. import org.eclipse.jgit.lib.Constants;
  25. import org.eclipse.jgit.lib.ObjectId;
  26. import org.eclipse.jgit.lib.Ref;
  27. import org.eclipse.jgit.lib.Repository;
  28. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. public class TransportTest extends SampleDataRepositoryTestCase {
  32. private RemoteConfig remoteConfig;
  33. @Override
  34. @Before
  35. public void setUp() throws Exception {
  36. super.setUp();
  37. final Config config = db.getConfig();
  38. remoteConfig = new RemoteConfig(config, "test");
  39. remoteConfig.addURI(new URIish("http://everyones.loves.git/u/2"));
  40. }
  41. /**
  42. * Test RefSpec to RemoteRefUpdate conversion with simple RefSpec - no
  43. * wildcard, no tracking ref in repo configuration.
  44. *
  45. * @throws IOException
  46. */
  47. @Test
  48. public void testFindRemoteRefUpdatesNoWildcardNoTracking()
  49. throws IOException {
  50. Collection<RemoteRefUpdate> result;
  51. try (Transport transport = Transport.open(db, remoteConfig)) {
  52. result = transport.findRemoteRefUpdatesFor(Collections.nCopies(1,
  53. new RefSpec("refs/heads/master:refs/heads/x")));
  54. }
  55. assertEquals(1, result.size());
  56. final RemoteRefUpdate rru = result.iterator().next();
  57. assertNull(rru.getExpectedOldObjectId());
  58. assertFalse(rru.isForceUpdate());
  59. assertEquals("refs/heads/master", rru.getSrcRef());
  60. assertEquals(db.resolve("refs/heads/master"), rru.getNewObjectId());
  61. assertEquals("refs/heads/x", rru.getRemoteName());
  62. }
  63. /**
  64. * Test RefSpec to RemoteRefUpdate conversion with no-destination RefSpec
  65. * (destination should be set up for the same name as source).
  66. *
  67. * @throws IOException
  68. */
  69. @Test
  70. public void testFindRemoteRefUpdatesNoWildcardNoDestination()
  71. throws IOException {
  72. Collection<RemoteRefUpdate> result;
  73. try (Transport transport = Transport.open(db, remoteConfig)) {
  74. result = transport.findRemoteRefUpdatesFor(
  75. Collections.nCopies(1, new RefSpec("+refs/heads/master")));
  76. }
  77. assertEquals(1, result.size());
  78. final RemoteRefUpdate rru = result.iterator().next();
  79. assertNull(rru.getExpectedOldObjectId());
  80. assertTrue(rru.isForceUpdate());
  81. assertEquals("refs/heads/master", rru.getSrcRef());
  82. assertEquals(db.resolve("refs/heads/master"), rru.getNewObjectId());
  83. assertEquals("refs/heads/master", rru.getRemoteName());
  84. }
  85. /**
  86. * Test RefSpec to RemoteRefUpdate conversion with wildcard RefSpec.
  87. *
  88. * @throws IOException
  89. */
  90. @Test
  91. public void testFindRemoteRefUpdatesWildcardNoTracking() throws IOException {
  92. Collection<RemoteRefUpdate> result;
  93. try (Transport transport = Transport.open(db, remoteConfig)) {
  94. result = transport.findRemoteRefUpdatesFor(Collections.nCopies(1,
  95. new RefSpec("+refs/heads/*:refs/heads/test/*")));
  96. }
  97. assertEquals(12, result.size());
  98. boolean foundA = false;
  99. boolean foundB = false;
  100. for (RemoteRefUpdate rru : result) {
  101. if ("refs/heads/a".equals(rru.getSrcRef())
  102. && "refs/heads/test/a".equals(rru.getRemoteName()))
  103. foundA = true;
  104. if ("refs/heads/b".equals(rru.getSrcRef())
  105. && "refs/heads/test/b".equals(rru.getRemoteName()))
  106. foundB = true;
  107. }
  108. assertTrue(foundA);
  109. assertTrue(foundB);
  110. }
  111. /**
  112. * Test RefSpec to RemoteRefUpdate conversion for more than one RefSpecs
  113. * handling.
  114. *
  115. * @throws IOException
  116. */
  117. @Test
  118. public void testFindRemoteRefUpdatesTwoRefSpecs() throws IOException {
  119. final RefSpec specA = new RefSpec("+refs/heads/a:refs/heads/b");
  120. final RefSpec specC = new RefSpec("+refs/heads/c:refs/heads/d");
  121. final Collection<RefSpec> specs = Arrays.asList(specA, specC);
  122. Collection<RemoteRefUpdate> result;
  123. try (Transport transport = Transport.open(db, remoteConfig)) {
  124. result = transport.findRemoteRefUpdatesFor(specs);
  125. }
  126. assertEquals(2, result.size());
  127. boolean foundA = false;
  128. boolean foundC = false;
  129. for (RemoteRefUpdate rru : result) {
  130. if ("refs/heads/a".equals(rru.getSrcRef())
  131. && "refs/heads/b".equals(rru.getRemoteName()))
  132. foundA = true;
  133. if ("refs/heads/c".equals(rru.getSrcRef())
  134. && "refs/heads/d".equals(rru.getRemoteName()))
  135. foundC = true;
  136. }
  137. assertTrue(foundA);
  138. assertTrue(foundC);
  139. }
  140. /**
  141. * Test RefSpec to RemoteRefUpdate conversion for tracking ref search.
  142. *
  143. * @throws IOException
  144. */
  145. @Test
  146. public void testFindRemoteRefUpdatesTrackingRef() throws IOException {
  147. remoteConfig.addFetchRefSpec(new RefSpec(
  148. "refs/heads/*:refs/remotes/test/*"));
  149. Collection<RemoteRefUpdate> result;
  150. try (Transport transport = Transport.open(db, remoteConfig)) {
  151. result = transport.findRemoteRefUpdatesFor(Collections.nCopies(1,
  152. new RefSpec("+refs/heads/a:refs/heads/a")));
  153. }
  154. assertEquals(1, result.size());
  155. final TrackingRefUpdate tru = result.iterator().next()
  156. .getTrackingRefUpdate();
  157. assertEquals("refs/remotes/test/a", tru.getLocalName());
  158. assertEquals("refs/heads/a", tru.getRemoteName());
  159. assertEquals(db.resolve("refs/heads/a"), tru.getNewObjectId());
  160. assertEquals(ObjectId.zeroId(), tru.getOldObjectId());
  161. }
  162. /**
  163. * Test RefSpec to RemoteRefUpdate conversion with leases.
  164. *
  165. * @throws IOException
  166. */
  167. @Test
  168. public void testFindRemoteRefUpdatesWithLeases() throws IOException {
  169. final RefSpec specA = new RefSpec("+refs/heads/a:refs/heads/b");
  170. final RefSpec specC = new RefSpec("+refs/heads/c:refs/heads/d");
  171. final Collection<RefSpec> specs = Arrays.asList(specA, specC);
  172. final Map<String, RefLeaseSpec> leases = new HashMap<>();
  173. leases.put("refs/heads/b",
  174. new RefLeaseSpec("refs/heads/b", "refs/heads/c"));
  175. Collection<RemoteRefUpdate> result;
  176. try (Transport transport = Transport.open(db, remoteConfig)) {
  177. result = transport.findRemoteRefUpdatesFor(specs, leases);
  178. }
  179. assertEquals(2, result.size());
  180. boolean foundA = false;
  181. boolean foundC = false;
  182. for (RemoteRefUpdate rru : result) {
  183. if ("refs/heads/a".equals(rru.getSrcRef())
  184. && "refs/heads/b".equals(rru.getRemoteName())) {
  185. foundA = true;
  186. assertEquals(db.exactRef("refs/heads/c").getObjectId(),
  187. rru.getExpectedOldObjectId());
  188. }
  189. if ("refs/heads/c".equals(rru.getSrcRef())
  190. && "refs/heads/d".equals(rru.getRemoteName())) {
  191. foundC = true;
  192. assertNull(rru.getExpectedOldObjectId());
  193. }
  194. }
  195. assertTrue(foundA);
  196. assertTrue(foundC);
  197. }
  198. @Test
  199. public void testLocalTransportWithRelativePath() throws Exception {
  200. Repository other = createWorkRepository();
  201. String otherDir = other.getWorkTree().getName();
  202. RemoteConfig config = new RemoteConfig(db.getConfig(), "other");
  203. config.addURI(new URIish("../" + otherDir));
  204. // Should not throw NoRemoteRepositoryException
  205. Transport.open(db, config).close();
  206. }
  207. @Test
  208. public void testLocalTransportFetchWithoutLocalRepository()
  209. throws Exception {
  210. URIish uri = new URIish("file://" + db.getWorkTree().getAbsolutePath());
  211. try (Transport transport = Transport.open(uri)) {
  212. try (FetchConnection fetchConnection = transport.openFetch()) {
  213. Ref head = fetchConnection.getRef(Constants.HEAD);
  214. assertNotNull(head);
  215. }
  216. }
  217. }
  218. @Test
  219. public void testSpi() {
  220. List<TransportProtocol> protocols = Transport.getTransportProtocols();
  221. assertNotNull(protocols);
  222. assertFalse(protocols.isEmpty());
  223. TransportProtocol found = null;
  224. for (TransportProtocol protocol : protocols)
  225. if (protocol.getSchemes().contains(SpiTransport.SCHEME)) {
  226. found = protocol;
  227. break;
  228. }
  229. assertEquals(SpiTransport.PROTO, found);
  230. }
  231. }