View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.install;
20  
21  import java.io.File;
22  import java.io.Reader;
23  
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.model.Model;
26  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.apache.maven.project.DefaultProjectBuildingRequest;
29  import org.apache.maven.project.ProjectBuildingRequest;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.xml.XmlStreamReader;
32  import org.eclipse.aether.DefaultRepositorySystemSession;
33  import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
34  import org.eclipse.aether.internal.impl.DefaultLocalPathPrefixComposerFactory;
35  import org.eclipse.aether.internal.impl.DefaultTrackingFileManager;
36  import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
37  import org.eclipse.aether.repository.LocalRepository;
38  import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
39  
40  import static org.mockito.Mockito.mock;
41  import static org.mockito.Mockito.when;
42  
43  /**
44   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
45   */
46  public class InstallFileMojoTest extends AbstractMojoTestCase {
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      private final String SPECIFIC_LOCAL_REPO = "target/specific-local-repo/";
62  
63      public void setUp() throws Exception {
64          super.setUp();
65  
66          FileUtils.deleteDirectory(new File(getBasedir() + "/" + LOCAL_REPO));
67          FileUtils.deleteDirectory(new File(getBasedir() + "/" + SPECIFIC_LOCAL_REPO));
68      }
69  
70      public void testInstallFileFromLocalRepositoryToLocalRepositoryPath() throws Exception {
71          File localRepository =
72                  new File(getBasedir(), "target/test-classes/unit/install-file-from-local-repository-test/target");
73  
74          File testPom = new File(localRepository.getParentFile(), "plugin-config.xml");
75  
76          InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
77  
78          assertNotNull(mojo);
79  
80          setVariableValueToObject(mojo, "session", createMavenSession(localRepository.getAbsolutePath()));
81  
82          File specificLocalRepositoryPath = new File(getBasedir() + "/" + SPECIFIC_LOCAL_REPO);
83  
84          setVariableValueToObject(mojo, "localRepositoryPath", specificLocalRepositoryPath);
85  
86          assignValuesForParameter(mojo);
87  
88          mojo.execute();
89  
90          String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/"
91                  + artifactId + "-" + version;
92  
93          File installedArtifact = new File(localPath + "." + "jar");
94  
95          assertTrue(installedArtifact.exists());
96  
97          assertEquals(
98                  FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString(),
99                  5,
100                 FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size());
101     }
102 
103     public void testInstallFileWithLocalRepositoryPath() throws Exception {
104         File testPom =
105                 new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml");
106 
107         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
108 
109         assertNotNull(mojo);
110 
111         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
112 
113         File specificLocalRepositoryPath = new File(getBasedir() + "/" + SPECIFIC_LOCAL_REPO);
114 
115         setVariableValueToObject(mojo, "localRepositoryPath", specificLocalRepositoryPath);
116 
117         assignValuesForParameter(mojo);
118 
119         mojo.execute();
120 
121         String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/"
122                 + artifactId + "-" + version;
123 
124         File installedArtifact = new File(localPath + "." + "jar");
125 
126         assertTrue(installedArtifact.exists());
127 
128         assertEquals(
129                 FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).toString(),
130                 5,
131                 FileUtils.getFiles(new File(SPECIFIC_LOCAL_REPO), null, null).size());
132     }
133 
134     public void testInstallFileTestEnvironment() throws Exception {
135         File testPom = new File(getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml");
136 
137         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
138 
139         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
140 
141         assertNotNull(mojo);
142     }
143 
144     public void testBasicInstallFile() throws Exception {
145         File testPom = new File(getBasedir(), "target/test-classes/unit/install-file-basic-test/plugin-config.xml");
146 
147         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
148 
149         assertNotNull(mojo);
150 
151         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
152 
153         assignValuesForParameter(mojo);
154 
155         mojo.execute();
156 
157         File installedArtifact = new File(
158                 getBasedir(),
159                 LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "."
160                         + packaging);
161 
162         assertTrue(installedArtifact.exists());
163 
164         assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
165     }
166 
167     public void testInstallFileWithClassifier() throws Exception {
168         File testPom =
169                 new File(getBasedir(), "target/test-classes/unit/install-file-with-classifier/plugin-config.xml");
170 
171         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
172 
173         assertNotNull(mojo);
174 
175         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
176 
177         assignValuesForParameter(mojo);
178 
179         assertNotNull(classifier);
180 
181         mojo.execute();
182 
183         File installedArtifact = new File(
184                 getBasedir(),
185                 LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "-"
186                         + classifier + "." + packaging);
187 
188         assertTrue(installedArtifact.exists());
189 
190         assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
191     }
192 
193     public void testInstallFileWithGeneratePom() throws Exception {
194         File testPom =
195                 new File(getBasedir(), "target/test-classes/unit/install-file-test-generatePom/plugin-config.xml");
196 
197         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
198 
199         assertNotNull(mojo);
200 
201         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
202 
203         assignValuesForParameter(mojo);
204 
205         mojo.execute();
206 
207         File installedArtifact = new File(
208                 getBasedir(),
209                 LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "."
210                         + packaging);
211 
212         assertTrue((Boolean) getVariableValueFromObject(mojo, "generatePom"));
213 
214         assertTrue(installedArtifact.exists());
215 
216         File installedPom = new File(
217                 getBasedir(),
218                 LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "."
219                         + "pom");
220 
221         try (Reader reader = new XmlStreamReader(installedPom)) {
222             Model model = new MavenXpp3Reader().read(reader);
223 
224             assertEquals("4.0.0", model.getModelVersion());
225 
226             assertEquals((String) getVariableValueFromObject(mojo, "groupId"), model.getGroupId());
227 
228             assertEquals(artifactId, model.getArtifactId());
229 
230             assertEquals(version, model.getVersion());
231         }
232 
233         assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
234     }
235 
236     public void testInstallFileWithPomFile() throws Exception {
237         File testPom =
238                 new File(getBasedir(), "target/test-classes/unit/install-file-with-pomFile-test/plugin-config.xml");
239 
240         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
241 
242         assertNotNull(mojo);
243 
244         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
245 
246         assignValuesForParameter(mojo);
247 
248         mojo.execute();
249 
250         File pomFile = (File) getVariableValueFromObject(mojo, "pomFile");
251 
252         assertTrue(pomFile.exists());
253 
254         File installedArtifact = new File(
255                 getBasedir(),
256                 LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "."
257                         + packaging);
258 
259         assertTrue(installedArtifact.exists());
260 
261         File installedPom = new File(
262                 getBasedir(),
263                 LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "."
264                         + "pom");
265 
266         assertTrue(installedPom.exists());
267 
268         assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
269     }
270 
271     public void testInstallFileWithPomAsPackaging() throws Exception {
272         File testPom = new File(
273                 getBasedir(), "target/test-classes/unit/install-file-with-pom-as-packaging/" + "plugin-config.xml");
274 
275         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
276 
277         assertNotNull(mojo);
278 
279         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
280 
281         assignValuesForParameter(mojo);
282 
283         assertTrue(file.exists());
284 
285         assertEquals("pom", packaging);
286 
287         mojo.execute();
288 
289         File installedPom = new File(
290                 getBasedir(),
291                 LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "."
292                         + "pom");
293 
294         assertTrue(installedPom.exists());
295 
296         assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
297     }
298 
299     public void testInstallFile() throws Exception {
300         File testPom =
301                 new File(getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml");
302 
303         InstallFileMojo mojo = (InstallFileMojo) lookupMojo("install-file", testPom);
304 
305         assertNotNull(mojo);
306 
307         setVariableValueToObject(mojo, "session", createMavenSession(LOCAL_REPO));
308 
309         assignValuesForParameter(mojo);
310 
311         mojo.execute();
312 
313         String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/"
314                 + artifactId + "-" + version;
315 
316         File installedArtifact = new File(localPath + "." + "jar");
317 
318         assertTrue(installedArtifact.exists());
319 
320         assertEquals(
321                 FileUtils.getFiles(new File(LOCAL_REPO), null, null).toString(),
322                 5,
323                 FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
324     }
325 
326     private void assignValuesForParameter(Object obj) throws Exception {
327         this.groupId = dotToSlashReplacer((String) getVariableValueFromObject(obj, "groupId"));
328 
329         this.artifactId = (String) getVariableValueFromObject(obj, "artifactId");
330 
331         this.version = (String) getVariableValueFromObject(obj, "version");
332 
333         this.packaging = (String) getVariableValueFromObject(obj, "packaging");
334 
335         this.classifier = (String) getVariableValueFromObject(obj, "classifier");
336 
337         this.file = (File) getVariableValueFromObject(obj, "file");
338     }
339 
340     private String dotToSlashReplacer(String parameter) {
341         return parameter.replace('.', '/');
342     }
343 
344     private MavenSession createMavenSession(String localRepositoryBaseDir) throws NoLocalRepositoryManagerException {
345         MavenSession session = mock(MavenSession.class);
346         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
347         repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory(
348                         new DefaultLocalPathComposer(),
349                         new DefaultTrackingFileManager(),
350                         new DefaultLocalPathPrefixComposerFactory())
351                 .newInstance(repositorySession, new LocalRepository(localRepositoryBaseDir)));
352         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
353         buildingRequest.setRepositorySession(repositorySession);
354         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
355         when(session.getRepositorySession()).thenReturn(repositorySession);
356         return session;
357     }
358 }