Skip to content

Commit a7e1f85

Browse files
author
Denis
committed
tests amend
1 parent a64e22c commit a7e1f85

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/main/java/com/projectkaiser/scm/vcs/GitVCS.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.eclipse.jgit.api.MergeResult;
2121
import org.eclipse.jgit.api.ResetCommand.ResetType;
2222
import org.eclipse.jgit.api.errors.GitAPIException;
23+
import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
2324
import org.eclipse.jgit.lib.Repository;
2425
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
2526
import org.eclipse.jgit.transport.CredentialsProvider;
@@ -30,6 +31,7 @@
3031
import com.projectkaiser.scm.vcs.api.IVCS;
3132
import com.projectkaiser.scm.vcs.api.PKVCSMergeResult;
3233
import com.projectkaiser.scm.vcs.api.VCSWorkspace;
34+
import com.projectkaiser.scm.vcs.api.exceptions.EVCSBranchExists;
3335
import com.projectkaiser.scm.vcs.api.exceptions.EVCSException;
3436

3537
public class GitVCS extends AbstractVCS implements IVCS {
@@ -75,6 +77,8 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
7577
} finally {
7678
workspace.unlock();
7779
}
80+
} catch (RefAlreadyExistsException e) {
81+
throw new EVCSBranchExists (e);
7882
} catch (GitAPIException e) {
7983
throw new EVCSException(e);
8084
} catch (Exception e) {

src/test/java/com/projectkaiser/scm/vcs/GitVCSTest.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,30 @@
2525

2626
import com.projectkaiser.scm.vcs.api.IVCS;
2727
import com.projectkaiser.scm.vcs.api.VCSWorkspace;
28+
import com.projectkaiser.scm.vcs.api.exceptions.EVCSBranchExists;
2829

2930
public class GitVCSTest {
3031

3132
private static final String FILE1_ADDED_COMMIT_MESSAGE = "file1 added";
3233
private static final String FILE2_ADDED_COMMIT_MESSAGE = "file2 added";
3334
private static final String WORKSPACE_DIR = System.getProperty("java.io.tmpdir") + "pk-vcs-workspaces";
3435
private static final String REPO_NAME = "pk-vcs-git-testrepo";
35-
private static final String GITHUB_USER = System.getenv("PK_VCS_TEST_GITHUB_USER");
36-
private static final String GITHUB_PASS = System.getenv("PK_VCS_TEST_GITHUB_PASS");
36+
private static final String GITHUB_USER = System.getProperty("PK_VCS_TEST_GITHUB_USER") == null ?
37+
System.getenv("PK_VCS_TEST_GITHUB_USER") : System.getProperty("PK_VCS_TEST_GITHUB_USER");
38+
private static final String GITHUB_PASS = System.getProperty("PK_VCS_TEST_GITHUB_PASS") == null ?
39+
System.getenv("PK_VCS_TEST_GITHUB_PASS") : System.getProperty("PK_VCS_TEST_GITHUB_PASS");
3740
private static final String NEW_BRANCH = "new-branch";
3841
private static final String SRC_BRANCH = "master";
3942
private static final String INITIAL_COMMIT_MESSAGE = "Initial commit";
4043
private static final String CREATED_DST_BRANCH_COMMIT_MESSAGE = "created dst branch";
4144
private static final String CONTENT_CHANGED_COMMIT_MESSAGE = "changed file content";
4245
private static final String MERGE_COMMIT_MESSAGE = "merged.";
4346
private static final String DELETE_BRANCH_COMMIT_MESSAGE = "deleted";
44-
private static final String PROXY_HOST = "localhost";
45-
private static final Integer PROXY_PORT = 3128;
47+
private static final String PROXY_HOST = getJvmProperty("https.proxyHost");
48+
private static final Integer PROXY_PORT = getJvmProperty("https.proxyPort") == null ? null :
49+
Integer.parseInt(getJvmProperty("https.proxyPort"));
50+
private static final String PROXY_USER = getJvmProperty("https.proxyUser");
51+
private static final String PROXY_PASS = getJvmProperty("https.proxyPassword");
4652
private static final String LINE_1 = "line 1";
4753
private static final String LINE_2 = "line 2";
4854

@@ -64,12 +70,37 @@ public static void setUpClass() {
6470
GITHUB_PASS != null);
6571
}
6672

73+
private static String getJvmProperty(String name) {
74+
if (name == null) {
75+
return null;
76+
}
77+
78+
String res = System.getProperty(name);
79+
if (res != null) {
80+
return res;
81+
}
82+
83+
res = System.getenv("JAVA_OPTS");
84+
if (res == null) {
85+
return null;
86+
}
87+
88+
Integer st = res.indexOf(name);
89+
if (st < 0) {
90+
return null;
91+
}
92+
93+
res = res.substring(st + name.length() + 1, res.indexOf(" -D", st + name.length() + 1) < 0 ? name.length() :
94+
res.indexOf(" -D", st + name.length())).trim();
95+
return res;
96+
}
97+
6798
@Before
6899
public void setUp() throws IOException {
69100
github = GitHub.connectUsingPassword(GITHUB_USER, GITHUB_PASS);
70101
String uuid = UUID.randomUUID().toString();
71102
repoName = (REPO_NAME + "_" + uuid);
72-
103+
73104
repo = github.createRepository(repoName)
74105
.issues(false)
75106
.wiki(false)
@@ -79,7 +110,9 @@ public void setUp() throws IOException {
79110
gitVCS = new GitVCS(null, WORKSPACE_DIR, gitUrl + repoName + ".git");
80111
vcs = gitVCS;
81112
vcs.setCredentials(GITHUB_USER, GITHUB_PASS);
82-
vcs.setProxy(PROXY_HOST, PROXY_PORT, "", "");
113+
if (PROXY_HOST != null) {
114+
vcs.setProxy(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS);
115+
}
83116
}
84117

85118
@After
@@ -98,6 +131,12 @@ public void testGitCreateAndDeleteBranch() throws InterruptedException, IOExcept
98131
assertTrue(repo.getBranches().containsKey(NEW_BRANCH));
99132
assertTrue(repo.getBranches().size() == 2); // master & NEW_BRANCH
100133

134+
try {
135+
vcs.createBranch(SRC_BRANCH, NEW_BRANCH, CREATED_DST_BRANCH_COMMIT_MESSAGE);
136+
fail("\"Branch exists\" situation not detected");
137+
} catch (EVCSBranchExists e) {
138+
}
139+
101140
vcs.deleteBranch(NEW_BRANCH, DELETE_BRANCH_COMMIT_MESSAGE);
102141
Thread.sleep(2000); // next operation fails from time to time. Looks like github has some latency on branch operations
103142
assertTrue (repo.getBranches().size() == 1);

0 commit comments

Comments
 (0)