Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

URIishTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. * 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 java.net.URISyntaxException;
  47. import junit.framework.TestCase;
  48. public class URIishTest extends TestCase {
  49. private static final String GIT_SCHEME = "git://";
  50. public void testUnixFile() throws Exception {
  51. final String str = "/home/m y";
  52. URIish u = new URIish(str);
  53. assertNull(u.getScheme());
  54. assertFalse(u.isRemote());
  55. assertEquals(str, u.getPath());
  56. assertEquals(str, u.toString());
  57. assertEquals(u, new URIish(str));
  58. }
  59. public void testWindowsFile() throws Exception {
  60. final String str = "D:/m y";
  61. URIish u = new URIish(str);
  62. assertNull(u.getScheme());
  63. assertFalse(u.isRemote());
  64. assertEquals(str, u.getPath());
  65. assertEquals(str, u.toString());
  66. assertEquals(u, new URIish(str));
  67. }
  68. public void testWindowsFile2() throws Exception {
  69. final String str = "D:\\m y";
  70. URIish u = new URIish(str);
  71. assertNull(u.getScheme());
  72. assertFalse(u.isRemote());
  73. assertEquals("D:/m y", u.getPath());
  74. assertEquals("D:/m y", u.toString());
  75. assertEquals(u, new URIish(str));
  76. }
  77. public void testUNC() throws Exception {
  78. final String str = "\\\\some\\place";
  79. URIish u = new URIish(str);
  80. assertNull(u.getScheme());
  81. assertFalse(u.isRemote());
  82. assertEquals("//some/place", u.getPath());
  83. assertEquals("//some/place", u.toString());
  84. assertEquals(u, new URIish(str));
  85. }
  86. public void testFileProtoUnix() throws Exception {
  87. final String str = "file:///home/m y";
  88. URIish u = new URIish(str);
  89. assertEquals("file", u.getScheme());
  90. assertFalse(u.isRemote());
  91. assertEquals("/home/m y", u.getPath());
  92. assertEquals(str, u.toString());
  93. assertEquals(u, new URIish(str));
  94. }
  95. public void testFileProtoWindows() throws Exception {
  96. final String str = "file:///D:/m y";
  97. URIish u = new URIish(str);
  98. assertEquals("file", u.getScheme());
  99. assertFalse(u.isRemote());
  100. assertEquals("D:/m y", u.getPath());
  101. assertEquals(str, u.toString());
  102. assertEquals(u, new URIish(str));
  103. }
  104. public void testGitProtoUnix() throws Exception {
  105. final String str = "git://example.com/home/m y";
  106. URIish u = new URIish(str);
  107. assertEquals("git", u.getScheme());
  108. assertTrue(u.isRemote());
  109. assertEquals("example.com", u.getHost());
  110. assertEquals("/home/m y", u.getPath());
  111. assertEquals(str, u.toString());
  112. assertEquals(u, new URIish(str));
  113. }
  114. public void testGitProtoUnixPort() throws Exception {
  115. final String str = "git://example.com:333/home/m y";
  116. URIish u = new URIish(str);
  117. assertEquals("git", u.getScheme());
  118. assertTrue(u.isRemote());
  119. assertEquals("example.com", u.getHost());
  120. assertEquals("/home/m y", u.getPath());
  121. assertEquals(333, u.getPort());
  122. assertEquals(str, u.toString());
  123. assertEquals(u, new URIish(str));
  124. }
  125. public void testGitProtoWindowsPort() throws Exception {
  126. final String str = "git://example.com:338/D:/m y";
  127. URIish u = new URIish(str);
  128. assertEquals("git", u.getScheme());
  129. assertTrue(u.isRemote());
  130. assertEquals("D:/m y", u.getPath());
  131. assertEquals(338, u.getPort());
  132. assertEquals("example.com", u.getHost());
  133. assertEquals(str, u.toString());
  134. assertEquals(u, new URIish(str));
  135. }
  136. public void testGitProtoWindows() throws Exception {
  137. final String str = "git://example.com/D:/m y";
  138. URIish u = new URIish(str);
  139. assertEquals("git", u.getScheme());
  140. assertTrue(u.isRemote());
  141. assertEquals("D:/m y", u.getPath());
  142. assertEquals("example.com", u.getHost());
  143. assertEquals(-1, u.getPort());
  144. assertEquals(str, u.toString());
  145. assertEquals(u, new URIish(str));
  146. }
  147. public void testScpStyleWithoutUser() throws Exception {
  148. final String str = "example.com:some/p ath";
  149. URIish u = new URIish(str);
  150. assertNull(u.getScheme());
  151. assertTrue(u.isRemote());
  152. assertEquals("some/p ath", u.getPath());
  153. assertEquals("example.com", u.getHost());
  154. assertEquals(-1, u.getPort());
  155. assertEquals(str, u.toString());
  156. assertEquals(u, new URIish(str));
  157. }
  158. public void testScpStyleWithUser() throws Exception {
  159. final String str = "user@example.com:some/p ath";
  160. URIish u = new URIish(str);
  161. assertNull(u.getScheme());
  162. assertTrue(u.isRemote());
  163. assertEquals("some/p ath", u.getPath());
  164. assertEquals("user", u.getUser());
  165. assertEquals("example.com", u.getHost());
  166. assertEquals(-1, u.getPort());
  167. assertEquals(str, u.toString());
  168. assertEquals(u, new URIish(str));
  169. }
  170. public void testGitSshProto() throws Exception {
  171. final String str = "git+ssh://example.com/some/p ath";
  172. URIish u = new URIish(str);
  173. assertEquals("git+ssh", u.getScheme());
  174. assertTrue(u.isRemote());
  175. assertEquals("/some/p ath", u.getPath());
  176. assertEquals("example.com", u.getHost());
  177. assertEquals(-1, u.getPort());
  178. assertEquals(str, u.toString());
  179. assertEquals(u, new URIish(str));
  180. }
  181. public void testSshGitProto() throws Exception {
  182. final String str = "ssh+git://example.com/some/p ath";
  183. URIish u = new URIish(str);
  184. assertEquals("ssh+git", u.getScheme());
  185. assertTrue(u.isRemote());
  186. assertEquals("/some/p ath", u.getPath());
  187. assertEquals("example.com", u.getHost());
  188. assertEquals(-1, u.getPort());
  189. assertEquals(str, u.toString());
  190. assertEquals(u, new URIish(str));
  191. }
  192. public void testSshProto() throws Exception {
  193. final String str = "ssh://example.com/some/p ath";
  194. URIish u = new URIish(str);
  195. assertEquals("ssh", u.getScheme());
  196. assertTrue(u.isRemote());
  197. assertEquals("/some/p ath", u.getPath());
  198. assertEquals("example.com", u.getHost());
  199. assertEquals(-1, u.getPort());
  200. assertEquals(str, u.toString());
  201. assertEquals(u, new URIish(str));
  202. }
  203. public void testSshProtoWithUserAndPort() throws Exception {
  204. final String str = "ssh://user@example.com:33/some/p ath";
  205. URIish u = new URIish(str);
  206. assertEquals("ssh", u.getScheme());
  207. assertTrue(u.isRemote());
  208. assertEquals("/some/p ath", u.getPath());
  209. assertEquals("example.com", u.getHost());
  210. assertEquals("user", u.getUser());
  211. assertNull(u.getPass());
  212. assertEquals(33, u.getPort());
  213. assertEquals(str, u.toString());
  214. assertEquals(u, new URIish(str));
  215. }
  216. public void testSshProtoWithUserPassAndPort() throws Exception {
  217. final String str = "ssh://user:pass@example.com:33/some/p ath";
  218. URIish u = new URIish(str);
  219. assertEquals("ssh", u.getScheme());
  220. assertTrue(u.isRemote());
  221. assertEquals("/some/p ath", u.getPath());
  222. assertEquals("example.com", u.getHost());
  223. assertEquals("user", u.getUser());
  224. assertEquals("pass", u.getPass());
  225. assertEquals(33, u.getPort());
  226. assertEquals(str, u.toPrivateString());
  227. assertEquals(u.setPass(null).toPrivateString(), u.toString());
  228. assertEquals(u, new URIish(str));
  229. }
  230. public void testGitWithUserHome() throws Exception {
  231. final String str = "git://example.com/~some/p ath";
  232. URIish u = new URIish(str);
  233. assertEquals("git", u.getScheme());
  234. assertTrue(u.isRemote());
  235. assertEquals("~some/p ath", u.getPath());
  236. assertEquals("example.com", u.getHost());
  237. assertNull(u.getUser());
  238. assertNull(u.getPass());
  239. assertEquals(-1, u.getPort());
  240. assertEquals(str, u.toPrivateString());
  241. assertEquals(u.setPass(null).toPrivateString(), u.toString());
  242. assertEquals(u, new URIish(str));
  243. }
  244. /* Resolving ~user is beyond standard Java API and need more support
  245. public void testFileWithUserHome() throws Exception {
  246. final String str = "~some/p ath";
  247. URIish u = new URIish(str);
  248. assertEquals("git", u.getScheme());
  249. assertTrue(u.isRemote());
  250. assertEquals("~some/p ath", u.getPath());
  251. assertEquals("example.com", u.getHost());
  252. assertNull(u.getUser());
  253. assertNull(u.getPass());
  254. assertEquals(-1, u.getPort());
  255. assertEquals(str, u.toPrivateString());
  256. assertEquals(u.setPass(null).toPrivateString(), u.toString());
  257. assertEquals(u, new URIish(str));
  258. }
  259. */
  260. public void testFileWithNoneUserHomeWithTilde() throws Exception {
  261. final String str = "/~some/p ath";
  262. URIish u = new URIish(str);
  263. assertNull(u.getScheme());
  264. assertFalse(u.isRemote());
  265. assertEquals("/~some/p ath", u.getPath());
  266. assertNull(u.getHost());
  267. assertNull(u.getUser());
  268. assertNull(u.getPass());
  269. assertEquals(-1, u.getPort());
  270. assertEquals(str, u.toPrivateString());
  271. assertEquals(u.setPass(null).toPrivateString(), u.toString());
  272. assertEquals(u, new URIish(str));
  273. }
  274. public void testGetNullHumanishName() {
  275. try {
  276. new URIish().getHumanishName();
  277. fail("path must be not null");
  278. } catch (IllegalArgumentException e) {
  279. // expected
  280. }
  281. }
  282. public void testGetEmptyHumanishName() throws URISyntaxException {
  283. try {
  284. new URIish(GIT_SCHEME).getHumanishName();
  285. fail("empty path is useless");
  286. } catch (IllegalArgumentException e) {
  287. // expected
  288. }
  289. }
  290. public void testGetAbsEmptyHumanishName() {
  291. try {
  292. new URIish().getHumanishName();
  293. fail("empty path is useless");
  294. } catch (IllegalArgumentException e) {
  295. // expected
  296. }
  297. }
  298. public void testGetValidWithEmptySlashDotGitHumanishName()
  299. throws IllegalArgumentException, URISyntaxException {
  300. String humanishName = new URIish("/a/b/.git").getHumanishName();
  301. assertEquals("b", humanishName);
  302. }
  303. public void testGetWithSlashDotGitHumanishName() throws URISyntaxException {
  304. assertEquals("", new URIish("/.git").getHumanishName());
  305. }
  306. public void testGetTwoSlashesDotGitHumanishName() throws URISyntaxException {
  307. assertEquals("", new URIish("/.git").getHumanishName());
  308. }
  309. public void testGetValidHumanishName() throws IllegalArgumentException,
  310. URISyntaxException {
  311. String humanishName = new URIish(GIT_SCHEME + "abc").getHumanishName();
  312. assertEquals("abc", humanishName);
  313. }
  314. public void testGetValidSlashHumanishName()
  315. throws IllegalArgumentException, URISyntaxException {
  316. String humanishName = new URIish(GIT_SCHEME + "abc/").getHumanishName();
  317. assertEquals("abc", humanishName);
  318. }
  319. public void testGetSlashValidSlashHumanishName()
  320. throws IllegalArgumentException, URISyntaxException {
  321. String humanishName = new URIish("/abc/").getHumanishName();
  322. assertEquals("abc", humanishName);
  323. }
  324. public void testGetSlashValidSlashDotGitSlashHumanishName()
  325. throws IllegalArgumentException, URISyntaxException {
  326. String humanishName = new URIish("/abc/.git").getHumanishName();
  327. assertEquals("abc", humanishName);
  328. }
  329. public void testGetSlashSlashDotGitSlashHumanishName()
  330. throws IllegalArgumentException, URISyntaxException {
  331. final String humanishName = new URIish(GIT_SCHEME + "/abc//.git")
  332. .getHumanishName();
  333. assertEquals("may return an empty humanish name", "", humanishName);
  334. }
  335. public void testGetSlashesValidSlashHumanishName()
  336. throws IllegalArgumentException, URISyntaxException {
  337. String humanishName = new URIish("/a/b/c/").getHumanishName();
  338. assertEquals("c", humanishName);
  339. }
  340. public void testGetValidDotGitHumanishName()
  341. throws IllegalArgumentException, URISyntaxException {
  342. String humanishName = new URIish(GIT_SCHEME + "abc.git")
  343. .getHumanishName();
  344. assertEquals("abc", humanishName);
  345. }
  346. public void testGetValidDotGitSlashHumanishName()
  347. throws IllegalArgumentException, URISyntaxException {
  348. String humanishName = new URIish(GIT_SCHEME + "abc.git/")
  349. .getHumanishName();
  350. assertEquals("abc", humanishName);
  351. }
  352. public void testGetValidWithSlashDotGitHumanishName()
  353. throws IllegalArgumentException, URISyntaxException {
  354. String humanishName = new URIish("/abc.git").getHumanishName();
  355. assertEquals("abc", humanishName);
  356. }
  357. public void testGetValidWithSlashDotGitSlashHumanishName()
  358. throws IllegalArgumentException, URISyntaxException {
  359. String humanishName = new URIish("/abc.git/").getHumanishName();
  360. assertEquals("abc", humanishName);
  361. }
  362. public void testGetValidWithSlashesDotGitHumanishName()
  363. throws IllegalArgumentException, URISyntaxException {
  364. String humanishName = new URIish("/a/b/c.git").getHumanishName();
  365. assertEquals("c", humanishName);
  366. }
  367. public void testGetValidWithSlashesDotGitSlashHumanishName()
  368. throws IllegalArgumentException, URISyntaxException {
  369. String humanishName = new URIish("/a/b/c.git/").getHumanishName();
  370. assertEquals("c", humanishName);
  371. }
  372. }