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

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