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