View Javadoc
1   package org.apache.maven.plugins.deploy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.mockito.ArgumentMatchers.any;
23  import static org.mockito.Mockito.doReturn;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.spy;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.File;
29  import java.util.ArrayList;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Properties;
33  import java.util.concurrent.ConcurrentHashMap;
34  
35  import org.apache.maven.execution.MavenSession;
36  import org.apache.maven.plugin.MojoExecutionException;
37  import org.apache.maven.plugin.MojoFailureException;
38  import org.apache.maven.plugin.descriptor.PluginDescriptor;
39  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
40  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
41  import org.apache.maven.plugins.deploy.stubs.ArtifactRepositoryStub;
42  import org.apache.maven.plugins.deploy.stubs.DeployArtifactStub;
43  import org.apache.maven.project.MavenProject;
44  import org.apache.maven.project.ProjectBuildingRequest;
45  import org.codehaus.plexus.util.FileUtils;
46  import org.eclipse.aether.DefaultRepositorySystemSession;
47  import org.eclipse.aether.RepositorySystem;
48  import org.eclipse.aether.repository.LocalRepository;
49  import org.eclipse.aether.repository.RemoteRepository;
50  import org.junit.Ignore;
51  import org.mockito.InjectMocks;
52  import org.mockito.MockitoAnnotations;
53  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
54  
55  /**
56   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
57   */
58  public class DeployMojoTest
59      extends AbstractMojoTestCase
60  {    
61      private File remoteRepo;
62      
63      private File localRepo;
64      
65      private String LOCAL_REPO = getBasedir() + "/target/local-repo";
66      
67      private String REMOTE_REPO = getBasedir() + "/target/remote-repo";
68      
69      DeployArtifactStub artifact;
70      
71      MavenProjectStub project = new MavenProjectStub();
72  
73      private MavenSession session;
74      
75      @InjectMocks
76      private DeployMojo mojo;
77  
78      public void setUp()
79          throws Exception
80      {
81          super.setUp();
82  
83          session = mock( MavenSession.class );
84          when( session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class)))
85                  .thenReturn( new ConcurrentHashMap<String, Object>() );
86          DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
87          repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) );
88          when( session.getRepositorySession() ).thenReturn( repositorySession );
89  
90          remoteRepo = new File( REMOTE_REPO );
91          
92          remoteRepo.mkdirs();  
93          
94          localRepo = new File( LOCAL_REPO );
95  
96          if ( localRepo.exists() )
97          {
98              FileUtils.deleteDirectory( localRepo );
99          }
100 
101         if ( remoteRepo.exists() )
102         {
103             FileUtils.deleteDirectory( remoteRepo );
104         }
105     }
106 
107     public void tearDown()
108         throws Exception
109     {
110         super.tearDown();
111         
112         if( remoteRepo.exists() )
113         {
114             //FileUtils.deleteDirectory( remoteRepo );
115         }
116     }
117     
118     public void testDeployTestEnvironment()
119         throws Exception
120     {
121         File testPom = new File( getBasedir(),
122                                  "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
123     
124         DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
125     
126         assertNotNull( mojo );
127     }
128     
129     public void testBasicDeploy()
130         throws Exception
131     {
132         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
133 
134         mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
135         
136         MockitoAnnotations.initMocks( this );
137         
138         assertNotNull( mojo );
139 
140         ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
141         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
142         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
143         repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) );
144         when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
145         when( session.getRepositorySession() ).thenReturn( repositorySession );
146 
147         File file = new File( getBasedir(),
148                               "target/test-classes/unit/basic-deploy-test/target/" +
149                               "deploy-test-file-1.0-SNAPSHOT.jar" );
150 
151         assertTrue( file.exists() );
152 
153         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
154         project.setGroupId( "org.apache.maven.test" );
155         project.setArtifactId( "maven-deploy-test" );
156         project.setVersion( "1.0-SNAPSHOT" );
157 
158         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
159         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
160         
161         artifact = ( DeployArtifactStub ) project.getArtifact();
162 
163         String packaging = project.getPackaging();
164         
165         assertEquals( "jar", packaging );
166         
167         artifact.setFile( file );        
168         
169         ArtifactRepositoryStub repo = getRepoStub( mojo );
170 
171         assertNotNull( repo );
172         
173         repo.setAppendToUrl( "basic-deploy-test" );
174         
175         assertEquals( "deploy-test", repo.getId() );
176         assertEquals( "deploy-test", repo.getKey() );
177         assertEquals( "file", repo.getProtocol() );
178         assertEquals( "file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl() );
179         
180         mojo.execute();
181 
182         //check the artifact in local repository
183         List<String> expectedFiles = new ArrayList<String>();
184         List<String> fileList = new ArrayList<String>();
185         
186         expectedFiles.add( "org" );
187         expectedFiles.add( "apache" );
188         expectedFiles.add( "maven" );
189         expectedFiles.add( "test" );
190         expectedFiles.add( "maven-deploy-test" );
191         expectedFiles.add( "1.0-SNAPSHOT" );
192         expectedFiles.add( "maven-metadata-deploy-test.xml" );
193         // expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
194         // expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
195         // as we are in SNAPSHOT the file is here twice
196         expectedFiles.add( "maven-metadata-deploy-test.xml" );
197         // extra Aether files
198         expectedFiles.add( "resolver-status.properties" );
199         expectedFiles.add( "resolver-status.properties" );
200         
201         File localRepo = new File( LOCAL_REPO, "" );
202         
203         File[] files = localRepo.listFiles();
204 
205         for (File file2 : files) {
206             addFileToList(file2, fileList);
207         }
208         
209         assertEquals( expectedFiles.size(), fileList.size() );
210 
211         assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );        
212                   
213         //check the artifact in remote repository
214         expectedFiles = new ArrayList<String>();
215         fileList = new ArrayList<String>();
216         
217         expectedFiles.add( "org" );
218         expectedFiles.add( "apache" );
219         expectedFiles.add( "maven" );
220         expectedFiles.add( "test" );
221         expectedFiles.add( "maven-deploy-test" );
222         expectedFiles.add( "1.0-SNAPSHOT" );
223         expectedFiles.add( "maven-metadata.xml" );
224         expectedFiles.add( "maven-metadata.xml.md5" );
225         expectedFiles.add( "maven-metadata.xml.sha1" );
226         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
227         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.md5" );
228         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.sha1" );
229         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
230         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
231         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
232         // as we are in SNAPSHOT the file is here twice
233         expectedFiles.add( "maven-metadata.xml" );
234         expectedFiles.add( "maven-metadata.xml.md5" );
235         expectedFiles.add( "maven-metadata.xml.sha1" );
236         
237         remoteRepo = new File( remoteRepo, "basic-deploy-test" );
238         
239         files = remoteRepo.listFiles();
240 
241         for (File file1 : files) {
242             addFileToList(file1, fileList);
243         }
244         
245         assertEquals( expectedFiles.size(), fileList.size() );
246 
247         assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );         
248     }
249 
250     public void testSkippingDeploy()
251         throws Exception
252     {
253         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
254 
255         DeployMojo mojo = (DeployMojo) lookupMojo( "deploy", testPom );
256 
257         assertNotNull( mojo );
258 
259         File file = new File( getBasedir(), "target/test-classes/unit/basic-deploy-test/target/"
260             + "deploy-test-file-1.0-SNAPSHOT.jar" );
261 
262         assertTrue( file.exists() );
263 
264         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
265 
266         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
267         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
268         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
269         setVariableValueToObject( mojo, "session", session );
270 
271         artifact = (DeployArtifactStub) project.getArtifact();
272 
273         String packaging = project.getPackaging();
274 
275         assertEquals( "jar", packaging );
276 
277         artifact.setFile( file );
278 
279         ArtifactRepositoryStub repo = getRepoStub( mojo );
280 
281         assertNotNull( repo );
282 
283         repo.setAppendToUrl( "basic-deploy-test" );
284 
285         assertEquals( "deploy-test", repo.getId() );
286         assertEquals( "deploy-test", repo.getKey() );
287         assertEquals( "file", repo.getProtocol() );
288         assertEquals( "file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl() );
289 
290         setVariableValueToObject( mojo, "skip", Boolean.TRUE.toString() );
291         
292         mojo.execute();
293 
294         File localRepo = new File( LOCAL_REPO, "" );
295 
296         File[] files = localRepo.listFiles();
297 
298         assertNull( files );
299        
300         remoteRepo = new File( remoteRepo, "basic-deploy-test" );
301 
302         files = remoteRepo.listFiles();
303 
304         assertNull( files );
305     }    
306     
307     public void testBasicDeployWithPackagingAsPom()
308         throws Exception
309     {
310         File testPom = new File( getBasedir(),
311                         "target/test-classes/unit/basic-deploy-pom/plugin-config.xml" );
312         
313         mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
314         
315         MockitoAnnotations.initMocks( this );
316         
317         assertNotNull( mojo );
318         
319         ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
320         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
321         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
322         repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) );
323         when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
324         when( session.getRepositorySession() ).thenReturn( repositorySession );
325         
326         File pomFile = new File( getBasedir(),
327                               "target/test-classes/unit/basic-deploy-pom/target/" +
328                               "deploy-test-file-1.0-SNAPSHOT.pom" );
329 
330         assertTrue( pomFile.exists() );
331         
332         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
333         project.setGroupId( "org.apache.maven.test" );
334         project.setArtifactId( "maven-deploy-test" );
335         project.setVersion( "1.0-SNAPSHOT" );
336 
337         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
338         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
339 
340         artifact = (DeployArtifactStub) project.getArtifact();
341         
342         artifact.setArtifactHandlerExtension( project.getPackaging() );
343         
344         artifact.setFile( pomFile );  
345         
346         ArtifactRepositoryStub repo = getRepoStub( mojo );
347         
348         repo.setAppendToUrl( "basic-deploy-pom" );
349         
350         mojo.execute();
351         
352         List<String> expectedFiles = new ArrayList<String>();
353         List<String> fileList = new ArrayList<String>();
354         
355         expectedFiles.add( "org" );
356         expectedFiles.add( "apache" );
357         expectedFiles.add( "maven" );
358         expectedFiles.add( "test" );
359         expectedFiles.add( "maven-deploy-test" );
360         expectedFiles.add( "1.0-SNAPSHOT" );
361         expectedFiles.add( "maven-metadata.xml" );
362         expectedFiles.add( "maven-metadata.xml.md5" );
363         expectedFiles.add( "maven-metadata.xml.sha1" );
364         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
365         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
366         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
367         // as we are in SNAPSHOT the file is here twice
368         expectedFiles.add( "maven-metadata.xml" );
369         expectedFiles.add( "maven-metadata.xml.md5" );
370         expectedFiles.add( "maven-metadata.xml.sha1" ); 
371         remoteRepo = new File( remoteRepo, "basic-deploy-pom" );
372         
373         File[] files = remoteRepo.listFiles();
374 
375         for (File file : files) {
376             addFileToList(file, fileList);
377         }
378         
379         assertEquals( expectedFiles.size(), fileList.size() );
380 
381         assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );    
382     }
383 
384     public void testDeployIfArtifactFileIsNull()
385         throws Exception
386     {
387         File testPom = new File( getBasedir(),
388                                  "target/test-classes/unit/basic-deploy-test/plugin-config.xml" );
389         
390         DeployMojo mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
391 
392         MockitoAnnotations.initMocks( this );
393 
394         ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
395         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
396         
397         setVariableValueToObject( mojo, "session", session );
398         
399         assertNotNull( mojo );
400         
401         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
402         project.setGroupId( "org.apache.maven.test" );
403         project.setArtifactId( "maven-deploy-test" );
404         project.setVersion( "1.0-SNAPSHOT" );
405 
406         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
407         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
408 
409         artifact = (DeployArtifactStub) project.getArtifact();
410         
411         artifact.setFile( null );
412         
413         assertNull( artifact.getFile() );
414         
415         try
416         {
417             mojo.execute();
418 
419             fail( "Did not throw mojo execution exception" );
420         }
421         catch( MojoExecutionException e )
422         {
423             //expected
424         }
425     }
426     
427     public void testDeployWithAttachedArtifacts()
428         throws Exception
429     {
430         File testPom = new File( getBasedir(),
431                                  "target/test-classes/unit/basic-deploy-with-attached-artifacts/" +
432                                  "plugin-config.xml" );
433 
434         mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
435         
436         MockitoAnnotations.initMocks( this );
437         
438         assertNotNull( mojo );
439         
440         ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
441         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
442         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
443         repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManagerFactory().newInstance( repositorySession, new LocalRepository( LOCAL_REPO ) ) );
444         when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
445         when( session.getRepositorySession() ).thenReturn( repositorySession );
446 
447         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
448         project.setGroupId( "org.apache.maven.test" );
449         project.setArtifactId( "maven-deploy-test" );
450         project.setVersion( "1.0-SNAPSHOT" );
451 
452         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
453         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
454 
455         artifact = (DeployArtifactStub) project.getArtifact();
456         
457         File file = new File( getBasedir(),
458                               "target/test-classes/unit/basic-deploy-with-attached-artifacts/target/" +
459                               "deploy-test-file-1.0-SNAPSHOT.jar" );
460         
461         artifact.setFile( file );
462         
463         ArtifactRepositoryStub repo = getRepoStub( mojo );
464         
465         repo.setAppendToUrl( "basic-deploy-with-attached-artifacts" );          
466         
467         mojo.execute();
468 
469         //check the artifacts in remote repository
470         List<String> expectedFiles = new ArrayList<String>();
471         List<String> fileList = new ArrayList<String>();
472         
473         expectedFiles.add( "org" );
474         expectedFiles.add( "apache" );
475         expectedFiles.add( "maven" );
476         expectedFiles.add( "test" );
477         expectedFiles.add( "maven-deploy-test" );
478         expectedFiles.add( "1.0-SNAPSHOT" );
479         expectedFiles.add( "maven-metadata.xml" );
480         expectedFiles.add( "maven-metadata.xml.md5" );
481         expectedFiles.add( "maven-metadata.xml.sha1" );
482         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
483         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.md5" );
484         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar.sha1" );
485         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
486         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.md5" );
487         expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom.sha1" );
488         // as we are in SNAPSHOT the file is here twice
489         expectedFiles.add( "maven-metadata.xml" );
490         expectedFiles.add( "maven-metadata.xml.md5" );
491         expectedFiles.add( "maven-metadata.xml.sha1" );         
492         expectedFiles.add( "attached-artifact-test-0" );
493         expectedFiles.add( "1.0-SNAPSHOT" );
494         expectedFiles.add( "maven-metadata.xml" );
495         expectedFiles.add( "maven-metadata.xml.md5" );
496         expectedFiles.add( "maven-metadata.xml.sha1" );
497         expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar" );
498         expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar.md5" );
499         expectedFiles.add( "attached-artifact-test-0-1.0-SNAPSHOT.jar.sha1" );
500         // as we are in SNAPSHOT the file is here twice
501         expectedFiles.add( "maven-metadata.xml" );
502         expectedFiles.add( "maven-metadata.xml.md5" );
503         expectedFiles.add( "maven-metadata.xml.sha1" );         
504         
505         remoteRepo = new File( remoteRepo, "basic-deploy-with-attached-artifacts" );
506         
507         File[] files = remoteRepo.listFiles();
508 
509         for (File file1 : files) {
510             addFileToList(file1, fileList);
511         }
512         
513         assertEquals( expectedFiles.size(), fileList.size() );
514 
515         assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );               
516     }
517     
518     @Ignore( "SCP is not part of Maven3 distribution. Aether handles transport extensions." )
519     public void _testBasicDeployWithScpAsProtocol()
520         throws Exception
521     {
522         String originalUserHome = System.getProperty( "user.home" );
523         
524         // FIX THE DAMN user.home BEFORE YOU DELETE IT!!!
525         File altHome = new File( getBasedir(), "target/ssh-user-home" );
526         altHome.mkdirs();
527         
528         System.out.println( "Testing user.home value for .ssh dir: " + altHome.getCanonicalPath() );
529         
530         Properties props = System.getProperties();
531         props.setProperty( "user.home", altHome.getCanonicalPath() );
532         
533         System.setProperties( props );
534         
535         File testPom = new File( getBasedir(),
536                                  "target/test-classes/unit/basic-deploy-scp/plugin-config.xml" );
537         
538         mojo = ( DeployMojo ) lookupMojo( "deploy", testPom );
539         
540         assertNotNull( mojo );
541         
542         RepositorySystem repositorySystem = mock( RepositorySystem.class );
543         
544         setVariableValueToObject( mojo, "repositorySystem", repositorySystem );
545         
546         File file = new File( getBasedir(),
547                               "target/test-classes/unit/basic-deploy-scp/target/" +
548                               "deploy-test-file-1.0-SNAPSHOT.jar" );
549 
550         assertTrue( file.exists() );
551         
552         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
553 
554         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
555         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
556 
557         artifact = (DeployArtifactStub) project.getArtifact();
558         
559         artifact.setFile( file );
560         
561         String altUserHome = System.getProperty( "user.home" );
562         
563         if ( altUserHome.equals( originalUserHome ) )
564         {
565             // this is *very* bad!
566             throw new IllegalStateException( "Setting 'user.home' system property to alternate value did NOT work. Aborting test." );
567         }
568         
569         File sshFile = new File( altUserHome, ".ssh" );
570         
571         System.out.println( "Testing .ssh dir: " + sshFile.getCanonicalPath() );
572         
573         //delete first the .ssh folder if existing before executing the mojo
574         if( sshFile.exists() )
575         {
576             FileUtils.deleteDirectory( sshFile );
577         }
578 
579         mojo.execute();
580             
581         assertTrue( sshFile.exists() );
582         
583         FileUtils.deleteDirectory( sshFile );
584     }
585 
586     public void testLegacyAltDeploymentRepositoryWithDefaultLayout()
587         throws Exception
588     {
589         DeployMojo mojo = new DeployMojo();
590 
591         setVariableValueToObject( mojo, "project", project );
592         setVariableValueToObject( mojo, "session", session );
593         setVariableValueToObject( mojo, "altDeploymentRepository",  "altDeploymentRepository::default::http://localhost" );
594 
595         project.setVersion( "1.0-SNAPSHOT" );
596 
597         assertEquals( new RemoteRepository.Builder( "altDeploymentRepository", "default", "http://localhost" ).build(),
598                 mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::default::http://localhost") );
599 
600     }
601 
602     public void testLegacyAltDeploymentRepositoryWithLegacyLayout()
603         throws Exception
604     {
605         DeployMojo mojo = new DeployMojo();
606 
607         setVariableValueToObject( mojo, "project", project );
608         setVariableValueToObject( mojo, "session", session );
609         setVariableValueToObject( mojo, "altDeploymentRepository",  "altDeploymentRepository::legacy::http://localhost" );
610 
611         project.setVersion( "1.0-SNAPSHOT" );
612         try
613         {
614             mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::legacy::http://localhost" );
615             fail( "Should throw: Invalid legacy syntax and layout for repository." );
616         }
617         catch( MojoFailureException e )
618         {
619             assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
620             assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::http://localhost\" instead, and only default layout is supported.");
621         }
622     }
623 
624     public void testInsaneAltDeploymentRepository()
625             throws Exception
626     {
627         DeployMojo mojo = new DeployMojo();
628 
629         setVariableValueToObject( mojo, "project", project );
630         setVariableValueToObject( mojo, "session", session );
631         setVariableValueToObject( mojo, "altDeploymentRepository",  "altDeploymentRepository::hey::wow::foo::http://localhost" );
632 
633         project.setVersion( "1.0-SNAPSHOT" );
634         try
635         {
636             mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::hey::wow::foo::http://localhost" );
637             fail( "Should throw: Invalid legacy syntax and layout for repository." );
638         }
639         catch( MojoFailureException e )
640         {
641             assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
642             assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::wow::foo::http://localhost\" instead, and only default layout is supported.");
643         }
644     }
645 
646     public void testDefaultScmSvnAltDeploymentRepository()
647             throws Exception
648     {
649         DeployMojo mojo = new DeployMojo();
650 
651         setVariableValueToObject( mojo, "project", project );
652         setVariableValueToObject( mojo, "session", session );
653         setVariableValueToObject( mojo, "altDeploymentRepository",  "altDeploymentRepository::default::scm:svn:http://localhost" );
654 
655         project.setVersion( "1.0-SNAPSHOT" );
656 
657         assertEquals( new RemoteRepository.Builder( "altDeploymentRepository", "default", "scm:svn:http://localhost" ).build(),
658                 mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::default::scm:svn:http://localhost" ) );
659     }
660     public void testLegacyScmSvnAltDeploymentRepository()
661             throws Exception
662     {
663         DeployMojo mojo = new DeployMojo();
664 
665         setVariableValueToObject( mojo, "project", project );
666         setVariableValueToObject( mojo, "altDeploymentRepository",  "altDeploymentRepository::legacy::scm:svn:http://localhost" );
667 
668         project.setVersion( "1.0-SNAPSHOT" );
669         try
670         {
671             mojo.getDeploymentRepository( project, null, null, "altDeploymentRepository::legacy::scm:svn:http://localhost" );
672             fail( "Should throw: Invalid legacy syntax and layout for repository." );
673         }
674         catch( MojoFailureException e )
675         {
676             assertEquals( e.getMessage(), "Invalid legacy syntax and layout for repository.");
677             assertEquals( e.getLongMessage(), "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::scm:svn:http://localhost\" instead, and only default layout is supported.");
678         }
679     }
680 
681     public void testAltSnapshotDeploymentRepository()
682         throws Exception
683     {
684         DeployMojo mojo = new DeployMojo();
685 
686         setVariableValueToObject( mojo, "project", project );
687         setVariableValueToObject( mojo, "session", session );
688         setVariableValueToObject( mojo, "altSnapshotDeploymentRepository",  "altSnapshotDeploymentRepository::http://localhost" );
689 
690         project.setVersion( "1.0-SNAPSHOT" );
691 
692         assertEquals( new RemoteRepository.Builder( "altSnapshotDeploymentRepository", "default", "http://localhost" ).build(),
693                       mojo.getDeploymentRepository( project, "altSnapshotDeploymentRepository::http://localhost", null, null ));
694     }
695 
696     public void testAltReleaseDeploymentRepository()
697         throws Exception
698     {
699         DeployMojo mojo = new DeployMojo();
700 
701         setVariableValueToObject( mojo, "project", project );
702         setVariableValueToObject( mojo, "session", session );
703         setVariableValueToObject( mojo, "altReleaseDeploymentRepository",  "altReleaseDeploymentRepository::http://localhost" );
704 
705         project.setVersion( "1.0" );
706 
707         assertEquals( new RemoteRepository.Builder("altReleaseDeploymentRepository", "default", "http://localhost").build(),
708                       mojo.getDeploymentRepository( project, null, "altReleaseDeploymentRepository::http://localhost", null ));
709     }
710     
711     private void addFileToList( File file, List<String> fileList )
712     {
713         if( !file.isDirectory() )
714         {
715             fileList.add( file.getName() );
716         }
717         else
718         {
719             fileList.add( file.getName() );
720 
721             File[] files = file.listFiles();
722 
723             for (File file1 : files) {
724                 addFileToList(file1, fileList);
725             }
726         }
727     }    
728     
729     private int getSizeOfExpectedFiles( List<String> fileList, List<String> expectedFiles )
730     {
731         for( String fileName : fileList )
732         {
733             // translate uniqueVersion to -SNAPSHOT 
734             fileName = fileName.replaceFirst( "-\\d{8}\\.\\d{6}-\\d+", "-SNAPSHOT" );
735             
736             if( !expectedFiles.remove( fileName ) )
737             {
738                 fail( fileName + " is not included in the expected files" );
739             }
740         }
741         return expectedFiles.size();
742     }    
743 
744     private ArtifactRepositoryStub getRepoStub( Object mojo )
745         throws Exception
746     {
747         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
748         return (ArtifactRepositoryStub) project.getDistributionManagementArtifactRepository();
749     }
750 
751 }