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.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
32  import org.apache.maven.project.ProjectBuildingRequest;
33  import org.apache.maven.repository.internal.MavenRepositorySystemSession;
34  import org.mockito.InjectMocks;
35  import org.mockito.Mock;
36  import org.mockito.MockitoAnnotations;
37  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
38  
39  /**
40   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
41   */
42  public class DeployFileMojoTest
43      extends AbstractMojoTestCase
44  {
45      private String LOCAL_REPO = getBasedir() + "/target/local-repo";
46      
47      private List<String> expectedFiles;
48  
49      private List<String> fileList;
50  
51      private File remoteRepo;
52  
53      @Mock
54      private MavenSession session;
55      
56      @InjectMocks
57      private DeployFileMojo mojo;
58      
59      public void setUp()
60          throws Exception
61      {
62          super.setUp();
63  
64          remoteRepo = new File( getBasedir(), "target/remote-repo" );
65  
66          if ( !remoteRepo.exists() )
67          {
68              remoteRepo.mkdirs();
69          }
70      }
71  
72      public void testDeployTestEnvironment()
73          throws Exception
74      {
75          File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml" );
76  
77          AbstractDeployMojo mojo = (AbstractDeployMojo) lookupMojo( "deploy-file", testPom );
78  
79          assertNotNull( mojo );
80      }
81  
82      public void testBasicDeployFile()
83          throws Exception
84      {
85          File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml" );
86  
87          mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
88  
89          MockitoAnnotations.initMocks( this );
90          
91          assertNotNull( mojo );
92          
93          ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
94          when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
95          MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
96          repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( LOCAL_REPO ) );
97          when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
98          
99          String groupId = (String) getVariableValueFromObject( mojo, "groupId" );
100 
101         String artifactId = (String) getVariableValueFromObject( mojo, "artifactId" );
102 
103         String version = (String) getVariableValueFromObject( mojo, "version" );
104 
105         String packaging = (String) getVariableValueFromObject( mojo, "packaging" );
106 
107         File file = (File) getVariableValueFromObject( mojo, "file" );
108 
109         String repositoryId = (String) getVariableValueFromObject( mojo, "repositoryId" );
110 
111         String url = (String) getVariableValueFromObject( mojo, "url" );
112 
113         assertEquals( "org.apache.maven.test", groupId );
114 
115         assertEquals( "maven-deploy-file-test", artifactId );
116 
117         assertEquals( "1.0", version );
118 
119         assertEquals( "jar", packaging );
120 
121         assertTrue( file.exists() );
122 
123         assertEquals( "deploy-test", repositoryId );
124 
125         assertEquals( "file://" + getBasedir() + "/target/remote-repo/deploy-file-test", url );
126         
127         mojo.execute();
128 
129         //check the generated pom
130         File pom = new File( remoteRepo, "deploy-file-test/" + groupId.replace( '.', '/' ) +
131                                           "/" + artifactId + "/" + version + "/" + artifactId +
132                                           "-" + version + ".pom" );
133 
134         assertTrue( pom.exists() );
135 
136         Model model = mojo.readModel( pom );
137 
138         assertEquals( "4.0.0", model.getModelVersion() );
139 
140         assertEquals( groupId, model.getGroupId() );
141 
142         assertEquals( artifactId, model.getArtifactId() );
143 
144         assertEquals( version, model.getVersion() );
145 
146         assertEquals( packaging, model.getPackaging() );
147 
148         assertEquals( "POM was created from deploy:deploy-file", model.getDescription() );
149 
150         //check the remote-repo
151         expectedFiles = new ArrayList<String>();
152         fileList = new ArrayList<String>();
153 
154         File repo = new File( remoteRepo, "deploy-file-test" );
155 
156         File[] files = repo.listFiles();
157 
158         for (File file1 : files) {
159             addFileToList(file1, fileList);
160         }
161 
162         expectedFiles.add( "org" );
163         expectedFiles.add( "apache" );
164         expectedFiles.add( "maven" );
165         expectedFiles.add( "test" );
166         expectedFiles.add( "maven-deploy-file-test" );
167         expectedFiles.add( "1.0" );
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-file-test-1.0.jar" );
172         expectedFiles.add( "maven-deploy-file-test-1.0.jar.md5" );
173         expectedFiles.add( "maven-deploy-file-test-1.0.jar.sha1" );
174         expectedFiles.add( "maven-deploy-file-test-1.0.pom" );
175         expectedFiles.add( "maven-deploy-file-test-1.0.pom.md5" );
176         expectedFiles.add( "maven-deploy-file-test-1.0.pom.sha1" );
177 
178         assertEquals( expectedFiles.size(), fileList.size() );
179 
180         assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
181     }
182 
183     public void testDeployIfClassifierIsSet()
184         throws Exception
185     {
186         File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-classifier/plugin-config.xml" );
187 
188         mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
189 
190         MockitoAnnotations.initMocks( this );
191         
192         assertNotNull( mojo );
193         
194         ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
195         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
196         MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
197         repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( LOCAL_REPO ) );
198         when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
199 
200         String classifier = ( String ) getVariableValueFromObject( mojo, "classifier" );
201 
202         String groupId = ( String ) getVariableValueFromObject( mojo, "groupId" );
203 
204         String artifactId = ( String ) getVariableValueFromObject( mojo, "artifactId" );
205 
206         String version = ( String ) getVariableValueFromObject( mojo, "version" );
207 
208         assertEquals( "bin", classifier );
209 
210         mojo.execute();
211 
212         File deployedArtifact = new File( remoteRepo, "deploy-file-classifier/" + groupId.replace( '.', '/' ) +
213                                           "/" + artifactId + "/" + version + "/" + artifactId +
214                                           "-" + version + "-" + classifier + ".jar");
215 
216         assertTrue( deployedArtifact.exists() );
217 
218         mojo.setClassifier( "prod" );
219 
220         assertEquals( "prod", mojo.getClassifier() );
221 
222         mojo.execute();
223 
224         File prodDeployedArtifact = new File( remoteRepo, "deploy-file-classifier/" + groupId.replace( '.', '/' ) +
225                                           "/" + artifactId + "/" + version + "/" + artifactId +
226                                           "-" + version + "-" + mojo.getClassifier() + ".jar");
227 
228         assertTrue( prodDeployedArtifact.exists() );
229     }
230 
231     public void testDeployIfArtifactIsNotJar()
232         throws Exception
233     {
234         File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-artifact-not-jar/plugin-config.xml" );
235 
236         mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
237 
238         MockitoAnnotations.initMocks( this );
239         
240         assertNotNull( mojo );
241         
242         ProjectBuildingRequest buildingRequest = mock ( ProjectBuildingRequest.class );
243         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
244         MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
245         repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( LOCAL_REPO ) );
246         when( buildingRequest.getRepositorySession() ).thenReturn( repositorySession );
247 
248         String groupId = (String) getVariableValueFromObject( mojo, "groupId" );
249 
250         String artifactId = (String) getVariableValueFromObject( mojo, "artifactId" );
251 
252         String version = (String) getVariableValueFromObject( mojo, "version" );
253 
254         assertEquals( "org.apache.maven.test", groupId );
255 
256         assertEquals( "maven-deploy-file-test", artifactId );
257 
258         assertEquals( "1.0", version );
259 
260         mojo.execute();
261 
262         File file = new File( remoteRepo, "deploy-file-artifact-not-jar/" + groupId.replace( '.', '/' ) +
263                                           "/" + artifactId + "/" + version + "/" + artifactId +
264                                           "-" + version + ".zip");
265 
266         assertTrue( file.exists() );
267     }
268 
269     private void addFileToList( File file, List<String> fileList )
270     {
271         if ( !file.isDirectory() )
272         {
273             fileList.add( file.getName() );
274         }
275         else
276         {
277             fileList.add( file.getName() );
278 
279             File[] files = file.listFiles();
280 
281             for (File file1 : files) {
282                 addFileToList(file1, fileList);
283             }
284         }
285     }
286 
287     private int getSizeOfExpectedFiles( List<String> fileList, List<String> expectedFiles )
288     {
289         for ( String fileName : fileList )
290         {
291             if ( expectedFiles.contains( fileName ) )
292             {
293                 expectedFiles.remove( fileName );
294             }
295             else
296             {
297                 fail( fileName + " is not included in the expected files" );
298             }
299         }
300         return expectedFiles.size();
301     }
302 
303 }
304