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