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.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
95
96
97
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
118
119
120
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
141
142
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
168
169
170
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
198
199
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
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 }