Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RemoteConfigTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. package org.eclipse.jgit.transport;
  45. import java.util.List;
  46. import junit.framework.TestCase;
  47. import org.eclipse.jgit.errors.ConfigInvalidException;
  48. import org.eclipse.jgit.lib.Config;
  49. public class RemoteConfigTest extends TestCase {
  50. private Config config;
  51. protected void setUp() throws Exception {
  52. super.setUp();
  53. config = new Config();
  54. }
  55. private void readConfig(final String dat) throws ConfigInvalidException {
  56. config = new Config();
  57. config.fromText(dat);
  58. }
  59. private void checkConfig(final String exp) {
  60. assertEquals(exp, config.toText());
  61. }
  62. private static void assertEquals(final String exp, final URIish act) {
  63. assertEquals(exp, act != null ? act.toString() : null);
  64. }
  65. public void testSimple() throws Exception {
  66. readConfig("[remote \"spearce\"]\n"
  67. + "url = http://www.spearce.org/egit.git\n"
  68. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  69. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  70. final List<URIish> allURIs = rc.getURIs();
  71. RefSpec spec;
  72. assertEquals("spearce", rc.getName());
  73. assertNotNull(allURIs);
  74. assertNotNull(rc.getFetchRefSpecs());
  75. assertNotNull(rc.getPushRefSpecs());
  76. assertNotNull(rc.getTagOpt());
  77. assertEquals(0, rc.getTimeout());
  78. assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());
  79. assertEquals(1, allURIs.size());
  80. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0));
  81. assertEquals(1, rc.getFetchRefSpecs().size());
  82. spec = rc.getFetchRefSpecs().get(0);
  83. assertTrue(spec.isForceUpdate());
  84. assertTrue(spec.isWildcard());
  85. assertEquals("refs/heads/*", spec.getSource());
  86. assertEquals("refs/remotes/spearce/*", spec.getDestination());
  87. assertEquals(0, rc.getPushRefSpecs().size());
  88. }
  89. public void testSimpleNoTags() 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 = --no-tags\n");
  94. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  95. assertSame(TagOpt.NO_TAGS, rc.getTagOpt());
  96. }
  97. public void testSimpleAlwaysTags() throws Exception {
  98. readConfig("[remote \"spearce\"]\n"
  99. + "url = http://www.spearce.org/egit.git\n"
  100. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  101. + "tagopt = --tags\n");
  102. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  103. assertSame(TagOpt.FETCH_TAGS, rc.getTagOpt());
  104. }
  105. public void testMirror() throws Exception {
  106. readConfig("[remote \"spearce\"]\n"
  107. + "url = http://www.spearce.org/egit.git\n"
  108. + "fetch = +refs/heads/*:refs/heads/*\n"
  109. + "fetch = refs/tags/*:refs/tags/*\n");
  110. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  111. final List<URIish> allURIs = rc.getURIs();
  112. RefSpec spec;
  113. assertEquals("spearce", rc.getName());
  114. assertNotNull(allURIs);
  115. assertNotNull(rc.getFetchRefSpecs());
  116. assertNotNull(rc.getPushRefSpecs());
  117. assertEquals(1, allURIs.size());
  118. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0));
  119. assertEquals(2, rc.getFetchRefSpecs().size());
  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. spec = rc.getFetchRefSpecs().get(1);
  126. assertFalse(spec.isForceUpdate());
  127. assertTrue(spec.isWildcard());
  128. assertEquals("refs/tags/*", spec.getSource());
  129. assertEquals("refs/tags/*", spec.getDestination());
  130. assertEquals(0, rc.getPushRefSpecs().size());
  131. }
  132. public void testBackup() throws Exception {
  133. readConfig("[remote \"backup\"]\n"
  134. + "url = http://www.spearce.org/egit.git\n"
  135. + "url = user@repo.or.cz:/srv/git/egit.git\n"
  136. + "push = +refs/heads/*:refs/heads/*\n"
  137. + "push = refs/tags/*:refs/tags/*\n");
  138. final RemoteConfig rc = new RemoteConfig(config, "backup");
  139. final List<URIish> allURIs = rc.getURIs();
  140. RefSpec spec;
  141. assertEquals("backup", rc.getName());
  142. assertNotNull(allURIs);
  143. assertNotNull(rc.getFetchRefSpecs());
  144. assertNotNull(rc.getPushRefSpecs());
  145. assertEquals(2, allURIs.size());
  146. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0));
  147. assertEquals("user@repo.or.cz:/srv/git/egit.git", allURIs.get(1));
  148. assertEquals(0, rc.getFetchRefSpecs().size());
  149. assertEquals(2, rc.getPushRefSpecs().size());
  150. spec = rc.getPushRefSpecs().get(0);
  151. assertTrue(spec.isForceUpdate());
  152. assertTrue(spec.isWildcard());
  153. assertEquals("refs/heads/*", spec.getSource());
  154. assertEquals("refs/heads/*", spec.getDestination());
  155. spec = rc.getPushRefSpecs().get(1);
  156. assertFalse(spec.isForceUpdate());
  157. assertTrue(spec.isWildcard());
  158. assertEquals("refs/tags/*", spec.getSource());
  159. assertEquals("refs/tags/*", spec.getDestination());
  160. }
  161. public void testUploadPack() throws Exception {
  162. readConfig("[remote \"example\"]\n"
  163. + "url = user@example.com:egit.git\n"
  164. + "fetch = +refs/heads/*:refs/remotes/example/*\n"
  165. + "uploadpack = /path/to/git/git-upload-pack\n"
  166. + "receivepack = /path/to/git/git-receive-pack\n");
  167. final RemoteConfig rc = new RemoteConfig(config, "example");
  168. final List<URIish> allURIs = rc.getURIs();
  169. RefSpec spec;
  170. assertEquals("example", rc.getName());
  171. assertNotNull(allURIs);
  172. assertNotNull(rc.getFetchRefSpecs());
  173. assertNotNull(rc.getPushRefSpecs());
  174. assertEquals(1, allURIs.size());
  175. assertEquals("user@example.com:egit.git", allURIs.get(0));
  176. assertEquals(1, rc.getFetchRefSpecs().size());
  177. spec = rc.getFetchRefSpecs().get(0);
  178. assertTrue(spec.isForceUpdate());
  179. assertTrue(spec.isWildcard());
  180. assertEquals("refs/heads/*", spec.getSource());
  181. assertEquals("refs/remotes/example/*", spec.getDestination());
  182. assertEquals(0, rc.getPushRefSpecs().size());
  183. assertEquals("/path/to/git/git-upload-pack", rc.getUploadPack());
  184. assertEquals("/path/to/git/git-receive-pack", rc.getReceivePack());
  185. }
  186. public void testUnknown() throws Exception {
  187. readConfig("");
  188. final RemoteConfig rc = new RemoteConfig(config, "backup");
  189. assertEquals(0, rc.getURIs().size());
  190. assertEquals(0, rc.getFetchRefSpecs().size());
  191. assertEquals(0, rc.getPushRefSpecs().size());
  192. assertEquals("git-upload-pack", rc.getUploadPack());
  193. assertEquals("git-receive-pack", rc.getReceivePack());
  194. }
  195. public void testAddURI() throws Exception {
  196. readConfig("");
  197. final URIish uri = new URIish("/some/dir");
  198. final RemoteConfig rc = new RemoteConfig(config, "backup");
  199. assertEquals(0, rc.getURIs().size());
  200. assertTrue(rc.addURI(uri));
  201. assertEquals(1, rc.getURIs().size());
  202. assertSame(uri, rc.getURIs().get(0));
  203. assertFalse(rc.addURI(new URIish(uri.toString())));
  204. assertEquals(1, rc.getURIs().size());
  205. }
  206. public void testRemoveFirstURI() throws Exception {
  207. readConfig("");
  208. final URIish a = new URIish("/some/dir");
  209. final URIish b = new URIish("/another/dir");
  210. final URIish c = new URIish("/more/dirs");
  211. final RemoteConfig rc = new RemoteConfig(config, "backup");
  212. assertTrue(rc.addURI(a));
  213. assertTrue(rc.addURI(b));
  214. assertTrue(rc.addURI(c));
  215. assertEquals(3, rc.getURIs().size());
  216. assertSame(a, rc.getURIs().get(0));
  217. assertSame(b, rc.getURIs().get(1));
  218. assertSame(c, rc.getURIs().get(2));
  219. assertTrue(rc.removeURI(a));
  220. assertEquals(2, rc.getURIs().size());
  221. assertSame(b, rc.getURIs().get(0));
  222. assertSame(c, rc.getURIs().get(1));
  223. }
  224. public void testRemoveMiddleURI() throws Exception {
  225. readConfig("");
  226. final URIish a = new URIish("/some/dir");
  227. final URIish b = new URIish("/another/dir");
  228. final URIish c = new URIish("/more/dirs");
  229. final RemoteConfig rc = new RemoteConfig(config, "backup");
  230. assertTrue(rc.addURI(a));
  231. assertTrue(rc.addURI(b));
  232. assertTrue(rc.addURI(c));
  233. assertEquals(3, rc.getURIs().size());
  234. assertSame(a, rc.getURIs().get(0));
  235. assertSame(b, rc.getURIs().get(1));
  236. assertSame(c, rc.getURIs().get(2));
  237. assertTrue(rc.removeURI(b));
  238. assertEquals(2, rc.getURIs().size());
  239. assertSame(a, rc.getURIs().get(0));
  240. assertSame(c, rc.getURIs().get(1));
  241. }
  242. public void testRemoveLastURI() throws Exception {
  243. readConfig("");
  244. final URIish a = new URIish("/some/dir");
  245. final URIish b = new URIish("/another/dir");
  246. final URIish c = new URIish("/more/dirs");
  247. final RemoteConfig rc = new RemoteConfig(config, "backup");
  248. assertTrue(rc.addURI(a));
  249. assertTrue(rc.addURI(b));
  250. assertTrue(rc.addURI(c));
  251. assertEquals(3, rc.getURIs().size());
  252. assertSame(a, rc.getURIs().get(0));
  253. assertSame(b, rc.getURIs().get(1));
  254. assertSame(c, rc.getURIs().get(2));
  255. assertTrue(rc.removeURI(c));
  256. assertEquals(2, rc.getURIs().size());
  257. assertSame(a, rc.getURIs().get(0));
  258. assertSame(b, rc.getURIs().get(1));
  259. }
  260. public void testRemoveOnlyURI() throws Exception {
  261. readConfig("");
  262. final URIish a = new URIish("/some/dir");
  263. final RemoteConfig rc = new RemoteConfig(config, "backup");
  264. assertTrue(rc.addURI(a));
  265. assertEquals(1, rc.getURIs().size());
  266. assertSame(a, rc.getURIs().get(0));
  267. assertTrue(rc.removeURI(a));
  268. assertEquals(0, rc.getURIs().size());
  269. }
  270. public void testCreateOrigin() throws Exception {
  271. final RemoteConfig rc = new RemoteConfig(config, "origin");
  272. rc.addURI(new URIish("/some/dir"));
  273. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  274. + rc.getName() + "/*"));
  275. rc.update(config);
  276. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  277. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
  278. }
  279. public void testSaveAddURI() throws Exception {
  280. readConfig("[remote \"spearce\"]\n"
  281. + "url = http://www.spearce.org/egit.git\n"
  282. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  283. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  284. rc.addURI(new URIish("/some/dir"));
  285. assertEquals(2, rc.getURIs().size());
  286. rc.update(config);
  287. checkConfig("[remote \"spearce\"]\n"
  288. + "\turl = http://www.spearce.org/egit.git\n"
  289. + "\turl = /some/dir\n"
  290. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  291. }
  292. public void testSaveRemoveLastURI() throws Exception {
  293. readConfig("[remote \"spearce\"]\n"
  294. + "url = http://www.spearce.org/egit.git\n"
  295. + "url = /some/dir\n"
  296. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  297. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  298. assertEquals(2, rc.getURIs().size());
  299. rc.removeURI(new URIish("/some/dir"));
  300. assertEquals(1, rc.getURIs().size());
  301. rc.update(config);
  302. checkConfig("[remote \"spearce\"]\n"
  303. + "\turl = http://www.spearce.org/egit.git\n"
  304. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  305. }
  306. public void testSaveRemoveFirstURI() throws Exception {
  307. readConfig("[remote \"spearce\"]\n"
  308. + "url = http://www.spearce.org/egit.git\n"
  309. + "url = /some/dir\n"
  310. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  311. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  312. assertEquals(2, rc.getURIs().size());
  313. rc.removeURI(new URIish("http://www.spearce.org/egit.git"));
  314. assertEquals(1, rc.getURIs().size());
  315. rc.update(config);
  316. checkConfig("[remote \"spearce\"]\n" + "\turl = /some/dir\n"
  317. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  318. }
  319. public void testSaveNoTags() throws Exception {
  320. final RemoteConfig rc = new RemoteConfig(config, "origin");
  321. rc.addURI(new URIish("/some/dir"));
  322. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  323. + rc.getName() + "/*"));
  324. rc.setTagOpt(TagOpt.NO_TAGS);
  325. rc.update(config);
  326. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  327. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  328. + "\ttagopt = --no-tags\n");
  329. }
  330. public void testSaveAllTags() throws Exception {
  331. final RemoteConfig rc = new RemoteConfig(config, "origin");
  332. rc.addURI(new URIish("/some/dir"));
  333. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  334. + rc.getName() + "/*"));
  335. rc.setTagOpt(TagOpt.FETCH_TAGS);
  336. rc.update(config);
  337. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  338. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  339. + "\ttagopt = --tags\n");
  340. }
  341. public void testSimpleTimeout() throws Exception {
  342. readConfig("[remote \"spearce\"]\n"
  343. + "url = http://www.spearce.org/egit.git\n"
  344. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  345. + "timeout = 12\n");
  346. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  347. assertEquals(12, rc.getTimeout());
  348. }
  349. public void testSaveTimeout() throws Exception {
  350. final RemoteConfig rc = new RemoteConfig(config, "origin");
  351. rc.addURI(new URIish("/some/dir"));
  352. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  353. + rc.getName() + "/*"));
  354. rc.setTimeout(60);
  355. rc.update(config);
  356. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  357. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  358. + "\ttimeout = 60\n");
  359. }
  360. }