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;
20  
21  import java.io.File;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  
25  import org.apache.maven.scm.PlexusJUnit4TestSupport;
26  import org.codehaus.plexus.util.FileUtils;
27  import org.codehaus.plexus.util.cli.CommandLineException;
28  import org.junit.Assert;
29  
30  /**
31   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
32   *
33   */
34  public final class GitScmTestUtils {
35      /** 'git' command line */
36      public static final String GIT_COMMAND_LINE = "git";
37  
38      private GitScmTestUtils() {}
39  
40      public static void initRepo(File repository, File workingDirectory, File assertionDirectory) throws IOException {
41          initRepo("src/test/repository/", repository, workingDirectory);
42  
43          FileUtils.deleteDirectory(assertionDirectory);
44  
45          Assert.assertTrue(assertionDirectory.mkdirs());
46      }
47  
48      public static void initRepo(String source, File repository, File workingDirectory) throws IOException {
49          // Copy the repository to target
50          File src = PlexusJUnit4TestSupport.getTestFile(source);
51  
52          FileUtils.deleteDirectory(repository);
53  
54          Assert.assertTrue(repository.mkdirs());
55  
56          FileUtils.copyDirectoryStructure(src, repository);
57  
58          File dotGitDirectory = new File(src, "dotgit");
59  
60          if (dotGitDirectory.exists()) {
61              FileUtils.copyDirectoryStructure(dotGitDirectory, new File(repository, ".git"));
62          }
63  
64          FileUtils.deleteDirectory(workingDirectory);
65  
66          Assert.assertTrue(workingDirectory.mkdirs());
67      }
68  
69      public static String getScmUrl(File repositoryRootFile, String provider) throws CommandLineException {
70          return "scm:" + provider + ":"
71                  + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
72      }
73  
74      public static void deleteAllDirectories(File startDirectory, String pattern) throws IOException {
75          if (startDirectory.isDirectory()) {
76              File[] childs = startDirectory.listFiles();
77              for (int i = 0; i < childs.length; i++) {
78                  File child = childs[i];
79                  if (child.isDirectory()) {
80                      if (child.getName().equals(pattern)) {
81                          FileUtils.deleteDirectory(child);
82                      } else {
83                          deleteAllDirectories(child, pattern);
84                      }
85                  }
86              }
87          }
88      }
89  
90      public static void setDefaultUser(File repositoryRootFile) {
91          File gitConfigFile = new File(new File(repositoryRootFile, ".git"), "config");
92  
93          FileWriter fw = null;
94          try {
95              fw = new FileWriter(gitConfigFile, true);
96              fw.append("[user]\n");
97              fw.append("\tname = John Doe\n");
98              fw.append("\temail = john.doe@nowhere.com\n");
99              fw.flush();
100             fw.close();
101         } catch (IOException e) {
102             System.err.println("cannot setup a default user for tests purpose inside " + gitConfigFile);
103             e.printStackTrace();
104         } finally {
105             if (fw != null) {
106                 try {
107                     fw.close();
108                 } catch (IOException ignore) {
109                     // ignored
110                 }
111             }
112         }
113     }
114 }