View Javadoc
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  
46  package org.eclipse.jgit.transport;
47  
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertFalse;
50  import static org.junit.Assert.assertNotNull;
51  import static org.junit.Assert.assertNotSame;
52  import static org.junit.Assert.assertNull;
53  import static org.junit.Assert.assertSame;
54  import static org.junit.Assert.assertTrue;
55  
56  import org.eclipse.jgit.lib.ObjectIdRef;
57  import org.eclipse.jgit.lib.Ref;
58  import org.junit.Test;
59  
60  public class RefSpecTest {
61  	@Test
62  	public void testMasterMaster() {
63  		final String sn = "refs/heads/master";
64  		final RefSpec rs = new RefSpec(sn + ":" + sn);
65  		assertFalse(rs.isForceUpdate());
66  		assertFalse(rs.isWildcard());
67  		assertEquals(sn, rs.getSource());
68  		assertEquals(sn, rs.getDestination());
69  		assertEquals(sn + ":" + sn, rs.toString());
70  		assertEquals(rs, new RefSpec(rs.toString()));
71  
72  		Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
73  		assertTrue(rs.matchSource(r));
74  		assertTrue(rs.matchDestination(r));
75  		assertSame(rs, rs.expandFromSource(r));
76  
77  		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
78  		assertFalse(rs.matchSource(r));
79  		assertFalse(rs.matchDestination(r));
80  	}
81  
82  	@Test
83  	public void testSplitLastColon() {
84  		final String lhs = ":m:a:i:n:t";
85  		final String rhs = "refs/heads/maint";
86  		final RefSpec rs = new RefSpec(lhs + ":" + rhs);
87  		assertFalse(rs.isForceUpdate());
88  		assertFalse(rs.isWildcard());
89  		assertEquals(lhs, rs.getSource());
90  		assertEquals(rhs, rs.getDestination());
91  		assertEquals(lhs + ":" + rhs, rs.toString());
92  		assertEquals(rs, new RefSpec(rs.toString()));
93  	}
94  
95  	@Test
96  	public void testForceMasterMaster() {
97  		final String sn = "refs/heads/master";
98  		final RefSpec rs = new RefSpec("+" + sn + ":" + sn);
99  		assertTrue(rs.isForceUpdate());
100 		assertFalse(rs.isWildcard());
101 		assertEquals(sn, rs.getSource());
102 		assertEquals(sn, rs.getDestination());
103 		assertEquals("+" + sn + ":" + sn, rs.toString());
104 		assertEquals(rs, new RefSpec(rs.toString()));
105 
106 		Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
107 		assertTrue(rs.matchSource(r));
108 		assertTrue(rs.matchDestination(r));
109 		assertSame(rs, rs.expandFromSource(r));
110 
111 		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
112 		assertFalse(rs.matchSource(r));
113 		assertFalse(rs.matchDestination(r));
114 	}
115 
116 	@Test
117 	public void testMaster() {
118 		final String sn = "refs/heads/master";
119 		final RefSpec rs = new RefSpec(sn);
120 		assertFalse(rs.isForceUpdate());
121 		assertFalse(rs.isWildcard());
122 		assertEquals(sn, rs.getSource());
123 		assertNull(rs.getDestination());
124 		assertEquals(sn, rs.toString());
125 		assertEquals(rs, new RefSpec(rs.toString()));
126 
127 		Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
128 		assertTrue(rs.matchSource(r));
129 		assertFalse(rs.matchDestination(r));
130 		assertSame(rs, rs.expandFromSource(r));
131 
132 		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
133 		assertFalse(rs.matchSource(r));
134 		assertFalse(rs.matchDestination(r));
135 	}
136 
137 	@Test
138 	public void testForceMaster() {
139 		final String sn = "refs/heads/master";
140 		final RefSpec rs = new RefSpec("+" + sn);
141 		assertTrue(rs.isForceUpdate());
142 		assertFalse(rs.isWildcard());
143 		assertEquals(sn, rs.getSource());
144 		assertNull(rs.getDestination());
145 		assertEquals("+" + sn, rs.toString());
146 		assertEquals(rs, new RefSpec(rs.toString()));
147 
148 		Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
149 		assertTrue(rs.matchSource(r));
150 		assertFalse(rs.matchDestination(r));
151 		assertSame(rs, rs.expandFromSource(r));
152 
153 		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
154 		assertFalse(rs.matchSource(r));
155 		assertFalse(rs.matchDestination(r));
156 	}
157 
158 	@Test
159 	public void testDeleteMaster() {
160 		final String sn = "refs/heads/master";
161 		final RefSpec rs = new RefSpec(":" + sn);
162 		assertFalse(rs.isForceUpdate());
163 		assertFalse(rs.isWildcard());
164 		assertNull(rs.getSource());
165 		assertEquals(sn, rs.getDestination());
166 		assertEquals(":" + sn, rs.toString());
167 		assertEquals(rs, new RefSpec(rs.toString()));
168 
169 		Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
170 		assertFalse(rs.matchSource(r));
171 		assertTrue(rs.matchDestination(r));
172 		assertSame(rs, rs.expandFromSource(r));
173 
174 		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
175 		assertFalse(rs.matchSource(r));
176 		assertFalse(rs.matchDestination(r));
177 	}
178 
179 	@Test
180 	public void testForceRemotesOrigin() {
181 		final String srcn = "refs/heads/*";
182 		final String dstn = "refs/remotes/origin/*";
183 		final RefSpec rs = new RefSpec("+" + srcn + ":" + dstn);
184 		assertTrue(rs.isForceUpdate());
185 		assertTrue(rs.isWildcard());
186 		assertEquals(srcn, rs.getSource());
187 		assertEquals(dstn, rs.getDestination());
188 		assertEquals("+" + srcn + ":" + dstn, rs.toString());
189 		assertEquals(rs, new RefSpec(rs.toString()));
190 
191 		Ref r;
192 		RefSpec expanded;
193 
194 		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master", null);
195 		assertTrue(rs.matchSource(r));
196 		assertFalse(rs.matchDestination(r));
197 		expanded = rs.expandFromSource(r);
198 		assertNotSame(rs, expanded);
199 		assertTrue(expanded.isForceUpdate());
200 		assertFalse(expanded.isWildcard());
201 		assertEquals(r.getName(), expanded.getSource());
202 		assertEquals("refs/remotes/origin/master", expanded.getDestination());
203 
204 		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/remotes/origin/next", null);
205 		assertFalse(rs.matchSource(r));
206 		assertTrue(rs.matchDestination(r));
207 
208 		r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/tags/v1.0", null);
209 		assertFalse(rs.matchSource(r));
210 		assertFalse(rs.matchDestination(r));
211 	}
212 
213 	@Test
214 	public void testCreateEmpty() {
215 		final RefSpec rs = new RefSpec();
216 		assertFalse(rs.isForceUpdate());
217 		assertFalse(rs.isWildcard());
218 		assertEquals("HEAD", rs.getSource());
219 		assertNull(rs.getDestination());
220 		assertEquals("HEAD", rs.toString());
221 	}
222 
223 	@Test
224 	public void testSetForceUpdate() {
225 		final String s = "refs/heads/*:refs/remotes/origin/*";
226 		final RefSpec a = new RefSpec(s);
227 		assertFalse(a.isForceUpdate());
228 		RefSpec b = a.setForceUpdate(true);
229 		assertNotSame(a, b);
230 		assertFalse(a.isForceUpdate());
231 		assertTrue(b.isForceUpdate());
232 		assertEquals(s, a.toString());
233 		assertEquals("+" + s, b.toString());
234 	}
235 
236 	@Test
237 	public void testSetSource() {
238 		final RefSpec a = new RefSpec();
239 		final RefSpec b = a.setSource("refs/heads/master");
240 		assertNotSame(a, b);
241 		assertEquals("HEAD", a.toString());
242 		assertEquals("refs/heads/master", b.toString());
243 	}
244 
245 	@Test
246 	public void testSetDestination() {
247 		final RefSpec a = new RefSpec();
248 		final RefSpec b = a.setDestination("refs/heads/master");
249 		assertNotSame(a, b);
250 		assertEquals("HEAD", a.toString());
251 		assertEquals("HEAD:refs/heads/master", b.toString());
252 	}
253 
254 	@Test
255 	public void testSetDestination_SourceNull() {
256 		final RefSpec a = new RefSpec();
257 		RefSpec b;
258 
259 		b = a.setDestination("refs/heads/master");
260 		b = b.setSource(null);
261 		assertNotSame(a, b);
262 		assertEquals("HEAD", a.toString());
263 		assertEquals(":refs/heads/master", b.toString());
264 	}
265 
266 	@Test
267 	public void testSetSourceDestination() {
268 		final RefSpec a = new RefSpec();
269 		final RefSpec b;
270 		b = a.setSourceDestination("refs/heads/*", "refs/remotes/origin/*");
271 		assertNotSame(a, b);
272 		assertEquals("HEAD", a.toString());
273 		assertEquals("refs/heads/*:refs/remotes/origin/*", b.toString());
274 	}
275 
276 	@Test
277 	public void testExpandFromDestination_NonWildcard() {
278 		final String src = "refs/heads/master";
279 		final String dst = "refs/remotes/origin/master";
280 		final RefSpec a = new RefSpec(src + ":" + dst);
281 		final RefSpec r = a.expandFromDestination(dst);
282 		assertSame(a, r);
283 		assertFalse(r.isWildcard());
284 		assertEquals(src, r.getSource());
285 		assertEquals(dst, r.getDestination());
286 	}
287 
288 	@Test
289 	public void testExpandFromDestination_Wildcard() {
290 		final String src = "refs/heads/master";
291 		final String dst = "refs/remotes/origin/master";
292 		final RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
293 		final RefSpec r = a.expandFromDestination(dst);
294 		assertNotSame(a, r);
295 		assertFalse(r.isWildcard());
296 		assertEquals(src, r.getSource());
297 		assertEquals(dst, r.getDestination());
298 	}
299 
300 	@Test
301 	public void isWildcardShouldWorkForWildcardSuffixAndComponent() {
302 		assertTrue(RefSpec.isWildcard("refs/heads/*"));
303 		assertTrue(RefSpec.isWildcard("refs/pull/*/head"));
304 		assertFalse(RefSpec.isWildcard("refs/heads/a"));
305 	}
306 
307 	@Test
308 	public void testWildcardInMiddleOfSource() {
309 		RefSpec a = new RefSpec("+refs/pull/*/head:refs/remotes/origin/pr/*");
310 		assertTrue(a.isWildcard());
311 		assertTrue(a.matchSource("refs/pull/a/head"));
312 		assertTrue(a.matchSource("refs/pull/foo/head"));
313 		assertTrue(a.matchSource("refs/pull/foo/bar/head"));
314 		assertFalse(a.matchSource("refs/pull/foo"));
315 		assertFalse(a.matchSource("refs/pull/head"));
316 		assertFalse(a.matchSource("refs/pull/foo/head/more"));
317 		assertFalse(a.matchSource("refs/pullx/head"));
318 
319 		RefSpec b = a.expandFromSource("refs/pull/foo/head");
320 		assertEquals("refs/remotes/origin/pr/foo", b.getDestination());
321 		RefSpec c = a.expandFromDestination("refs/remotes/origin/pr/foo");
322 		assertEquals("refs/pull/foo/head", c.getSource());
323 	}
324 
325 	@Test
326 	public void testWildcardInMiddleOfDestionation() {
327 		RefSpec a = new RefSpec("+refs/heads/*:refs/remotes/origin/*/head");
328 		assertTrue(a.isWildcard());
329 		assertTrue(a.matchDestination("refs/remotes/origin/a/head"));
330 		assertTrue(a.matchDestination("refs/remotes/origin/foo/head"));
331 		assertTrue(a.matchDestination("refs/remotes/origin/foo/bar/head"));
332 		assertFalse(a.matchDestination("refs/remotes/origin/foo"));
333 		assertFalse(a.matchDestination("refs/remotes/origin/head"));
334 		assertFalse(a.matchDestination("refs/remotes/origin/foo/head/more"));
335 		assertFalse(a.matchDestination("refs/remotes/originx/head"));
336 
337 		RefSpec b = a.expandFromSource("refs/heads/foo");
338 		assertEquals("refs/remotes/origin/foo/head", b.getDestination());
339 		RefSpec c = a.expandFromDestination("refs/remotes/origin/foo/head");
340 		assertEquals("refs/heads/foo", c.getSource());
341 	}
342 
343 	@Test
344 	public void testWildcardMirror() {
345 		RefSpec a = new RefSpec("*:*");
346 		assertTrue(a.isWildcard());
347 		assertTrue(a.matchSource("a"));
348 		assertTrue(a.matchSource("foo"));
349 		assertTrue(a.matchSource("foo/bar"));
350 		assertTrue(a.matchDestination("a"));
351 		assertTrue(a.matchDestination("foo"));
352 		assertTrue(a.matchDestination("foo/bar"));
353 
354 		RefSpec b = a.expandFromSource("refs/heads/foo");
355 		assertEquals("refs/heads/foo", b.getDestination());
356 		RefSpec c = a.expandFromDestination("refs/heads/foo");
357 		assertEquals("refs/heads/foo", c.getSource());
358 	}
359 
360 	@Test
361 	public void testWildcardAtStart() {
362 		RefSpec a = new RefSpec("*/head:refs/heads/*");
363 		assertTrue(a.isWildcard());
364 		assertTrue(a.matchSource("a/head"));
365 		assertTrue(a.matchSource("foo/head"));
366 		assertTrue(a.matchSource("foo/bar/head"));
367 		assertFalse(a.matchSource("/head"));
368 		assertFalse(a.matchSource("a/head/extra"));
369 
370 		RefSpec b = a.expandFromSource("foo/head");
371 		assertEquals("refs/heads/foo", b.getDestination());
372 		RefSpec c = a.expandFromDestination("refs/heads/foo");
373 		assertEquals("foo/head", c.getSource());
374 	}
375 
376 	@Test(expected = IllegalArgumentException.class)
377 	public void invalidWhenSourceOnlyAndWildcard() {
378 		assertNotNull(new RefSpec("refs/heads/*"));
379 	}
380 
381 	@Test(expected = IllegalArgumentException.class)
382 	public void invalidWhenDestinationOnlyAndWildcard() {
383 		assertNotNull(new RefSpec(":refs/heads/*"));
384 	}
385 
386 	@Test(expected = IllegalArgumentException.class)
387 	public void invalidWhenOnlySourceWildcard() {
388 		assertNotNull(new RefSpec("refs/heads/*:refs/heads/foo"));
389 	}
390 
391 	@Test(expected = IllegalArgumentException.class)
392 	public void invalidWhenOnlyDestinationWildcard() {
393 		assertNotNull(new RefSpec("refs/heads/foo:refs/heads/*"));
394 	}
395 
396 	@Test(expected = IllegalArgumentException.class)
397 	public void invalidWhenMoreThanOneWildcardInSource() {
398 		assertNotNull(new RefSpec("refs/heads/*/*:refs/heads/*"));
399 	}
400 
401 	@Test(expected = IllegalArgumentException.class)
402 	public void invalidWhenMoreThanOneWildcardInDestination() {
403 		assertNotNull(new RefSpec("refs/heads/*:refs/heads/*/*"));
404 	}
405 
406 	@Test(expected = IllegalArgumentException.class)
407 	public void invalidWhenWildcardAfterText() {
408 		assertNotNull(new RefSpec("refs/heads/wrong*:refs/heads/right/*"));
409 	}
410 
411 	@Test(expected = IllegalArgumentException.class)
412 	public void invalidWhenWildcardBeforeText() {
413 		assertNotNull(new RefSpec("*wrong:right/*"));
414 	}
415 
416 	@Test(expected = IllegalArgumentException.class)
417 	public void invalidWhenWildcardBeforeTextAtEnd() {
418 		assertNotNull(new RefSpec("refs/heads/*wrong:right/*"));
419 	}
420 
421 	@Test(expected = IllegalArgumentException.class)
422 	public void invalidSourceDoubleSlashes() {
423 		assertNotNull(new RefSpec("refs/heads//wrong"));
424 	}
425 
426 	@Test(expected = IllegalArgumentException.class)
427 	public void invalidSlashAtStart() {
428 		assertNotNull(new RefSpec("/foo:/foo"));
429 	}
430 
431 	@Test(expected = IllegalArgumentException.class)
432 	public void invalidDestinationDoubleSlashes() {
433 		assertNotNull(new RefSpec(":refs/heads//wrong"));
434 	}
435 
436 	@Test(expected = IllegalArgumentException.class)
437 	public void invalidSetSource() {
438 		RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
439 		a.setSource("refs/heads/*/*");
440 	}
441 
442 	@Test(expected = IllegalArgumentException.class)
443 	public void invalidSetDestination() {
444 		RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
445 		a.setDestination("refs/remotes/origin/*/*");
446 	}
447 }