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  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       * {@inheritDoc}
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          // store as file
59          Path privateKeyFile = tmpDirectory.newFile().toPath();
60          gitSshServer.writePrivateKeyAsPkcs8(privateKeyFile, passphrase);
61          providerRepository.setPrivateKey(privateKeyFile.toString());
62          providerRepository.setPassphrase(passphrase); // may be null
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      public void initRepo() throws Exception {
69          GitScmTestUtils.initRepo("src/test/resources/repository/", getRepositoryRoot(), getWorkingDirectory());
70          gitSshServer.start(getRepositoryRoot().getParentFile().toPath());
71  
72          // as checkout also already happens in setup() make sure to configure credentials here as well
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         // TODO: currently no easy way to pass passphrase in gitexe
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 }