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.junit.Assert;
27  
28  import org.codehaus.plexus.PlexusTestCase;
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      private GitScmTestUtils()
39      {
40      }
41  
42      public static void initRepo( File repository, File workingDirectory, File assertionDirectory )
43          throws IOException
44      {
45          initRepo( "src/test/repository/", repository, workingDirectory );
46  
47          FileUtils.deleteDirectory( assertionDirectory );
48  
49          Assert.assertTrue( assertionDirectory.mkdirs() );
50      }
51  
52      public static void initRepo( String source, File repository, File workingDirectory )
53          throws IOException
54      {
55          // Copy the repository to target
56          File src = PlexusTestCase.getTestFile( source );
57  
58          FileUtils.deleteDirectory( repository );
59  
60          Assert.assertTrue( repository.mkdirs() );
61  
62          FileUtils.copyDirectoryStructure( src, repository );
63  
64          File dotGitDirectory = new File( src, "dotgit" );
65  
66          if ( dotGitDirectory.exists() )
67          {
68              FileUtils.copyDirectoryStructure( dotGitDirectory, new File( repository, ".git" ) );
69          }
70  
71          FileUtils.deleteDirectory( workingDirectory );
72  
73          Assert.assertTrue( workingDirectory.mkdirs() );
74      }
75  
76      public static String getScmUrl( File repositoryRootFile, String provider )
77          throws CommandLineException
78      {
79          return "scm:" + provider + ":" + repositoryRootFile.toPath().toAbsolutePath().toUri().toASCIIString();
80      }
81  
82  
83      public static void deleteAllDirectories( File startDirectory, String pattern )
84          throws IOException
85      {
86          if ( startDirectory.isDirectory() )
87          {
88              File[] childs = startDirectory.listFiles();
89              for ( int i = 0; i < childs.length; i++ )
90              {
91                  File child = childs[i];
92                  if ( child.isDirectory() )
93                  {
94                      if ( child.getName().equals( pattern ) )
95                      {
96                          FileUtils.deleteDirectory( child );
97                      }
98                      else
99                      {
100                         deleteAllDirectories( child, pattern );
101                     }
102                 }
103             }
104         }
105     }
106 
107     public static void setDefaultUser( File repositoryRootFile )
108     {
109         File gitConfigFile = new File( new File( repositoryRootFile, ".git" ), "config" );
110 
111         FileWriter fw = null;
112         try
113         {
114             fw = new FileWriter( gitConfigFile , true );
115             fw.append( "[user]\n" );
116             fw.append( "\tname = John Doe\n" );
117             fw.append( "\temail = john.doe@nowhere.com\n" );
118             fw.flush();
119             fw.close();
120         }
121         catch ( IOException e )
122         {
123             System.err.println( "cannot setup a default user for tests purpose inside " + gitConfigFile );
124             e.printStackTrace();
125         }
126         finally
127         {
128             if ( fw != null )
129             {
130                 try
131                 {
132                     fw.close();
133                 }
134                 catch ( IOException ignore )
135                 {
136                     // ignored
137                 }
138             }
139         }
140     }
141 }