View Javadoc
1   /*
2    * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5    * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.eclipse.jgit.transport;
48  
49  import static org.junit.Assert.assertEquals;
50  import static org.junit.Assert.assertFalse;
51  import static org.junit.Assert.assertNull;
52  import static org.junit.Assert.assertTrue;
53  import static org.junit.Assert.fail;
54  
55  import java.io.File;
56  import java.io.IOException;
57  import java.net.URISyntaxException;
58  
59  import org.junit.Ignore;
60  import org.junit.Test;
61  
62  public class URIishTest {
63  
64  	private static final String GIT_SCHEME = "git://";
65  
66  	@Test
67  	public void shouldRaiseErrorOnEmptyURI() throws Exception {
68  		try {
69  			new URIish("");
70  			fail("expecting an exception");
71  		} catch (URISyntaxException e) {
72  			// expected
73  		}
74  	}
75  
76  	@Test
77  	public void shouldRaiseErrorOnNullURI() throws Exception {
78  		try {
79  			new URIish((String) null);
80  			fail("expecting an exception");
81  		} catch (URISyntaxException e) {
82  			// expected
83  		}
84  	}
85  
86  	@Test
87  	public void testUnixFile() throws Exception {
88  		final String str = "/home/m y";
89  		URIish u = new URIish(str);
90  		assertNull(u.getScheme());
91  		assertFalse(u.isRemote());
92  		assertEquals(str, u.getRawPath());
93  		assertEquals(str, u.getPath());
94  		assertEquals(str, u.toString());
95  		assertEquals(str, u.toASCIIString());
96  		assertEquals(u, new URIish(str));
97  	}
98  
99  	@Test
100 	public void testWindowsFile() throws Exception {
101 		final String str = "D:/m y";
102 		URIish u = new URIish(str);
103 		assertNull(u.getScheme());
104 		assertFalse(u.isRemote());
105 		assertEquals(str, u.getRawPath());
106 		assertEquals(str, u.getPath());
107 		assertEquals(str, u.toString());
108 		assertEquals(str, u.toASCIIString());
109 		assertEquals(u, new URIish(str));
110 	}
111 
112 	@Test
113 	public void testWindowsFile2() throws Exception {
114 		final String str = "D:\\m y";
115 		URIish u = new URIish(str);
116 		assertNull(u.getScheme());
117 		assertFalse(u.isRemote());
118 		assertEquals("D:\\m y", u.getRawPath());
119 		assertEquals("D:\\m y", u.getPath());
120 		assertEquals("D:\\m y", u.toString());
121 		assertEquals("D:\\m y", u.toASCIIString());
122 		assertEquals(u, new URIish(str));
123 	}
124 
125 	@Test
126 	public void testRelativePath() throws Exception {
127 		final String str = "../../foo/bar";
128 		URIish u = new URIish(str);
129 		assertNull(u.getScheme());
130 		assertFalse(u.isRemote());
131 		assertEquals(str, u.getRawPath());
132 		assertEquals(str, u.getPath());
133 		assertEquals(str, u.toString());
134 		assertEquals(str, u.toASCIIString());
135 		assertEquals(u, new URIish(str));
136 	}
137 
138 	@Test
139 	public void testUNC() throws Exception {
140 		final String str = "\\\\some\\place";
141 		URIish u = new URIish(str);
142 		assertNull(u.getScheme());
143 		assertFalse(u.isRemote());
144 		assertEquals("\\\\some\\place", u.getRawPath());
145 		assertEquals("\\\\some\\place", u.getPath());
146 		assertEquals("\\\\some\\place", u.toString());
147 		assertEquals("\\\\some\\place", u.toASCIIString());
148 		assertEquals(u, new URIish(str));
149 	}
150 
151 	@Test
152 	public void testFileProtoUnix() throws Exception {
153 		final String str = "file:///home/m y";
154 		URIish u = new URIish(str);
155 		assertEquals("file", u.getScheme());
156 		assertFalse(u.isRemote());
157 		assertEquals("/home/m y", u.getRawPath());
158 		assertEquals("/home/m y", u.getPath());
159 		assertEquals("file:///home/m y", u.toString());
160 		assertEquals("file:///home/m%20y", u.toASCIIString());
161 		assertEquals(u, new URIish(str));
162 	}
163 
164 	@Test
165 	public void testURIEncode_00() throws Exception {
166 		final String str = "file:///home/m%00y";
167 		URIish u = new URIish(str);
168 		assertEquals("file", u.getScheme());
169 		assertFalse(u.isRemote());
170 		assertEquals("/home/m%00y", u.getRawPath());
171 		assertEquals("/home/m\u0000y", u.getPath());
172 		assertEquals("file:///home/m%00y", u.toString());
173 		assertEquals("file:///home/m%00y", u.toASCIIString());
174 		assertEquals(u, new URIish(str));
175 	}
176 
177 	@Test
178 	public void testURIEncode_0a() throws Exception {
179 		final String str = "file:///home/m%0ay";
180 		URIish u = new URIish(str);
181 		assertEquals("file", u.getScheme());
182 		assertFalse(u.isRemote());
183 		assertEquals("/home/m%0ay", u.getRawPath());
184 		assertEquals("/home/m\ny", u.getPath());
185 		assertEquals("file:///home/m%0ay", u.toString());
186 		assertEquals("file:///home/m%0ay", u.toASCIIString());
187 		assertEquals(u, new URIish(str));
188 	}
189 
190 	@Test
191 	public void testURIEncode_unicode() throws Exception {
192 		final String str = "file:///home/m%c3%a5y";
193 		URIish u = new URIish(str);
194 		assertEquals("file", u.getScheme());
195 		assertFalse(u.isRemote());
196 		assertEquals("/home/m%c3%a5y", u.getRawPath());
197 		assertEquals("/home/m\u00e5y", u.getPath());
198 		assertEquals("file:///home/m%c3%a5y", u.toString());
199 		assertEquals("file:///home/m%c3%a5y", u.toASCIIString());
200 		assertEquals(u, new URIish(str));
201 	}
202 
203 	@Test
204 	public void testFileProtoWindows() throws Exception {
205 		final String str = "file:///D:/m y";
206 		URIish u = new URIish(str);
207 		assertEquals("file", u.getScheme());
208 		assertFalse(u.isRemote());
209 		assertEquals("D:/m y", u.getRawPath());
210 		assertEquals("D:/m y", u.getPath());
211 		assertEquals("file:///D:/m y", u.toString());
212 		assertEquals("file:///D:/m%20y", u.toASCIIString());
213 		assertEquals(u, new URIish(str));
214 	}
215 
216 	@Test
217 	public void testGitProtoUnix() throws Exception {
218 		final String str = "git://example.com/home/m y";
219 		URIish u = new URIish(str);
220 		assertEquals("git", u.getScheme());
221 		assertTrue(u.isRemote());
222 		assertEquals("example.com", u.getHost());
223 		assertEquals("/home/m y", u.getRawPath());
224 		assertEquals("/home/m y", u.getPath());
225 		assertEquals("git://example.com/home/m y", u.toString());
226 		assertEquals("git://example.com/home/m%20y", u.toASCIIString());
227 		assertEquals(u, new URIish(str));
228 	}
229 
230 	@Test
231 	public void testGitProtoUnixPort() throws Exception {
232 		final String str = "git://example.com:333/home/m y";
233 		URIish u = new URIish(str);
234 		assertEquals("git", u.getScheme());
235 		assertTrue(u.isRemote());
236 		assertEquals("example.com", u.getHost());
237 		assertEquals("/home/m y", u.getRawPath());
238 		assertEquals("/home/m y", u.getPath());
239 		assertEquals(333, u.getPort());
240 		assertEquals("git://example.com:333/home/m y", u.toString());
241 		assertEquals("git://example.com:333/home/m%20y", u.toASCIIString());
242 		assertEquals(u, new URIish(str));
243 	}
244 
245 	@Test
246 	public void testGitProtoWindowsPort() throws Exception {
247 		final String str = "git://example.com:338/D:/m y";
248 		URIish u = new URIish(str);
249 		assertEquals("git", u.getScheme());
250 		assertTrue(u.isRemote());
251 		assertEquals("D:/m y", u.getRawPath());
252 		assertEquals("D:/m y", u.getPath());
253 		assertEquals(338, u.getPort());
254 		assertEquals("example.com", u.getHost());
255 		assertEquals("git://example.com:338/D:/m y", u.toString());
256 		assertEquals("git://example.com:338/D:/m%20y", u.toASCIIString());
257 		assertEquals(u, new URIish(str));
258 	}
259 
260 	@Test
261 	public void testGitProtoWindows() throws Exception {
262 		final String str = "git://example.com/D:/m y";
263 		URIish u = new URIish(str);
264 		assertEquals("git", u.getScheme());
265 		assertTrue(u.isRemote());
266 		assertEquals("D:/m y", u.getRawPath());
267 		assertEquals("D:/m y", u.getPath());
268 		assertEquals("example.com", u.getHost());
269 		assertEquals(-1, u.getPort());
270 		assertEquals("git://example.com/D:/m y", u.toString());
271 		assertEquals("git://example.com/D:/m%20y", u.toASCIIString());
272 		assertEquals(u, new URIish(str));
273 	}
274 
275 	@Test
276 	public void testScpStyleNoURIDecoding() throws Exception {
277 		final String str = "example.com:some/p%20ath";
278 		URIish u = new URIish(str);
279 		assertNull(u.getScheme());
280 		assertTrue(u.isRemote());
281 		assertEquals("some/p%20ath", u.getRawPath());
282 		assertEquals("some/p%20ath", u.getPath());
283 		assertEquals("example.com", u.getHost());
284 		assertEquals(-1, u.getPort());
285 		assertEquals(str, u.toString());
286 		assertEquals(str, u.toASCIIString());
287 		assertEquals(u, new URIish(str));
288 	}
289 
290 	@Test
291 	public void testScpStyleWithoutUserRelativePath() throws Exception {
292 		final String str = "example.com:some/p ath";
293 		URIish u = new URIish(str);
294 		assertNull(u.getScheme());
295 		assertTrue(u.isRemote());
296 		assertEquals("some/p ath", u.getRawPath());
297 		assertEquals("some/p ath", u.getPath());
298 		assertEquals("example.com", u.getHost());
299 		assertEquals(-1, u.getPort());
300 		assertEquals(str, u.toString());
301 		assertEquals(str, u.toASCIIString());
302 		assertEquals(u, new URIish(str));
303 	}
304 
305 	@Test
306 	public void testScpStyleWithoutUserAbsolutePath() throws Exception {
307 		final String str = "example.com:/some/p ath";
308 		URIish u = new URIish(str);
309 		assertNull(u.getScheme());
310 		assertTrue(u.isRemote());
311 		assertEquals("/some/p ath", u.getRawPath());
312 		assertEquals("/some/p ath", u.getPath());
313 		assertEquals("example.com", u.getHost());
314 		assertEquals(-1, u.getPort());
315 		assertEquals(str, u.toString());
316 		assertEquals(str, u.toASCIIString());
317 		assertEquals(u, new URIish(str));
318 	}
319 
320 	@Test
321 	public void testScpStyleWithUser() throws Exception {
322 		final String str = "user@example.com:some/p ath";
323 		URIish u = new URIish(str);
324 		assertNull(u.getScheme());
325 		assertTrue(u.isRemote());
326 		assertEquals("some/p ath", u.getRawPath());
327 		assertEquals("some/p ath", u.getPath());
328 		assertEquals("user", u.getUser());
329 		assertEquals("example.com", u.getHost());
330 		assertEquals(-1, u.getPort());
331 		assertEquals(str, u.toString());
332 		assertEquals(str, u.toASCIIString());
333 		assertEquals(u, new URIish(str));
334 	}
335 
336 	@Test
337 	public void testGitSshProto() throws Exception {
338 		final String str = "git+ssh://example.com/some/p ath";
339 		URIish u = new URIish(str);
340 		assertEquals("git+ssh", u.getScheme());
341 		assertTrue(u.isRemote());
342 		assertEquals("/some/p ath", u.getRawPath());
343 		assertEquals("/some/p ath", u.getPath());
344 		assertEquals("example.com", u.getHost());
345 		assertEquals(-1, u.getPort());
346 		assertEquals("git+ssh://example.com/some/p ath", u.toString());
347 		assertEquals("git+ssh://example.com/some/p%20ath", u.toASCIIString());
348 		assertEquals(u, new URIish(str));
349 	}
350 
351 	@Test
352 	public void testSshGitProto() throws Exception {
353 		final String str = "ssh+git://example.com/some/p ath";
354 		URIish u = new URIish(str);
355 		assertEquals("ssh+git", u.getScheme());
356 		assertTrue(u.isRemote());
357 		assertEquals("/some/p ath", u.getRawPath());
358 		assertEquals("/some/p ath", u.getPath());
359 		assertEquals("example.com", u.getHost());
360 		assertEquals(-1, u.getPort());
361 		assertEquals("ssh+git://example.com/some/p ath", u.toString());
362 		assertEquals("ssh+git://example.com/some/p%20ath", u.toASCIIString());
363 		assertEquals(u, new URIish(str));
364 	}
365 
366 	@Test
367 	public void testSshProto() throws Exception {
368 		final String str = "ssh://example.com/some/p ath";
369 		URIish u = new URIish(str);
370 		assertEquals("ssh", u.getScheme());
371 		assertTrue(u.isRemote());
372 		assertEquals("/some/p ath", u.getRawPath());
373 		assertEquals("/some/p ath", u.getPath());
374 		assertEquals("example.com", u.getHost());
375 		assertEquals(-1, u.getPort());
376 		assertEquals("ssh://example.com/some/p ath", u.toString());
377 		assertEquals("ssh://example.com/some/p%20ath", u.toASCIIString());
378 		assertEquals(u, new URIish(str));
379 	}
380 
381 	@Test
382 	public void testSshProtoWithUserAndPort() throws Exception {
383 		final String str = "ssh://user@example.com:33/some/p ath";
384 		URIish u = new URIish(str);
385 		assertEquals("ssh", u.getScheme());
386 		assertTrue(u.isRemote());
387 		assertEquals("/some/p ath", u.getRawPath());
388 		assertEquals("/some/p ath", u.getPath());
389 		assertEquals("example.com", u.getHost());
390 		assertEquals("user", u.getUser());
391 		assertNull(u.getPass());
392 		assertEquals(33, u.getPort());
393 		assertEquals("ssh://user@example.com:33/some/p ath", u.toString());
394 		assertEquals("ssh://user@example.com:33/some/p%20ath",
395 				u.toASCIIString());
396 		assertEquals(u, new URIish(str));
397 	}
398 
399 	@Test
400 	public void testSshProtoWithUserPassAndPort() throws Exception {
401 		final String str = "ssh://user:pass@example.com:33/some/p ath";
402 		URIish u = new URIish(str);
403 		assertEquals("ssh", u.getScheme());
404 		assertTrue(u.isRemote());
405 		assertEquals("/some/p ath", u.getRawPath());
406 		assertEquals("/some/p ath", u.getPath());
407 		assertEquals("example.com", u.getHost());
408 		assertEquals("user", u.getUser());
409 		assertEquals("pass", u.getPass());
410 		assertEquals(33, u.getPort());
411 		assertEquals("ssh://user:pass@example.com:33/some/p ath",
412 				u.toPrivateString());
413 		assertEquals("ssh://user:pass@example.com:33/some/p%20ath",
414 				u.toPrivateASCIIString());
415 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
416 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
417 		assertEquals(u, new URIish(str));
418 	}
419 
420 	@Test
421 	public void testSshProtoWithADUserPassAndPort() throws Exception {
422 		final String str = "ssh://DOMAIN\\user:pass@example.com:33/some/p ath";
423 		URIish u = new URIish(str);
424 		assertEquals("ssh", u.getScheme());
425 		assertTrue(u.isRemote());
426 		assertEquals("/some/p ath", u.getRawPath());
427 		assertEquals("/some/p ath", u.getPath());
428 		assertEquals("example.com", u.getHost());
429 		assertEquals("DOMAIN\\user", u.getUser());
430 		assertEquals("pass", u.getPass());
431 		assertEquals(33, u.getPort());
432 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p ath",
433 				u.toPrivateString());
434 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p%20ath",
435 				u.toPrivateASCIIString());
436 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
437 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
438 		assertEquals(u, new URIish(str));
439 	}
440 
441 	@Test
442 	public void testSshProtoWithEscapedADUserPassAndPort() throws Exception {
443 		final String str = "ssh://DOMAIN%5c\u00fcser:pass@example.com:33/some/p ath";
444 		URIish u = new URIish(str);
445 		assertEquals("ssh", u.getScheme());
446 		assertTrue(u.isRemote());
447 		assertEquals("/some/p ath", u.getRawPath());
448 		assertEquals("/some/p ath", u.getPath());
449 		assertEquals("example.com", u.getHost());
450 		assertEquals("DOMAIN\\\u00fcser", u.getUser());
451 		assertEquals("pass", u.getPass());
452 		assertEquals(33, u.getPort());
453 		assertEquals("ssh://DOMAIN\\\u00fcser:pass@example.com:33/some/p ath",
454 				u.toPrivateString());
455 		assertEquals(
456 				"ssh://DOMAIN\\%c3%bcser:pass@example.com:33/some/p%20ath",
457 				u.toPrivateASCIIString());
458 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
459 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
460 		assertEquals(u, new URIish(str));
461 	}
462 
463 	@Test
464 	public void testURIEncodeDecode() throws Exception {
465 		final String str = "ssh://%3ax%25:%40%41x@example.com:33/some%c3%a5/p%20a th";
466 		URIish u = new URIish(str);
467 		assertEquals("ssh", u.getScheme());
468 		assertTrue(u.isRemote());
469 		assertEquals("/some%c3%a5/p%20a th", u.getRawPath());
470 		assertEquals("/some\u00e5/p a th", u.getPath());
471 		assertEquals("example.com", u.getHost());
472 		assertEquals(":x%", u.getUser());
473 		assertEquals("@Ax", u.getPass());
474 		assertEquals(33, u.getPort());
475 		assertEquals("ssh://%3ax%25:%40Ax@example.com:33/some%c3%a5/p%20a th",
476 				u.toPrivateString());
477 		assertEquals(
478 				"ssh://%3ax%25:%40Ax@example.com:33/some%c3%a5/p%20a%20th",
479 				u.toPrivateASCIIString());
480 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
481 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
482 		assertEquals(u, new URIish(str));
483 	}
484 
485 	@Test
486 	public void testGitWithUserHome() throws Exception {
487 		final String str = "git://example.com/~some/p ath";
488 		URIish u = new URIish(str);
489 		assertEquals("git", u.getScheme());
490 		assertTrue(u.isRemote());
491 		assertEquals("~some/p ath", u.getRawPath());
492 		assertEquals("~some/p ath", u.getPath());
493 		assertEquals("example.com", u.getHost());
494 		assertNull(u.getUser());
495 		assertNull(u.getPass());
496 		assertEquals(-1, u.getPort());
497 		assertEquals("git://example.com/~some/p ath", u.toPrivateString());
498 		assertEquals("git://example.com/~some/p%20ath",
499 				u.toPrivateASCIIString());
500 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
501 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
502 		assertEquals(u, new URIish(str));
503 	}
504 
505 	@Test
506 	@Ignore("Resolving ~user is beyond standard Java API and need more support")
507 	public void testFileWithUserHome() throws Exception {
508 		final String str = "~some/p ath";
509 		URIish u = new URIish(str);
510 		assertEquals("git", u.getScheme());
511 		assertTrue(u.isRemote());
512 		assertEquals("~some/p ath", u.getRawPath());
513 		assertEquals("~some/p ath", u.getPath());
514 		assertEquals("example.com", u.getHost());
515 		assertNull(u.getUser());
516 		assertNull(u.getPass());
517 		assertEquals(-1, u.getPort());
518 		assertEquals(str, u.toPrivateString());
519 		assertEquals(str, u.toPrivateASCIIString());
520 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
521 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
522 		assertEquals(u, new URIish(str));
523 	}
524 
525 	@Test
526 	public void testFileWithNoneUserHomeWithTilde() throws Exception {
527 		final String str = "/~some/p ath";
528 		URIish u = new URIish(str);
529 		assertNull(u.getScheme());
530 		assertFalse(u.isRemote());
531 		assertEquals("/~some/p ath", u.getRawPath());
532 		assertEquals("/~some/p ath", u.getPath());
533 		assertNull(u.getHost());
534 		assertNull(u.getUser());
535 		assertNull(u.getPass());
536 		assertEquals(-1, u.getPort());
537 		assertEquals(str, u.toPrivateString());
538 		assertEquals(str, u.toPrivateASCIIString());
539 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
540 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
541 		assertEquals(u, new URIish(str));
542 	}
543 
544 	@Test
545 	public void testGetNullHumanishName() {
546 		try {
547 			new URIish().getHumanishName();
548 			fail("path must be not null");
549 		} catch (IllegalArgumentException e) {
550 			// expected
551 		}
552 	}
553 
554 	@Test
555 	public void testGetEmptyHumanishName() throws URISyntaxException {
556 		try {
557 			new URIish(GIT_SCHEME).getHumanishName();
558 			fail("empty path is useless");
559 		} catch (IllegalArgumentException e) {
560 			// expected
561 		}
562 	}
563 
564 	@Test
565 	public void testGetAbsEmptyHumanishName() {
566 		try {
567 			new URIish().getHumanishName();
568 			fail("empty path is useless");
569 		} catch (IllegalArgumentException e) {
570 			// expected
571 		}
572 	}
573 
574 	@Test
575 	public void testGetSet() throws Exception {
576 		final String str = "ssh://DOMAIN\\user:pass@example.com:33/some/p ath%20";
577 		URIish u = new URIish(str);
578 		u = u.setHost(u.getHost());
579 		u = u.setPass(u.getPass());
580 		u = u.setPort(u.getPort());
581 		assertEquals("ssh", u.getScheme());
582 		assertTrue(u.isRemote());
583 		u = u.setRawPath(u.getRawPath());
584 		assertEquals("/some/p ath%20", u.getRawPath());
585 		u = u.setPath(u.getPath());
586 		assertEquals("/some/p ath ", u.getRawPath());
587 		assertEquals("/some/p ath ", u.getPath());
588 		assertEquals("example.com", u.getHost());
589 		assertEquals("DOMAIN\\user", u.getUser());
590 		assertEquals("pass", u.getPass());
591 		assertEquals(33, u.getPort());
592 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p ath ",
593 				u.toPrivateString());
594 		assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p%20ath%20",
595 				u.toPrivateASCIIString());
596 		assertEquals(u.setPass(null).toPrivateString(), u.toString());
597 		assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
598 		assertEquals(u, new URIish(str));
599 	}
600 
601 	@Test
602 	public void testGetValidWithEmptySlashDotGitHumanishName()
603 			throws IllegalArgumentException, URISyntaxException {
604 		String humanishName = new URIish("/a/b/.git").getHumanishName();
605 		assertEquals("b", humanishName);
606 	}
607 
608 	@Test
609 	public void testGetWithSlashDotGitHumanishName() throws URISyntaxException {
610 		assertEquals("", new URIish("/.git").getHumanishName());
611 	}
612 
613 	@Test
614 	public void testGetTwoSlashesDotGitHumanishName() throws URISyntaxException {
615 		assertEquals("", new URIish("//.git").getHumanishName());
616 	}
617 
618 	@Test
619 	public void testGetValidHumanishName() throws IllegalArgumentException,
620 			URISyntaxException {
621 		String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
622 		assertEquals("abc", humanishName);
623 	}
624 
625 	@Test
626 	public void testGetValidSlashHumanishName()
627 			throws IllegalArgumentException, URISyntaxException {
628 		String humanishName = new URIish(GIT_SCHEME + "host/abc/")
629 				.getHumanishName();
630 		assertEquals("abc", humanishName);
631 	}
632 
633 	@Test
634 	public void testGetSlashValidSlashHumanishName()
635 			throws IllegalArgumentException, URISyntaxException {
636 		String humanishName = new URIish("/abc/").getHumanishName();
637 		assertEquals("abc", humanishName);
638 	}
639 
640 	@Test
641 	public void testGetSlashValidSlashDotGitSlashHumanishName()
642 			throws IllegalArgumentException, URISyntaxException {
643 		String humanishName = new URIish("/abc/.git").getHumanishName();
644 		assertEquals("abc", humanishName);
645 	}
646 
647 	@Test
648 	public void testGetSlashSlashDotGitSlashHumanishName()
649 			throws IllegalArgumentException, URISyntaxException {
650 		final String humanishName = new URIish(GIT_SCHEME + "/.git")
651 				.getHumanishName();
652 		assertEquals("may return an empty humanish name", "", humanishName);
653 	}
654 
655 	@Test
656 	public void testGetSlashesValidSlashHumanishName()
657 			throws IllegalArgumentException, URISyntaxException {
658 		String humanishName = new URIish("/a/b/c/").getHumanishName();
659 		assertEquals("c", humanishName);
660 	}
661 
662 	@Test
663 	public void testGetValidDotGitHumanishName()
664 			throws IllegalArgumentException, URISyntaxException {
665 		String humanishName = new URIish(GIT_SCHEME + "abc.git")
666 				.getHumanishName();
667 		assertEquals("abc", humanishName);
668 	}
669 
670 	@Test
671 	public void testGetValidDotGitSlashHumanishName()
672 			throws IllegalArgumentException, URISyntaxException {
673 		String humanishName = new URIish(GIT_SCHEME + "host.xy/abc.git/")
674 				.getHumanishName();
675 		assertEquals("abc", humanishName);
676 	}
677 
678 	@Test
679 	public void testGetValidWithSlashDotGitHumanishName()
680 			throws IllegalArgumentException, URISyntaxException {
681 		String humanishName = new URIish("/abc.git").getHumanishName();
682 		assertEquals("abc", humanishName);
683 	}
684 
685 	@Test
686 	public void testGetValidWithSlashDotGitSlashHumanishName()
687 			throws IllegalArgumentException, URISyntaxException {
688 		String humanishName = new URIish("/abc.git/").getHumanishName();
689 		assertEquals("abc", humanishName);
690 	}
691 
692 	@Test
693 	public void testGetValidWithSlashesDotGitHumanishName()
694 			throws IllegalArgumentException, URISyntaxException {
695 		String humanishName = new URIish("/a/b/c.git").getHumanishName();
696 		assertEquals("c", humanishName);
697 	}
698 
699 	@Test
700 	public void testGetValidWithSlashesDotGitSlashHumanishName()
701 			throws IllegalArgumentException, URISyntaxException {
702 		String humanishName = new URIish("/a/b/c.git/").getHumanishName();
703 		assertEquals("c", humanishName);
704 	}
705 
706 	@Test
707 	public void testGetValidLocalWithTwoSlashesHumanishName()
708 			throws IllegalArgumentException, URISyntaxException {
709 		String humanishName = new URIish("/a/b/c//").getHumanishName();
710 		assertEquals("c", humanishName);
711 	}
712 
713 	@Test
714 	public void testGetValidGitSchemeWithTwoSlashesHumanishName()
715 			throws IllegalArgumentException, URISyntaxException {
716 		String humanishName = new URIish(GIT_SCHEME + "/a/b/c//")
717 				.getHumanishName();
718 		assertEquals("c", humanishName);
719 	}
720 
721 	@Test
722 	public void testGetWindowsPathHumanishName()
723 			throws IllegalArgumentException,
724 			URISyntaxException {
725 		if (File.separatorChar == '\\') {
726 			String humanishName = new URIish("file:///C\\a\\b\\c.git/")
727 					.getHumanishName();
728 			assertEquals("c", humanishName);
729 		}
730 	}
731 
732 	@Test
733 	public void testUserPasswordAndPort() throws URISyntaxException {
734 		String str = "http://user:secret@host.xy:80/some/path";
735 		URIish u = new URIish(str);
736 		assertEquals("http", u.getScheme());
737 		assertTrue(u.isRemote());
738 		assertEquals("/some/path", u.getRawPath());
739 		assertEquals("/some/path", u.getPath());
740 		assertEquals("host.xy", u.getHost());
741 		assertEquals(80, u.getPort());
742 		assertEquals("user", u.getUser());
743 		assertEquals("secret", u.getPass());
744 		assertEquals(u, new URIish(str));
745 
746 		str = "http://user:secret@pass@host.xy:80/some/path";
747 		u = new URIish(str);
748 		assertEquals("http", u.getScheme());
749 		assertTrue(u.isRemote());
750 		assertEquals("/some/path", u.getPath());
751 		assertEquals("host.xy", u.getHost());
752 		assertEquals(80, u.getPort());
753 		assertEquals("user", u.getUser());
754 		assertEquals("secret@pass", u.getPass());
755 		assertEquals(u, new URIish(str));
756 	}
757 
758 	/**
759 	 * Exemplify what happens with the special case of encoding '/' as %2F. Web
760 	 * services in general parse path components before decoding the characters.
761 	 *
762 	 * @throws URISyntaxException
763 	 */
764 	@Test
765 	public void testPathSeparator() throws URISyntaxException {
766 		String str = "http://user:secret@host.xy:80/some%2Fpath";
767 		URIish u = new URIish(str);
768 		assertEquals("http", u.getScheme());
769 		assertTrue(u.isRemote());
770 		assertEquals("/some%2Fpath", u.getRawPath());
771 		assertEquals("/some/path", u.getPath());
772 		assertEquals("host.xy", u.getHost());
773 		assertEquals(80, u.getPort());
774 		assertEquals("user", u.getUser());
775 		assertEquals("secret", u.getPass());
776 		assertEquals(u, new URIish(str));
777 	}
778 
779 	@Test
780 	public void testFileProtocol() throws IllegalArgumentException,
781 			URISyntaxException, IOException {
782 		// as defined by git docu
783 		URIish u = new URIish("file:///a/b.txt");
784 		assertEquals("file", u.getScheme());
785 		assertFalse(u.isRemote());
786 		assertNull(u.getHost());
787 		assertNull(u.getPass());
788 		assertEquals("/a/b.txt", u.getRawPath());
789 		assertEquals("/a/b.txt", u.getPath());
790 		assertEquals(-1, u.getPort());
791 		assertNull(u.getUser());
792 		assertEquals("b.txt", u.getHumanishName());
793 
794 		File tmp = File.createTempFile("jgitUnitTest", ".tmp");
795 		u = new URIish(tmp.toURI().toString());
796 		assertEquals("file", u.getScheme());
797 		assertFalse(u.isRemote());
798 		assertNull(u.getHost());
799 		assertNull(u.getPass());
800 		assertTrue(u.getPath().contains("jgitUnitTest"));
801 		assertEquals(-1, u.getPort());
802 		assertNull(u.getUser());
803 		assertTrue(u.getHumanishName().startsWith("jgitUnitTest"));
804 
805 		u = new URIish("file:/a/b.txt");
806 		assertEquals("file", u.getScheme());
807 		assertFalse(u.isRemote());
808 		assertNull(u.getHost());
809 		assertNull(u.getPass());
810 		assertEquals("/a/b.txt", u.getRawPath());
811 		assertEquals("/a/b.txt", u.getPath());
812 		assertEquals(-1, u.getPort());
813 		assertNull(u.getUser());
814 		assertEquals("b.txt", u.getHumanishName());
815 	}
816 
817 	@Test
818 	public void testMissingPort() throws URISyntaxException {
819 		final String incorrectSshUrl = "ssh://some-host:/path/to/repository.git";
820 		URIish u = new URIish(incorrectSshUrl);
821 		assertFalse(TransportGitSsh.PROTO_SSH.canHandle(u));
822 	}
823 
824 	@Test
825 	public void testALot() throws URISyntaxException {
826 		// user pass host port path
827 		// 1 2 3 4 5
828 		String[][] tests = {
829 				new String[] { "%1$s://%2$s:%3$s@%4$s:%5$s/%6$s", "%1$s",
830 						"%2$s", "%3$s", "%4$s", "%5$s", "%6$s" },
831 				new String[] { "%1$s://%2$s@%4$s:%5$s/%6$s", "%1$s", "%2$s",
832 						null, "%4$s", "%5$s", "%6$s" },
833 				new String[] { "%1$s://%2$s@%4$s/%6$s", "%1$s", "%2$s", null,
834 						"%4$s", null, "%6$s" },
835 				new String[] { "%1$s://%4$s/%6$s", "%1$s", null, null, "%4$s",
836 						null, "%6$s" }, };
837 		String[] schemes = new String[] { "ssh", "ssh+git", "http", "https" };
838 		String[] users = new String[] { "me", "l usr\\example.com",
839 				"lusr\\example" };
840 		String[] passes = new String[] { "wtf", };
841 		String[] hosts = new String[] { "example.com", "1.2.3.4", "[::1]" };
842 		String[] ports = new String[] { "1234", "80" };
843 		String[] paths = new String[] { "/", "/abc", "D:/x", "D:\\x" };
844 		for (String[] test : tests) {
845 			String fmt = test[0];
846 			for (String scheme : schemes) {
847 				for (String user : users) {
848 					for (String pass : passes) {
849 						for (String host : hosts) {
850 							for (String port : ports) {
851 								for (String path : paths) {
852 									String url = String.format(fmt, scheme,
853 											user, pass, host, port, path);
854 									String[] expect = new String[test.length];
855 									for (int i = 1; i < expect.length; ++i)
856 										if (test[i] != null)
857 											expect[i] = String.format(test[i],
858 													scheme, user, pass, host,
859 													port, path);
860 									URIish urIish = new URIish(url);
861 									assertEquals(url, expect[1],
862 											urIish.getScheme());
863 									assertEquals(url, expect[2],
864 											urIish.getUser());
865 								}
866 							}
867 						}
868 					}
869 				}
870 			}
871 		}
872 	}
873 }