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