1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.transport;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertSame;
18 import static org.junit.Assert.assertTrue;
19
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23
24 import org.eclipse.jgit.errors.ConfigInvalidException;
25 import org.eclipse.jgit.lib.Config;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 public class RemoteConfigTest {
30 private Config config;
31
32 @Before
33 public void setUp() throws Exception {
34 config = new Config();
35 }
36
37 private void readConfig(String dat) throws ConfigInvalidException {
38 config = new Config();
39 config.fromText(dat);
40 }
41
42 private void checkConfig(String exp) {
43 assertEquals(exp, config.toText());
44 }
45
46 @Test
47 public void testSimple() throws Exception {
48 readConfig("[remote \"spearce\"]\n"
49 + "url = http://www.spearce.org/egit.git\n"
50 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
51
52 final RemoteConfig rc = new RemoteConfig(config, "spearce");
53 final List<URIish> allURIs = rc.getURIs();
54 RefSpec spec;
55
56 assertEquals("spearce", rc.getName());
57 assertNotNull(allURIs);
58 assertNotNull(rc.getFetchRefSpecs());
59 assertNotNull(rc.getPushRefSpecs());
60 assertNotNull(rc.getTagOpt());
61 assertEquals(0, rc.getTimeout());
62 assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());
63
64 assertEquals(1, allURIs.size());
65 assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
66 .toString());
67
68 assertEquals(1, rc.getFetchRefSpecs().size());
69 spec = rc.getFetchRefSpecs().get(0);
70 assertTrue(spec.isForceUpdate());
71 assertTrue(spec.isWildcard());
72 assertEquals("refs/heads/*", spec.getSource());
73 assertEquals("refs/remotes/spearce/*", spec.getDestination());
74
75 assertEquals(0, rc.getPushRefSpecs().size());
76 }
77
78 @Test
79 public void testSimpleNoTags() throws Exception {
80 readConfig("[remote \"spearce\"]\n"
81 + "url = http://www.spearce.org/egit.git\n"
82 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
83 + "tagopt = --no-tags\n");
84 final RemoteConfig rc = new RemoteConfig(config, "spearce");
85 assertSame(TagOpt.NO_TAGS, rc.getTagOpt());
86 }
87
88 @Test
89 public void testSimpleAlwaysTags() throws Exception {
90 readConfig("[remote \"spearce\"]\n"
91 + "url = http://www.spearce.org/egit.git\n"
92 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
93 + "tagopt = --tags\n");
94 final RemoteConfig rc = new RemoteConfig(config, "spearce");
95 assertSame(TagOpt.FETCH_TAGS, rc.getTagOpt());
96 }
97
98 @Test
99 public void testMirror() throws Exception {
100 readConfig("[remote \"spearce\"]\n"
101 + "url = http://www.spearce.org/egit.git\n"
102 + "fetch = +refs/heads/*:refs/heads/*\n"
103 + "fetch = refs/tags/*:refs/tags/*\n");
104
105 final RemoteConfig rc = new RemoteConfig(config, "spearce");
106 final List<URIish> allURIs = rc.getURIs();
107 RefSpec spec;
108
109 assertEquals("spearce", rc.getName());
110 assertNotNull(allURIs);
111 assertNotNull(rc.getFetchRefSpecs());
112 assertNotNull(rc.getPushRefSpecs());
113
114 assertEquals(1, allURIs.size());
115 assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
116 .toString());
117
118 assertEquals(2, rc.getFetchRefSpecs().size());
119
120 spec = rc.getFetchRefSpecs().get(0);
121 assertTrue(spec.isForceUpdate());
122 assertTrue(spec.isWildcard());
123 assertEquals("refs/heads/*", spec.getSource());
124 assertEquals("refs/heads/*", spec.getDestination());
125
126 spec = rc.getFetchRefSpecs().get(1);
127 assertFalse(spec.isForceUpdate());
128 assertTrue(spec.isWildcard());
129 assertEquals("refs/tags/*", spec.getSource());
130 assertEquals("refs/tags/*", spec.getDestination());
131
132 assertEquals(0, rc.getPushRefSpecs().size());
133 }
134
135 @Test
136 public void testBackup() throws Exception {
137 readConfig("[remote \"backup\"]\n"
138 + "url = http://www.spearce.org/egit.git\n"
139 + "url = user@repo.or.cz:/srv/git/egit.git\n"
140 + "push = +refs/heads/*:refs/heads/*\n"
141 + "push = refs/tags/*:refs/tags/*\n");
142
143 final RemoteConfig rc = new RemoteConfig(config, "backup");
144 final List<URIish> allURIs = rc.getURIs();
145 RefSpec spec;
146
147 assertEquals("backup", rc.getName());
148 assertNotNull(allURIs);
149 assertNotNull(rc.getFetchRefSpecs());
150 assertNotNull(rc.getPushRefSpecs());
151
152 assertEquals(2, allURIs.size());
153 assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
154 .toString());
155 assertEquals("user@repo.or.cz:/srv/git/egit.git", allURIs.get(1)
156 .toString());
157
158 assertEquals(0, rc.getFetchRefSpecs().size());
159
160 assertEquals(2, rc.getPushRefSpecs().size());
161 spec = rc.getPushRefSpecs().get(0);
162 assertTrue(spec.isForceUpdate());
163 assertTrue(spec.isWildcard());
164 assertEquals("refs/heads/*", spec.getSource());
165 assertEquals("refs/heads/*", spec.getDestination());
166
167 spec = rc.getPushRefSpecs().get(1);
168 assertFalse(spec.isForceUpdate());
169 assertTrue(spec.isWildcard());
170 assertEquals("refs/tags/*", spec.getSource());
171 assertEquals("refs/tags/*", spec.getDestination());
172 }
173
174 @Test
175 public void testUploadPack() throws Exception {
176 readConfig("[remote \"example\"]\n"
177 + "url = user@example.com:egit.git\n"
178 + "fetch = +refs/heads/*:refs/remotes/example/*\n"
179 + "uploadpack = /path/to/git/git-upload-pack\n"
180 + "receivepack = /path/to/git/git-receive-pack\n");
181
182 final RemoteConfig rc = new RemoteConfig(config, "example");
183 final List<URIish> allURIs = rc.getURIs();
184 RefSpec spec;
185
186 assertEquals("example", rc.getName());
187 assertNotNull(allURIs);
188 assertNotNull(rc.getFetchRefSpecs());
189 assertNotNull(rc.getPushRefSpecs());
190
191 assertEquals(1, allURIs.size());
192 assertEquals("user@example.com:egit.git", allURIs.get(0).toString());
193
194 assertEquals(1, rc.getFetchRefSpecs().size());
195 spec = rc.getFetchRefSpecs().get(0);
196 assertTrue(spec.isForceUpdate());
197 assertTrue(spec.isWildcard());
198 assertEquals("refs/heads/*", spec.getSource());
199 assertEquals("refs/remotes/example/*", spec.getDestination());
200
201 assertEquals(0, rc.getPushRefSpecs().size());
202
203 assertEquals("/path/to/git/git-upload-pack", rc.getUploadPack());
204 assertEquals("/path/to/git/git-receive-pack", rc.getReceivePack());
205 }
206
207 @Test
208 public void testUnknown() throws Exception {
209 readConfig("");
210
211 final RemoteConfig rc = new RemoteConfig(config, "backup");
212 assertEquals(0, rc.getURIs().size());
213 assertEquals(0, rc.getFetchRefSpecs().size());
214 assertEquals(0, rc.getPushRefSpecs().size());
215 assertEquals("git-upload-pack", rc.getUploadPack());
216 assertEquals("git-receive-pack", rc.getReceivePack());
217 }
218
219 @Test
220 public void testAddURI() throws Exception {
221 readConfig("");
222
223 final URIish uri = new URIish("/some/dir");
224 final RemoteConfig rc = new RemoteConfig(config, "backup");
225 assertEquals(0, rc.getURIs().size());
226
227 assertTrue(rc.addURI(uri));
228 assertEquals(1, rc.getURIs().size());
229 assertSame(uri, rc.getURIs().get(0));
230
231 assertFalse(rc.addURI(new URIish(uri.toString())));
232 assertEquals(1, rc.getURIs().size());
233 }
234
235 @Test
236 public void testRemoveFirstURI() throws Exception {
237 readConfig("");
238
239 final URIish a = new URIish("/some/dir");
240 final URIish b = new URIish("/another/dir");
241 final URIish c = new URIish("/more/dirs");
242 final RemoteConfig rc = new RemoteConfig(config, "backup");
243 assertTrue(rc.addURI(a));
244 assertTrue(rc.addURI(b));
245 assertTrue(rc.addURI(c));
246
247 assertEquals(3, rc.getURIs().size());
248 assertSame(a, rc.getURIs().get(0));
249 assertSame(b, rc.getURIs().get(1));
250 assertSame(c, rc.getURIs().get(2));
251
252 assertTrue(rc.removeURI(a));
253 assertEquals(2, rc.getURIs().size());
254 assertSame(b, rc.getURIs().get(0));
255 assertSame(c, rc.getURIs().get(1));
256 }
257
258 @Test
259 public void testRemoveMiddleURI() throws Exception {
260 readConfig("");
261
262 final URIish a = new URIish("/some/dir");
263 final URIish b = new URIish("/another/dir");
264 final URIish c = new URIish("/more/dirs");
265 final RemoteConfig rc = new RemoteConfig(config, "backup");
266 assertTrue(rc.addURI(a));
267 assertTrue(rc.addURI(b));
268 assertTrue(rc.addURI(c));
269
270 assertEquals(3, rc.getURIs().size());
271 assertSame(a, rc.getURIs().get(0));
272 assertSame(b, rc.getURIs().get(1));
273 assertSame(c, rc.getURIs().get(2));
274
275 assertTrue(rc.removeURI(b));
276 assertEquals(2, rc.getURIs().size());
277 assertSame(a, rc.getURIs().get(0));
278 assertSame(c, rc.getURIs().get(1));
279 }
280
281 @Test
282 public void testRemoveLastURI() throws Exception {
283 readConfig("");
284
285 final URIish a = new URIish("/some/dir");
286 final URIish b = new URIish("/another/dir");
287 final URIish c = new URIish("/more/dirs");
288 final RemoteConfig rc = new RemoteConfig(config, "backup");
289 assertTrue(rc.addURI(a));
290 assertTrue(rc.addURI(b));
291 assertTrue(rc.addURI(c));
292
293 assertEquals(3, rc.getURIs().size());
294 assertSame(a, rc.getURIs().get(0));
295 assertSame(b, rc.getURIs().get(1));
296 assertSame(c, rc.getURIs().get(2));
297
298 assertTrue(rc.removeURI(c));
299 assertEquals(2, rc.getURIs().size());
300 assertSame(a, rc.getURIs().get(0));
301 assertSame(b, rc.getURIs().get(1));
302 }
303
304 @Test
305 public void testRemoveOnlyURI() throws Exception {
306 readConfig("");
307
308 final URIish a = new URIish("/some/dir");
309 final RemoteConfig rc = new RemoteConfig(config, "backup");
310 assertTrue(rc.addURI(a));
311
312 assertEquals(1, rc.getURIs().size());
313 assertSame(a, rc.getURIs().get(0));
314
315 assertTrue(rc.removeURI(a));
316 assertEquals(0, rc.getURIs().size());
317 }
318
319 @Test
320 public void testCreateOrigin() throws Exception {
321 final RemoteConfig rc = new RemoteConfig(config, "origin");
322 rc.addURI(new URIish("/some/dir"));
323 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
324 + rc.getName() + "/*"));
325 rc.update(config);
326 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
327 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
328 }
329
330 @Test
331 public void testSaveAddURI() throws Exception {
332 readConfig("[remote \"spearce\"]\n"
333 + "url = http://www.spearce.org/egit.git\n"
334 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
335
336 final RemoteConfig rc = new RemoteConfig(config, "spearce");
337 rc.addURI(new URIish("/some/dir"));
338 assertEquals(2, rc.getURIs().size());
339 rc.update(config);
340 checkConfig("[remote \"spearce\"]\n"
341 + "\turl = http://www.spearce.org/egit.git\n"
342 + "\turl = /some/dir\n"
343 + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
344 }
345
346 @Test
347 public void testSaveRemoveLastURI() throws Exception {
348 readConfig("[remote \"spearce\"]\n"
349 + "url = http://www.spearce.org/egit.git\n"
350 + "url = /some/dir\n"
351 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
352
353 final RemoteConfig rc = new RemoteConfig(config, "spearce");
354 assertEquals(2, rc.getURIs().size());
355 rc.removeURI(new URIish("/some/dir"));
356 assertEquals(1, rc.getURIs().size());
357 rc.update(config);
358 checkConfig("[remote \"spearce\"]\n"
359 + "\turl = http://www.spearce.org/egit.git\n"
360 + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
361 }
362
363 @Test
364 public void testSaveRemoveFirstURI() throws Exception {
365 readConfig("[remote \"spearce\"]\n"
366 + "url = http://www.spearce.org/egit.git\n"
367 + "url = /some/dir\n"
368 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
369
370 final RemoteConfig rc = new RemoteConfig(config, "spearce");
371 assertEquals(2, rc.getURIs().size());
372 rc.removeURI(new URIish("http://www.spearce.org/egit.git"));
373 assertEquals(1, rc.getURIs().size());
374 rc.update(config);
375 checkConfig("[remote \"spearce\"]\n" + "\turl = /some/dir\n"
376 + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
377 }
378
379 @Test
380 public void testSaveNoTags() throws Exception {
381 final RemoteConfig rc = new RemoteConfig(config, "origin");
382 rc.addURI(new URIish("/some/dir"));
383 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
384 + rc.getName() + "/*"));
385 rc.setTagOpt(TagOpt.NO_TAGS);
386 rc.update(config);
387 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
388 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
389 + "\ttagopt = --no-tags\n");
390 }
391
392 @Test
393 public void testSaveAllTags() throws Exception {
394 final RemoteConfig rc = new RemoteConfig(config, "origin");
395 rc.addURI(new URIish("/some/dir"));
396 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
397 + rc.getName() + "/*"));
398 rc.setTagOpt(TagOpt.FETCH_TAGS);
399 rc.update(config);
400 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
401 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
402 + "\ttagopt = --tags\n");
403 }
404
405 @Test
406 public void testSimpleTimeout() throws Exception {
407 readConfig("[remote \"spearce\"]\n"
408 + "url = http://www.spearce.org/egit.git\n"
409 + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
410 + "timeout = 12\n");
411 final RemoteConfig rc = new RemoteConfig(config, "spearce");
412 assertEquals(12, rc.getTimeout());
413 }
414
415 @Test
416 public void testSaveTimeout() throws Exception {
417 final RemoteConfig rc = new RemoteConfig(config, "origin");
418 rc.addURI(new URIish("/some/dir"));
419 rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
420 + rc.getName() + "/*"));
421 rc.setTimeout(60);
422 rc.update(config);
423 checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
424 + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
425 + "\ttimeout = 60\n");
426 }
427
428 @Test
429 public void noInsteadOf() throws Exception {
430 config.setString("remote", "origin", "url", "short:project.git");
431 config.setString("url", "https://server/repos/", "name", "short:");
432 RemoteConfig rc = new RemoteConfig(config, "origin");
433 assertFalse(rc.getURIs().isEmpty());
434 assertEquals("short:project.git", rc.getURIs().get(0).toASCIIString());
435 }
436
437 @Test
438 public void singleInsteadOf() throws Exception {
439 config.setString("remote", "origin", "url", "short:project.git");
440 config.setString("url", "https://server/repos/", "insteadOf", "short:");
441 RemoteConfig rc = new RemoteConfig(config, "origin");
442 assertFalse(rc.getURIs().isEmpty());
443 assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
444 .toASCIIString());
445 }
446
447 @Test
448 public void multipleInsteadOf() throws Exception {
449 config.setString("remote", "origin", "url", "prefixproject.git");
450 config.setStringList("url", "https://server/repos/", "insteadOf",
451 Arrays.asList("pre", "prefix", "pref", "perf"));
452 RemoteConfig rc = new RemoteConfig(config, "origin");
453 assertFalse(rc.getURIs().isEmpty());
454 assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
455 .toASCIIString());
456 }
457
458 @Test
459 public void noPushInsteadOf() throws Exception {
460 config.setString("remote", "origin", "pushurl", "short:project.git");
461 config.setString("url", "https://server/repos/", "name", "short:");
462 RemoteConfig rc = new RemoteConfig(config, "origin");
463 assertFalse(rc.getPushURIs().isEmpty());
464 assertEquals("short:project.git", rc.getPushURIs().get(0)
465 .toASCIIString());
466 }
467
468 @Test
469 public void pushInsteadOfNotAppliedToPushUri() throws Exception {
470 config.setString("remote", "origin", "pushurl", "short:project.git");
471 config.setString("url", "https://server/repos/", "pushInsteadOf",
472 "short:");
473 RemoteConfig rc = new RemoteConfig(config, "origin");
474 assertFalse(rc.getPushURIs().isEmpty());
475 assertEquals("short:project.git",
476 rc.getPushURIs().get(0).toASCIIString());
477 }
478
479 @Test
480 public void pushInsteadOfAppliedToUri() throws Exception {
481 config.setString("remote", "origin", "url", "short:project.git");
482 config.setString("url", "https://server/repos/", "pushInsteadOf",
483 "short:");
484 RemoteConfig rc = new RemoteConfig(config, "origin");
485 assertFalse(rc.getPushURIs().isEmpty());
486 assertEquals("https://server/repos/project.git",
487 rc.getPushURIs().get(0).toASCIIString());
488 }
489
490 @Test
491 public void multiplePushInsteadOf() throws Exception {
492 config.setString("remote", "origin", "url", "prefixproject.git");
493 config.setStringList("url", "https://server/repos/", "pushInsteadOf",
494 Arrays.asList("pre", "prefix", "pref", "perf"));
495 RemoteConfig rc = new RemoteConfig(config, "origin");
496 assertFalse(rc.getPushURIs().isEmpty());
497 assertEquals("https://server/repos/project.git", rc.getPushURIs()
498 .get(0).toASCIIString());
499 }
500
501 @Test
502 public void pushInsteadOfNoPushUrl() throws Exception {
503 config.setString("remote", "origin", "url",
504 "http://git.eclipse.org/gitroot/jgit/jgit");
505 config.setStringList("url", "ssh://someone@git.eclipse.org:29418/",
506 "pushInsteadOf",
507 Collections.singletonList("http://git.eclipse.org/gitroot/"));
508 RemoteConfig rc = new RemoteConfig(config, "origin");
509 assertFalse(rc.getPushURIs().isEmpty());
510 assertEquals("ssh://someone@git.eclipse.org:29418/jgit/jgit",
511 rc.getPushURIs().get(0).toASCIIString());
512 }
513 }