PageRenderTime 30ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/github/test/org/jetbrains/plugins/github/GithubUrlUtilTest.java

http://github.com/JetBrains/intellij-community
Java | 315 lines | 235 code | 62 blank | 18 comment | 1 complexity | aefa4f428404b98c909900f0cc0dba9c MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. /*
  2. * Copyright 2000-2013 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jetbrains.plugins.github;
  17. import com.intellij.openapi.util.Pair;
  18. import com.intellij.testFramework.UsefulTestCase;
  19. import com.intellij.util.containers.Convertor;
  20. import org.jetbrains.annotations.NotNull;
  21. import org.jetbrains.annotations.Nullable;
  22. import org.jetbrains.plugins.github.api.GithubFullPath;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import static org.jetbrains.plugins.github.util.GithubUrlUtil.*;
  26. /**
  27. * @author Aleksey Pivovarov
  28. */
  29. public class GithubUrlUtilTest extends UsefulTestCase {
  30. private static class TestCase<T> {
  31. @NotNull final public List<Pair<String, T>> tests = new ArrayList<>();
  32. public void add(@NotNull String in, @Nullable T out) {
  33. tests.add(Pair.create(in, out));
  34. }
  35. }
  36. private static <T> void runTestCase(@NotNull TestCase<T> tests, @NotNull Convertor<String, T> func) {
  37. for (Pair<String, T> test : tests.tests) {
  38. assertEquals(test.getFirst(), test.getSecond(), func.convert(test.getFirst()));
  39. }
  40. }
  41. public void testRemoveTrailingSlash() throws Throwable {
  42. TestCase<String> tests = new TestCase<>();
  43. tests.add("http://github.com/", "http://github.com");
  44. tests.add("http://github.com", "http://github.com");
  45. tests.add("http://github.com/user/repo/", "http://github.com/user/repo");
  46. tests.add("http://github.com/user/repo", "http://github.com/user/repo");
  47. runTestCase(tests, new Convertor<String, String>() {
  48. @Override
  49. public String convert(String in) {
  50. return removeTrailingSlash(in);
  51. }
  52. });
  53. }
  54. public void testRemoveProtocolPrefix() throws Throwable {
  55. TestCase<String> tests = new TestCase<>();
  56. tests.add("github.com/user/repo/", "github.com/user/repo/");
  57. tests.add("api.github.com/user/repo/", "api.github.com/user/repo/");
  58. tests.add("http://github.com/user/repo/", "github.com/user/repo/");
  59. tests.add("https://github.com/user/repo/", "github.com/user/repo/");
  60. tests.add("git://github.com/user/repo/", "github.com/user/repo/");
  61. tests.add("git@github.com:user/repo/", "github.com/user/repo/");
  62. tests.add("git@github.com:username/repo/", "github.com/username/repo/");
  63. tests.add("https://username:password@github.com/user/repo/", "github.com/user/repo/");
  64. tests.add("https://username@github.com/user/repo/", "github.com/user/repo/");
  65. tests.add("https://github.com:2233/user/repo/", "github.com:2233/user/repo/");
  66. tests.add("HTTP://GITHUB.com/user/repo/", "GITHUB.com/user/repo/");
  67. tests.add("HttP://GitHub.com/user/repo/", "GitHub.com/user/repo/");
  68. runTestCase(tests, new Convertor<String, String>() {
  69. @Override
  70. public String convert(String in) {
  71. return removeProtocolPrefix(in);
  72. }
  73. });
  74. }
  75. public void testIsGithubUrl1() throws Throwable {
  76. TestCase<Boolean> tests = new TestCase<>();
  77. tests.add("http://github.com/user/repo", true);
  78. tests.add("https://github.com/user/repo", true);
  79. tests.add("git://github.com/user/repo", true);
  80. tests.add("git@github.com:user/repo", true);
  81. tests.add("https://github.com/", true);
  82. tests.add("github.com", true);
  83. tests.add("https://user@github.com/user/repo", true);
  84. tests.add("https://user:password@github.com/user/repo", true);
  85. tests.add("git@github.com:user/repo", true);
  86. tests.add("https://github.com:2233/", true);
  87. tests.add("HTTPS://GitHub.com:2233/", true);
  88. tests.add("google.com", false);
  89. tests.add("github.com.site.ua", false);
  90. tests.add("sf@hskfh../.#fwenj 32#$", false);
  91. tests.add("api.github.com", false);
  92. tests.add("site.com//github.com", false);
  93. runTestCase(tests, new Convertor<String, Boolean>() {
  94. @Override
  95. public Boolean convert(String in) {
  96. return isGithubUrl(in, "https://github.com/");
  97. }
  98. });
  99. runTestCase(tests, new Convertor<String, Boolean>() {
  100. @Override
  101. public Boolean convert(String in) {
  102. return isGithubUrl(in, "http://GitHub.com");
  103. }
  104. });
  105. }
  106. public void testIsGithubUrl2() throws Throwable {
  107. TestCase<Boolean> tests = new TestCase<>();
  108. tests.add("http://git.code.example.co.jp/user/repo", true);
  109. tests.add("https://git.code.example.co.jp/user/repo", true);
  110. tests.add("git://git.code.example.co.jp/user/repo", true);
  111. tests.add("git@git.code.example.co.jp:user/repo", true);
  112. tests.add("http://git.code.example.co/user/repo", false);
  113. tests.add("http://code.example.co.jp/user/repo", false);
  114. runTestCase(tests, new Convertor<String, Boolean>() {
  115. @Override
  116. public Boolean convert(String in) {
  117. return isGithubUrl(in, "git.code.example.co.jp");
  118. }
  119. });
  120. runTestCase(tests, new Convertor<String, Boolean>() {
  121. @Override
  122. public Boolean convert(String in) {
  123. return isGithubUrl(in, "http://git.code.example.co.jp");
  124. }
  125. });
  126. runTestCase(tests, new Convertor<String, Boolean>() {
  127. @Override
  128. public Boolean convert(String in) {
  129. return isGithubUrl(in, "https://git.code.example.co.jp/github/server");
  130. }
  131. });
  132. runTestCase(tests, new Convertor<String, Boolean>() {
  133. @Override
  134. public Boolean convert(String in) {
  135. return isGithubUrl(in, "git.code.example.co.jp/api");
  136. }
  137. });
  138. }
  139. public void testGetApiUrlWithoutProtocol() throws Throwable {
  140. TestCase<String> tests = new TestCase<>();
  141. tests.add("github.com", "api.github.com");
  142. tests.add("https://github.com/", "api.github.com");
  143. tests.add("api.github.com/", "api.github.com");
  144. tests.add("http://my.site.com/", "my.site.com/api/v3");
  145. tests.add("http://api.site.com/", "api.site.com/api/v3");
  146. tests.add("http://url.github.com/", "url.github.com/api/v3");
  147. tests.add("HTTP://GITHUB.com", "api.github.com");
  148. tests.add("HttP://GitHub.com/", "api.github.com");
  149. runTestCase(tests, new Convertor<String, String>() {
  150. @Override
  151. public String convert(String in) {
  152. return getApiUrlWithoutProtocol(in);
  153. }
  154. });
  155. }
  156. public void testGetUserAndRepositoryFromRemoteUrl() throws Throwable {
  157. TestCase<GithubFullPath> tests = new TestCase<>();
  158. tests.add("http://github.com/username/reponame/", new GithubFullPath("username", "reponame"));
  159. tests.add("https://github.com/username/reponame/", new GithubFullPath("username", "reponame"));
  160. tests.add("git://github.com/username/reponame/", new GithubFullPath("username", "reponame"));
  161. tests.add("git@github.com:username/reponame/", new GithubFullPath("username", "reponame"));
  162. tests.add("https://github.com/username/reponame", new GithubFullPath("username", "reponame"));
  163. tests.add("https://github.com/username/reponame.git", new GithubFullPath("username", "reponame"));
  164. tests.add("https://github.com/username/reponame.git/", new GithubFullPath("username", "reponame"));
  165. tests.add("git@github.com:username/reponame.git/", new GithubFullPath("username", "reponame"));
  166. tests.add("http://login:passsword@github.com/username/reponame/", new GithubFullPath("username", "reponame"));
  167. tests.add("HTTPS://GitHub.com/username/reponame/", new GithubFullPath("username", "reponame"));
  168. tests.add("https://github.com/UserName/RepoName/", new GithubFullPath("UserName", "RepoName"));
  169. tests.add("https://github.com/RepoName/", null);
  170. tests.add("git@github.com:user/", null);
  171. tests.add("https://user:pass@github.com/", null);
  172. runTestCase(tests, new Convertor<String, GithubFullPath>() {
  173. @Override
  174. @Nullable
  175. public GithubFullPath convert(String in) {
  176. return getUserAndRepositoryFromRemoteUrl(in);
  177. }
  178. });
  179. }
  180. public void testMakeGithubRepoFromRemoteUrl() throws Throwable {
  181. TestCase<String> tests = new TestCase<>();
  182. tests.add("http://github.com/username/reponame/", "https://github.com/username/reponame");
  183. tests.add("https://github.com/username/reponame/", "https://github.com/username/reponame");
  184. tests.add("git://github.com/username/reponame/", "https://github.com/username/reponame");
  185. tests.add("git@github.com:username/reponame/", "https://github.com/username/reponame");
  186. tests.add("https://github.com/username/reponame", "https://github.com/username/reponame");
  187. tests.add("https://github.com/username/reponame.git", "https://github.com/username/reponame");
  188. tests.add("https://github.com/username/reponame.git/", "https://github.com/username/reponame");
  189. tests.add("git@github.com:username/reponame.git/", "https://github.com/username/reponame");
  190. tests.add("git@github.com:username/reponame/", "https://github.com/username/reponame");
  191. tests.add("http://login:passsword@github.com/username/reponame/", "https://github.com/username/reponame");
  192. tests.add("HTTPS://GitHub.com/username/reponame/", "https://github.com/username/reponame");
  193. tests.add("https://github.com/UserName/RepoName/", "https://github.com/UserName/RepoName");
  194. tests.add("https://github.com/RepoName/", null);
  195. tests.add("git@github.com:user/", null);
  196. tests.add("https://user:pass@github.com/", null);
  197. runTestCase(tests, new Convertor<String, String>() {
  198. @Override
  199. @Nullable
  200. public String convert(String in) {
  201. return makeGithubRepoUrlFromRemoteUrl(in, "https://github.com");
  202. }
  203. });
  204. }
  205. public void testGetHostFromUrl() throws Throwable {
  206. TestCase<String> tests = new TestCase<>();
  207. tests.add("github.com", "github.com");
  208. tests.add("api.github.com", "api.github.com");
  209. tests.add("github.com/", "github.com");
  210. tests.add("api.github.com/", "api.github.com");
  211. tests.add("github.com/user/repo/", "github.com");
  212. tests.add("api.github.com/user/repo/", "api.github.com");
  213. tests.add("http://github.com/user/repo/", "github.com");
  214. tests.add("https://github.com/user/repo/", "github.com");
  215. tests.add("git://github.com/user/repo/", "github.com");
  216. tests.add("git@github.com:user/repo/", "github.com");
  217. tests.add("git@github.com:username/repo/", "github.com");
  218. tests.add("https://username:password@github.com/user/repo/", "github.com");
  219. tests.add("https://username@github.com/user/repo/", "github.com");
  220. tests.add("https://github.com:2233/user/repo/", "github.com");
  221. tests.add("HTTP://GITHUB.com/user/repo/", "GITHUB.com");
  222. tests.add("HttP://GitHub.com/user/repo/", "GitHub.com");
  223. runTestCase(tests, new Convertor<String, String>() {
  224. @Override
  225. public String convert(String in) {
  226. return getHostFromUrl(in);
  227. }
  228. });
  229. }
  230. public void testGetApiUrl() throws Throwable {
  231. TestCase<String> tests = new TestCase<>();
  232. tests.add("github.com", "https://api.github.com");
  233. tests.add("https://github.com/", "https://api.github.com");
  234. tests.add("api.github.com/", "https://api.github.com");
  235. tests.add("https://my.site.com/", "https://my.site.com/api/v3");
  236. tests.add("https://api.site.com/", "https://api.site.com/api/v3");
  237. tests.add("https://url.github.com/", "https://url.github.com/api/v3");
  238. tests.add("my.site.com/", "https://my.site.com/api/v3");
  239. tests.add("api.site.com/", "https://api.site.com/api/v3");
  240. tests.add("url.github.com/", "https://url.github.com/api/v3");
  241. tests.add("http://my.site.com/", "http://my.site.com/api/v3");
  242. tests.add("http://api.site.com/", "http://api.site.com/api/v3");
  243. tests.add("http://url.github.com/", "http://url.github.com/api/v3");
  244. tests.add("HTTP://GITHUB.com", "http://api.github.com");
  245. tests.add("HttP://GitHub.com/", "http://api.github.com");
  246. runTestCase(tests, new Convertor<String, String>() {
  247. @Override
  248. public String convert(String in) {
  249. return getApiUrl(in);
  250. }
  251. });
  252. }
  253. }