View Javadoc

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