You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RefSpecTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.transport;
  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.assertNotSame;
  50. import static org.junit.Assert.assertNull;
  51. import static org.junit.Assert.assertSame;
  52. import static org.junit.Assert.assertTrue;
  53. import org.eclipse.jgit.lib.ObjectIdRef;
  54. import org.eclipse.jgit.lib.Ref;
  55. import org.junit.Test;
  56. public class RefSpecTest {
  57. @Test
  58. public void testMasterMaster() {
  59. final String sn = "refs/heads/master";
  60. final RefSpec rs = new RefSpec(sn + ":" + sn);
  61. assertFalse(rs.isForceUpdate());
  62. assertFalse(rs.isWildcard());
  63. assertEquals(sn, rs.getSource());
  64. assertEquals(sn, rs.getDestination());
  65. assertEquals(sn + ":" + sn, rs.toString());
  66. assertEquals(rs, new RefSpec(rs.toString()));
  67. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  68. assertTrue(rs.matchSource(r));
  69. assertTrue(rs.matchDestination(r));
  70. assertSame(rs, rs.expandFromSource(r));
  71. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  72. assertFalse(rs.matchSource(r));
  73. assertFalse(rs.matchDestination(r));
  74. }
  75. @Test
  76. public void testSplitLastColon() {
  77. final String lhs = ":m:a:i:n:t";
  78. final String rhs = "refs/heads/maint";
  79. final RefSpec rs = new RefSpec(lhs + ":" + rhs);
  80. assertFalse(rs.isForceUpdate());
  81. assertFalse(rs.isWildcard());
  82. assertEquals(lhs, rs.getSource());
  83. assertEquals(rhs, rs.getDestination());
  84. assertEquals(lhs + ":" + rhs, rs.toString());
  85. assertEquals(rs, new RefSpec(rs.toString()));
  86. }
  87. @Test
  88. public void testForceMasterMaster() {
  89. final String sn = "refs/heads/master";
  90. final RefSpec rs = new RefSpec("+" + sn + ":" + sn);
  91. assertTrue(rs.isForceUpdate());
  92. assertFalse(rs.isWildcard());
  93. assertEquals(sn, rs.getSource());
  94. assertEquals(sn, rs.getDestination());
  95. assertEquals("+" + sn + ":" + sn, rs.toString());
  96. assertEquals(rs, new RefSpec(rs.toString()));
  97. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  98. assertTrue(rs.matchSource(r));
  99. assertTrue(rs.matchDestination(r));
  100. assertSame(rs, rs.expandFromSource(r));
  101. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  102. assertFalse(rs.matchSource(r));
  103. assertFalse(rs.matchDestination(r));
  104. }
  105. @Test
  106. public void testMaster() {
  107. final String sn = "refs/heads/master";
  108. final RefSpec rs = new RefSpec(sn);
  109. assertFalse(rs.isForceUpdate());
  110. assertFalse(rs.isWildcard());
  111. assertEquals(sn, rs.getSource());
  112. assertNull(rs.getDestination());
  113. assertEquals(sn, rs.toString());
  114. assertEquals(rs, new RefSpec(rs.toString()));
  115. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  116. assertTrue(rs.matchSource(r));
  117. assertFalse(rs.matchDestination(r));
  118. assertSame(rs, rs.expandFromSource(r));
  119. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  120. assertFalse(rs.matchSource(r));
  121. assertFalse(rs.matchDestination(r));
  122. }
  123. @Test
  124. public void testForceMaster() {
  125. final String sn = "refs/heads/master";
  126. final RefSpec rs = new RefSpec("+" + sn);
  127. assertTrue(rs.isForceUpdate());
  128. assertFalse(rs.isWildcard());
  129. assertEquals(sn, rs.getSource());
  130. assertNull(rs.getDestination());
  131. assertEquals("+" + sn, rs.toString());
  132. assertEquals(rs, new RefSpec(rs.toString()));
  133. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  134. assertTrue(rs.matchSource(r));
  135. assertFalse(rs.matchDestination(r));
  136. assertSame(rs, rs.expandFromSource(r));
  137. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  138. assertFalse(rs.matchSource(r));
  139. assertFalse(rs.matchDestination(r));
  140. }
  141. @Test
  142. public void testDeleteMaster() {
  143. final String sn = "refs/heads/master";
  144. final RefSpec rs = new RefSpec(":" + sn);
  145. assertFalse(rs.isForceUpdate());
  146. assertFalse(rs.isWildcard());
  147. assertNull(rs.getSource());
  148. assertEquals(sn, rs.getDestination());
  149. assertEquals(":" + sn, rs.toString());
  150. assertEquals(rs, new RefSpec(rs.toString()));
  151. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  152. assertFalse(rs.matchSource(r));
  153. assertTrue(rs.matchDestination(r));
  154. assertSame(rs, rs.expandFromSource(r));
  155. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  156. assertFalse(rs.matchSource(r));
  157. assertFalse(rs.matchDestination(r));
  158. }
  159. @Test
  160. public void testForceRemotesOrigin() {
  161. final String srcn = "refs/heads/*";
  162. final String dstn = "refs/remotes/origin/*";
  163. final RefSpec rs = new RefSpec("+" + srcn + ":" + dstn);
  164. assertTrue(rs.isForceUpdate());
  165. assertTrue(rs.isWildcard());
  166. assertEquals(srcn, rs.getSource());
  167. assertEquals(dstn, rs.getDestination());
  168. assertEquals("+" + srcn + ":" + dstn, rs.toString());
  169. assertEquals(rs, new RefSpec(rs.toString()));
  170. Ref r;
  171. RefSpec expanded;
  172. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master", null);
  173. assertTrue(rs.matchSource(r));
  174. assertFalse(rs.matchDestination(r));
  175. expanded = rs.expandFromSource(r);
  176. assertNotSame(rs, expanded);
  177. assertTrue(expanded.isForceUpdate());
  178. assertFalse(expanded.isWildcard());
  179. assertEquals(r.getName(), expanded.getSource());
  180. assertEquals("refs/remotes/origin/master", expanded.getDestination());
  181. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/remotes/origin/next", null);
  182. assertFalse(rs.matchSource(r));
  183. assertTrue(rs.matchDestination(r));
  184. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/tags/v1.0", null);
  185. assertFalse(rs.matchSource(r));
  186. assertFalse(rs.matchDestination(r));
  187. }
  188. @Test
  189. public void testCreateEmpty() {
  190. final RefSpec rs = new RefSpec();
  191. assertFalse(rs.isForceUpdate());
  192. assertFalse(rs.isWildcard());
  193. assertEquals("HEAD", rs.getSource());
  194. assertNull(rs.getDestination());
  195. assertEquals("HEAD", rs.toString());
  196. }
  197. @Test
  198. public void testSetForceUpdate() {
  199. final String s = "refs/heads/*:refs/remotes/origin/*";
  200. final RefSpec a = new RefSpec(s);
  201. assertFalse(a.isForceUpdate());
  202. RefSpec b = a.setForceUpdate(true);
  203. assertNotSame(a, b);
  204. assertFalse(a.isForceUpdate());
  205. assertTrue(b.isForceUpdate());
  206. assertEquals(s, a.toString());
  207. assertEquals("+" + s, b.toString());
  208. }
  209. @Test
  210. public void testSetSource() {
  211. final RefSpec a = new RefSpec();
  212. final RefSpec b = a.setSource("refs/heads/master");
  213. assertNotSame(a, b);
  214. assertEquals("HEAD", a.toString());
  215. assertEquals("refs/heads/master", b.toString());
  216. }
  217. @Test
  218. public void testSetDestination() {
  219. final RefSpec a = new RefSpec();
  220. final RefSpec b = a.setDestination("refs/heads/master");
  221. assertNotSame(a, b);
  222. assertEquals("HEAD", a.toString());
  223. assertEquals("HEAD:refs/heads/master", b.toString());
  224. }
  225. @Test
  226. public void testSetDestination_SourceNull() {
  227. final RefSpec a = new RefSpec();
  228. RefSpec b;
  229. b = a.setDestination("refs/heads/master");
  230. b = b.setSource(null);
  231. assertNotSame(a, b);
  232. assertEquals("HEAD", a.toString());
  233. assertEquals(":refs/heads/master", b.toString());
  234. }
  235. @Test
  236. public void testSetSourceDestination() {
  237. final RefSpec a = new RefSpec();
  238. final RefSpec b;
  239. b = a.setSourceDestination("refs/heads/*", "refs/remotes/origin/*");
  240. assertNotSame(a, b);
  241. assertEquals("HEAD", a.toString());
  242. assertEquals("refs/heads/*:refs/remotes/origin/*", b.toString());
  243. }
  244. @Test
  245. public void testExpandFromDestination_NonWildcard() {
  246. final String src = "refs/heads/master";
  247. final String dst = "refs/remotes/origin/master";
  248. final RefSpec a = new RefSpec(src + ":" + dst);
  249. final RefSpec r = a.expandFromDestination(dst);
  250. assertSame(a, r);
  251. assertFalse(r.isWildcard());
  252. assertEquals(src, r.getSource());
  253. assertEquals(dst, r.getDestination());
  254. }
  255. @Test
  256. public void testExpandFromDestination_Wildcard() {
  257. final String src = "refs/heads/master";
  258. final String dst = "refs/remotes/origin/master";
  259. final RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
  260. final RefSpec r = a.expandFromDestination(dst);
  261. assertNotSame(a, r);
  262. assertFalse(r.isWildcard());
  263. assertEquals(src, r.getSource());
  264. assertEquals(dst, r.getDestination());
  265. }
  266. @Test
  267. public void isWildcardShouldWorkForWildcardSuffixAndComponent() {
  268. assertTrue(RefSpec.isWildcard("refs/heads/*"));
  269. assertTrue(RefSpec.isWildcard("refs/pull/*/head"));
  270. assertFalse(RefSpec.isWildcard("refs/heads/a"));
  271. }
  272. @Test
  273. public void testWildcardInMiddleOfSource() {
  274. RefSpec a = new RefSpec("+refs/pull/*/head:refs/remotes/origin/pr/*");
  275. assertTrue(a.isWildcard());
  276. assertTrue(a.matchSource("refs/pull/a/head"));
  277. assertTrue(a.matchSource("refs/pull/foo/head"));
  278. assertTrue(a.matchSource("refs/pull/foo/bar/head"));
  279. assertFalse(a.matchSource("refs/pull/foo"));
  280. assertFalse(a.matchSource("refs/pull/head"));
  281. assertFalse(a.matchSource("refs/pull/foo/head/more"));
  282. assertFalse(a.matchSource("refs/pullx/head"));
  283. RefSpec b = a.expandFromSource("refs/pull/foo/head");
  284. assertEquals("refs/remotes/origin/pr/foo", b.getDestination());
  285. RefSpec c = a.expandFromDestination("refs/remotes/origin/pr/foo");
  286. assertEquals("refs/pull/foo/head", c.getSource());
  287. }
  288. @Test
  289. public void testWildcardInMiddleOfDestionation() {
  290. RefSpec a = new RefSpec("+refs/heads/*:refs/remotes/origin/*/head");
  291. assertTrue(a.isWildcard());
  292. assertTrue(a.matchDestination("refs/remotes/origin/a/head"));
  293. assertTrue(a.matchDestination("refs/remotes/origin/foo/head"));
  294. assertTrue(a.matchDestination("refs/remotes/origin/foo/bar/head"));
  295. assertFalse(a.matchDestination("refs/remotes/origin/foo"));
  296. assertFalse(a.matchDestination("refs/remotes/origin/head"));
  297. assertFalse(a.matchDestination("refs/remotes/origin/foo/head/more"));
  298. assertFalse(a.matchDestination("refs/remotes/originx/head"));
  299. RefSpec b = a.expandFromSource("refs/heads/foo");
  300. assertEquals("refs/remotes/origin/foo/head", b.getDestination());
  301. RefSpec c = a.expandFromDestination("refs/remotes/origin/foo/head");
  302. assertEquals("refs/heads/foo", c.getSource());
  303. }
  304. @Test
  305. public void testWildcardMirror() {
  306. RefSpec a = new RefSpec("*:*");
  307. assertTrue(a.isWildcard());
  308. assertTrue(a.matchSource("a"));
  309. assertTrue(a.matchSource("foo"));
  310. assertTrue(a.matchSource("foo/bar"));
  311. assertTrue(a.matchDestination("a"));
  312. assertTrue(a.matchDestination("foo"));
  313. assertTrue(a.matchDestination("foo/bar"));
  314. RefSpec b = a.expandFromSource("refs/heads/foo");
  315. assertEquals("refs/heads/foo", b.getDestination());
  316. RefSpec c = a.expandFromDestination("refs/heads/foo");
  317. assertEquals("refs/heads/foo", c.getSource());
  318. }
  319. @Test
  320. public void testWildcardAtStart() {
  321. RefSpec a = new RefSpec("*/head:refs/heads/*");
  322. assertTrue(a.isWildcard());
  323. assertTrue(a.matchSource("a/head"));
  324. assertTrue(a.matchSource("foo/head"));
  325. assertTrue(a.matchSource("foo/bar/head"));
  326. assertFalse(a.matchSource("/head"));
  327. assertFalse(a.matchSource("a/head/extra"));
  328. RefSpec b = a.expandFromSource("foo/head");
  329. assertEquals("refs/heads/foo", b.getDestination());
  330. RefSpec c = a.expandFromDestination("refs/heads/foo");
  331. assertEquals("foo/head", c.getSource());
  332. }
  333. @Test(expected = IllegalArgumentException.class)
  334. public void invalidWhenSourceOnlyAndWildcard() {
  335. assertNotNull(new RefSpec("refs/heads/*"));
  336. }
  337. @Test(expected = IllegalArgumentException.class)
  338. public void invalidWhenDestinationOnlyAndWildcard() {
  339. assertNotNull(new RefSpec(":refs/heads/*"));
  340. }
  341. @Test(expected = IllegalArgumentException.class)
  342. public void invalidWhenOnlySourceWildcard() {
  343. assertNotNull(new RefSpec("refs/heads/*:refs/heads/foo"));
  344. }
  345. @Test(expected = IllegalArgumentException.class)
  346. public void invalidWhenOnlyDestinationWildcard() {
  347. assertNotNull(new RefSpec("refs/heads/foo:refs/heads/*"));
  348. }
  349. @Test(expected = IllegalArgumentException.class)
  350. public void invalidWhenMoreThanOneWildcardInSource() {
  351. assertNotNull(new RefSpec("refs/heads/*/*:refs/heads/*"));
  352. }
  353. @Test(expected = IllegalArgumentException.class)
  354. public void invalidWhenMoreThanOneWildcardInDestination() {
  355. assertNotNull(new RefSpec("refs/heads/*:refs/heads/*/*"));
  356. }
  357. @Test(expected = IllegalArgumentException.class)
  358. public void invalidWhenWildcardAfterText() {
  359. assertNotNull(new RefSpec("refs/heads/wrong*:refs/heads/right/*"));
  360. }
  361. @Test(expected = IllegalArgumentException.class)
  362. public void invalidWhenWildcardBeforeText() {
  363. assertNotNull(new RefSpec("*wrong:right/*"));
  364. }
  365. @Test(expected = IllegalArgumentException.class)
  366. public void invalidWhenWildcardBeforeTextAtEnd() {
  367. assertNotNull(new RefSpec("refs/heads/*wrong:right/*"));
  368. }
  369. @Test(expected = IllegalArgumentException.class)
  370. public void invalidSourceDoubleSlashes() {
  371. assertNotNull(new RefSpec("refs/heads//wrong"));
  372. }
  373. @Test(expected = IllegalArgumentException.class)
  374. public void invalidSlashAtStart() {
  375. assertNotNull(new RefSpec("/foo:/foo"));
  376. }
  377. @Test(expected = IllegalArgumentException.class)
  378. public void invalidDestinationDoubleSlashes() {
  379. assertNotNull(new RefSpec(":refs/heads//wrong"));
  380. }
  381. @Test(expected = IllegalArgumentException.class)
  382. public void invalidSetSource() {
  383. RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
  384. a.setSource("refs/heads/*/*");
  385. }
  386. @Test(expected = IllegalArgumentException.class)
  387. public void invalidSetDestination() {
  388. RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
  389. a.setDestination("refs/remotes/origin/*/*");
  390. }
  391. }