1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
83
84
85
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
107
108
109
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
131
132
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
159
160
161
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
191
192
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
216
217
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
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 }