View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      /** {@inheritDoc} */
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          // store as file
60          Path privateKeyFile = tmpDirectory.newFile().toPath();
61          gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
62          providerRepository.setPrivateKey(privateKeyFile.toString());
63          providerRepository.setPassphrase(passphrase); // may be null
64      }
65  
66      /** {@inheritDoc} */
67      public void initRepo() throws Exception {
68          GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
69          gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
70  
71          // as checkout also already happens in setup() make sure to configure credentials here as well
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          // TODO: currently no easy way to pass passphrase in gitexe
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 }