1 package org.apache.maven.plugin.deploy;
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.Properties;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.deploy.DeployMojo;
30 import org.apache.maven.plugin.deploy.stubs.ArtifactRepositoryStub;
31 import org.apache.maven.plugin.deploy.stubs.AttachedArtifactStub;
32 import org.apache.maven.plugin.deploy.stubs.DeployArtifactStub;
33 import org.apache.maven.plugin.deploy.stubs.ArtifactDeployerStub;
34 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
36 import org.apache.maven.artifact.repository.ArtifactRepository;
37 import org.codehaus.plexus.util.FileUtils;
38
39
40
41
42 public class DeployMojoTest
43 extends AbstractMojoTestCase
44 {
45 private File remoteRepo;
46
47 private File localRepo;
48
49 private String LOCAL_REPO = getBasedir() + "/target/local-repo";
50
51 private String REMOTE_REPO = getBasedir() + "/target/remote-repo";
52
53 DeployArtifactStub artifact;
54
55 MavenProjectStub project = new MavenProjectStub();
56
57 public void setUp()
58 throws Exception
59 {
60 super.setUp();
61
62 remoteRepo = new File( REMOTE_REPO );
63
64 remoteRepo.mkdirs();
65
66 localRepo = new File( LOCAL_REPO );
67
68 if( localRepo.exists() )
69 {
70 FileUtils.deleteDirectory( localRepo );
71 }
72
73 if( remoteRepo.exists() )
74 {
75 FileUtils.deleteDirectory( remoteRepo );
76 }
77 }
78
79 public void testDeployTestEnvironment()
80 throws Exception
81 {
82 File testPom = new File( getBasedir(),
83 "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
84
85 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
86
87 assertNotNull( mojo );
88 }
89
90 public void testBasicDeploy()
91 throws Exception
92 {
93 File testPom = new File( getBasedir(),
94 "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
95
96 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
97
98 assertNotNull( mojo );
99
100 File file = new File( getBasedir(),
101 "target/test-classes/unit/basic-deploy-test/target/" +
102 "deploy-test-file-1.0-SNAPSHOT.jar" );
103
104 assertTrue( file.exists() );
105
106 ArtifactRepository loc = ( ArtifactRepository ) getVariableValueFromObject( mojo, "localRepository" );
107
108 artifact = ( DeployArtifactStub ) getVariableValueFromObject( mojo, "artifact" );
109
110 String packaging = ( String ) getVariableValueFromObject( mojo, "packaging" );
111
112 assertEquals( "jar", packaging );
113
114 artifact.setFile( file );
115
116 ArtifactRepositoryStub repo = ( ArtifactRepositoryStub ) getVariableValueFromObject( mojo, "deploymentRepository" );
117
118 assertNotNull( repo );
119
120 repo.setAppendToUrl( "basic-deploy-test" );
121
122 assertEquals( "deploy-test", repo.getId() );
123 assertEquals( "deploy-test", repo.getKey() );
124 assertEquals( "file", repo.getProtocol() );
125 assertEquals( "file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl() );
126
127 mojo.execute();
128
129
130 List expectedFiles = new ArrayList();
131 List fileList = new ArrayList();
132
133 expectedFiles.add( "org" );
134 expectedFiles.add( "apache" );
135 expectedFiles.add( "maven" );
136 expectedFiles.add( "test" );
137 expectedFiles.add( "maven-deploy-test" );
138 expectedFiles.add( "1.0-SNAPSHOT" );
139 expectedFiles.add( "maven-metadata-deploy-test.xml" );
140 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
141 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
142
143 expectedFiles.add( "maven-metadata-deploy-test.xml" );
144
145 File localRepo = new File( LOCAL_REPO, "" );
146
147 File[] files = localRepo.listFiles();
148
149 for( int i=0; i<files.length; i++ )
150 {
151 addFileToList( files[i], fileList );
152 }
153
154 assertEquals( expectedFiles.size(), fileList.size() );
155
156 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
157
158
159 expectedFiles = new ArrayList();
160 fileList = new ArrayList();
161
162 expectedFiles.add( "org" );
163 expectedFiles.add( "apache" );
164 expectedFiles.add( "maven" );
165 expectedFiles.add( "test" );
166 expectedFiles.add( "maven-deploy-test" );
167 expectedFiles.add( "1.0-SNAPSHOT" );
168 expectedFiles.add( "maven-metadata.xml" );
169 expectedFiles.add( "maven-metadata.xml.md5" );
170 expectedFiles.add( "maven-metadata.xml.sha1" );
171 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
172 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.md5" );
173 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.sha1" );
174 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
175 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
176 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
177
178 expectedFiles.add( "maven-metadata.xml" );
179 expectedFiles.add( "maven-metadata.xml.md5" );
180 expectedFiles.add( "maven-metadata.xml.sha1" );
181
182 remoteRepo = new File( remoteRepo, "basic-deploy-test" );
183
184 files = remoteRepo.listFiles();
185
186 for( int i=0; i<files.length; i++ )
187 {
188 addFileToList( files[i], fileList );
189 }
190
191 assertEquals( expectedFiles.size(), fileList.size() );
192
193 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
194 }
195
196 public void testSkippingDeploy()
197 throws Exception
198 {
199 File testPom = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
200
201 DeployMojo mojo = (DeployMojo) lookupMojo( "deploy", testPom );
202
203 assertNotNull( mojo );
204
205 File file = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/target/"
206 + "deploy-test-file-1.0-SNAPSHOT.jar" );
207
208 assertTrue( file.exists() );
209
210 ArtifactRepository loc = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
211
212 artifact = (DeployArtifactStub) getVariableValueFromObject( mojo, "artifact" );
213
214 String packaging = (String) getVariableValueFromObject( mojo, "packaging" );
215
216 assertEquals( "jar", packaging );
217
218 artifact.setFile( file );
219
220 ArtifactRepositoryStub repo = (ArtifactRepositoryStub) getVariableValueFromObject( mojo, "deploymentRepository" );
221
222 assertNotNull( repo );
223
224 repo.setAppendToUrl( "basic-deploy-test" );
225
226 assertEquals( "deploy-test", repo.getId() );
227 assertEquals( "deploy-test", repo.getKey() );
228 assertEquals( "file", repo.getProtocol() );
229 assertEquals( "file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl() );
230
231 setVariableValueToObject( mojo, "skip", Boolean.TRUE );
232
233 mojo.execute();
234
235 File localRepo = new File( LOCAL_REPO, "" );
236
237 File[] files = localRepo.listFiles();
238
239 assertNull( files );
240
241 remoteRepo = new File( remoteRepo, "basic-deploy-test" );
242
243 files = remoteRepo.listFiles();
244
245 assertNull( files );
246 }
247
248 public void testBasicDeployWithPackagingAsPom()
249 throws Exception
250 {
251 File testPom = new File( getBasedir(),
252 "target/test-classes/unit/basic-deploy-pom/plugin-config.xml" );
253
254 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
255
256 assertNotNull( mojo );
257
258 String packaging = ( String ) getVariableValueFromObject( mojo, "packaging" );
259
260 assertEquals( "pom", packaging );
261
262 artifact = ( DeployArtifactStub ) getVariableValueFromObject( mojo, "artifact" );
263
264 artifact.setArtifactHandlerExtension( packaging );
265
266 ArtifactRepositoryStub repo = ( ArtifactRepositoryStub ) getVariableValueFromObject( mojo, "deploymentRepository" );
267
268 repo.setAppendToUrl( "basic-deploy-pom" );
269
270 mojo.execute();
271
272 List expectedFiles = new ArrayList();
273 List fileList = new ArrayList();
274
275 expectedFiles.add( "org" );
276 expectedFiles.add( "apache" );
277 expectedFiles.add( "maven" );
278 expectedFiles.add( "test" );
279 expectedFiles.add( "maven-deploy-test" );
280 expectedFiles.add( "1.0-SNAPSHOT" );
281 expectedFiles.add( "maven-metadata.xml" );
282 expectedFiles.add( "maven-metadata.xml.md5" );
283 expectedFiles.add( "maven-metadata.xml.sha1" );
284 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
285 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
286 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
287
288 expectedFiles.add( "maven-metadata.xml" );
289 expectedFiles.add( "maven-metadata.xml.md5" );
290 expectedFiles.add( "maven-metadata.xml.sha1" );
291 remoteRepo = new File( remoteRepo, "basic-deploy-pom" );
292
293 File[] files = remoteRepo.listFiles();
294
295 for( int i=0; i<files.length; i++ )
296 {
297 addFileToList( files[i], fileList );
298 }
299
300 assertEquals( expectedFiles.size(), fileList.size() );
301
302 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
303 }
304
305 public void testUpdateReleaseParamSetToTrue()
306 throws Exception
307 {
308 File testPom = new File( getBasedir(),
309 "target/test-classes/unit/basic-deploy-pom/plugin-config.xml" );
310
311 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
312
313 assertNotNull( mojo );
314
315 boolean updateReleaseInfo = ( ( Boolean ) getVariableValueFromObject( mojo, "updateReleaseInfo" ) ).booleanValue();
316
317 assertTrue( updateReleaseInfo );
318
319 artifact = ( DeployArtifactStub ) getVariableValueFromObject( mojo, "artifact" );
320
321 artifact.setFile( testPom );
322
323 ArtifactRepositoryStub repo = ( ArtifactRepositoryStub ) getVariableValueFromObject( mojo, "deploymentRepository" );
324
325 repo.setAppendToUrl( "basic-deploy-updateReleaseParam" );
326
327 mojo.execute();
328
329 assertTrue( artifact.isRelease() );
330 }
331
332 public void testDeployIfArtifactFileIsNull()
333 throws Exception
334 {
335 File testPom = new File( getBasedir(),
336 "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
337
338 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
339
340 assertNotNull( mojo );
341
342 artifact = ( DeployArtifactStub ) getVariableValueFromObject( mojo, "artifact" );
343
344 artifact.setFile( null );
345
346 assertNull( artifact.getFile() );
347
348 try
349 {
350 mojo.execute();
351
352 fail( "Did not throw mojo execution exception" );
353 }
354 catch( MojoExecutionException e )
355 {
356
357 }
358 }
359
360 public void testDeployWithAttachedArtifacts()
361 throws Exception
362 {
363 File testPom = new File( getBasedir(),
364 "target/test-classes/unit/basic-deploy-with-attached-artifacts/" +
365 "plugin-config.xml" );
366
367 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
368
369 assertNotNull( mojo );
370
371 artifact = ( DeployArtifactStub ) getVariableValueFromObject( mojo, "artifact" );
372
373 File file = new File( getBasedir(),
374 "target/test-classes/unit/basic-deploy-with-attached-artifacts/target/" +
375 "deploy-test-file-1.0-SNAPSHOT.jar" );
376
377 artifact.setFile( file );
378
379 List attachedArtifacts = ( ArrayList ) getVariableValueFromObject( mojo, "attachedArtifacts" );
380
381 ArtifactRepositoryStub repo = ( ArtifactRepositoryStub ) getVariableValueFromObject( mojo, "deploymentRepository" );
382
383 repo.setAppendToUrl( "basic-deploy-with-attached-artifacts" );
384
385 mojo.execute();
386
387 String packaging = getVariableValueFromObject( mojo, "packaging" ).toString();
388
389 for( Iterator iter=attachedArtifacts.iterator(); iter.hasNext(); )
390 {
391 AttachedArtifactStub attachedArtifact = ( AttachedArtifactStub ) iter.next();
392
393 File deployedArtifact = new File( remoteRepo, "basic-deploy-with-attached-artifacts" + "/" +
394 attachedArtifact.getGroupId().replace( '.', '/' ) + "/" +
395 attachedArtifact.getArtifactId() + "/" +
396 attachedArtifact.getVersion() + "/" + attachedArtifact.getArtifactId() + "-" +
397 attachedArtifact.getVersion() + "." + packaging );
398 assertTrue( deployedArtifact.exists() );
399 }
400
401
402 List expectedFiles = new ArrayList();
403 List fileList = new ArrayList();
404
405 expectedFiles.add( "org" );
406 expectedFiles.add( "apache" );
407 expectedFiles.add( "maven" );
408 expectedFiles.add( "test" );
409 expectedFiles.add( "maven-deploy-test" );
410 expectedFiles.add( "1.0-SNAPSHOT" );
411 expectedFiles.add( "maven-metadata.xml" );
412 expectedFiles.add( "maven-metadata.xml.md5" );
413 expectedFiles.add( "maven-metadata.xml.sha1" );
414 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
415 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.md5" );
416 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.sha1" );
417 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
418 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
419 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
420
421 expectedFiles.add( "maven-metadata.xml" );
422 expectedFiles.add( "maven-metadata.xml.md5" );
423 expectedFiles.add( "maven-metadata.xml.sha1" );
424 expectedFiles.add( "attached-artifact-test-0" );
425 expectedFiles.add( "1.0-SNAPSHOT" );
426 expectedFiles.add( "maven-metadata.xml" );
427 expectedFiles.add( "maven-metadata.xml.md5" );
428 expectedFiles.add( "maven-metadata.xml.sha1" );
429 expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar" );
430 expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar.md5" );
431 expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar.sha1" );
432
433 expectedFiles.add( "maven-metadata.xml" );
434 expectedFiles.add( "maven-metadata.xml.md5" );
435 expectedFiles.add( "maven-metadata.xml.sha1" );
436
437 remoteRepo = new File( remoteRepo, "basic-deploy-with-attached-artifacts" );
438
439 File[] files = remoteRepo.listFiles();
440
441 for( int i=0; i<files.length; i++ )
442 {
443 addFileToList( files[i], fileList );
444 }
445
446 assertEquals( expectedFiles.size(), fileList.size() );
447
448 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
449 }
450
451
452 public void testBasicDeployWithScpAsProtocol()
453 throws Exception
454 {
455 String originalUserHome = System.getProperty( "user.home" );
456
457
458 File altHome = new File( getBasedir(), "target/ssh-user-home" );
459 altHome.mkdirs();
460
461 System.out.println( "Testing user.home value for .ssh dir: " + altHome.getCanonicalPath() );
462
463 Properties props = System.getProperties();
464 props.setProperty( "user.home", altHome.getCanonicalPath() );
465
466 System.setProperties( props );
467
468 File testPom = new File( getBasedir(),
469 "target/test-classes/unit/basic-deploy-scp/plugin-config.xml" );
470
471 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
472
473 assertNotNull( mojo );
474
475 ArtifactDeployerStub deployer = new ArtifactDeployerStub();
476
477 setVariableValueToObject( mojo, "deployer", deployer );
478
479 File file = new File( getBasedir(),
480 "target/test-classes/unit/basic-deploy-scp/target/" +
481 "deploy-test-file-1.0-SNAPSHOT.jar" );
482
483 assertTrue( file.exists() );
484
485 DeployArtifactStub artifact = ( DeployArtifactStub ) getVariableValueFromObject( mojo, "artifact" );
486
487 artifact.setFile( file );
488
489 String altUserHome = System.getProperty( "user.home" );
490
491 if ( altUserHome.equals( originalUserHome ) )
492 {
493
494 throw new IllegalStateException( "Setting 'user.home' system property to alternate value did NOT work. Aborting test." );
495 }
496
497 File sshFile = new File( altUserHome, ".ssh" );
498
499 System.out.println( "Testing .ssh dir: " + sshFile.getCanonicalPath() );
500
501
502 if( sshFile.exists() )
503 {
504 FileUtils.deleteDirectory( sshFile );
505 }
506
507 mojo.execute();
508
509 assertTrue( sshFile.exists() );
510
511 FileUtils.deleteDirectory( sshFile );
512 }
513
514
515 private void addFileToList( File file, List fileList )
516 {
517 if( !file.isDirectory() )
518 {
519 fileList.add( file.getName() );
520 }
521 else
522 {
523 fileList.add( file.getName() );
524
525 File[] files = file.listFiles();
526
527 for( int i=0; i<files.length; i++ )
528 {
529 addFileToList( files[i], fileList );
530 }
531 }
532 }
533
534 private int getSizeOfExpectedFiles( List fileList, List expectedFiles )
535 {
536 for( Iterator iter=fileList.iterator(); iter.hasNext(); )
537 {
538 String fileName = ( String ) iter.next();
539
540 if( expectedFiles.contains( fileName ) )
541 {
542 expectedFiles.remove( fileName );
543 }
544 else
545 {
546 fail( fileName + " is not included in the expected files" );
547 }
548 }
549 return expectedFiles.size();
550 }
551
552 public void tearDown()
553 throws Exception
554 {
555 super.tearDown();
556
557 if( remoteRepo.exists() )
558 {
559
560 }
561 }
562 }