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 java.io.File;
23  import java.io.Reader;
24  
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.model.Model;
27  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  import org.apache.maven.project.DefaultProjectBuildingRequest;
30  import org.apache.maven.project.ProjectBuildingRequest;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.xml.XmlStreamReader;
33  import org.eclipse.aether.DefaultRepositorySystemSession;
34  import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
35  import org.eclipse.aether.repository.LocalRepository;
36  import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
37  
38  import static org.mockito.Mockito.mock;
39  import static org.mockito.Mockito.when;
40  
41  /**
42   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
43   */
44  public class InstallFileMojoTest
45      extends AbstractMojoTestCase
46  {
47      private String groupId;
48  
49      private String artifactId;
50  
51      private String version;
52  
53      private String packaging;
54  
55      private String classifier;
56  
57      private File file;
58  
59      private final String LOCAL_REPO = "target/local-repo/";
60  
61      public void setUp()
62          throws Exception
63      {
64          super.setUp();
65  
66          FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
67      }
68  
69      public void testInstallFileTestEnvironment()
70          throws Exception
71      {
72          File testPom = new File( getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml" );
73  
74          InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
75          
76          setVariableValueToObject( mojo, "session", createMavenSession() );
77  
78          assertNotNull( mojo );
79      }
80  
81      public void testBasicInstallFile()
82          throws Exception
83      {
84          File testPom = new File( getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml" );
85  
86          InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
87  
88          assertNotNull( mojo );
89          
90          setVariableValueToObject( mojo, "session", createMavenSession() );
91  
92          assignValuesForParameter( mojo );
93  
94          mojo.execute();
95          
96          File pomFile = (File) getVariableValueFromObject( mojo, "pomFile" );
97          org.codehaus.plexus.util.FileUtils.forceDelete( pomFile );
98  
99          File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
100             artifactId + "-" + version + "." + packaging );
101 
102         assertTrue( installedArtifact.exists() );
103         
104         assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
105     }
106 
107     public void testInstallFileWithClassifier()
108         throws Exception
109     {
110         File testPom =
111             new File( getBasedir(), "target/test-classes/unit/install-file-with-classifier/plugin-config.xml" );
112 
113         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
114 
115         assertNotNull( mojo );
116         
117         setVariableValueToObject( mojo, "session", createMavenSession() );
118 
119         assignValuesForParameter( mojo );
120 
121         assertNotNull( classifier );
122 
123         mojo.execute();
124 
125         File pomFile = (File) getVariableValueFromObject( mojo, "pomFile" );
126         org.codehaus.plexus.util.FileUtils.forceDelete( pomFile );
127 
128         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
129             artifactId + "-" + version + "-" + classifier + "." + packaging );
130 
131         assertTrue( installedArtifact.exists() );
132         
133         assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
134     }
135 
136     public void testInstallFileWithGeneratePom()
137         throws Exception
138     {
139         File testPom =
140             new File( getBasedir(), "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml" );
141 
142         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
143 
144         assertNotNull( mojo );
145         
146         setVariableValueToObject( mojo, "session", createMavenSession() );
147 
148         assignValuesForParameter( mojo );
149 
150         mojo.execute();
151 
152         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
153             artifactId + "-" + version + "." + packaging );
154 
155         assertTrue( (Boolean) getVariableValueFromObject( mojo, "generatePom" ) );
156 
157         assertTrue( installedArtifact.exists() );
158 
159         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
160             artifactId + "-" + version + "." + "pom" );
161 
162         try ( Reader reader = new XmlStreamReader( installedPom ) ) {
163             Model model = new MavenXpp3Reader().read( reader );
164 
165             assertEquals( "4.0.0", model.getModelVersion() );
166     
167             assertEquals( (String) getVariableValueFromObject( mojo, "groupId" ), model.getGroupId() );
168     
169             assertEquals( artifactId, model.getArtifactId() );
170     
171             assertEquals( version, model.getVersion() );
172         }
173 
174         assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
175     }
176 
177     public void testInstallFileWithPomFile()
178         throws Exception
179     {
180         File testPom =
181             new File( getBasedir(), "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml" );
182 
183         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
184 
185         assertNotNull( mojo );
186         
187         setVariableValueToObject( mojo, "session", createMavenSession() );
188 
189         assignValuesForParameter( mojo );
190 
191         mojo.execute();
192 
193         File pomFile = (File) getVariableValueFromObject( mojo, "pomFile" );
194 
195         assertTrue( pomFile.exists() );
196 
197         File installedArtifact = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
198             artifactId + "-" + version + "." + packaging );
199 
200         assertTrue( installedArtifact.exists() );
201 
202         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
203             artifactId + "-" + version + "." + "pom" );
204 
205         assertTrue( installedPom.exists() );
206         
207         assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
208     }
209 
210     public void testInstallFileWithPomAsPackaging()
211         throws Exception
212     {
213         File testPom = new File( getBasedir(),
214                                  "target/test-classes/unit/install-file-with-pom-as-packaging/" + "plugin-config.xml" );
215 
216         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
217 
218         assertNotNull( mojo );
219         
220         setVariableValueToObject( mojo, "session", createMavenSession() );
221 
222         assignValuesForParameter( mojo );
223 
224         assertTrue( file.exists() );
225 
226         assertEquals( "pom", packaging );
227 
228         mojo.execute();
229 
230         File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
231             artifactId + "-" + version + "." + "pom" );
232 
233         assertTrue( installedPom.exists() );
234 
235         assertEquals( 4, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
236     }
237 
238     public void testInstallFile()
239         throws Exception
240     {
241         File testPom =
242             new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" );
243 
244         InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );
245 
246         assertNotNull( mojo );
247         
248         setVariableValueToObject( mojo, "session", createMavenSession() );
249 
250         assignValuesForParameter( mojo );
251 
252         mojo.execute();
253 
254         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" +
255                         artifactId + "-" + version;
256         
257         File installedArtifact = new File( localPath + "." + "jar" );
258         
259         assertTrue( installedArtifact.exists() );
260         
261         assertEquals( FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).toString(), 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() );
262     }
263 
264     private void assignValuesForParameter( Object obj )
265         throws Exception
266     {
267         this.groupId = dotToSlashReplacer( (String) getVariableValueFromObject( obj, "groupId" ) );
268 
269         this.artifactId = (String) getVariableValueFromObject( obj, "artifactId" );
270 
271         this.version = (String) getVariableValueFromObject( obj, "version" );
272 
273         this.packaging = (String) getVariableValueFromObject( obj, "packaging" );
274 
275         this.classifier = (String) getVariableValueFromObject( obj, "classifier" );
276 
277         this.file = (File) getVariableValueFromObject( obj, "file" );
278     }
279 
280     private String dotToSlashReplacer( String parameter )
281     {
282         return parameter.replace( '.', '/' );
283     }
284 
285     private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
286     {
287         MavenSession session = mock( MavenSession.class );
288         DefaultRepositorySystemSession repositorySession  = new DefaultRepositorySystemSession();
289         repositorySession.setLocalRepositoryManager(
290                 new EnhancedLocalRepositoryManagerFactory().newInstance(
291                         repositorySession, new LocalRepository( LOCAL_REPO )
292                 )
293         );
294         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
295         buildingRequest.setRepositorySession( repositorySession );
296         when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest );
297         when( session.getRepositorySession() ).thenReturn( repositorySession );
298         return session;
299     }
300 }