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.0KB

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