1 package org.apache.maven.scm;
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.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 import org.apache.maven.scm.command.add.AddScmResult;
30 import org.apache.maven.scm.command.checkin.CheckInScmResult;
31 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
32 import org.apache.maven.scm.command.edit.EditScmResult;
33 import org.apache.maven.scm.command.remove.RemoveScmResult;
34 import org.apache.maven.scm.provider.ScmProvider;
35 import org.apache.maven.scm.repository.ScmRepository;
36 import org.codehaus.plexus.util.StringUtils;
37 import org.junit.After;
38 import org.junit.Before;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42
43 import static org.junit.Assume.assumeTrue;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public abstract class ScmTckTestCase
60 extends ScmTestCase
61 {
62 private ScmRepository scmRepository;
63
64 private List<String> scmFileNames;
65
66
67
68
69
70
71 public String getScmProviderCommand()
72 {
73 return null;
74 }
75
76
77
78
79
80 public abstract String getScmUrl()
81 throws Exception;
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 protected List<String> getScmFileNames()
97 {
98 return scmFileNames;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public abstract void initRepo()
114 throws Exception;
115
116 public void checkScmPresence()
117 {
118 String scmProviderCommand = getScmProviderCommand();
119 if ( scmProviderCommand != null )
120 {
121 assumeTrue( "Skipping tests because the required command '" + scmProviderCommand + "' is not available.",
122 ScmTestCase.isSystemCmd( scmProviderCommand ) );
123 }
124 }
125
126
127
128
129 @Before
130 @Override
131 public void setUp()
132 throws Exception
133 {
134 checkScmPresence();
135 super.setUp();
136
137 scmRepository = null;
138
139 scmFileNames = new ArrayList<>( 4 );
140 scmFileNames.add( "/pom.xml" );
141 scmFileNames.add( "/readme.txt" );
142 scmFileNames.add( "/src/main/java/Application.java" );
143 scmFileNames.add( "/src/test/java/Test.java" );
144
145 initRepo();
146
147 checkOut( getWorkingCopy(), getScmRepository() );
148
149 Iterator<String> it = getScmFileNames().iterator();
150 while ( it.hasNext() )
151 {
152 assertFile( getWorkingCopy(), it.next() );
153 }
154 }
155
156
157
158
159
160
161
162 public void removeRepo()
163 throws Exception
164 {
165 }
166
167
168
169
170 @After
171 @Override
172 public void tearDown()
173 throws Exception
174 {
175 super.tearDown();
176 removeRepo();
177 }
178
179
180
181
182 protected ScmRepository getScmRepository()
183 throws Exception
184 {
185 if ( scmRepository == null )
186 {
187 scmRepository = getScmManager().makeScmRepository( getScmUrl() );
188 }
189
190 return scmRepository;
191 }
192
193
194
195
196 protected CheckOutScmResult checkOut( File workingDirectory, ScmRepository repository )
197 throws Exception
198 {
199 CheckOutScmResult result =
200 getScmManager().getProviderByUrl( getScmUrl() ).checkOut( repository, new ScmFileSet( workingDirectory ),
201 (ScmVersion) null );
202
203 assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
204
205 return result;
206 }
207
208
209
210
211 protected CheckInScmResult checkIn( File workingDirectory, ScmRepository repository )
212 throws Exception
213 {
214 CheckInScmResult result = getScmManager().getProviderByUrl( getScmUrl() )
215 .checkIn( repository, new ScmFileSet( workingDirectory ), (ScmVersion) null, "Initial Checkin" );
216
217 assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
218
219 return result;
220 }
221
222
223
224
225 protected RemoveScmResult remove( File workingDirectory, ScmRepository repository )
226 throws Exception
227 {
228 RemoveScmResult result = getScmManager().getProviderByUrl( getScmUrl() )
229 .remove( repository, new ScmFileSet( workingDirectory ), "Initial Checkin" );
230
231 assertTrue( "Remove result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
232
233 return result;
234 }
235
236
237
238
239 protected void addToWorkingTree( File workingDirectory, File file, ScmRepository repository )
240 throws Exception
241 {
242 ScmProvider provider = getScmManager().getProviderByUrl( getScmUrl() );
243
244 CommandParameters commandParameters = new CommandParameters();
245 commandParameters.setString( CommandParameter.FORCE_ADD, Boolean.TRUE.toString() );
246
247 AddScmResult result = provider.add( repository, new ScmFileSet( workingDirectory, file ), commandParameters );
248
249 assertTrue( "Check result was successful, output: " + result.getCommandOutput(), result.isSuccess() );
250
251 List<ScmFile> addedFiles = result.getAddedFiles();
252
253 if ( new File( workingDirectory, file.getPath() ).isFile() )
254 {
255
256 assertEquals( "Expected 1 file in the added files list " + addedFiles, 1, addedFiles.size() );
257 }
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272 protected Map<String, ScmFile> mapFilesByPath( List<ScmFile> files )
273 {
274 if ( files == null )
275 {
276 return null;
277 }
278
279 Map<String, ScmFile> mappedFiles = new TreeMap<>();
280 for ( ScmFile scmFile : files )
281 {
282 String path = StringUtils.replace( scmFile.getPath(), "\\", "/" );
283 mappedFiles.put( path, scmFile );
284 }
285
286 return mappedFiles;
287 }
288
289 protected EditScmResult edit( File basedir, String includes, String excludes, ScmRepository repository )
290 throws Exception
291 {
292 if ( this.getScmManager().getProviderByRepository( this.getScmRepository() ).requiresEditMode() )
293 {
294 ScmFileSet fileSet = new ScmFileSet( basedir, includes, excludes );
295 return getScmManager().edit( getScmRepository(), fileSet );
296 }
297 return new EditScmResult( "", "", "", true );
298 }
299
300 }