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.

TransportTest.java 9.6KB

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