diff options
author | Sam Davis | 2015-07-13 18:50:39 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2015-08-07 21:34:09 +0000 |
commit | 6ae37a9fe619a4fd3138f97c3c7585d5b1abe83f (patch) | |
tree | 4fce3e35b961d788b4199169f81a7de93f476609 | |
parent | e484223fba61856d3a3b8be363a02f09fe3e1a1b (diff) | |
download | org.eclipse.mylyn.reviews-6ae37a9fe619a4fd3138f97c3c7585d5b1abe83f.tar.gz org.eclipse.mylyn.reviews-6ae37a9fe619a4fd3138f97c3c7585d5b1abe83f.tar.xz org.eclipse.mylyn.reviews-6ae37a9fe619a4fd3138f97c3c7585d5b1abe83f.zip |
469216: Gerrit connector tests cannot connect to outside servers
Change-Id: I0fdf1d0b4ebf6d6827725446ff3265715fe8099f
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=469216
-rw-r--r-- | org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/support/GerritProject.java | 63 |
1 files changed, 52 insertions, 11 deletions
diff --git a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/support/GerritProject.java b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/support/GerritProject.java index 78b46ee57..35daa505a 100644 --- a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/support/GerritProject.java +++ b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/support/GerritProject.java @@ -11,8 +11,10 @@ package org.eclipse.mylyn.gerrit.tests.support; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.net.Authenticator; import java.net.PasswordAuthentication; @@ -35,6 +37,8 @@ import org.junit.Test; */ public class GerritProject { + private static final String PROP_ALTERNATE_PUSH = "org.eclipse.mylyn.gerrit.tests.alternate.push"; + public static class GerritProjectTest { @Test @@ -142,23 +146,60 @@ public class GerritProject { public CommitResult commitAndPush(CommitCommand command, String refSpec, PrivilegeLevel privilegeLevel) throws Exception { - String email = registerAuthenticator(privilegeLevel); + AuthenticationCredentials credentials = registerAuthenticator(privilegeLevel); + String email = credentials.getUserName(); RevCommit call = command.setAuthor("Test", email) //$NON-NLS-1$ .setCommitter("Test", email) .call(); - Iterable<PushResult> result = git.push() - .setCredentialsProvider(getCredentialsProvider(privilegeLevel)) - .setRefSpecs(new RefSpec(refSpec)) - .call(); - //Safe to assume one and only one result? - return new CommitResult(call, result.iterator().next()); + if (Boolean.getBoolean(PROP_ALTERNATE_PUSH)) { + String username = StringUtils.substringBefore(email, "@"); + String protocol = StringUtils.substringBefore(fixture.getRepositoryUrl(), "://"); + String hostAndPath = StringUtils.substringAfter(fixture.getRepositoryUrl(), "://"); + String project = "org.eclipse.mylyn.test/"; + String url = protocol + "://" + username + ":" + credentials.getPassword() + "@" + hostAndPath + project; + File directory = command.getRepository().getDirectory().getParentFile(); + final String responseMessage = execute(directory, "git", "push", url, refSpec); + System.out.println("Response message:"); + System.out.println(responseMessage); + System.out.println("######"); + PushResult result = new PushResult() { + @Override + public String getMessages() { + return responseMessage.toString(); + } + }; + return new CommitResult(call, result); + } else { + Iterable<PushResult> result = git.push() + .setCredentialsProvider(getCredentialsProvider(privilegeLevel)) + .setRefSpecs(new RefSpec(refSpec)) + .call(); + //Safe to assume one and only one result? + return new CommitResult(call, result.iterator().next()); + } } - public String registerAuthenticator() throws Exception { - return registerAuthenticator(PrivilegeLevel.USER); + private String execute(File directory, String... command) throws IOException { +// System.out.println("# Executing " + StringUtils.join(command, " ")); + Process process = Runtime.getRuntime().exec(command, null, directory); + BufferedReader r = new BufferedReader(new InputStreamReader(process.getErrorStream())); + final StringBuilder responseMessage = new StringBuilder(); + try { + String line; + while ((line = r.readLine()) != null) { + if (!line.startsWith("remote:")) { + line = line.replace('/', '\\');// don't break parsing of short id + } + responseMessage.append(line); + responseMessage.append('\n'); + } + } finally { + r.close(); + } + return responseMessage.toString(); } - public String registerAuthenticator(PrivilegeLevel privilegeLevel) throws Exception { + public AuthenticationCredentials registerAuthenticator(PrivilegeLevel privilegeLevel) throws Exception { // register authenticator to avoid HTTP password prompt AuthenticationCredentials credentials = fixture.location(privilegeLevel).getCredentials( AuthenticationType.REPOSITORY); @@ -170,7 +211,7 @@ public class GerritProject { return authentication; } }); - return credentials.getUserName(); + return credentials; } public void dispose() { |