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 public abstract class GitSshBranchCommandTckTest extends BranchCommandTckTest {
37 protected final GitSshServer gitSshServer;
38
39 @Rule
40 public TemporaryFolder tmpDirectory = new TemporaryFolder();
41
42 protected GitSshBranchCommandTckTest() throws GeneralSecurityException {
43 gitSshServer = new GitSshServer();
44 }
45
46 protected abstract String getScmProvider();
47
48
49
50
51 public String getScmUrl() throws Exception {
52 return "scm:" + getScmProvider() + ":ssh://localhost:" + gitSshServer.getPort() + "/repository";
53 }
54
55 public void configureCredentials(ScmRepository repository, String passphrase) throws Exception {
56 ScmProviderRepositoryWithHost providerRepository =
57 ScmProviderRepositoryWithHost.class.cast(repository.getProviderRepository());
58
59 Path privateKeyFile = tmpDirectory.newFile().toPath();
60 gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
61 providerRepository.setPrivateKey(privateKeyFile.toString());
62 providerRepository.setPassphrase(passphrase);
63 }
64
65
66
67
68 public void initRepo() throws Exception {
69 GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
70 gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
71
72
73 configureCredentials(getScmRepository(), null);
74 }
75
76 @Override
77 public void removeRepo() throws Exception {
78 gitSshServer.stop();
79 super.removeRepo();
80 }
81
82 @Override
83 protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository repository) throws Exception {
84 try {
85 return super.checkOut(workingDirectory, repository);
86 } finally {
87 GitScmTestUtils.setDefaultGitConfig(workingDirectory);
88 }
89 }
90
91 @Test
92 public void testBranchCommandTestWithPush() throws Exception {
93 configureCredentials(getScmRepository(), null);
94 getScmRepository().getProviderRepository().setPushChanges(true);
95 super.testBranchCommandTest();
96 }
97
98 @Test
99 public void testBranchCommandWithPassphraseAndPushTest() throws Exception {
100
101 Assume.assumeTrue(
102 "Ignore test with passphrase for provider " + getScmProvider(), "jgit".equals(getScmProvider()));
103 configureCredentials(getScmRepository(), "mySecret");
104 getScmRepository().getProviderRepository().setPushChanges(true);
105 super.testBranchCommandTest();
106 }
107 }