1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 package org.eclipse.jgit.transport;
49
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertFalse;
52 import static org.junit.Assert.assertNull;
53 import static org.junit.Assert.assertTrue;
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 @SuppressWarnings("unused")
67 @Test(expected = URISyntaxException.class)
68 public void shouldRaiseErrorOnEmptyURI() throws Exception {
69 new URIish("");
70 }
71
72 @SuppressWarnings("unused")
73 @Test(expected = URISyntaxException.class)
74 public void shouldRaiseErrorOnNullURI() throws Exception {
75 new URIish((String) null);
76 }
77
78 @Test
79 public void testUnixFile() throws Exception {
80 final String str = "/home/m y";
81 URIish u = new URIish(str);
82 assertNull(u.getScheme());
83 assertFalse(u.isRemote());
84 assertEquals(str, u.getRawPath());
85 assertEquals(str, u.getPath());
86 assertEquals(str, u.toString());
87 assertEquals(str, u.toASCIIString());
88 assertEquals(u, new URIish(str));
89 }
90
91 @Test
92 public void testWindowsFile() throws Exception {
93 final String str = "D:/m y";
94 URIish u = new URIish(str);
95 assertNull(u.getScheme());
96 assertFalse(u.isRemote());
97 assertEquals(str, u.getRawPath());
98 assertEquals(str, u.getPath());
99 assertEquals(str, u.toString());
100 assertEquals(str, u.toASCIIString());
101 assertEquals(u, new URIish(str));
102 }
103
104 @Test
105 public void testWindowsFile2() throws Exception {
106 final String str = "D:\\m y";
107 URIish u = new URIish(str);
108 assertNull(u.getScheme());
109 assertFalse(u.isRemote());
110 assertEquals("D:\\m y", u.getRawPath());
111 assertEquals("D:\\m y", u.getPath());
112 assertEquals("D:\\m y", u.toString());
113 assertEquals("D:\\m y", u.toASCIIString());
114 assertEquals(u, new URIish(str));
115 }
116
117 @Test
118 public void testRelativePath() throws Exception {
119 final String str = "../../foo/bar";
120 URIish u = new URIish(str);
121 assertNull(u.getScheme());
122 assertFalse(u.isRemote());
123 assertEquals(str, u.getRawPath());
124 assertEquals(str, u.getPath());
125 assertEquals(str, u.toString());
126 assertEquals(str, u.toASCIIString());
127 assertEquals(u, new URIish(str));
128 }
129
130 @Test
131 public void testUNC() throws Exception {
132 final String str = "\\\\some\\place";
133 URIish u = new URIish(str);
134 assertNull(u.getScheme());
135 assertFalse(u.isRemote());
136 assertEquals("\\\\some\\place", u.getRawPath());
137 assertEquals("\\\\some\\place", u.getPath());
138 assertEquals("\\\\some\\place", u.toString());
139 assertEquals("\\\\some\\place", u.toASCIIString());
140 assertEquals(u, new URIish(str));
141 }
142
143 @Test
144 public void testFileProtoUnix() throws Exception {
145 final String str = "file:///home/m y";
146 URIish u = new URIish(str);
147 assertEquals("file", u.getScheme());
148 assertFalse(u.isRemote());
149 assertEquals("/home/m y", u.getRawPath());
150 assertEquals("/home/m y", u.getPath());
151 assertEquals("file:///home/m y", u.toString());
152 assertEquals("file:///home/m%20y", u.toASCIIString());
153 assertEquals(u, new URIish(str));
154 }
155
156 @Test
157 public void testURIEncode_00() throws Exception {
158 final String str = "file:///home/m%00y";
159 URIish u = new URIish(str);
160 assertEquals("file", u.getScheme());
161 assertFalse(u.isRemote());
162 assertEquals("/home/m%00y", u.getRawPath());
163 assertEquals("/home/m\u0000y", u.getPath());
164 assertEquals("file:///home/m%00y", u.toString());
165 assertEquals("file:///home/m%00y", u.toASCIIString());
166 assertEquals(u, new URIish(str));
167 }
168
169 @Test
170 public void testURIEncode_0a() throws Exception {
171 final String str = "file:///home/m%0ay";
172 URIish u = new URIish(str);
173 assertEquals("file", u.getScheme());
174 assertFalse(u.isRemote());
175 assertEquals("/home/m%0ay", u.getRawPath());
176 assertEquals("/home/m\ny", u.getPath());
177 assertEquals("file:///home/m%0ay", u.toString());
178 assertEquals("file:///home/m%0ay", u.toASCIIString());
179 assertEquals(u, new URIish(str));
180 }
181
182 @Test
183 public void testURIEncode_unicode() throws Exception {
184 final String str = "file:///home/m%c3%a5y";
185 URIish u = new URIish(str);
186 assertEquals("file", u.getScheme());
187 assertFalse(u.isRemote());
188 assertEquals("/home/m%c3%a5y", u.getRawPath());
189 assertEquals("/home/m\u00e5y", u.getPath());
190 assertEquals("file:///home/m%c3%a5y", u.toString());
191 assertEquals("file:///home/m%c3%a5y", u.toASCIIString());
192 assertEquals(u, new URIish(str));
193 }
194
195 @Test
196 public void testFileProtoWindows() throws Exception {
197 final String str = "file:///D:/m y";
198 URIish u = new URIish(str);
199 assertEquals("file", u.getScheme());
200 assertFalse(u.isRemote());
201 assertEquals("D:/m y", u.getRawPath());
202 assertEquals("D:/m y", u.getPath());
203 assertEquals("file:///D:/m y", u.toString());
204 assertEquals("file:///D:/m%20y", u.toASCIIString());
205 assertEquals(u, new URIish(str));
206 }
207
208 @Test
209 public void testGitProtoUnix() throws Exception {
210 final String str = "git://example.com/home/m y";
211 URIish u = new URIish(str);
212 assertEquals("git", u.getScheme());
213 assertTrue(u.isRemote());
214 assertEquals("example.com", u.getHost());
215 assertEquals("/home/m y", u.getRawPath());
216 assertEquals("/home/m y", u.getPath());
217 assertEquals("git://example.com/home/m y", u.toString());
218 assertEquals("git://example.com/home/m%20y", u.toASCIIString());
219 assertEquals(u, new URIish(str));
220 }
221
222 @Test
223 public void testGitProtoUnixPort() throws Exception {
224 final String str = "git://example.com:333/home/m y";
225 URIish u = new URIish(str);
226 assertEquals("git", u.getScheme());
227 assertTrue(u.isRemote());
228 assertEquals("example.com", u.getHost());
229 assertEquals("/home/m y", u.getRawPath());
230 assertEquals("/home/m y", u.getPath());
231 assertEquals(333, u.getPort());
232 assertEquals("git://example.com:333/home/m y", u.toString());
233 assertEquals("git://example.com:333/home/m%20y", u.toASCIIString());
234 assertEquals(u, new URIish(str));
235 }
236
237 @Test
238 public void testGitProtoWindowsPort() throws Exception {
239 final String str = "git://example.com:338/D:/m y";
240 URIish u = new URIish(str);
241 assertEquals("git", u.getScheme());
242 assertTrue(u.isRemote());
243 assertEquals("D:/m y", u.getRawPath());
244 assertEquals("D:/m y", u.getPath());
245 assertEquals(338, u.getPort());
246 assertEquals("example.com", u.getHost());
247 assertEquals("git://example.com:338/D:/m y", u.toString());
248 assertEquals("git://example.com:338/D:/m%20y", u.toASCIIString());
249 assertEquals(u, new URIish(str));
250 }
251
252 @Test
253 public void testGitProtoWindows() throws Exception {
254 final String str = "git://example.com/D:/m y";
255 URIish u = new URIish(str);
256 assertEquals("git", u.getScheme());
257 assertTrue(u.isRemote());
258 assertEquals("D:/m y", u.getRawPath());
259 assertEquals("D:/m y", u.getPath());
260 assertEquals("example.com", u.getHost());
261 assertEquals(-1, u.getPort());
262 assertEquals("git://example.com/D:/m y", u.toString());
263 assertEquals("git://example.com/D:/m%20y", u.toASCIIString());
264 assertEquals(u, new URIish(str));
265 }
266
267 @Test
268 public void testScpStyleNoURIDecoding() throws Exception {
269 final String str = "example.com:some/p%20ath";
270 URIish u = new URIish(str);
271 assertNull(u.getScheme());
272 assertTrue(u.isRemote());
273 assertEquals("some/p%20ath", u.getRawPath());
274 assertEquals("some/p%20ath", u.getPath());
275 assertEquals("example.com", u.getHost());
276 assertEquals(-1, u.getPort());
277 assertEquals(str, u.toString());
278 assertEquals(str, u.toASCIIString());
279 assertEquals(u, new URIish(str));
280 }
281
282 @Test
283 public void testScpStyleWithoutUserRelativePath() throws Exception {
284 final String str = "example.com:some/p ath";
285 URIish u = new URIish(str);
286 assertNull(u.getScheme());
287 assertTrue(u.isRemote());
288 assertEquals("some/p ath", u.getRawPath());
289 assertEquals("some/p ath", u.getPath());
290 assertEquals("example.com", u.getHost());
291 assertEquals(-1, u.getPort());
292 assertEquals(str, u.toString());
293 assertEquals(str, u.toASCIIString());
294 assertEquals(u, new URIish(str));
295 }
296
297 @Test
298 public void testScpStyleWithoutUserAbsolutePath() throws Exception {
299 final String str = "example.com:/some/p ath";
300 URIish u = new URIish(str);
301 assertNull(u.getScheme());
302 assertTrue(u.isRemote());
303 assertEquals("/some/p ath", u.getRawPath());
304 assertEquals("/some/p ath", u.getPath());
305 assertEquals("example.com", u.getHost());
306 assertEquals(-1, u.getPort());
307 assertEquals(str, u.toString());
308 assertEquals(str, u.toASCIIString());
309 assertEquals(u, new URIish(str));
310 }
311
312 @Test
313 public void testScpStyleWithUser() throws Exception {
314 final String str = "user@example.com:some/p ath";
315 URIish u = new URIish(str);
316 assertNull(u.getScheme());
317 assertTrue(u.isRemote());
318 assertEquals("some/p ath", u.getRawPath());
319 assertEquals("some/p ath", u.getPath());
320 assertEquals("user", u.getUser());
321 assertEquals("example.com", u.getHost());
322 assertEquals(-1, u.getPort());
323 assertEquals(str, u.toString());
324 assertEquals(str, u.toASCIIString());
325 assertEquals(u, new URIish(str));
326 }
327
328 @Test
329 public void testGitSshProto() throws Exception {
330 final String str = "git+ssh://example.com/some/p ath";
331 URIish u = new URIish(str);
332 assertEquals("git+ssh", u.getScheme());
333 assertTrue(u.isRemote());
334 assertEquals("/some/p ath", u.getRawPath());
335 assertEquals("/some/p ath", u.getPath());
336 assertEquals("example.com", u.getHost());
337 assertEquals(-1, u.getPort());
338 assertEquals("git+ssh://example.com/some/p ath", u.toString());
339 assertEquals("git+ssh://example.com/some/p%20ath", u.toASCIIString());
340 assertEquals(u, new URIish(str));
341 }
342
343 @Test
344 public void testSshGitProto() throws Exception {
345 final String str = "ssh+git://example.com/some/p ath";
346 URIish u = new URIish(str);
347 assertEquals("ssh+git", u.getScheme());
348 assertTrue(u.isRemote());
349 assertEquals("/some/p ath", u.getRawPath());
350 assertEquals("/some/p ath", u.getPath());
351 assertEquals("example.com", u.getHost());
352 assertEquals(-1, u.getPort());
353 assertEquals("ssh+git://example.com/some/p ath", u.toString());
354 assertEquals("ssh+git://example.com/some/p%20ath", u.toASCIIString());
355 assertEquals(u, new URIish(str));
356 }
357
358 @Test
359 public void testSshProto() throws Exception {
360 final String str = "ssh://example.com/some/p ath";
361 URIish u = new URIish(str);
362 assertEquals("ssh", u.getScheme());
363 assertTrue(u.isRemote());
364 assertEquals("/some/p ath", u.getRawPath());
365 assertEquals("/some/p ath", u.getPath());
366 assertEquals("example.com", u.getHost());
367 assertEquals(-1, u.getPort());
368 assertEquals("ssh://example.com/some/p ath", u.toString());
369 assertEquals("ssh://example.com/some/p%20ath", u.toASCIIString());
370 assertEquals(u, new URIish(str));
371 }
372
373 @Test
374 public void testSshProtoHostOnly() throws Exception {
375 final String str = "ssh://example.com/";
376 URIish u = new URIish(str);
377 assertEquals("ssh", u.getScheme());
378 assertTrue(u.isRemote());
379 assertEquals("/", u.getRawPath());
380 assertEquals("/", u.getPath());
381 assertEquals("example.com", u.getHost());
382 assertEquals(-1, u.getPort());
383 assertEquals("ssh://example.com/", u.toString());
384 assertEquals("ssh://example.com/", u.toASCIIString());
385 assertEquals("example.com", u.getHumanishName());
386 assertEquals(u, new URIish(str));
387 }
388
389 @Test
390 public void testSshProtoHostWithAuthentication() throws Exception {
391 final String str = "ssh://user:secret@pass@example.com/";
392 URIish u = new URIish(str);
393 assertEquals("ssh", u.getScheme());
394 assertTrue(u.isRemote());
395 assertEquals("/", u.getRawPath());
396 assertEquals("/", u.getPath());
397 assertEquals("example.com", u.getHost());
398 assertEquals(-1, u.getPort());
399 assertEquals("ssh://user@example.com/", u.toString());
400 assertEquals("ssh://user@example.com/", u.toASCIIString());
401 assertEquals("example.com", u.getHumanishName());
402 assertEquals("user", u.getUser());
403 assertEquals("secret@pass", u.getPass());
404 assertEquals(u, new URIish(str));
405 }
406
407 @Test
408 public void testSshProtoHostWithPort() throws Exception {
409 final String str = "ssh://example.com:2222/";
410 URIish u = new URIish(str);
411 assertEquals("ssh", u.getScheme());
412 assertTrue(u.isRemote());
413 assertEquals("/", u.getRawPath());
414 assertEquals("/", u.getPath());
415 assertEquals("example.com", u.getHost());
416 assertEquals(2222, u.getPort());
417 assertEquals("ssh://example.com:2222/", u.toString());
418 assertEquals("ssh://example.com:2222/", u.toASCIIString());
419 assertEquals("example.com", u.getHumanishName());
420 assertEquals(u, new URIish(str));
421 }
422
423 @Test
424 public void testSshProtoWithUserAndPort() throws Exception {
425 final String str = "ssh://user@example.com:33/some/p ath";
426 URIish u = new URIish(str);
427 assertEquals("ssh", u.getScheme());
428 assertTrue(u.isRemote());
429 assertEquals("/some/p ath", u.getRawPath());
430 assertEquals("/some/p ath", u.getPath());
431 assertEquals("example.com", u.getHost());
432 assertEquals("user", u.getUser());
433 assertNull(u.getPass());
434 assertEquals(33, u.getPort());
435 assertEquals("ssh://user@example.com:33/some/p ath", u.toString());
436 assertEquals("ssh://user@example.com:33/some/p%20ath",
437 u.toASCIIString());
438 assertEquals(u, new URIish(str));
439 }
440
441 @Test
442 public void testSshProtoWithUserPassAndPort() throws Exception {
443 final String str = "ssh://user: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("user", u.getUser());
451 assertEquals("pass", u.getPass());
452 assertEquals(33, u.getPort());
453 assertEquals("ssh://user:pass@example.com:33/some/p ath",
454 u.toPrivateString());
455 assertEquals("ssh://user:pass@example.com:33/some/p%20ath",
456 u.toPrivateASCIIString());
457 assertEquals(u.setPass(null).toPrivateString(), u.toString());
458 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
459 assertEquals(u, new URIish(str));
460 }
461
462 @Test
463 public void testSshProtoWithEmailUserAndPort() throws Exception {
464 final String str = "ssh://user.name@email.com@example.com:33/some/p ath";
465 URIish u = new URIish(str);
466 assertEquals("ssh", u.getScheme());
467 assertTrue(u.isRemote());
468 assertEquals("/some/p ath", u.getRawPath());
469 assertEquals("/some/p ath", u.getPath());
470 assertEquals("example.com", u.getHost());
471 assertEquals("user.name@email.com", u.getUser());
472 assertNull(u.getPass());
473 assertEquals(33, u.getPort());
474 assertEquals("ssh://user.name%40email.com@example.com:33/some/p ath",
475 u.toPrivateString());
476 assertEquals("ssh://user.name%40email.com@example.com:33/some/p%20ath",
477 u.toPrivateASCIIString());
478 assertEquals(u.setPass(null).toPrivateString(), u.toString());
479 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
480 assertEquals(u, new URIish(str));
481 }
482
483 @Test
484 public void testSshProtoWithEmailUserPassAndPort() throws Exception {
485 final String str = "ssh://user.name@email.com:pass@wor:d@example.com:33/some/p ath";
486 URIish u = new URIish(str);
487 assertEquals("ssh", u.getScheme());
488 assertTrue(u.isRemote());
489 assertEquals("/some/p ath", u.getRawPath());
490 assertEquals("/some/p ath", u.getPath());
491 assertEquals("example.com", u.getHost());
492 assertEquals("user.name@email.com", u.getUser());
493 assertEquals("pass@wor:d", u.getPass());
494 assertEquals(33, u.getPort());
495 assertEquals("ssh://user.name%40email.com:pass%40wor%3ad@example.com:33/some/p ath",
496 u.toPrivateString());
497 assertEquals("ssh://user.name%40email.com:pass%40wor%3ad@example.com:33/some/p%20ath",
498 u.toPrivateASCIIString());
499 assertEquals(u.setPass(null).toPrivateString(), u.toString());
500 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
501 assertEquals(u, new URIish(str));
502 }
503
504 @Test
505 public void testSshProtoWithADUserPassAndPort() throws Exception {
506 final String str = "ssh://DOMAIN\\user:pass@example.com:33/some/p ath";
507 URIish u = new URIish(str);
508 assertEquals("ssh", u.getScheme());
509 assertTrue(u.isRemote());
510 assertEquals("/some/p ath", u.getRawPath());
511 assertEquals("/some/p ath", u.getPath());
512 assertEquals("example.com", u.getHost());
513 assertEquals("DOMAIN\\user", u.getUser());
514 assertEquals("pass", u.getPass());
515 assertEquals(33, u.getPort());
516 assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p ath",
517 u.toPrivateString());
518 assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p%20ath",
519 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 testSshProtoWithEscapedADUserPassAndPort() throws Exception {
527 final String str = "ssh://DOMAIN%5c\u00fcser:pass@example.com:33/some/p ath";
528 URIish u = new URIish(str);
529 assertEquals("ssh", u.getScheme());
530 assertTrue(u.isRemote());
531 assertEquals("/some/p ath", u.getRawPath());
532 assertEquals("/some/p ath", u.getPath());
533 assertEquals("example.com", u.getHost());
534 assertEquals("DOMAIN\\\u00fcser", u.getUser());
535 assertEquals("pass", u.getPass());
536 assertEquals(33, u.getPort());
537 assertEquals("ssh://DOMAIN\\\u00fcser:pass@example.com:33/some/p ath",
538 u.toPrivateString());
539 assertEquals(
540 "ssh://DOMAIN\\%c3%bcser:pass@example.com:33/some/p%20ath",
541 u.toPrivateASCIIString());
542 assertEquals(u.setPass(null).toPrivateString(), u.toString());
543 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
544 assertEquals(u, new URIish(str));
545 }
546
547 @Test
548 public void testURIEncodeDecode() throws Exception {
549 final String str = "ssh://%3ax%25:%40%41x@example.com:33/some%c3%a5/p%20a th";
550 URIish u = new URIish(str);
551 assertEquals("ssh", u.getScheme());
552 assertTrue(u.isRemote());
553 assertEquals("/some%c3%a5/p%20a th", u.getRawPath());
554 assertEquals("/some\u00e5/p a th", u.getPath());
555 assertEquals("example.com", u.getHost());
556 assertEquals(":x%", u.getUser());
557 assertEquals("@Ax", u.getPass());
558 assertEquals(33, u.getPort());
559 assertEquals("ssh://%3ax%25:%40Ax@example.com:33/some%c3%a5/p%20a th",
560 u.toPrivateString());
561 assertEquals(
562 "ssh://%3ax%25:%40Ax@example.com:33/some%c3%a5/p%20a%20th",
563 u.toPrivateASCIIString());
564 assertEquals(u.setPass(null).toPrivateString(), u.toString());
565 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
566 assertEquals(u, new URIish(str));
567 }
568
569 @Test
570 public void testGitWithUserHome() throws Exception {
571 final String str = "git://example.com/~some/p ath";
572 URIish u = new URIish(str);
573 assertEquals("git", u.getScheme());
574 assertTrue(u.isRemote());
575 assertEquals("~some/p ath", u.getRawPath());
576 assertEquals("~some/p ath", u.getPath());
577 assertEquals("example.com", u.getHost());
578 assertNull(u.getUser());
579 assertNull(u.getPass());
580 assertEquals(-1, u.getPort());
581 assertEquals("git://example.com/~some/p ath", u.toPrivateString());
582 assertEquals("git://example.com/~some/p%20ath",
583 u.toPrivateASCIIString());
584 assertEquals(u.setPass(null).toPrivateString(), u.toString());
585 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
586 assertEquals(u, new URIish(str));
587 }
588
589 @Test
590 @Ignore("Resolving ~user is beyond standard Java API and need more support")
591 public void testFileWithUserHome() throws Exception {
592 final String str = "~some/p ath";
593 URIish u = new URIish(str);
594 assertEquals("git", u.getScheme());
595 assertTrue(u.isRemote());
596 assertEquals("~some/p ath", u.getRawPath());
597 assertEquals("~some/p ath", u.getPath());
598 assertEquals("example.com", u.getHost());
599 assertNull(u.getUser());
600 assertNull(u.getPass());
601 assertEquals(-1, u.getPort());
602 assertEquals(str, u.toPrivateString());
603 assertEquals(str, u.toPrivateASCIIString());
604 assertEquals(u.setPass(null).toPrivateString(), u.toString());
605 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
606 assertEquals(u, new URIish(str));
607 }
608
609 @Test
610 public void testFileWithNoneUserHomeWithTilde() throws Exception {
611 final String str = "/~some/p ath";
612 URIish u = new URIish(str);
613 assertNull(u.getScheme());
614 assertFalse(u.isRemote());
615 assertEquals("/~some/p ath", u.getRawPath());
616 assertEquals("/~some/p ath", u.getPath());
617 assertNull(u.getHost());
618 assertNull(u.getUser());
619 assertNull(u.getPass());
620 assertEquals(-1, u.getPort());
621 assertEquals(str, u.toPrivateString());
622 assertEquals(str, u.toPrivateASCIIString());
623 assertEquals(u.setPass(null).toPrivateString(), u.toString());
624 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
625 assertEquals(u, new URIish(str));
626 }
627
628 @Test(expected = IllegalArgumentException.class)
629 public void testGetNullHumanishName() {
630 new URIish().getHumanishName();
631 }
632
633 @Test(expected = IllegalArgumentException.class)
634 public void testGetEmptyHumanishName() throws URISyntaxException {
635 new URIish(GIT_SCHEME).getHumanishName();
636 }
637
638 @Test(expected = IllegalArgumentException.class)
639 public void testGetAbsEmptyHumanishName() {
640 new URIish().getHumanishName();
641 }
642
643 @Test
644 public void testGetSet() throws Exception {
645 final String str = "ssh://DOMAIN\\user:pass@example.com:33/some/p ath%20";
646 URIish u = new URIish(str);
647 u = u.setHost(u.getHost());
648 u = u.setPass(u.getPass());
649 u = u.setPort(u.getPort());
650 assertEquals("ssh", u.getScheme());
651 assertTrue(u.isRemote());
652 u = u.setRawPath(u.getRawPath());
653 assertEquals("/some/p ath%20", u.getRawPath());
654 u = u.setPath(u.getPath());
655 assertEquals("/some/p ath ", u.getRawPath());
656 assertEquals("/some/p ath ", u.getPath());
657 assertEquals("example.com", u.getHost());
658 assertEquals("DOMAIN\\user", u.getUser());
659 assertEquals("pass", u.getPass());
660 assertEquals(33, u.getPort());
661 assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p ath ",
662 u.toPrivateString());
663 assertEquals("ssh://DOMAIN\\user:pass@example.com:33/some/p%20ath%20",
664 u.toPrivateASCIIString());
665 assertEquals(u.setPass(null).toPrivateString(), u.toString());
666 assertEquals(u.setPass(null).toPrivateASCIIString(), u.toASCIIString());
667 assertEquals(u, new URIish(str));
668 }
669
670 @Test
671 public void testGetValidWithEmptySlashDotGitHumanishName()
672 throws IllegalArgumentException, URISyntaxException {
673 String humanishName = new URIish("/a/b/.git").getHumanishName();
674 assertEquals("b", humanishName);
675 }
676
677 @Test
678 public void testGetWithSlashDotGitHumanishName() throws URISyntaxException {
679 assertEquals("", new URIish("/.git").getHumanishName());
680 }
681
682 @Test
683 public void testGetTwoSlashesDotGitHumanishName() throws URISyntaxException {
684 assertEquals("", new URIish("//.git").getHumanishName());
685 }
686
687 @Test
688 public void testGetValidHumanishName() throws IllegalArgumentException,
689 URISyntaxException {
690 String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
691 assertEquals("abc", humanishName);
692 }
693
694 @Test
695 public void testGetEmptyHumanishNameWithAuthorityOnly() throws IllegalArgumentException,
696 URISyntaxException {
697 String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
698 assertEquals("abc", humanishName);
699 }
700
701 @Test
702 public void testGetValidSlashHumanishName()
703 throws IllegalArgumentException, URISyntaxException {
704 String humanishName = new URIish(GIT_SCHEME + "host/abc/")
705 .getHumanishName();
706 assertEquals("abc", humanishName);
707 }
708
709 @Test
710 public void testGetSlashValidSlashHumanishName()
711 throws IllegalArgumentException, URISyntaxException {
712 String humanishName = new URIish("/abc/").getHumanishName();
713 assertEquals("abc", humanishName);
714 }
715
716 @Test
717 public void testGetSlashValidSlashDotGitSlashHumanishName()
718 throws IllegalArgumentException, URISyntaxException {
719 String humanishName = new URIish("/abc/.git").getHumanishName();
720 assertEquals("abc", humanishName);
721 }
722
723 @Test
724 public void testGetSlashSlashDotGitSlashHumanishName()
725 throws IllegalArgumentException, URISyntaxException {
726 final String humanishName = new URIish(GIT_SCHEME + "/.git")
727 .getHumanishName();
728 assertEquals("may return an empty humanish name", "", humanishName);
729 }
730
731 @Test
732 public void testGetSlashesValidSlashHumanishName()
733 throws IllegalArgumentException, URISyntaxException {
734 String humanishName = new URIish("/a/b/c/").getHumanishName();
735 assertEquals("c", humanishName);
736 }
737
738 @Test
739 public void testGetValidDotGitHumanishName()
740 throws IllegalArgumentException, URISyntaxException {
741 String humanishName = new URIish(GIT_SCHEME + "abc.git")
742 .getHumanishName();
743 assertEquals("abc", humanishName);
744 }
745
746 @Test
747 public void testGetValidDotGitSlashHumanishName()
748 throws IllegalArgumentException, URISyntaxException {
749 String humanishName = new URIish(GIT_SCHEME + "host.xy/abc.git/")
750 .getHumanishName();
751 assertEquals("abc", humanishName);
752 }
753
754 @Test
755 public void testGetValidWithSlashDotGitHumanishName()
756 throws IllegalArgumentException, URISyntaxException {
757 String humanishName = new URIish("/abc.git").getHumanishName();
758 assertEquals("abc", humanishName);
759 }
760
761 @Test
762 public void testGetValidWithSlashDotGitSlashHumanishName()
763 throws IllegalArgumentException, URISyntaxException {
764 String humanishName = new URIish("/abc.git/").getHumanishName();
765 assertEquals("abc", humanishName);
766 }
767
768 @Test
769 public void testGetValidWithSlashesDotGitHumanishName()
770 throws IllegalArgumentException, URISyntaxException {
771 String humanishName = new URIish("/a/b/c.git").getHumanishName();
772 assertEquals("c", humanishName);
773 }
774
775 @Test
776 public void testGetValidWithSlashesDotGitSlashHumanishName()
777 throws IllegalArgumentException, URISyntaxException {
778 String humanishName = new URIish("/a/b/c.git/").getHumanishName();
779 assertEquals("c", humanishName);
780 }
781
782 @Test
783 public void testGetValidLocalWithTwoSlashesHumanishName()
784 throws IllegalArgumentException, URISyntaxException {
785 String humanishName = new URIish("/a/b/c//").getHumanishName();
786 assertEquals("c", humanishName);
787 }
788
789 @Test
790 public void testGetValidGitSchemeWithTwoSlashesHumanishName()
791 throws IllegalArgumentException, URISyntaxException {
792 String humanishName = new URIish(GIT_SCHEME + "/a/b/c//")
793 .getHumanishName();
794 assertEquals("c", humanishName);
795 }
796
797 @Test
798 public void testGetWindowsPathHumanishName()
799 throws IllegalArgumentException,
800 URISyntaxException {
801 if (File.separatorChar == '\\') {
802 String humanishName = new URIish("file:///C\\a\\b\\c.git/")
803 .getHumanishName();
804 assertEquals("c", humanishName);
805 }
806 }
807
808 @Test
809 public void testUserPasswordAndPort() throws URISyntaxException {
810 String str = "http://user:secret@host.xy:80/some/path";
811 URIish u = new URIish(str);
812 assertEquals("http", u.getScheme());
813 assertTrue(u.isRemote());
814 assertEquals("/some/path", u.getRawPath());
815 assertEquals("/some/path", u.getPath());
816 assertEquals("host.xy", u.getHost());
817 assertEquals(80, u.getPort());
818 assertEquals("user", u.getUser());
819 assertEquals("secret", u.getPass());
820 assertEquals(u, new URIish(str));
821
822 str = "http://user:secret@pass@host.xy:80/some/path";
823 u = new URIish(str);
824 assertEquals("http", u.getScheme());
825 assertTrue(u.isRemote());
826 assertEquals("/some/path", u.getPath());
827 assertEquals("host.xy", u.getHost());
828 assertEquals(80, u.getPort());
829 assertEquals("user", u.getUser());
830 assertEquals("secret@pass", u.getPass());
831 assertEquals(u, new URIish(str));
832 }
833
834
835
836
837
838
839
840 @Test
841 public void testPathSeparator() throws URISyntaxException {
842 String str = "http://user:secret@host.xy:80/some%2Fpath";
843 URIish u = new URIish(str);
844 assertEquals("http", u.getScheme());
845 assertTrue(u.isRemote());
846 assertEquals("/some%2Fpath", u.getRawPath());
847 assertEquals("/some/path", u.getPath());
848 assertEquals("host.xy", u.getHost());
849 assertEquals(80, u.getPort());
850 assertEquals("user", u.getUser());
851 assertEquals("secret", u.getPass());
852 assertEquals(u, new URIish(str));
853 }
854
855 @Test
856 public void testFileProtocol() throws IllegalArgumentException,
857 URISyntaxException, IOException {
858
859 URIish u = new URIish("file:///a/b.txt");
860 assertEquals("file", u.getScheme());
861 assertFalse(u.isRemote());
862 assertNull(u.getHost());
863 assertNull(u.getPass());
864 assertEquals("/a/b.txt", u.getRawPath());
865 assertEquals("/a/b.txt", u.getPath());
866 assertEquals(-1, u.getPort());
867 assertNull(u.getUser());
868 assertEquals("b.txt", u.getHumanishName());
869
870 File tmp = File.createTempFile("jgitUnitTest", ".tmp");
871 u = new URIish(tmp.toURI().toString());
872 assertEquals("file", u.getScheme());
873 assertFalse(u.isRemote());
874 assertNull(u.getHost());
875 assertNull(u.getPass());
876 assertTrue(u.getPath().contains("jgitUnitTest"));
877 assertEquals(-1, u.getPort());
878 assertNull(u.getUser());
879 assertTrue(u.getHumanishName().startsWith("jgitUnitTest"));
880
881 u = new URIish("file:/a/b.txt");
882 assertEquals("file", u.getScheme());
883 assertFalse(u.isRemote());
884 assertNull(u.getHost());
885 assertNull(u.getPass());
886 assertEquals("/a/b.txt", u.getRawPath());
887 assertEquals("/a/b.txt", u.getPath());
888 assertEquals(-1, u.getPort());
889 assertNull(u.getUser());
890 assertEquals("b.txt", u.getHumanishName());
891 }
892
893 @Test
894 public void testMissingPort() throws URISyntaxException {
895 final String incorrectSshUrl = "ssh://some-host:/path/to/repository.git";
896 URIish u = new URIish(incorrectSshUrl);
897 assertFalse(TransportGitSsh.PROTO_SSH.canHandle(u));
898 }
899
900 @Test
901 public void testALot() throws URISyntaxException {
902
903
904 String[][] tests = {
905 new String[] { "%1$s://%2$s:%3$s@%4$s:%5$s/%6$s", "%1$s",
906 "%2$s", "%3$s", "%4$s", "%5$s", "%6$s" },
907 new String[] { "%1$s://%2$s@%4$s:%5$s/%6$s", "%1$s", "%2$s",
908 null, "%4$s", "%5$s", "%6$s" },
909 new String[] { "%1$s://%2$s@%4$s/%6$s", "%1$s", "%2$s", null,
910 "%4$s", null, "%6$s" },
911 new String[] { "%1$s://%4$s/%6$s", "%1$s", null, null, "%4$s",
912 null, "%6$s" }, };
913 String[] schemes = new String[] { "ssh", "ssh+git", "http", "https" };
914 String[] users = new String[] { "me", "l usr\\example.com",
915 "lusr\\example" };
916 String[] passes = new String[] { "wtf", };
917 String[] hosts = new String[] { "example.com", "1.2.3.4", "[::1]" };
918 String[] ports = new String[] { "1234", "80" };
919 String[] paths = new String[] { "/", "/abc", "D:/x", "D:\\x" };
920 for (String[] test : tests) {
921 String fmt = test[0];
922 for (String scheme : schemes) {
923 for (String user : users) {
924 for (String pass : passes) {
925 for (String host : hosts) {
926 for (String port : ports) {
927 for (String path : paths) {
928 String url = String.format(fmt, scheme,
929 user, pass, host, port, path);
930 String[] expect = new String[test.length];
931 for (int i = 1; i < expect.length; ++i)
932 if (test[i] != null)
933 expect[i] = String.format(test[i],
934 scheme, user, pass, host,
935 port, path);
936 URIish urIish = new URIish(url);
937 assertEquals(url, expect[1],
938 urIish.getScheme());
939 assertEquals(url, expect[2],
940 urIish.getUser());
941 }
942 }
943 }
944 }
945 }
946 }
947 }
948 }
949
950 @Test
951 public void testStringConstructor() throws Exception {
952 String str = "http://example.com/";
953 URIish u = new URIish(str);
954 assertEquals("example.com", u.getHost());
955 assertEquals("/", u.getPath());
956 assertEquals(str, u.toString());
957
958 str = "http://example.com";
959 u = new URIish(str);
960 assertEquals("example.com", u.getHost());
961 assertEquals("", u.getPath());
962 assertEquals(str, u.toString());
963 }
964 }