1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.git.command.branch;
20
21 import java.io.File;
22 import java.nio.file.Path;
23 import java.security.GeneralSecurityException;
24
25 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
26 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
27 import org.apache.maven.scm.provider.git.GitScmTestUtils;
28 import org.apache.maven.scm.provider.git.GitSshServer;
29 import org.apache.maven.scm.repository.ScmRepository;
30 import org.apache.maven.scm.tck.command.branch.BranchCommandTckTest;
31 import org.junit.Assume;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.TemporaryFolder;
35
36
37
38
39 public abstract class GitSshBranchCommandTckTest extends BranchCommandTckTest {
40 protected final GitSshServer gitSshServer;
41
42 @Rule
43 public TemporaryFolder tmpDirectory = new TemporaryFolder();
44
45 protected GitSshBranchCommandTckTest() throws GeneralSecurityException {
46 gitSshServer = new GitSshServer();
47 }
48
49 protected abstract String getScmProvider();
50
51
52 public String getScmUrl() throws Exception {
53 return "scm:" + getScmProvider() + ":ssh://localhost:" + gitSshServer.getPort() + "/repository";
54 }
55
56 public void configureCredentials(ScmRepository repository, String passphrase) throws Exception {
57 ScmProviderRepositoryWithHost providerRepository =
58 ScmProviderRepositoryWithHost.class.cast(repository.getProviderRepository());
59
60 Path privateKeyFile = tmpDirectory.newFile().toPath();
61 gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
62 providerRepository.setPrivateKey(privateKeyFile.toString());
63 providerRepository.setPassphrase(passphrase);
64 }
65
66
67 public void initRepo() throws Exception {
68 GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
69 gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
70
71
72 configureCredentials(getScmRepository(), null);
73 }
74
75 @Override
76 public void removeRepo() throws Exception {
77 gitSshServer.stop();
78 super.removeRepo();
79 }
80
81 @Override
82 protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
83 try {
84 return super.checkOut(workingDirectory, repository);
85 } finally {
86 GitScmTestUtils.setDefaultGitConfig(workingDirectory);
87 }
88 }
89
90 @Test
91 public void testBranchCommandTestWithPush() throws Exception {
92 configureCredentials(getScmRepository(), null);
93 getScmRepository().getProviderRepository().setPushChanges(true);
94 super.testBranchCommandTest();
95 }
96
97 @Test
98 public void testBranchCommandWithPassphraseAndPushTest() throws Exception {
99
100 Assume.assumeTrue(
101 "Ignore test with passphrase for provider " + getScmProvider(), "jgit".equals(getScmProvider()));
102 configureCredentials(getScmRepository(), "mySecret");
103 getScmRepository().getProviderRepository().setPushChanges(true);
104 super.testBranchCommandTest();
105 }
106 }