View Javadoc
1   package org.apache.maven.scm.provider.git;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  
26  import org.apache.maven.scm.PlexusJUnit4TestSupport;
27  import org.junit.Assert;
28  
29  import org.codehaus.plexus.util.FileUtils;
30  import org.codehaus.plexus.util.cli.CommandLineException;
31  
32  /**
33   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
34   *
35   */
36  public final class GitScmTestUtils
37  {
38      /** 'git' command line */
39      public static final String GIT_COMMAND_LINE = "git";
40  
41      private GitScmTestUtils()
42      {
43      }
44  
45      public static void initRepo( File repository, File workingDirectory, File assertionDirectory )
46          throws IOException
47      {
48          initRepo( "src/test/repository/", repository, workingDirectory );
49  
50          FileUtils.deleteDirectory( assertionDirectory );
51  
52          Assert.assertTrue( assertionDirectory.mkdirs() );
53      }
54  
55      public static void initRepo( String source, File repository, File workingDirectory )
56          throws IOException
57      {
58          // Copy the repository to target
59          File src = PlexusJUnit4TestSupport.getTestFile( source );
60  
61          FileUtils.deleteDirectory( repository );
62  
63          Assert.assertTrue( repository.mkdirs() );
64  
65          FileUtils.copyDirectoryStructure( src, repository );
66  
67          File dotGitDirectory = new File( src, "dotgit" );
68  
69          if ( dotGitDirectory.exists() )
70          {
71              FileUtils.copyDirectoryStructure( dotGitDirectory, new File( repository, ".git" ) );
72          }
73  
74          FileUtils.deleteDirectory( workingDirectory );
75  
76          Assert.assertTrue( workingDirectory.mkdirs() );
77      }
78  
79      public static String getScmUrl( File repositoryRootFile, String provider )
80          throws CommandLineException
81      {
82          return "scm:" + provider + ":" + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
83      }
84  
85  
86      public static void deleteAllDirectories( File startDirectory, String pattern )
87          throws IOException
88      {
89          if ( startDirectory.isDirectory() )
90          {
91              File[] childs = startDirectory.listFiles();
92              for ( int i = 0; i < childs.length; i++ )
93              {
94                  File child = childs[i];
95                  if ( child.isDirectory() )
96                  {
97                      if ( child.getName().equals( pattern ) )
98                      {
99                          FileUtils.deleteDirectory( child );
100                     }
101                     else
102                     {
103                         deleteAllDirectories( child, pattern );
104                     }
105                 }
106             }
107         }
108     }
109 
110     public static void setDefaultUser( File repositoryRootFile )
111     {
112         File gitConfigFile = new File( new File( repositoryRootFile, ".git" ), "config" );
113 
114         FileWriter fw = null;
115         try
116         {
117             fw = new FileWriter( gitConfigFile , true );
118             fw.append( "[user]\n" );
119             fw.append( "\tname = John Doe\n" );
120             fw.append( "\temail = john.doe@nowhere.com\n" );
121             fw.flush();
122             fw.close();
123         }
124         catch ( IOException e )
125         {
126             System.err.println( "cannot setup a default user for tests purpose inside " + gitConfigFile );
127             e.printStackTrace();
128         }
129         finally
130         {
131             if ( fw != null )
132             {
133                 try
134                 {
135                     fw.close();
136                 }
137                 catch ( IOException ignore )
138                 {
139                     // ignored
140                 }
141             }
142         }
143     }
144 }