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 org.apache.maven.scm.manager.ScmManager;
23 import org.apache.maven.scm.repository.ScmRepository;
24 import org.codehaus.plexus.PlexusTestCase;
25 import org.codehaus.plexus.util.FileUtils;
26 import org.codehaus.plexus.util.IOUtil;
27 import org.codehaus.plexus.util.StringUtils;
28 import org.codehaus.plexus.util.cli.CommandLineUtils;
29 import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
30 import org.codehaus.plexus.util.cli.Commandline;
31 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
32
33 import java.io.File;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.util.Calendar;
37 import java.util.Date;
38 import java.util.TimeZone;
39
40
41
42
43
44
45
46
47
48
49
50 public abstract class ScmTestCase
51 extends PlexusTestCase
52 {
53 protected static final TimeZone GMT_TIME_ZONE = TimeZone.getTimeZone( "GMT" );
54
55 private static boolean debugExecute;
56
57 private ScmManager scmManager;
58
59 private SecDispatcher secDispatcher;
60
61 protected void setUp()
62 throws Exception
63 {
64 super.setUp();
65
66 deleteDirectory( getRepositoryRoot() );
67 assertFalse( getRepositoryRoot().exists() );
68 deleteDirectory( getWorkingCopy() );
69 assertFalse( getWorkingCopy().exists() );
70 deleteDirectory( getWorkingDirectory() );
71 assertFalse( getWorkingDirectory().exists() );
72 deleteDirectory( getAssertionCopy() );
73 assertFalse( getAssertionCopy().exists() );
74 deleteDirectory( getUpdatingCopy() );
75 assertFalse( getUpdatingCopy().exists() );
76
77 scmManager = null;
78 }
79
80 protected String getModule()
81 {
82 fail( "getModule() must be overridden." );
83
84 return null;
85 }
86
87
88
89
90 protected File getRepositoryRoot()
91 {
92 return PlexusTestCase.getTestFile( "target/scm-test/repository" );
93 }
94
95
96
97
98 protected File getRepository()
99 {
100 return PlexusTestCase.getTestFile( "/src/test/repository" );
101 }
102
103
104
105
106 protected File getWorkingCopy()
107 {
108 return PlexusTestCase.getTestFile( "target/scm-test/working-copy" );
109 }
110
111
112
113
114
115
116 protected File getWorkingDirectory()
117 {
118 return getWorkingCopy();
119 }
120
121
122
123
124 protected File getAssertionCopy()
125 {
126 return PlexusTestCase.getTestFile( "target/scm-test/assertion-copy" );
127 }
128
129
130
131
132 protected File getUpdatingCopy()
133 {
134 return PlexusTestCase.getTestFile( "target/scm-test/updating-copy" );
135 }
136
137 protected ScmManager getScmManager()
138 throws Exception
139 {
140 if ( scmManager == null )
141 {
142 scmManager = (ScmManager) lookup( ScmManager.ROLE );
143 }
144
145 return scmManager;
146 }
147
148
149
150
151
152
153
154
155 public SecDispatcher getSecDispatcher()
156 throws Exception
157 {
158 if ( secDispatcher == null )
159 {
160 secDispatcher = (SecDispatcher) lookup( SecDispatcher.ROLE, "mng-4384" );
161 }
162
163 return secDispatcher;
164 }
165
166 protected ScmRepository makeScmRepository( String scmUrl )
167 throws Exception
168 {
169 return getScmManager().makeScmRepository( scmUrl );
170 }
171
172
173
174
175
176 public void assertPath( String expectedPath, String actualPath )
177 throws Exception
178 {
179 assertEquals( StringUtils.replace( expectedPath, "\\", "/" ), StringUtils.replace( expectedPath, "\\", "/" ) );
180 }
181
182 protected void assertFile( File root, String fileName )
183 throws Exception
184 {
185 File file = new File( root, fileName );
186
187 assertTrue( "Missing file: '" + file.getAbsolutePath() + "'.", file.exists() );
188
189 assertTrue( "File isn't a file: '" + file.getAbsolutePath() + "'.", file.isFile() );
190
191 String expected = fileName;
192
193 String actual = FileUtils.fileRead( file );
194
195 assertEquals( "The file doesn't contain the expected contents. File: " + file.getAbsolutePath(), expected,
196 actual );
197 }
198
199 protected void assertResultIsSuccess( ScmResult result )
200 {
201 if ( result.isSuccess() )
202 {
203 return;
204 }
205
206 printOutputError( result );
207
208 fail( "The check out result success flag was false." );
209 }
210
211 protected void printOutputError( ScmResult result )
212 {
213 System.err.println( "----------------------------------------------------------------------" );
214 System.err.println( "Provider message" );
215 System.err.println( "----------------------------------------------------------------------" );
216 System.err.println( result.getProviderMessage() );
217 System.err.println( "----------------------------------------------------------------------" );
218
219 System.err.println( "----------------------------------------------------------------------" );
220 System.err.println( "Command output" );
221 System.err.println( "----------------------------------------------------------------------" );
222 System.err.println( result.getCommandOutput() );
223 System.err.println( "----------------------------------------------------------------------" );
224 }
225
226 protected ScmFileSet getScmFileSet()
227 {
228 return new ScmFileSet( getWorkingCopy() );
229 }
230
231 protected static void setDebugExecute( boolean debugExecute )
232 {
233 ScmTestCase.debugExecute = debugExecute;
234 }
235
236
237
238
239
240
241
242
243
244
245
246 public static void execute( File workingDirectory, String executable, String arguments )
247 throws Exception
248 {
249 Commandline cl = new Commandline();
250
251 cl.setExecutable( executable );
252
253 cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
254
255 cl.addArguments( CommandLineUtils.translateCommandline( arguments ) );
256
257 StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
258
259 StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
260
261 System.out.println( "Test command line: " + cl );
262
263 int exitValue = CommandLineUtils.executeCommandLine( cl, stdout, stderr );
264
265 if ( debugExecute || exitValue != 0 )
266 {
267 System.err.println( "-----------------------------------------" );
268 System.err.println( "Command line: " + cl );
269 System.err.println( "Working directory: " + cl.getWorkingDirectory() );
270 System.err.println( "-----------------------------------------" );
271 System.err.println( "Standard output: " );
272 System.err.println( "-----------------------------------------" );
273 System.err.println( stdout.getOutput() );
274 System.err.println( "-----------------------------------------" );
275
276 System.err.println( "Standard error: " );
277 System.err.println( "-----------------------------------------" );
278 System.err.println( stderr.getOutput() );
279 System.err.println( "-----------------------------------------" );
280 }
281
282 if ( exitValue != 0 )
283 {
284 fail( "Exit value wasn't 0, was:" + exitValue );
285 }
286 }
287
288 protected static void makeDirectory( File basedir, String fileName )
289 {
290 File dir = new File( basedir, fileName );
291
292 if ( !dir.exists() )
293 {
294 assertTrue( dir.mkdirs() );
295 }
296 }
297
298 protected static void makeFile( File basedir, String fileName )
299 throws IOException
300 {
301 makeFile( basedir, fileName, fileName );
302 }
303
304 public static void makeFile( File basedir, String fileName, String contents )
305 throws IOException
306 {
307 File file = new File( basedir, fileName );
308
309 File parent = file.getParentFile();
310
311 if ( !parent.exists() )
312 {
313 assertTrue( parent.mkdirs() );
314 }
315
316 FileWriter writer = new FileWriter( file );
317 try
318 {
319 writer.write( contents );
320 }
321 finally
322 {
323 IOUtil.close( writer );
324 }
325 }
326
327 protected void deleteDirectory( File directory )
328 throws IOException
329 {
330 FileUtils.deleteDirectory( directory );
331 }
332
333 public static Date getDate( int year, int month, int day )
334 {
335 return getDate( year, month, day, 0, 0, 0, null );
336 }
337
338 protected static Date getDate( int year, int month, int day, TimeZone tz )
339 {
340 return getDate( year, month, day, 0, 0, 0, tz );
341 }
342
343 protected static Date getDate( int year, int month, int day, int hourOfDay, int minute, int second, TimeZone tz )
344 {
345 Calendar cal = Calendar.getInstance();
346
347 if ( tz != null )
348 {
349 cal.setTimeZone( tz );
350 }
351 cal.set( year, month, day, hourOfDay, minute, second );
352 cal.set( Calendar.MILLISECOND, 0 );
353
354 return cal.getTime();
355 }
356
357 public void assertCommandLine( String expectedCommand, File expectedWorkingDirectory, Commandline actualCommand )
358 throws IOException
359 {
360 Commandline cl = new Commandline( expectedCommand );
361 if ( expectedWorkingDirectory != null )
362 {
363 cl.setWorkingDirectory( expectedWorkingDirectory.getAbsolutePath() );
364 }
365 String expectedCommandLineAsExecuted = StringUtils.join( cl.getShellCommandline(), " " );
366 String actualCommandLineAsExecuted = StringUtils.join( actualCommand.getShellCommandline(), " " );
367 assertEquals( expectedCommandLineAsExecuted, actualCommandLineAsExecuted );
368 }
369
370
371
372
373
374 public static boolean isSystemCmd( String cmd )
375 {
376 try
377 {
378 Runtime.getRuntime().exec( cmd );
379
380 return true;
381 }
382 catch ( IOException e )
383 {
384 return false;
385 }
386 }
387 }