1 package org.apache.maven.scm.provider.git;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25
26 import junit.framework.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
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
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
137 }
138 }
139 }
140 }
141 }