View Javadoc
1   package org.apache.maven.plugin.install;
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 org.apache.maven.artifact.metadata.ArtifactMetadata;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.install.stubs.AttachedArtifactStub0;
26  import org.apache.maven.plugin.install.stubs.InstallArtifactStub;
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.shared.utils.io.FileUtils;
30  
31  import java.io.File;
32  import java.util.Collections;
33  import java.util.List;
34  
35  /**
36   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
37   */
38  
39  public class InstallMojoTest
40      extends AbstractMojoTestCase
41  {
42  
43      InstallArtifactStub artifact;
44  
45      private final String LOCAL_REPO = "target/local-repo/";
46  
47      public void setUp()
48          throws Exception
49      {
50          super.setUp();
51  
52          System.out.println( ">>>Cleaning local repo " + getBasedir() + "/" + LOCAL_REPO + "..." );
53  
54          FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
55      }
56  
57      public void testInstallTestEnvironment()
58          throws Exception
59      {
60          File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
61  
62          InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
63  
64          assertNotNull( mojo );
65      }
66  
67      public void testBasicInstall()
68          throws Exception
69      {
70          File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
71  
72          InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
73  
74          assertNotNull( mojo );
75  
76          File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/"
77              + "maven-install-test-1.0-SNAPSHOT.jar" );
78  
79          MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
80          
81          setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
82          
83          artifact = (InstallArtifactStub) project.getArtifact();
84  
85          artifact.setFile( file );
86  
87          mojo.execute();
88  
89          String groupId = dotToSlashReplacer( artifact.getGroupId() );
90  
91          String packaging = getVariableValueFromObject( mojo, "packaging" ).toString();
92  
93          File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
94              artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging );
95  
96          assertTrue( installedArtifact.exists() );
97      }
98  
99      public void testBasicInstallWithAttachedArtifacts()
100         throws Exception
101     {
102         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test-with-attached-artifacts/"
103             + "plugin-config.xml" );
104 
105         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
106 
107         assertNotNull( mojo );
108 
109         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
110 
111         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
112 
113         List attachedArtifacts = project.getAttachedArtifacts();
114 
115         mojo.execute();
116 
117         String packaging = project.getPackaging();
118 
119         String groupId;
120 
121         for ( Object attachedArtifact1 : attachedArtifacts )
122         {
123             AttachedArtifactStub0 attachedArtifact = (AttachedArtifactStub0) attachedArtifact1;
124 
125             groupId = dotToSlashReplacer( attachedArtifact.getGroupId() );
126 
127             File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" +
128                 attachedArtifact.getArtifactId() + "/" + attachedArtifact.getVersion() + "/" +
129                 attachedArtifact.getArtifactId() + "-" + attachedArtifact.getVersion() + "." + packaging );
130 
131             assertTrue( installedArtifact.exists() );
132         }
133     }
134 
135     public void testUpdateReleaseParamSetToTrue()
136         throws Exception
137     {
138         File testPom = new File( getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml" );
139 
140         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
141 
142         assertNotNull( mojo );
143 
144         File file = new File( getBasedir(), "target/test-classes/unit/configured-install-test/target/"
145             + "maven-install-test-1.0-SNAPSHOT.jar" );
146 
147         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
148         
149         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
150 
151         artifact = (InstallArtifactStub) project.getArtifact();
152 
153         artifact.setFile( file );
154 
155         mojo.execute();
156 
157         assertTrue( artifact.isRelease() );
158     }
159 
160     public void testInstallIfArtifactFileIsNull()
161         throws Exception
162     {
163         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
164 
165         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
166 
167         assertNotNull( mojo );
168 
169         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
170 
171         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
172 
173         artifact = (InstallArtifactStub) project.getArtifact();
174 
175         artifact.setFile( null );
176 
177         assertNull( artifact.getFile() );
178 
179         try
180         {
181             mojo.execute();
182 
183             fail( "Did not throw mojo execution exception" );
184         }
185         catch ( MojoExecutionException e )
186         {
187             //expected
188         }
189     }
190 
191     public void testInstallIfPackagingIsPom()
192         throws Exception
193     {
194         File testPom = new File( getBasedir(),
195                                  "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml" );
196 
197         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
198 
199         assertNotNull( mojo );
200 
201         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
202 
203         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
204 
205         String packaging = project.getPackaging();
206 
207         assertEquals( "pom", packaging );
208 
209         artifact = (InstallArtifactStub) project.getArtifact();
210 
211         mojo.execute();
212 
213         String groupId = dotToSlashReplacer( artifact.getGroupId() );
214 
215         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
216             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "jar" );
217 
218         assertTrue( installedArtifact.exists() );
219     }
220 
221     public void testBasicInstallAndCreateChecksumIsTrue()
222         throws Exception
223     {
224         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml" );
225 
226         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
227 
228         assertNotNull( mojo );
229 
230         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar" );
231 
232         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
233 
234         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
235 
236         artifact = (InstallArtifactStub) project.getArtifact();
237 
238         boolean createChecksum = (Boolean) getVariableValueFromObject( mojo, "createChecksum" );
239 
240         assertTrue( createChecksum );
241 
242         artifact.setFile( file );
243 
244         mojo.execute();
245 
246         ArtifactMetadata metadata = null;
247         for ( Object o : artifact.getMetadataList() )
248         {
249             metadata = (ArtifactMetadata) o;
250             if ( metadata.getRemoteFilename().endsWith( "pom" ) )
251             {
252                 break;
253             }
254         }
255 
256         ArtifactRepository localRepo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
257 
258         File pom = new File( localRepo.getBasedir(), localRepo.pathOfLocalRepositoryMetadata( metadata, localRepo ) );
259 
260         assertTrue( pom.exists() );
261 
262         //get the actual checksum of the pom
263         mojo.digester.calculate( pom );
264         String actualPomMd5Sum = mojo.digester.getMd5();
265         String actualPomSha1Sum = mojo.digester.getSha1();
266 
267         //get the actual checksum of the artifact
268         mojo.digester.calculate( file );
269         String actualMd5Sum = mojo.digester.getMd5();
270         String actualSha1Sum = mojo.digester.getSha1();
271 
272         String groupId = dotToSlashReplacer( artifact.getGroupId() );
273 
274         String packaging = project.getPackaging();
275 
276         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
277             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion();
278 
279         File installedArtifact = new File( localPath + "." + packaging );
280 
281         File pomMd5 = new File( localPath + ".pom.md5" );
282         File pomSha1 = new File( localPath + ".pom.sha1" );
283 
284         File md5 = new File( localPath + "." + packaging + ".md5" );
285         File sha1 = new File( localPath + "." + packaging + ".sha1" );
286 
287         assertTrue( pomMd5.exists() );
288         assertTrue( pomSha1.exists() );
289         assertTrue( md5.exists() );
290         assertTrue( sha1.exists() );
291 
292         String generatedMd5 = FileUtils.fileRead( md5, "UTF-8" );
293         String generatedSha1 = FileUtils.fileRead( sha1, "UTF-8" );
294         String generatedPomMd5 = FileUtils.fileRead( pomMd5, "UTF-8" );
295         String generatedPomSha1 = FileUtils.fileRead( pomSha1, "UTF-8" );
296 
297         assertEquals( actualMd5Sum, generatedMd5 );
298         assertEquals( actualSha1Sum, generatedSha1 );
299         assertEquals( actualPomMd5Sum, generatedPomMd5 );
300         assertEquals( actualPomSha1Sum, generatedPomSha1 );
301 
302         assertTrue( installedArtifact.exists() );
303     }
304 
305     public void testSkip()
306         throws Exception
307     {
308         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
309 
310         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
311 
312         assertNotNull( mojo );
313 
314         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/"
315             + "maven-install-test-1.0-SNAPSHOT.jar" );
316 
317         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
318 
319         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
320 
321         artifact = (InstallArtifactStub) project.getArtifact();
322 
323         artifact.setFile( file );
324 
325         mojo.setSkip( true );
326 
327         mojo.execute();
328 
329         String groupId = dotToSlashReplacer( artifact.getGroupId() );
330 
331         String packaging = project.getPackaging();
332 
333         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
334             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging );
335 
336         assertFalse( installedArtifact.exists() );
337     }
338 
339 
340     private String dotToSlashReplacer( String parameter )
341     {
342         return parameter.replace( '.', '/' );
343     }
344 }