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