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