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