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