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