1 package org.apache.maven.plugins.deploy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Properties;
31
32 import org.apache.maven.artifact.repository.ArtifactRepository;
33 import org.apache.maven.execution.MavenSession;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
36 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
37 import org.apache.maven.plugins.deploy.stubs.ArtifactDeployerStub;
38 import org.apache.maven.plugins.deploy.stubs.ArtifactRepositoryStub;
39 import org.apache.maven.plugins.deploy.stubs.DeployArtifactStub;
40 import org.apache.maven.project.MavenProject;
41 import org.apache.maven.project.ProjectBuildingRequest;
42 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
43 import org.apache.maven.shared.transfer.project.deploy.ProjectDeployerRequest;
44 import org.codehaus.plexus.util.FileUtils;
45 import org.junit.Ignore;
46 import org.mockito.InjectMocks;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
50
51
52
53
54 public class DeployMojoTest
55 extends AbstractMojoTestCase
56 {
57 private File remoteRepo;
58
59 private File localRepo;
60
61 private String LOCAL_REPO = getBasedir() + "/target/local-repo";
62
63 private String REMOTE_REPO = getBasedir() + "/target/remote-repo";
64
65 DeployArtifactStub artifact;
66
67 MavenProjectStub project = new MavenProjectStub();
68
69 @Mock
70 private MavenSession session;
71
72 @InjectMocks
73 private DeployMojo mojo;
74
75 public void setUp()
76 throws Exception
77 {
78 super.setUp();
79
80 remoteRepo = new File( REMOTE_REPO );
81
82 remoteRepo.mkdirs();
83
84 localRepo = new File( LOCAL_REPO );
85
86 if ( localRepo.exists() )
87 {
88 FileUtils.deleteDirectory( localRepo );
89 }
90
91 if ( remoteRepo.exists() )
92 {
93 FileUtils.deleteDirectory( remoteRepo );
94 }
95
96
97 }
98
99 public void tearDown()
100 throws Exception
101 {
102 super.tearDown();
103
104 if( remoteRepo.exists() )
105 {
106
107 }
108 }
109
110 public void testDeployTestEnvironment()
111 throws Exception
112 {
113 File testPom = new File( getBasedir(),
114 "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
115
116 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
117
118 assertNotNull( mojo );
119 }
120
121 public void testBasicDeploy()
122 throws Exception
123 {
124 File testPom = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
125
126 mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
127
128 MockitoAnnotations.initMocks( this );
129
130 assertNotNull( mojo );
131
132 ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
133 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
134 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
135 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( LOCAL_REPO ) );
136 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
137
138 File file = new File( getBasedir(),
139 "target/test-classes/unit/basic-deploy-test/target/" +
140 "deploy-test-file-1.0-SNAPSHOT.jar" );
141
142 assertTrue( file.exists() );
143
144 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
145
146 setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
147
148 artifact = ( DeployArtifactStub ) project.getArtifact();
149
150 String packaging = project.getPackaging();
151
152 assertEquals( "jar", packaging );
153
154 artifact.setFile( file );
155
156 ArtifactRepositoryStub repo = getRepoStub( mojo );
157
158 assertNotNull( repo );
159
160 repo.setAppendToUrl( "basic-deploy-test" );
161
162 assertEquals( "deploy-test", repo.getId() );
163 assertEquals( "deploy-test", repo.getKey() );
164 assertEquals( "file", repo.getProtocol() );
165 assertEquals( "file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl() );
166
167 mojo.execute();
168
169
170 List<String> expectedFiles = new ArrayList<String>();
171 List<String> fileList = new ArrayList<String>();
172
173 expectedFiles.add( "org" );
174 expectedFiles.add( "apache" );
175 expectedFiles.add( "maven" );
176 expectedFiles.add( "test" );
177 expectedFiles.add( "maven-deploy-test" );
178 expectedFiles.add( "1.0-SNAPSHOT" );
179 expectedFiles.add( "maven-metadata-deploy-test.xml" );
180
181
182
183 expectedFiles.add( "maven-metadata-deploy-test.xml" );
184
185 expectedFiles.add( "resolver-status.properties" );
186 expectedFiles.add( "resolver-status.properties" );
187
188 File localRepo = new File( LOCAL_REPO, "" );
189
190 File[] files = localRepo.listFiles();
191
192 for (File file2 : files) {
193 addFileToList(file2, fileList);
194 }
195
196 assertEquals( expectedFiles.size(), fileList.size() );
197
198 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
199
200
201 expectedFiles = new ArrayList<String>();
202 fileList = new ArrayList<String>();
203
204 expectedFiles.add( "org" );
205 expectedFiles.add( "apache" );
206 expectedFiles.add( "maven" );
207 expectedFiles.add( "test" );
208 expectedFiles.add( "maven-deploy-test" );
209 expectedFiles.add( "1.0-SNAPSHOT" );
210 expectedFiles.add( "maven-metadata.xml" );
211 expectedFiles.add( "maven-metadata.xml.md5" );
212 expectedFiles.add( "maven-metadata.xml.sha1" );
213 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
214 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.md5" );
215 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.sha1" );
216 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
217 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
218 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
219
220 expectedFiles.add( "maven-metadata.xml" );
221 expectedFiles.add( "maven-metadata.xml.md5" );
222 expectedFiles.add( "maven-metadata.xml.sha1" );
223
224 remoteRepo = new File( remoteRepo, "basic-deploy-test" );
225
226 files = remoteRepo.listFiles();
227
228 for (File file1 : files) {
229 addFileToList(file1, fileList);
230 }
231
232 assertEquals( expectedFiles.size(), fileList.size() );
233
234 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
235 }
236
237 public void testSkippingDeploy()
238 throws Exception
239 {
240 File testPom = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
241
242 DeployMojo mojo = (DeployMojo) lookupMojo( "deploy", testPom );
243
244 assertNotNull( mojo );
245
246 File file = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/target/"
247 + "deploy-test-file-1.0-SNAPSHOT.jar" );
248
249 assertTrue( file.exists() );
250
251 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
252
253 setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
254
255 artifact = (DeployArtifactStub) project.getArtifact();
256
257 String packaging = project.getPackaging();
258
259 assertEquals( "jar", packaging );
260
261 artifact.setFile( file );
262
263 ArtifactRepositoryStub repo = getRepoStub( mojo );
264
265 assertNotNull( repo );
266
267 repo.setAppendToUrl( "basic-deploy-test" );
268
269 assertEquals( "deploy-test", repo.getId() );
270 assertEquals( "deploy-test", repo.getKey() );
271 assertEquals( "file", repo.getProtocol() );
272 assertEquals( "file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl() );
273
274 setVariableValueToObject( mojo, "skip", Boolean.TRUE );
275
276 mojo.execute();
277
278 File localRepo = new File( LOCAL_REPO, "" );
279
280 File[] files = localRepo.listFiles();
281
282 assertNull( files );
283
284 remoteRepo = new File( remoteRepo, "basic-deploy-test" );
285
286 files = remoteRepo.listFiles();
287
288 assertNull( files );
289 }
290
291 public void testBasicDeployWithPackagingAsPom()
292 throws Exception
293 {
294 File testPom = new File( getBasedir(),
295 "target/test-classes/unit/basic-deploy-pom/plugin-config.xml" );
296
297 mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
298
299 MockitoAnnotations.initMocks( this );
300
301 assertNotNull( mojo );
302
303 ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
304 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
305 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
306 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( LOCAL_REPO ) );
307 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
308
309 File pomFile = new File( getBasedir(),
310 "target/test-classes/unit/basic-deploy-pom/target/" +
311 "deploy-test-file-1.0-SNAPSHOT.pom" );
312
313 assertTrue( pomFile.exists() );
314
315 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
316
317 setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
318
319 artifact = (DeployArtifactStub) project.getArtifact();
320
321 artifact.setArtifactHandlerExtension( project.getPackaging() );
322
323 artifact.setFile( pomFile );
324
325 ArtifactRepositoryStub repo = getRepoStub( mojo );
326
327 repo.setAppendToUrl( "basic-deploy-pom" );
328
329 mojo.execute();
330
331 List<String> expectedFiles = new ArrayList<String>();
332 List<String> fileList = new ArrayList<String>();
333
334 expectedFiles.add( "org" );
335 expectedFiles.add( "apache" );
336 expectedFiles.add( "maven" );
337 expectedFiles.add( "test" );
338 expectedFiles.add( "maven-deploy-test" );
339 expectedFiles.add( "1.0-SNAPSHOT" );
340 expectedFiles.add( "maven-metadata.xml" );
341 expectedFiles.add( "maven-metadata.xml.md5" );
342 expectedFiles.add( "maven-metadata.xml.sha1" );
343 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
344 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
345 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
346
347 expectedFiles.add( "maven-metadata.xml" );
348 expectedFiles.add( "maven-metadata.xml.md5" );
349 expectedFiles.add( "maven-metadata.xml.sha1" );
350 remoteRepo = new File( remoteRepo, "basic-deploy-pom" );
351
352 File[] files = remoteRepo.listFiles();
353
354 for (File file : files) {
355 addFileToList(file, fileList);
356 }
357
358 assertEquals( expectedFiles.size(), fileList.size() );
359
360 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
361 }
362
363 public void testDeployIfArtifactFileIsNull()
364 throws Exception
365 {
366 File testPom = new File( getBasedir(),
367 "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
368
369 DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
370
371 MockitoAnnotations.initMocks( this );
372
373 ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
374 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
375
376 setVariableValueToObject( mojo, "session", session );
377
378 assertNotNull( mojo );
379
380 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
381
382 setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
383
384 artifact = (DeployArtifactStub) project.getArtifact();
385
386 artifact.setFile( null );
387
388 assertNull( artifact.getFile() );
389
390 try
391 {
392 mojo.execute();
393
394 fail( "Did not throw mojo execution exception" );
395 }
396 catch( MojoExecutionException e )
397 {
398
399 }
400 }
401
402 public void testDeployWithAttachedArtifacts()
403 throws Exception
404 {
405 File testPom = new File( getBasedir(),
406 "target/test-classes/unit/basic-deploy-with-attached-artifacts/" +
407 "plugin-config.xml" );
408
409 mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
410
411 MockitoAnnotations.initMocks( this );
412
413 assertNotNull( mojo );
414
415 ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
416 when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
417 MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
418 repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( LOCAL_REPO ) );
419 when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
420
421 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
422
423 setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
424
425 artifact = (DeployArtifactStub) project.getArtifact();
426
427 File file = new File( getBasedir(),
428 "target/test-classes/unit/basic-deploy-with-attached-artifacts/target/" +
429 "deploy-test-file-1.0-SNAPSHOT.jar" );
430
431 artifact.setFile( file );
432
433 ArtifactRepositoryStub repo = getRepoStub( mojo );
434
435 repo.setAppendToUrl( "basic-deploy-with-attached-artifacts" );
436
437 mojo.execute();
438
439
440 List<String> expectedFiles = new ArrayList<String>();
441 List<String> fileList = new ArrayList<String>();
442
443 expectedFiles.add( "org" );
444 expectedFiles.add( "apache" );
445 expectedFiles.add( "maven" );
446 expectedFiles.add( "test" );
447 expectedFiles.add( "maven-deploy-test" );
448 expectedFiles.add( "1.0-SNAPSHOT" );
449 expectedFiles.add( "maven-metadata.xml" );
450 expectedFiles.add( "maven-metadata.xml.md5" );
451 expectedFiles.add( "maven-metadata.xml.sha1" );
452 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
453 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.md5" );
454 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.sha1" );
455 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
456 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
457 expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
458
459 expectedFiles.add( "maven-metadata.xml" );
460 expectedFiles.add( "maven-metadata.xml.md5" );
461 expectedFiles.add( "maven-metadata.xml.sha1" );
462 expectedFiles.add( "attached-artifact-test-0" );
463 expectedFiles.add( "1.0-SNAPSHOT" );
464 expectedFiles.add( "maven-metadata.xml" );
465 expectedFiles.add( "maven-metadata.xml.md5" );
466 expectedFiles.add( "maven-metadata.xml.sha1" );
467 expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar" );
468 expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar.md5" );
469 expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar.sha1" );
470
471 expectedFiles.add( "maven-metadata.xml" );
472 expectedFiles.add( "maven-metadata.xml.md5" );
473 expectedFiles.add( "maven-metadata.xml.sha1" );
474
475 remoteRepo = new File( remoteRepo, "basic-deploy-with-attached-artifacts" );
476
477 File[] files = remoteRepo.listFiles();
478
479 for (File file1 : files) {
480 addFileToList(file1, fileList);
481 }
482
483 assertEquals( expectedFiles.size(), fileList.size() );
484
485 assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
486 }
487
488 @Ignore( "SCP is not part of Maven3 distribution. Aether handles transport extensions." )
489 public void _testBasicDeployWithScpAsProtocol()
490 throws Exception
491 {
492 String originalUserHome = System.getProperty( "user.home" );
493
494
495 File altHome = new File( getBasedir(), "target/ssh-user-home" );
496 altHome.mkdirs();
497
498 System.out.println( "Testing user.home value for .ssh dir: " + altHome.getCanonicalPath() );
499
500 Properties props = System.getProperties();
501 props.setProperty( "user.home", altHome.getCanonicalPath() );
502
503 System.setProperties( props );
504
505 File testPom = new File( getBasedir(),
506 "target/test-classes/unit/basic-deploy-scp/plugin-config.xml" );
507
508 mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
509
510 assertNotNull( mojo );
511
512 ArtifactDeployerStub deployer = new ArtifactDeployerStub();
513
514 setVariableValueToObject( mojo, "deployer", deployer );
515
516 File file = new File( getBasedir(),
517 "target/test-classes/unit/basic-deploy-scp/target/" +
518 "deploy-test-file-1.0-SNAPSHOT.jar" );
519
520 assertTrue( file.exists() );
521
522 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
523
524 setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
525
526 artifact = (DeployArtifactStub) project.getArtifact();
527
528 artifact.setFile( file );
529
530 String altUserHome = System.getProperty( "user.home" );
531
532 if ( altUserHome.equals( originalUserHome ) )
533 {
534
535 throw new IllegalStateException( "Setting 'user.home' system property to alternate value did NOT work. Aborting test." );
536 }
537
538 File sshFile = new File( altUserHome, ".ssh" );
539
540 System.out.println( "Testing .ssh dir: " + sshFile.getCanonicalPath() );
541
542
543 if( sshFile.exists() )
544 {
545 FileUtils.deleteDirectory( sshFile );
546 }
547
548 mojo.execute();
549
550 assertTrue( sshFile.exists() );
551
552 FileUtils.deleteDirectory( sshFile );
553 }
554
555 public void testAltSnapshotDeploymentRepository()
556 throws Exception
557 {
558 DeployMojo mojo = spy( new DeployMojo() );
559
560 ArtifactRepository repository = mock( ArtifactRepository.class );
561 when( mojo.createDeploymentArtifactRepository( "altSnapshotDeploymentRepository", "http://localhost"
562 ) ).thenReturn( repository );
563
564 project.setVersion( "1.0-SNAPSHOT" );
565
566 ProjectDeployerRequest pdr =
567 new ProjectDeployerRequest()
568 .setProject( project )
569 .setAltDeploymentRepository( "altSnapshotDeploymentRepository::http://localhost" );
570 assertEquals( repository,
571 mojo.getDeploymentRepository( pdr ));
572 }
573
574 public void testAltReleaseDeploymentRepository()
575 throws Exception
576 {
577 DeployMojo mojo = spy( new DeployMojo() );
578
579 ArtifactRepository repository = mock( ArtifactRepository.class );
580 when( mojo.createDeploymentArtifactRepository( "altReleaseDeploymentRepository", "http://localhost" ) ).thenReturn( repository );
581
582 project.setVersion( "1.0" );
583
584 ProjectDeployerRequest pdr =
585 new ProjectDeployerRequest()
586 .setProject( project )
587 .setAltReleaseDeploymentRepository( "altReleaseDeploymentRepository::http://localhost" );
588
589 assertEquals( repository,
590 mojo.getDeploymentRepository( pdr ));
591 }
592
593 private void addFileToList( File file, List<String> fileList )
594 {
595 if( !file.isDirectory() )
596 {
597 fileList.add( file.getName() );
598 }
599 else
600 {
601 fileList.add( file.getName() );
602
603 File[] files = file.listFiles();
604
605 for (File file1 : files) {
606 addFileToList(file1, fileList);
607 }
608 }
609 }
610
611 private int getSizeOfExpectedFiles( List<String> fileList, List<String> expectedFiles )
612 {
613 for( String fileName : fileList )
614 {
615
616 fileName = fileName.replaceFirst( "-\\d{8}\\.\\d{6}-\\d+", "-SNAPSHOT" );
617
618 if( !expectedFiles.remove( fileName ) )
619 {
620 fail( fileName + " is not included in the expected files" );
621 }
622 }
623 return expectedFiles.size();
624 }
625
626 private ArtifactRepositoryStub getRepoStub( Object mojo )
627 throws Exception
628 {
629 MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
630 return (ArtifactRepositoryStub) project.getDistributionManagementArtifactRepository();
631 }
632
633 }