View Javadoc
1   /*
2    * Copyright (C) 2015, Kaloyan Raev <kaloyan.r@zend.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.pgm;
11  
12  
13  import static org.junit.Assert.assertArrayEquals;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.util.List;
18  
19  import org.eclipse.jgit.api.Git;
20  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
21  import org.eclipse.jgit.lib.Repository;
22  import org.eclipse.jgit.lib.StoredConfig;
23  import org.eclipse.jgit.transport.RefSpec;
24  import org.eclipse.jgit.transport.RemoteConfig;
25  import org.eclipse.jgit.transport.URIish;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  public class RemoteTest extends CLIRepositoryTestCase {
30  
31  	private StoredConfig config;
32  
33  	private RemoteConfig remote;
34  
35  	@Before
36  	@Override
37  	public void setUp() throws Exception {
38  		super.setUp();
39  
40  		// create another repository
41  		Repository remoteRepository = createWorkRepository();
42  
43  		// set it up as a remote to this repository
44  		config = db.getConfig();
45  		remote = new RemoteConfig(config, "test");
46  		remote.addFetchRefSpec(
47  				new RefSpec("+refs/heads/*:refs/remotes/test/*"));
48  		URIish uri = new URIish(
49  				remoteRepository.getDirectory().toURI().toURL());
50  		remote.addURI(uri);
51  		remote.update(config);
52  		config.save();
53  
54  		Git.wrap(remoteRepository).commit().setMessage("initial commit").call();
55  	}
56  
57  	@Test
58  	public void testList() throws Exception {
59  		assertArrayEquals(new String[] { remote.getName(), "" },
60  				execute("git remote"));
61  	}
62  
63  	@Test
64  	public void testVerboseList() throws Exception {
65  		assertArrayEquals(
66  				new String[] {
67  						String.format("%s\t%s (fetch)", remote.getName(),
68  								remote.getURIs().get(0)),
69  						String.format("%s\t%s (push)", remote.getName(),
70  								remote.getURIs().get(0)),
71  						"" },
72  				execute("git remote -v"));
73  	}
74  
75  	@Test
76  	public void testAdd() throws Exception {
77  		assertArrayEquals(new String[] { "" },
78  				execute("git remote add second git://test.com/second"));
79  
80  		List<RemoteConfig> remotes = RemoteConfig.getAllRemoteConfigs(config);
81  		assertEquals(2, remotes.size());
82  		assertEquals("second", remotes.get(0).getName());
83  		assertEquals("test", remotes.get(1).getName());
84  	}
85  
86  	@Test
87  	public void testRemove() throws Exception {
88  		assertArrayEquals(new String[] { "" },
89  				execute("git remote remove test"));
90  
91  		assertTrue(RemoteConfig.getAllRemoteConfigs(config).isEmpty());
92  	}
93  
94  	@Test
95  	public void testSetUrl() throws Exception {
96  		assertArrayEquals(new String[] { "" },
97  				execute("git remote set-url test git://test.com/test"));
98  
99  		RemoteConfig result = new RemoteConfig(config, "test");
100 		assertEquals("test", result.getName());
101 		assertArrayEquals(new URIish[] { new URIish("git://test.com/test") },
102 				result.getURIs().toArray());
103 		assertTrue(result.getPushURIs().isEmpty());
104 	}
105 
106 	@Test
107 	public void testSetUrlPush() throws Exception {
108 		assertArrayEquals(new String[] { "" },
109 				execute("git remote set-url --push test git://test.com/test"));
110 
111 		RemoteConfig result = new RemoteConfig(config, "test");
112 		assertEquals("test", result.getName());
113 		assertEquals(remote.getURIs(), result.getURIs());
114 		assertArrayEquals(new URIish[] { new URIish("git://test.com/test") },
115 				result.getPushURIs().toArray());
116 	}
117 
118 	@Test
119 	public void testUpdate() throws Exception {
120 		assertArrayEquals(new String[] {
121 				"From " + remote.getURIs().get(0).toString(),
122 				" * [new branch]      master     -> test/master", "", "" },
123 				execute("git remote update test"));
124 	}
125 
126 }