2 * Copyright 2000-2012 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package jetbrains.buildServer.buildTriggers.vcs.git.tests;
19 import jetbrains.buildServer.BaseTestCase;
20 import jetbrains.buildServer.TempFiles;
21 import jetbrains.buildServer.buildTriggers.vcs.git.*;
22 import jetbrains.buildServer.serverSide.ServerPaths;
23 import jetbrains.buildServer.util.StringUtil;
24 import jetbrains.buildServer.vcs.VcsException;
25 import jetbrains.buildServer.vcs.VcsUrl;
26 import jetbrains.buildServer.vcs.impl.VcsRootImpl;
27 import org.eclipse.jgit.transport.URIish;
28 import org.testng.annotations.AfterMethod;
29 import org.testng.annotations.BeforeMethod;
30 import org.testng.annotations.Test;
32 import java.io.IOException;
33 import java.net.MalformedURLException;
34 import java.net.URISyntaxException;
35 import java.util.Arrays;
36 import java.util.List;
40 * @author dmitry.neverov
42 public class GitUrlSupportTest extends BaseTestCase {
44 private TempFiles myTempFiles = new TempFiles();
45 private GitUrlSupport mySupport;
46 private MirrorManager myMirrorManager;
49 public void setUp() throws IOException {
50 mySupport = new GitUrlSupport();
51 ServerPaths paths = new ServerPaths(myTempFiles.createTempDir().getAbsolutePath());
52 PluginConfig config = new PluginConfigBuilder(paths).build();
53 myMirrorManager = new MirrorManagerImpl(config, new HashCalculatorImpl());
57 public void tearDown() {
58 myTempFiles.cleanup();
62 public void test_convert() throws MalformedURLException, VcsException, URISyntaxException {
63 List<String> urls = Arrays.asList("scm:git:git://github.com/path_to_repository",
64 "scm:git:http://github.com/path_to_repository",
65 "scm:git:https://github.com/path_to_repository",
66 "scm:git:ssh://github.com/path_to_repository",
67 "scm:git:file://localhost/path_to_repository",
68 "scm:git:ssh://github.com/nd/regex.git/pom.xml");
70 for (String url : urls) {
71 VcsUrl vcsUrl = new VcsUrl(url);
72 GitVcsRoot root = toGitRoot(vcsUrl);
73 assertEquals(new URIish(vcsUrl.getProviderSpecificPart()), root.getRepositoryFetchURL());
74 checkAuthMethod(vcsUrl, root);
79 for (String url : urls) {
80 VcsUrl vcsUrl = new VcsUrl(url, user, pass);
81 GitVcsRoot s = toGitRoot(vcsUrl);
82 checkAuthMethod(vcsUrl, s);
88 public void should_throw_exception_when_url_incorrect() throws MalformedURLException, VcsException {
89 VcsUrl url = new VcsUrl("scm:svn:ssh://svn.repo.com/path_to_repository");
92 fail("Should fail here");
93 } catch (VcsException e) {
95 assertTrue(e.getMessage().contains("Unknown provider schema"));
101 public void convert_scp_like_syntax() throws Exception {
102 VcsUrl url = new VcsUrl("scm:git:git@github.com:user/repo.git");
103 GitVcsRoot root = toGitRoot(url);
104 assertEquals(new URIish(url.getProviderSpecificPart()), root.getRepositoryFetchURL());
105 assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT, root.getAuthSettings().getAuthMethod());
106 assertEquals("git", root.getAuthSettings().toMap().get(Constants.USERNAME));
111 public void convert_scp_like_syntax_with_credentials() throws Exception {
112 VcsUrl url = new VcsUrl("scm:git:git@github.com:user/repo.git", "user", "pass");
113 GitVcsRoot root = toGitRoot(url);
114 assertEquals(new URIish("user@github.com:user/repo.git"), root.getRepositoryFetchURL());
115 assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT, root.getAuthSettings().getAuthMethod());
116 assertEquals("user", root.getAuthSettings().toMap().get(Constants.USERNAME));
117 assertNull(root.getAuthSettings().toMap().get(Constants.PASSWORD));
121 private void checkAuthMethod(VcsUrl url, GitVcsRoot root) {
122 if (url.getProviderSpecificPart().startsWith("ssh")) {
123 assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT, root.getAuthSettings().getAuthMethod());
124 assertTrue(root.getAuthSettings().isIgnoreKnownHosts());
125 assertEquals(url.getUsername(), root.getAuthSettings().toMap().get(Constants.USERNAME));
126 assertNull(root.getAuthSettings().toMap().get(Constants.PASSWORD));
128 if (url.getUsername() != null && url.getPassword() != null &&
129 !StringUtil.isEmptyOrSpaces(url.getUsername()) &&
130 !StringUtil.isEmptyOrSpaces(url.getPassword())) {
131 assertEquals(AuthenticationMethod.PASSWORD, root.getAuthSettings().getAuthMethod());
132 assertEquals(url.getUsername(), root.getAuthSettings().toMap().get(Constants.USERNAME));
133 assertEquals(url.getPassword(), root.getAuthSettings().toMap().get(Constants.PASSWORD));
135 assertEquals(AuthenticationMethod.ANONYMOUS, root.getAuthSettings().getAuthMethod());
136 assertNull(root.getAuthSettings().toMap().get(Constants.USERNAME));
137 assertNull(root.getAuthSettings().toMap().get(Constants.PASSWORD));
142 private GitVcsRoot toGitRoot(VcsUrl url) throws VcsException {
143 Map<String, String> properties = mySupport.convertToVcsRootProperties(url.getUrl());
144 VcsRootImpl myRoot = new VcsRootImpl(1, properties);
145 return new GitVcsRoot(myMirrorManager, myRoot);