View Javadoc
1   /*
2    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.transport;
46  
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertNotNull;
50  import static org.junit.Assert.assertSame;
51  import static org.junit.Assert.assertTrue;
52  
53  import java.util.Arrays;
54  import java.util.List;
55  
56  import org.eclipse.jgit.errors.ConfigInvalidException;
57  import org.eclipse.jgit.lib.Config;
58  import org.junit.Before;
59  import org.junit.Test;
60  
61  public class RemoteConfigTest {
62  	private Config config;
63  
64  	@Before
65  	public void setUp() throws Exception {
66  		config = new Config();
67  	}
68  
69  	private void readConfig(final String dat) throws ConfigInvalidException {
70  		config = new Config();
71  		config.fromText(dat);
72  	}
73  
74  	private void checkConfig(final String exp) {
75  		assertEquals(exp, config.toText());
76  	}
77  
78  	@Test
79  	public void testSimple() 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  
84  		final RemoteConfig rc = new RemoteConfig(config, "spearce");
85  		final List<URIish> allURIs = rc.getURIs();
86  		RefSpec spec;
87  
88  		assertEquals("spearce", rc.getName());
89  		assertNotNull(allURIs);
90  		assertNotNull(rc.getFetchRefSpecs());
91  		assertNotNull(rc.getPushRefSpecs());
92  		assertNotNull(rc.getTagOpt());
93  		assertEquals(0, rc.getTimeout());
94  		assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());
95  
96  		assertEquals(1, allURIs.size());
97  		assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
98  				.toString());
99  
100 		assertEquals(1, rc.getFetchRefSpecs().size());
101 		spec = rc.getFetchRefSpecs().get(0);
102 		assertTrue(spec.isForceUpdate());
103 		assertTrue(spec.isWildcard());
104 		assertEquals("refs/heads/*", spec.getSource());
105 		assertEquals("refs/remotes/spearce/*", spec.getDestination());
106 
107 		assertEquals(0, rc.getPushRefSpecs().size());
108 	}
109 
110 	@Test
111 	public void testSimpleNoTags() throws Exception {
112 		readConfig("[remote \"spearce\"]\n"
113 				+ "url = http://www.spearce.org/egit.git\n"
114 				+ "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
115 				+ "tagopt = --no-tags\n");
116 		final RemoteConfig rc = new RemoteConfig(config, "spearce");
117 		assertSame(TagOpt.NO_TAGS, rc.getTagOpt());
118 	}
119 
120 	@Test
121 	public void testSimpleAlwaysTags() throws Exception {
122 		readConfig("[remote \"spearce\"]\n"
123 				+ "url = http://www.spearce.org/egit.git\n"
124 				+ "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
125 				+ "tagopt = --tags\n");
126 		final RemoteConfig rc = new RemoteConfig(config, "spearce");
127 		assertSame(TagOpt.FETCH_TAGS, rc.getTagOpt());
128 	}
129 
130 	@Test
131 	public void testMirror() throws Exception {
132 		readConfig("[remote \"spearce\"]\n"
133 				+ "url = http://www.spearce.org/egit.git\n"
134 				+ "fetch = +refs/heads/*:refs/heads/*\n"
135 				+ "fetch = refs/tags/*:refs/tags/*\n");
136 
137 		final RemoteConfig rc = new RemoteConfig(config, "spearce");
138 		final List<URIish> allURIs = rc.getURIs();
139 		RefSpec spec;
140 
141 		assertEquals("spearce", rc.getName());
142 		assertNotNull(allURIs);
143 		assertNotNull(rc.getFetchRefSpecs());
144 		assertNotNull(rc.getPushRefSpecs());
145 
146 		assertEquals(1, allURIs.size());
147 		assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
148 				.toString());
149 
150 		assertEquals(2, rc.getFetchRefSpecs().size());
151 
152 		spec = rc.getFetchRefSpecs().get(0);
153 		assertTrue(spec.isForceUpdate());
154 		assertTrue(spec.isWildcard());
155 		assertEquals("refs/heads/*", spec.getSource());
156 		assertEquals("refs/heads/*", spec.getDestination());
157 
158 		spec = rc.getFetchRefSpecs().get(1);
159 		assertFalse(spec.isForceUpdate());
160 		assertTrue(spec.isWildcard());
161 		assertEquals("refs/tags/*", spec.getSource());
162 		assertEquals("refs/tags/*", spec.getDestination());
163 
164 		assertEquals(0, rc.getPushRefSpecs().size());
165 	}
166 
167 	@Test
168 	public void testBackup() throws Exception {
169 		readConfig("[remote \"backup\"]\n"
170 				+ "url = http://www.spearce.org/egit.git\n"
171 				+ "url = user@repo.or.cz:/srv/git/egit.git\n"
172 				+ "push = +refs/heads/*:refs/heads/*\n"
173 				+ "push = refs/tags/*:refs/tags/*\n");
174 
175 		final RemoteConfig rc = new RemoteConfig(config, "backup");
176 		final List<URIish> allURIs = rc.getURIs();
177 		RefSpec spec;
178 
179 		assertEquals("backup", rc.getName());
180 		assertNotNull(allURIs);
181 		assertNotNull(rc.getFetchRefSpecs());
182 		assertNotNull(rc.getPushRefSpecs());
183 
184 		assertEquals(2, allURIs.size());
185 		assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
186 				.toString());
187 		assertEquals("user@repo.or.cz:/srv/git/egit.git", allURIs.get(1)
188 				.toString());
189 
190 		assertEquals(0, rc.getFetchRefSpecs().size());
191 
192 		assertEquals(2, rc.getPushRefSpecs().size());
193 		spec = rc.getPushRefSpecs().get(0);
194 		assertTrue(spec.isForceUpdate());
195 		assertTrue(spec.isWildcard());
196 		assertEquals("refs/heads/*", spec.getSource());
197 		assertEquals("refs/heads/*", spec.getDestination());
198 
199 		spec = rc.getPushRefSpecs().get(1);
200 		assertFalse(spec.isForceUpdate());
201 		assertTrue(spec.isWildcard());
202 		assertEquals("refs/tags/*", spec.getSource());
203 		assertEquals("refs/tags/*", spec.getDestination());
204 	}
205 
206 	@Test
207 	public void testUploadPack() throws Exception {
208 		readConfig("[remote \"example\"]\n"
209 				+ "url = user@example.com:egit.git\n"
210 				+ "fetch = +refs/heads/*:refs/remotes/example/*\n"
211 				+ "uploadpack = /path/to/git/git-upload-pack\n"
212 				+ "receivepack = /path/to/git/git-receive-pack\n");
213 
214 		final RemoteConfig rc = new RemoteConfig(config, "example");
215 		final List<URIish> allURIs = rc.getURIs();
216 		RefSpec spec;
217 
218 		assertEquals("example", rc.getName());
219 		assertNotNull(allURIs);
220 		assertNotNull(rc.getFetchRefSpecs());
221 		assertNotNull(rc.getPushRefSpecs());
222 
223 		assertEquals(1, allURIs.size());
224 		assertEquals("user@example.com:egit.git", allURIs.get(0).toString());
225 
226 		assertEquals(1, rc.getFetchRefSpecs().size());
227 		spec = rc.getFetchRefSpecs().get(0);
228 		assertTrue(spec.isForceUpdate());
229 		assertTrue(spec.isWildcard());
230 		assertEquals("refs/heads/*", spec.getSource());
231 		assertEquals("refs/remotes/example/*", spec.getDestination());
232 
233 		assertEquals(0, rc.getPushRefSpecs().size());
234 
235 		assertEquals("/path/to/git/git-upload-pack", rc.getUploadPack());
236 		assertEquals("/path/to/git/git-receive-pack", rc.getReceivePack());
237 	}
238 
239 	@Test
240 	public void testUnknown() throws Exception {
241 		readConfig("");
242 
243 		final RemoteConfig rc = new RemoteConfig(config, "backup");
244 		assertEquals(0, rc.getURIs().size());
245 		assertEquals(0, rc.getFetchRefSpecs().size());
246 		assertEquals(0, rc.getPushRefSpecs().size());
247 		assertEquals("git-upload-pack", rc.getUploadPack());
248 		assertEquals("git-receive-pack", rc.getReceivePack());
249 	}
250 
251 	@Test
252 	public void testAddURI() throws Exception {
253 		readConfig("");
254 
255 		final URIish uri = new URIish("/some/dir");
256 		final RemoteConfig rc = new RemoteConfig(config, "backup");
257 		assertEquals(0, rc.getURIs().size());
258 
259 		assertTrue(rc.addURI(uri));
260 		assertEquals(1, rc.getURIs().size());
261 		assertSame(uri, rc.getURIs().get(0));
262 
263 		assertFalse(rc.addURI(new URIish(uri.toString())));
264 		assertEquals(1, rc.getURIs().size());
265 	}
266 
267 	@Test
268 	public void testRemoveFirstURI() throws Exception {
269 		readConfig("");
270 
271 		final URIish a = new URIish("/some/dir");
272 		final URIish b = new URIish("/another/dir");
273 		final URIish c = new URIish("/more/dirs");
274 		final RemoteConfig rc = new RemoteConfig(config, "backup");
275 		assertTrue(rc.addURI(a));
276 		assertTrue(rc.addURI(b));
277 		assertTrue(rc.addURI(c));
278 
279 		assertEquals(3, rc.getURIs().size());
280 		assertSame(a, rc.getURIs().get(0));
281 		assertSame(b, rc.getURIs().get(1));
282 		assertSame(c, rc.getURIs().get(2));
283 
284 		assertTrue(rc.removeURI(a));
285 		assertEquals(2, rc.getURIs().size());
286 		assertSame(b, rc.getURIs().get(0));
287 		assertSame(c, rc.getURIs().get(1));
288 	}
289 
290 	@Test
291 	public void testRemoveMiddleURI() throws Exception {
292 		readConfig("");
293 
294 		final URIish a = new URIish("/some/dir");
295 		final URIish b = new URIish("/another/dir");
296 		final URIish c = new URIish("/more/dirs");
297 		final RemoteConfig rc = new RemoteConfig(config, "backup");
298 		assertTrue(rc.addURI(a));
299 		assertTrue(rc.addURI(b));
300 		assertTrue(rc.addURI(c));
301 
302 		assertEquals(3, rc.getURIs().size());
303 		assertSame(a, rc.getURIs().get(0));
304 		assertSame(b, rc.getURIs().get(1));
305 		assertSame(c, rc.getURIs().get(2));
306 
307 		assertTrue(rc.removeURI(b));
308 		assertEquals(2, rc.getURIs().size());
309 		assertSame(a, rc.getURIs().get(0));
310 		assertSame(c, rc.getURIs().get(1));
311 	}
312 
313 	@Test
314 	public void testRemoveLastURI() throws Exception {
315 		readConfig("");
316 
317 		final URIish a = new URIish("/some/dir");
318 		final URIish b = new URIish("/another/dir");
319 		final URIish c = new URIish("/more/dirs");
320 		final RemoteConfig rc = new RemoteConfig(config, "backup");
321 		assertTrue(rc.addURI(a));
322 		assertTrue(rc.addURI(b));
323 		assertTrue(rc.addURI(c));
324 
325 		assertEquals(3, rc.getURIs().size());
326 		assertSame(a, rc.getURIs().get(0));
327 		assertSame(b, rc.getURIs().get(1));
328 		assertSame(c, rc.getURIs().get(2));
329 
330 		assertTrue(rc.removeURI(c));
331 		assertEquals(2, rc.getURIs().size());
332 		assertSame(a, rc.getURIs().get(0));
333 		assertSame(b, rc.getURIs().get(1));
334 	}
335 
336 	@Test
337 	public void testRemoveOnlyURI() throws Exception {
338 		readConfig("");
339 
340 		final URIish a = new URIish("/some/dir");
341 		final RemoteConfig rc = new RemoteConfig(config, "backup");
342 		assertTrue(rc.addURI(a));
343 
344 		assertEquals(1, rc.getURIs().size());
345 		assertSame(a, rc.getURIs().get(0));
346 
347 		assertTrue(rc.removeURI(a));
348 		assertEquals(0, rc.getURIs().size());
349 	}
350 
351 	@Test
352 	public void testCreateOrigin() throws Exception {
353 		final RemoteConfig rc = new RemoteConfig(config, "origin");
354 		rc.addURI(new URIish("/some/dir"));
355 		rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
356 				+ rc.getName() + "/*"));
357 		rc.update(config);
358 		checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
359 				+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
360 	}
361 
362 	@Test
363 	public void testSaveAddURI() throws Exception {
364 		readConfig("[remote \"spearce\"]\n"
365 				+ "url = http://www.spearce.org/egit.git\n"
366 				+ "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
367 
368 		final RemoteConfig rc = new RemoteConfig(config, "spearce");
369 		rc.addURI(new URIish("/some/dir"));
370 		assertEquals(2, rc.getURIs().size());
371 		rc.update(config);
372 		checkConfig("[remote \"spearce\"]\n"
373 				+ "\turl = http://www.spearce.org/egit.git\n"
374 				+ "\turl = /some/dir\n"
375 				+ "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
376 	}
377 
378 	@Test
379 	public void testSaveRemoveLastURI() throws Exception {
380 		readConfig("[remote \"spearce\"]\n"
381 				+ "url = http://www.spearce.org/egit.git\n"
382 				+ "url = /some/dir\n"
383 				+ "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
384 
385 		final RemoteConfig rc = new RemoteConfig(config, "spearce");
386 		assertEquals(2, rc.getURIs().size());
387 		rc.removeURI(new URIish("/some/dir"));
388 		assertEquals(1, rc.getURIs().size());
389 		rc.update(config);
390 		checkConfig("[remote \"spearce\"]\n"
391 				+ "\turl = http://www.spearce.org/egit.git\n"
392 				+ "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
393 	}
394 
395 	@Test
396 	public void testSaveRemoveFirstURI() throws Exception {
397 		readConfig("[remote \"spearce\"]\n"
398 				+ "url = http://www.spearce.org/egit.git\n"
399 				+ "url = /some/dir\n"
400 				+ "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
401 
402 		final RemoteConfig rc = new RemoteConfig(config, "spearce");
403 		assertEquals(2, rc.getURIs().size());
404 		rc.removeURI(new URIish("http://www.spearce.org/egit.git"));
405 		assertEquals(1, rc.getURIs().size());
406 		rc.update(config);
407 		checkConfig("[remote \"spearce\"]\n" + "\turl = /some/dir\n"
408 				+ "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
409 	}
410 
411 	@Test
412 	public void testSaveNoTags() throws Exception {
413 		final RemoteConfig rc = new RemoteConfig(config, "origin");
414 		rc.addURI(new URIish("/some/dir"));
415 		rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
416 				+ rc.getName() + "/*"));
417 		rc.setTagOpt(TagOpt.NO_TAGS);
418 		rc.update(config);
419 		checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
420 				+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
421 				+ "\ttagopt = --no-tags\n");
422 	}
423 
424 	@Test
425 	public void testSaveAllTags() throws Exception {
426 		final RemoteConfig rc = new RemoteConfig(config, "origin");
427 		rc.addURI(new URIish("/some/dir"));
428 		rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
429 				+ rc.getName() + "/*"));
430 		rc.setTagOpt(TagOpt.FETCH_TAGS);
431 		rc.update(config);
432 		checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
433 				+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
434 				+ "\ttagopt = --tags\n");
435 	}
436 
437 	@Test
438 	public void testSimpleTimeout() throws Exception {
439 		readConfig("[remote \"spearce\"]\n"
440 				+ "url = http://www.spearce.org/egit.git\n"
441 				+ "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
442 				+ "timeout = 12\n");
443 		final RemoteConfig rc = new RemoteConfig(config, "spearce");
444 		assertEquals(12, rc.getTimeout());
445 	}
446 
447 	@Test
448 	public void testSaveTimeout() throws Exception {
449 		final RemoteConfig rc = new RemoteConfig(config, "origin");
450 		rc.addURI(new URIish("/some/dir"));
451 		rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
452 				+ rc.getName() + "/*"));
453 		rc.setTimeout(60);
454 		rc.update(config);
455 		checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
456 				+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
457 				+ "\ttimeout = 60\n");
458 	}
459 
460 	@Test
461 	public void noInsteadOf() throws Exception {
462 		config.setString("remote", "origin", "url", "short:project.git");
463 		config.setString("url", "https://server/repos/", "name", "short:");
464 		RemoteConfig rc = new RemoteConfig(config, "origin");
465 		assertFalse(rc.getURIs().isEmpty());
466 		assertEquals("short:project.git", rc.getURIs().get(0).toASCIIString());
467 	}
468 
469 	@Test
470 	public void singleInsteadOf() throws Exception {
471 		config.setString("remote", "origin", "url", "short:project.git");
472 		config.setString("url", "https://server/repos/", "insteadOf", "short:");
473 		RemoteConfig rc = new RemoteConfig(config, "origin");
474 		assertFalse(rc.getURIs().isEmpty());
475 		assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
476 				.toASCIIString());
477 	}
478 
479 	@Test
480 	public void multipleInsteadOf() throws Exception {
481 		config.setString("remote", "origin", "url", "prefixproject.git");
482 		config.setStringList("url", "https://server/repos/", "insteadOf",
483 				Arrays.asList("pre", "prefix", "pref", "perf"));
484 		RemoteConfig rc = new RemoteConfig(config, "origin");
485 		assertFalse(rc.getURIs().isEmpty());
486 		assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
487 				.toASCIIString());
488 	}
489 
490 	@Test
491 	public void noPushInsteadOf() throws Exception {
492 		config.setString("remote", "origin", "pushurl", "short:project.git");
493 		config.setString("url", "https://server/repos/", "name", "short:");
494 		RemoteConfig rc = new RemoteConfig(config, "origin");
495 		assertFalse(rc.getPushURIs().isEmpty());
496 		assertEquals("short:project.git", rc.getPushURIs().get(0)
497 				.toASCIIString());
498 	}
499 
500 	@Test
501 	public void singlePushInsteadOf() throws Exception {
502 		config.setString("remote", "origin", "pushurl", "short:project.git");
503 		config.setString("url", "https://server/repos/", "pushInsteadOf",
504 				"short:");
505 		RemoteConfig rc = new RemoteConfig(config, "origin");
506 		assertFalse(rc.getPushURIs().isEmpty());
507 		assertEquals("https://server/repos/project.git", rc.getPushURIs()
508 				.get(0).toASCIIString());
509 	}
510 
511 	@Test
512 	public void multiplePushInsteadOf() throws Exception {
513 		config.setString("remote", "origin", "pushurl", "prefixproject.git");
514 		config.setStringList("url", "https://server/repos/", "pushInsteadOf",
515 				Arrays.asList("pre", "prefix", "pref", "perf"));
516 		RemoteConfig rc = new RemoteConfig(config, "origin");
517 		assertFalse(rc.getPushURIs().isEmpty());
518 		assertEquals("https://server/repos/project.git", rc.getPushURIs()
519 				.get(0).toASCIIString());
520 	}
521 }