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