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.List;
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
29  import org.apache.maven.model.Model;
30  
31  /**
32   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
33   */
34  public class DeployFileMojoTest
35      extends AbstractMojoTestCase
36  {
37      private List expectedFiles;
38  
39      private List fileList;
40  
41      private File remoteRepo;
42  
43      MavenProjectStub projectStub = new MavenProjectStub();
44      
45      public void setUp()
46          throws Exception
47      {
48          super.setUp();
49  
50          remoteRepo = new File( getBasedir(), "target/remote-repo" );
51  
52          if ( !remoteRepo.exists() )
53          {
54              remoteRepo.mkdirs();
55          }
56          
57          projectStub.setAttachedArtifacts( new ArrayList() );
58      }
59  
60      public void testDeployTestEnvironment()
61          throws Exception
62      {
63          File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml" );
64  
65          DeployFileMojo mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
66  
67          assertNotNull( mojo );
68      }
69  
70      public void testBasicDeployFile()
71          throws Exception
72      {
73          File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-test/plugin-config.xml" );
74  
75          DeployFileMojo mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
76  
77          assertNotNull( mojo );
78          
79          setVariableValueToObject( mojo, "project", projectStub );
80  
81          String groupId = (String) getVariableValueFromObject( mojo, "groupId" );
82  
83          String artifactId = (String) getVariableValueFromObject( mojo, "artifactId" );
84  
85          String version = (String) getVariableValueFromObject( mojo, "version" );
86  
87          String packaging = (String) getVariableValueFromObject( mojo, "packaging" );
88  
89          File file = (File) getVariableValueFromObject( mojo, "file" );
90  
91          String repositoryId = (String) getVariableValueFromObject( mojo, "repositoryId" );
92  
93          String url = (String) getVariableValueFromObject( mojo, "url" );
94  
95          assertEquals( "org.apache.maven.test", groupId );
96  
97          assertEquals( "maven-deploy-file-test", artifactId );
98  
99          assertEquals( "1.0", version );
100 
101         assertEquals( "jar", packaging );
102 
103         assertTrue( file.exists() );
104 
105         assertEquals( "deploy-test", repositoryId );
106 
107         assertEquals( "file://" + getBasedir() + "/target/remote-repo/deploy-file-test", url );
108         
109         mojo.execute();
110 
111         //check the generated pom
112         File pom = new File( remoteRepo, "deploy-file-test/" + groupId.replace( '.', '/' ) +
113                                           "/" + artifactId + "/" + version + "/" + artifactId +
114                                           "-" + version + ".pom" );
115 
116         assertTrue( pom.exists() );
117 
118         Model model = mojo.readModel( pom );
119 
120         assertEquals( "4.0.0", model.getModelVersion() );
121 
122         assertEquals( groupId, model.getGroupId() );
123 
124         assertEquals( artifactId, model.getArtifactId() );
125 
126         assertEquals( version, model.getVersion() );
127 
128         assertEquals( packaging, model.getPackaging() );
129 
130         assertEquals( "POM was created from deploy:deploy-file", model.getDescription() );
131 
132         //check the remote-repo
133         expectedFiles = new ArrayList();
134         fileList = new ArrayList();
135 
136         File repo = new File( remoteRepo, "deploy-file-test" );
137 
138         File[] files = repo.listFiles();
139 
140         for ( int i = 0; i < files.length; i++ )
141         {
142             addFileToList( files[i], fileList );
143         }
144 
145         expectedFiles.add( "org" );
146         expectedFiles.add( "apache" );
147         expectedFiles.add( "maven" );
148         expectedFiles.add( "test" );
149         expectedFiles.add( "maven-deploy-file-test" );
150         expectedFiles.add( "1.0" );
151         expectedFiles.add( "maven-metadata.xml" );
152         expectedFiles.add( "maven-metadata.xml.md5" );
153         expectedFiles.add( "maven-metadata.xml.sha1" );
154         expectedFiles.add( "maven-deploy-file-test-1.0.jar" );
155         expectedFiles.add( "maven-deploy-file-test-1.0.jar.md5" );
156         expectedFiles.add( "maven-deploy-file-test-1.0.jar.sha1" );
157         expectedFiles.add( "maven-deploy-file-test-1.0.pom" );
158         expectedFiles.add( "maven-deploy-file-test-1.0.pom.md5" );
159         expectedFiles.add( "maven-deploy-file-test-1.0.pom.sha1" );
160 
161         assertEquals( expectedFiles.size(), fileList.size() );
162 
163         assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
164     }
165 
166     public void testDeployIfPomFileParamIsSet()
167         throws Exception
168     {
169         File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-pom-file/plugin-config.xml" );
170 
171         DeployFileMojo mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
172 
173         assertNotNull( mojo );
174 
175         setVariableValueToObject( mojo, "project", projectStub );
176 
177         File pomFile = ( File ) getVariableValueFromObject( mojo, "pomFile" );
178 
179         assertNotNull( pomFile );
180 
181         mojo.execute();
182 
183         assertTrue( pomFile.exists() );
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         DeployFileMojo mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
192 
193         assertNotNull( mojo );
194 
195         setVariableValueToObject( mojo, "project", projectStub );
196 
197         String classifier = ( String ) getVariableValueFromObject( mojo, "classifier" );
198 
199         String groupId = ( String ) getVariableValueFromObject( mojo, "groupId" );
200 
201         String artifactId = ( String ) getVariableValueFromObject( mojo, "artifactId" );
202 
203         String version = ( String ) getVariableValueFromObject( mojo, "version" );
204 
205         assertEquals( "bin", classifier );
206 
207         mojo.execute();
208 
209         File deployedArtifact = new File( remoteRepo, "deploy-file-classifier/" + groupId.replace( '.', '/' ) +
210                                           "/" + artifactId + "/" + version + "/" + artifactId +
211                                           "-" + version + "-" + classifier + ".jar");
212 
213         assertTrue( deployedArtifact.exists() );
214 
215         mojo.setClassifier( "prod" );
216 
217         assertEquals( "prod", mojo.getClassifier() );
218 
219         mojo.execute();
220 
221         File prodDeployedArtifact = new File( remoteRepo, "deploy-file-classifier/" + groupId.replace( '.', '/' ) +
222                                           "/" + artifactId + "/" + version + "/" + artifactId +
223                                           "-" + version + "-" + mojo.getClassifier() + ".jar");
224 
225         assertTrue( prodDeployedArtifact.exists() );
226     }
227 
228     public void testDeployIfArtifactIsNotJar()
229         throws Exception
230     {
231         File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-artifact-not-jar/plugin-config.xml" );
232 
233         DeployFileMojo mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
234 
235         assertNotNull( mojo );
236 
237         setVariableValueToObject( mojo, "project", projectStub );
238 
239         String groupId = (String) getVariableValueFromObject( mojo, "groupId" );
240 
241         String artifactId = (String) getVariableValueFromObject( mojo, "artifactId" );
242 
243         String version = (String) getVariableValueFromObject( mojo, "version" );
244 
245         String packaging = (String) getVariableValueFromObject( mojo, "packaging" );
246 
247         assertEquals( "org.apache.maven.test", groupId );
248 
249         assertEquals( "maven-deploy-file-test", artifactId );
250 
251         assertEquals( "1.0", version );
252 
253         assertEquals( "zip", packaging );
254 
255         mojo.execute();
256 
257         File file = new File( remoteRepo, "deploy-file-artifact-not-jar/" + groupId.replace( '.', '/' ) +
258                                           "/" + artifactId + "/" + version + "/" + artifactId +
259                                           "-" + version + ".zip");
260 
261         assertTrue( file.exists() );
262     }
263 
264     public void testDeployIfRepositoryLayoutIsLegacy()
265         throws Exception
266     {
267         File testPom = new File( getBasedir(), "target/test-classes/unit/deploy-file-legacy-repository-layout/plugin-config.xml" );
268 
269         DeployFileMojo mojo = (DeployFileMojo) lookupMojo( "deploy-file", testPom );
270 
271         assertNotNull( mojo );
272 
273         setVariableValueToObject( mojo, "project", projectStub );
274 
275         String repositoryLayout = (String) getVariableValueFromObject(  mojo, "repositoryLayout" );
276 
277         String groupId = (String) getVariableValueFromObject( mojo, "groupId" );
278 
279         String artifactId = (String) getVariableValueFromObject( mojo, "artifactId" );
280 
281         String version = (String) getVariableValueFromObject( mojo, "version" );
282 
283         assertEquals( "legacy", repositoryLayout );
284 
285         mojo.execute();
286 
287         File artifactFile = new File( remoteRepo, "deploy-file-legacy-repository-layout/" + groupId + "/jars/" + artifactId + "-" + version + ".jar" );
288 
289         assertTrue( artifactFile.exists() );
290 
291         //check the remote-repo
292         expectedFiles = new ArrayList();
293         fileList = new ArrayList();
294 
295         File repo = new File( remoteRepo, "deploy-file-legacy-repository-layout" );
296 
297         File[] files = repo.listFiles();
298 
299         for ( int i = 0; i < files.length; i++ )
300         {
301             addFileToList( files[i], fileList );
302         }
303 
304         expectedFiles.add( "org.apache.maven.test" );
305         expectedFiles.add( "jars" );
306         expectedFiles.add( "maven-deploy-file-test-1.0.jar" );
307         expectedFiles.add( "maven-deploy-file-test-1.0.jar.md5" );
308         expectedFiles.add( "maven-deploy-file-test-1.0.jar.sha1" );
309         expectedFiles.add( "poms" );
310         expectedFiles.add( "maven-deploy-file-test-1.0.pom" );
311         expectedFiles.add( "maven-deploy-file-test-1.0.pom.md5" );
312         expectedFiles.add( "maven-deploy-file-test-1.0.pom.sha1" );
313         expectedFiles.add( "maven-metadata.xml" );
314         expectedFiles.add( "maven-metadata.xml.md5" );
315         expectedFiles.add( "maven-metadata.xml.sha1" );
316 
317         assertEquals( expectedFiles.size(), fileList.size() );
318 
319         assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) );
320     }
321 
322     private void addFileToList( File file, List fileList )
323     {
324         if ( !file.isDirectory() )
325         {
326             fileList.add( file.getName() );
327         }
328         else
329         {
330             fileList.add( file.getName() );
331 
332             File[] files = file.listFiles();
333 
334             for ( int i = 0; i < files.length; i++ )
335             {
336                 addFileToList( files[i], fileList );
337             }
338         }
339     }
340 
341     private int getSizeOfExpectedFiles( List fileList, List expectedFiles )
342     {
343         for ( Iterator iter = fileList.iterator(); iter.hasNext(); )
344         {
345             String fileName = (String) iter.next();
346 
347             if ( expectedFiles.contains( fileName ) )
348             {
349                 expectedFiles.remove( fileName );
350             }
351             else
352             {
353                 fail( fileName + " is not included in the expected files" );
354             }
355         }
356         return expectedFiles.size();
357     }
358 
359 }
360