View Javadoc
1   package org.apache.maven.plugins.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 static org.mockito.ArgumentMatchers.any;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.File;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.artifact.metadata.ArtifactMetadata;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.plugin.AbstractMojo;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.apache.maven.plugin.descriptor.PluginDescriptor;
38  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
39  import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0;
40  import org.apache.maven.plugins.install.stubs.InstallArtifactStub;
41  import org.apache.maven.project.DefaultProjectBuildingRequest;
42  import org.apache.maven.project.MavenProject;
43  import org.apache.maven.project.ProjectBuildingRequest;
44  import org.codehaus.plexus.util.FileUtils;
45  import org.eclipse.aether.DefaultRepositorySystemSession;
46  import org.eclipse.aether.artifact.DefaultArtifact;
47  import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
48  import org.eclipse.aether.repository.LocalRepository;
49  import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
50  
51  /**
52   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
53   */
54  
55  public class InstallMojoTest
56      extends AbstractMojoTestCase
57  {
58  
59      InstallArtifactStub artifact;
60  
61      private final String LOCAL_REPO = "target/local-repo/";
62  
63      public void setUp()
64          throws Exception
65      {
66          super.setUp();
67  
68          FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
69      }
70  
71      public void testInstallTestEnvironment()
72          throws Exception
73      {
74          File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
75  
76          AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom );
77  
78          assertNotNull( mojo );
79      }
80  
81      public void testBasicInstall()
82          throws Exception
83      {
84          File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
85  
86          AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom );
87  
88          assertNotNull( mojo );
89  
90          File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/"
91              + "maven-install-test-1.0-SNAPSHOT.jar" );
92  
93          MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
94          updateMavenProject( project );
95  
96          setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
97          setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
98          setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
99          setVariableValueToObject( mojo, "session", createMavenSession() );
100 
101         artifact = (InstallArtifactStub) project.getArtifact();
102 
103         artifact.setFile( file );
104 
105         mojo.execute();
106 
107         String groupId = dotToSlashReplacer( artifact.getGroupId() );
108 
109         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
110             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + artifact.getArtifactHandler().getExtension() );
111 
112         assertTrue( installedArtifact.exists() );
113         
114         assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
115     }
116 
117     public void testBasicInstallWithAttachedArtifacts()
118         throws Exception
119     {
120         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test-with-attached-artifacts/"
121             + "plugin-config.xml" );
122 
123         AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom );
124 
125         assertNotNull( mojo );
126 
127         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
128         updateMavenProject( project );
129 
130         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
131         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
132         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
133         setVariableValueToObject( mojo, "session", createMavenSession() );
134 
135         List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
136 
137         mojo.execute();
138 
139         String packaging = project.getPackaging();
140 
141         String groupId;
142 
143         for ( Object attachedArtifact1 : attachedArtifacts )
144         {
145             AttachedArtifactStub0 attachedArtifact = (AttachedArtifactStub0) attachedArtifact1;
146 
147             groupId = dotToSlashReplacer( attachedArtifact.getGroupId() );
148 
149             File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" +
150                 attachedArtifact.getArtifactId() + "/" + attachedArtifact.getVersion() + "/" +
151                 attachedArtifact.getArtifactId() + "-" + attachedArtifact.getVersion() + "." + packaging );
152 
153             assertTrue( installedArtifact.getPath() + " does not exist", installedArtifact.exists() );
154         }
155         
156         assertEquals( 13, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
157     }
158 
159     public void testUpdateReleaseParamSetToTrue()
160         throws Exception
161     {
162         File testPom = new File( getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml" );
163 
164         AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom );
165 
166         assertNotNull( mojo );
167 
168         File file = new File( getBasedir(), "target/test-classes/unit/configured-install-test/target/"
169             + "maven-install-test-1.0-SNAPSHOT.jar" );
170 
171         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
172         updateMavenProject( project );
173 
174         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
175         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
176         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
177         setVariableValueToObject( mojo, "session", createMavenSession() );
178 
179         artifact = (InstallArtifactStub) project.getArtifact();
180 
181         artifact.setFile( file );
182 
183         mojo.execute();
184 
185 //        assertTrue( artifact.isRelease() );
186         
187         assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
188     }
189 
190     public void testInstallIfArtifactFileIsNull()
191         throws Exception
192     {
193         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
194 
195         AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom );
196 
197         assertNotNull( mojo );
198 
199         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
200         updateMavenProject( project );
201 
202         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
203         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
204         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
205         setVariableValueToObject( mojo, "session", createMavenSession() );
206 
207         artifact = (InstallArtifactStub) project.getArtifact();
208 
209         artifact.setFile( null );
210 
211         assertNull( artifact.getFile() );
212 
213         try
214         {
215             mojo.execute();
216 
217             fail( "Did not throw mojo execution exception" );
218         }
219         catch ( MojoFailureException e )
220         {
221             //expected
222         }
223         
224         assertFalse( new File( LOCAL_REPO ).exists() );
225     }
226 
227     public void testInstallIfPackagingIsPom()
228         throws Exception
229     {
230         File testPom = new File( getBasedir(),
231                                  "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml" );
232 
233         AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom );
234 
235         assertNotNull( mojo );
236 
237         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
238         updateMavenProject( project );
239 
240         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
241         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
242         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
243         setVariableValueToObject( mojo, "session", createMavenSession() );
244 
245         String packaging = project.getPackaging();
246 
247         assertEquals( "pom", packaging );
248 
249         artifact = (InstallArtifactStub) project.getArtifact();
250 
251         mojo.execute();
252 
253         String groupId = dotToSlashReplacer( artifact.getGroupId() );
254 
255         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
256             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "pom" );
257 
258         assertTrue( installedArtifact.exists() );
259         
260         assertEquals( 4, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
261     }
262 
263     public void testBasicInstallAndCreate()
264         throws Exception
265     {
266         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml" );
267 
268         AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom );
269 
270         assertNotNull( mojo );
271 
272         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar" );
273 
274         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
275         MavenSession mavenSession = createMavenSession();
276         updateMavenProject( project );
277 
278         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
279         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
280         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
281         setVariableValueToObject( mojo, "session", mavenSession );
282 
283         artifact = (InstallArtifactStub) project.getArtifact();
284 
285         artifact.setFile( file );
286 
287         mojo.execute();
288 
289         ArtifactMetadata metadata = null;
290         for ( Object o : artifact.getMetadataList() )
291         {
292             metadata = (ArtifactMetadata) o;
293             if ( metadata.getRemoteFilename().endsWith( "pom" ) )
294             {
295                 break;
296             }
297         }
298 
299         File pom = new File( new File( LOCAL_REPO ), mavenSession.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact( new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion() ) ) );
300 
301         assertTrue( pom.exists() );
302 
303         String groupId = dotToSlashReplacer( artifact.getGroupId() );
304         String packaging = project.getPackaging();
305         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
306                         artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion();
307         
308 
309         File installedArtifact = new File( localPath + "." + packaging );
310 
311         assertTrue( installedArtifact.exists() );
312         
313         assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
314     }
315 
316     public void testSkip()
317         throws Exception
318     {
319         File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" );
320 
321         InstallMojo mojo = (InstallMojo) lookupMojo( "install", testPom );
322 
323         assertNotNull( mojo );
324 
325         File file = new File( getBasedir(), "target/test-classes/unit/basic-install-test/target/"
326             + "maven-install-test-1.0-SNAPSHOT.jar" );
327 
328         MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );
329         updateMavenProject( project );
330 
331         setVariableValueToObject( mojo, "pluginContext", new ConcurrentHashMap<>() );
332         setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() );
333         setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) );
334         setVariableValueToObject( mojo, "session", createMavenSession() );
335         setVariableValueToObject( mojo, "skip", Boolean.TRUE );
336 
337         artifact = (InstallArtifactStub) project.getArtifact();
338 
339         artifact.setFile( file );
340 
341         mojo.execute();
342 
343         String groupId = dotToSlashReplacer( artifact.getGroupId() );
344 
345         String packaging = project.getPackaging();
346 
347         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" +
348             artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging );
349 
350         assertFalse( installedArtifact.exists() );
351         
352         assertFalse( new File( LOCAL_REPO ).exists() );
353     }
354 
355 
356     private String dotToSlashReplacer( String parameter )
357     {
358         return parameter.replace( '.', '/' );
359     }
360     
361     private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
362     {
363         MavenSession session = mock( MavenSession.class );
364         DefaultRepositorySystemSession repositorySession  = new DefaultRepositorySystemSession();
365         repositorySession.setLocalRepositoryManager(
366                 new EnhancedLocalRepositoryManagerFactory().newInstance(
367                         repositorySession, new LocalRepository( LOCAL_REPO )
368                 )
369         );
370         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
371         buildingRequest.setRepositorySession( repositorySession );
372         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
373         when( session.getRepositorySession() ).thenReturn( repositorySession );
374         when( session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class)))
375             .thenReturn( new ConcurrentHashMap<String, Object>() );
376         return session;
377     }
378     
379     private void updateMavenProject( MavenProject project )
380     {
381        project.setGroupId( project.getArtifact().getGroupId() );
382        project.setArtifactId( project.getArtifact().getArtifactId() );
383        project.setVersion( project.getArtifact().getVersion() );
384     }
385 }