1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.install;
20
21 import java.io.File;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.execution.MavenSession;
28 import org.apache.maven.model.Build;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.descriptor.PluginDescriptor;
33 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
34 import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0;
35 import org.apache.maven.plugins.install.stubs.InstallArtifactStub;
36 import org.apache.maven.project.DefaultProjectBuildingRequest;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.ProjectBuildingRequest;
39 import org.codehaus.plexus.util.FileUtils;
40 import org.eclipse.aether.DefaultRepositorySystemSession;
41 import org.eclipse.aether.artifact.DefaultArtifact;
42 import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
43 import org.eclipse.aether.internal.impl.DefaultLocalPathPrefixComposerFactory;
44 import org.eclipse.aether.internal.impl.DefaultTrackingFileManager;
45 import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
46 import org.eclipse.aether.repository.LocalRepository;
47 import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
48
49 import static org.mockito.ArgumentMatchers.any;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
52
53
54
55
56 public class InstallMojoTest extends AbstractMojoTestCase {
57
58 InstallArtifactStub artifact;
59
60 private final String LOCAL_REPO = "target/local-repo/";
61
62 public void setUp() throws Exception {
63 super.setUp();
64
65 FileUtils.deleteDirectory(new File(getBasedir() + "/" + LOCAL_REPO));
66 }
67
68 public void testInstallTestEnvironment() throws Exception {
69 File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml");
70
71 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
72
73 assertNotNull(mojo);
74 }
75
76 public void testBasicInstall() throws Exception {
77 File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml");
78
79 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
80
81 assertNotNull(mojo);
82
83 File file = new File(
84 getBasedir(),
85 "target/test-classes/unit/basic-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar");
86
87 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
88 updateMavenProject(project);
89
90 MavenSession session = createMavenSession();
91 session.setProjects(Collections.singletonList(project));
92 setVariableValueToObject(mojo, "session", session);
93 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
94 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
95
96 artifact = (InstallArtifactStub) project.getArtifact();
97
98 artifact.setFile(file);
99
100 mojo.execute();
101
102 String groupId = dotToSlashReplacer(artifact.getGroupId());
103
104 File installedArtifact = new File(
105 getBasedir(),
106 LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/"
107 + artifact.getArtifactId() + "-" + artifact.getVersion() + "."
108 + artifact.getArtifactHandler().getExtension());
109
110 assertTrue(installedArtifact.exists());
111
112 assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
113 }
114
115 public void testBasicInstallWithAttachedArtifacts() throws Exception {
116 File testPom = new File(
117 getBasedir(),
118 "target/test-classes/unit/basic-install-test-with-attached-artifacts/" + "plugin-config.xml");
119
120 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
121
122 assertNotNull(mojo);
123
124 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
125 updateMavenProject(project);
126
127 MavenSession session = createMavenSession();
128 session.setProjects(Collections.singletonList(project));
129 setVariableValueToObject(mojo, "session", session);
130 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
131 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
132
133 List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
134
135 mojo.execute();
136
137 String packaging = project.getPackaging();
138
139 String groupId;
140
141 for (Object attachedArtifact1 : attachedArtifacts) {
142 AttachedArtifactStub0 attachedArtifact = (AttachedArtifactStub0) attachedArtifact1;
143
144 groupId = dotToSlashReplacer(attachedArtifact.getGroupId());
145
146 File installedArtifact = new File(
147 getBasedir(),
148 LOCAL_REPO + groupId + "/" + attachedArtifact.getArtifactId()
149 + "/" + attachedArtifact.getVersion() + "/" + attachedArtifact.getArtifactId()
150 + "-" + attachedArtifact.getVersion() + "." + packaging);
151
152 assertTrue(installedArtifact.getPath() + " does not exist", installedArtifact.exists());
153 }
154
155 assertEquals(13, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
156 }
157
158 public void testNonPomInstallWithAttachedArtifactsOnly() throws Exception {
159 File testPom = new File(
160 getBasedir(),
161 "target/test-classes/unit/basic-install-test-with-attached-artifacts/" + "plugin-config.xml");
162
163 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
164
165 assertNotNull(mojo);
166
167 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
168 updateMavenProject(project);
169
170 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
171 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
172 setVariableValueToObject(mojo, "session", createMavenSession());
173
174 artifact = (InstallArtifactStub) project.getArtifact();
175
176 artifact.setFile(null);
177
178 try {
179 mojo.execute();
180 fail("Did not throw mojo execution exception");
181 } catch (MojoExecutionException e) {
182
183 assertEquals(
184 "The packaging plugin for project maven-install-test did not assign a main file to the project "
185 + "but it has attachments. Change packaging to 'pom'.",
186 e.getMessage());
187 }
188 }
189
190 public void testUpdateReleaseParamSetToTrue() throws Exception {
191 File testPom = new File(getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml");
192
193 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
194
195 assertNotNull(mojo);
196
197 File file = new File(
198 getBasedir(),
199 "target/test-classes/unit/configured-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar");
200
201 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
202 updateMavenProject(project);
203
204 MavenSession session = createMavenSession();
205 session.setProjects(Collections.singletonList(project));
206 setVariableValueToObject(mojo, "session", session);
207 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
208 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
209
210 artifact = (InstallArtifactStub) project.getArtifact();
211
212 artifact.setFile(file);
213
214 mojo.execute();
215
216
217
218 assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
219 }
220
221 public void testInstallIfArtifactFileIsNull() throws Exception {
222 File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml");
223
224 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
225
226 assertNotNull(mojo);
227
228 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
229 updateMavenProject(project);
230
231 MavenSession session = createMavenSession();
232 session.setProjects(Collections.singletonList(project));
233 setVariableValueToObject(mojo, "session", session);
234 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
235 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
236
237 artifact = (InstallArtifactStub) project.getArtifact();
238
239 artifact.setFile(null);
240
241 assertNull(artifact.getFile());
242
243 try {
244 mojo.execute();
245 fail("Did not throw mojo execution exception");
246 } catch (MojoExecutionException e) {
247
248 assertEquals(
249 "The packaging plugin for project maven-install-test did not assign a file to the build artifact",
250 e.getMessage());
251 }
252
253 assertFalse(new File(LOCAL_REPO).exists());
254 }
255
256 public void testInstallIfProjectFileIsNull() throws Exception {
257 File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml");
258
259 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
260
261 assertNotNull(mojo);
262
263 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
264 updateMavenProject(project);
265
266 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
267 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
268 setVariableValueToObject(mojo, "session", createMavenSession());
269
270 project.setFile(null);
271
272 assertNull(project.getFile());
273
274 try {
275 mojo.execute();
276 fail("Did not throw mojo execution exception");
277 } catch (MojoExecutionException e) {
278
279 assertEquals("The POM for project maven-install-test could not be attached", e.getMessage());
280 }
281 }
282
283 public void testInstallIfPackagingIsPom() throws Exception {
284 File testPom = new File(
285 getBasedir(), "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml");
286
287 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
288
289 assertNotNull(mojo);
290
291 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
292 updateMavenProject(project);
293
294 MavenSession session = createMavenSession();
295 session.setProjects(Collections.singletonList(project));
296 setVariableValueToObject(mojo, "session", session);
297 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
298 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
299
300 String packaging = project.getPackaging();
301
302 assertEquals("pom", packaging);
303
304 artifact = (InstallArtifactStub) project.getArtifact();
305
306 mojo.execute();
307
308 String groupId = dotToSlashReplacer(artifact.getGroupId());
309
310 File installedArtifact = new File(
311 getBasedir(),
312 LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/"
313 + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "pom");
314
315 assertTrue(installedArtifact.exists());
316
317 assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
318 }
319
320 public void testInstallIfPackagingIsBom() throws Exception {
321 File testPom = new File(
322 getBasedir(), "target/test-classes/unit/basic-install-test-packaging-bom/" + "plugin-config.xml");
323
324 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
325
326 assertNotNull(mojo);
327
328 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
329 updateMavenProject(project);
330
331 MavenSession session = createMavenSession();
332 session.setProjects(Collections.singletonList(project));
333 setVariableValueToObject(mojo, "session", session);
334 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
335 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
336
337 String packaging = project.getPackaging();
338
339 assertEquals("bom", packaging);
340
341 artifact = (InstallArtifactStub) project.getArtifact();
342
343 mojo.execute();
344
345 String groupId = dotToSlashReplacer(artifact.getGroupId());
346
347 File installedArtifact = new File(
348 getBasedir(),
349 LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/"
350 + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + "pom");
351
352 assertTrue(installedArtifact.exists());
353
354 assertEquals(4, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
355 }
356
357 public void testBasicInstallAndCreate() throws Exception {
358 File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml");
359
360 AbstractMojo mojo = (AbstractMojo) lookupMojo("install", testPom);
361
362 assertNotNull(mojo);
363
364 File file = new File(getBasedir(), "target/test-classes/unit/basic-install-checksum/" + "maven-test-jar.jar");
365
366 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
367 MavenSession mavenSession = createMavenSession();
368 updateMavenProject(project);
369
370 MavenSession session = createMavenSession();
371 session.setProjects(Collections.singletonList(project));
372 setVariableValueToObject(mojo, "session", session);
373 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
374 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
375
376 artifact = (InstallArtifactStub) project.getArtifact();
377
378 artifact.setFile(file);
379
380 mojo.execute();
381
382 File pom = new File(
383 new File(LOCAL_REPO),
384 mavenSession
385 .getRepositorySession()
386 .getLocalRepositoryManager()
387 .getPathForLocalArtifact(new DefaultArtifact(
388 artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion())));
389
390 assertTrue(pom.exists());
391
392 String groupId = dotToSlashReplacer(artifact.getGroupId());
393 String packaging = project.getPackaging();
394 String localPath = getBasedir() + "/" + LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/"
395 + artifact.getVersion() + "/" + artifact.getArtifactId() + "-" + artifact.getVersion();
396
397 File installedArtifact = new File(localPath + "." + packaging);
398
399 assertTrue(installedArtifact.exists());
400
401 assertEquals(5, FileUtils.getFiles(new File(LOCAL_REPO), null, null).size());
402 }
403
404 public void testSkip() throws Exception {
405 File testPom = new File(getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml");
406
407 InstallMojo mojo = (InstallMojo) lookupMojo("install", testPom);
408
409 assertNotNull(mojo);
410
411 File file = new File(
412 getBasedir(),
413 "target/test-classes/unit/basic-install-test/target/" + "maven-install-test-1.0-SNAPSHOT.jar");
414
415 MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
416 updateMavenProject(project);
417
418 MavenSession session = createMavenSession();
419 session.setProjects(Collections.singletonList(project));
420 setVariableValueToObject(mojo, "session", session);
421 setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
422 setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
423 setVariableValueToObject(mojo, "skip", Boolean.TRUE);
424
425 artifact = (InstallArtifactStub) project.getArtifact();
426
427 artifact.setFile(file);
428
429 mojo.execute();
430
431 String groupId = dotToSlashReplacer(artifact.getGroupId());
432
433 String packaging = project.getPackaging();
434
435 File installedArtifact = new File(
436 getBasedir(),
437 LOCAL_REPO + groupId + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/"
438 + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + packaging);
439
440 assertFalse(installedArtifact.exists());
441
442 assertFalse(new File(LOCAL_REPO).exists());
443 }
444
445 private String dotToSlashReplacer(String parameter) {
446 return parameter.replace('.', '/');
447 }
448
449 private MavenSession createMavenSession() throws NoLocalRepositoryManagerException {
450 MavenSession session = mock(MavenSession.class);
451 DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
452 repositorySession.setLocalRepositoryManager(new EnhancedLocalRepositoryManagerFactory(
453 new DefaultLocalPathComposer(),
454 new DefaultTrackingFileManager(),
455 new DefaultLocalPathPrefixComposerFactory())
456 .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
457 ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
458 buildingRequest.setRepositorySession(repositorySession);
459 when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
460 when(session.getRepositorySession()).thenReturn(repositorySession);
461 when(session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class)))
462 .thenReturn(new ConcurrentHashMap<String, Object>());
463 return session;
464 }
465
466 private void updateMavenProject(MavenProject project) {
467 project.setGroupId(project.getArtifact().getGroupId());
468 project.setArtifactId(project.getArtifact().getArtifactId());
469 project.setVersion(project.getArtifact().getVersion());
470
471 Plugin plugin = new Plugin();
472 plugin.setArtifactId("maven-install-plugin");
473 project.setBuild(new Build());
474 project.getBuild().addPlugin(plugin);
475 }
476 }