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.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
81
82
83
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
105
106
107
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
129
130
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
157
158
159
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
189
190
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
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 }