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.deploy;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Objects;
26  import java.util.Properties;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
33  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
34  import org.apache.maven.plugins.deploy.stubs.ArtifactRepositoryStub;
35  import org.apache.maven.plugins.deploy.stubs.DeployArtifactStub;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.project.ProjectBuildingRequest;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.eclipse.aether.DefaultRepositorySystemSession;
40  import org.eclipse.aether.RepositorySystem;
41  import org.eclipse.aether.internal.impl.DefaultLocalPathComposer;
42  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
43  import org.eclipse.aether.repository.LocalRepository;
44  import org.eclipse.aether.repository.RemoteRepository;
45  import org.junit.Ignore;
46  import org.mockito.InjectMocks;
47  import org.mockito.MockitoAnnotations;
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   * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
55   */
56  public class DeployMojoTest extends AbstractMojoTestCase {
57      private File remoteRepo;
58  
59      private File localRepo;
60  
61      private final String LOCAL_REPO = getBasedir() + "/target/local-repo";
62  
63      private final String REMOTE_REPO = getBasedir() + "/target/remote-repo";
64  
65      DeployArtifactStub artifact;
66  
67      final MavenProjectStub project = new MavenProjectStub();
68  
69      private MavenSession session;
70  
71      @InjectMocks
72      private DeployMojo mojo;
73  
74      public void setUp() throws Exception {
75          super.setUp();
76  
77          session = mock(MavenSession.class);
78          when(session.getPluginContext(any(PluginDescriptor.class), any(MavenProject.class)))
79                  .thenReturn(new ConcurrentHashMap<>());
80          DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
81          repositorySession.setLocalRepositoryManager(
82                  new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
83                          .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
84          when(session.getRepositorySession()).thenReturn(repositorySession);
85  
86          remoteRepo = new File(REMOTE_REPO);
87  
88          remoteRepo.mkdirs();
89  
90          localRepo = new File(LOCAL_REPO);
91  
92          if (localRepo.exists()) {
93              FileUtils.deleteDirectory(localRepo);
94          }
95  
96          if (remoteRepo.exists()) {
97              FileUtils.deleteDirectory(remoteRepo);
98          }
99      }
100 
101     public void tearDown() throws Exception {
102         super.tearDown();
103 
104         if (remoteRepo.exists()) {
105             // FileUtils.deleteDirectory( remoteRepo );
106         }
107     }
108 
109     public void testDeployTestEnvironment() throws Exception {
110         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml");
111 
112         DeployMojo mojo = (DeployMojo) lookupMojo("deploy", testPom);
113 
114         assertNotNull(mojo);
115     }
116 
117     public void testBasicDeploy() throws Exception {
118         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml");
119 
120         mojo = (DeployMojo) lookupMojo("deploy", testPom);
121 
122         MockitoAnnotations.initMocks(this);
123 
124         assertNotNull(mojo);
125 
126         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
127         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
128         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
129         repositorySession.setLocalRepositoryManager(
130                 new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
131                         .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
132         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
133         when(session.getRepositorySession()).thenReturn(repositorySession);
134 
135         File file = new File(
136                 getBasedir(),
137                 "target/test-classes/unit/basic-deploy-test/target/" + "deploy-test-file-1.0-SNAPSHOT.jar");
138 
139         assertTrue(file.exists());
140 
141         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
142         project.setGroupId("org.apache.maven.test");
143         project.setArtifactId("maven-deploy-test");
144         project.setVersion("1.0-SNAPSHOT");
145 
146         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
147         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
148 
149         artifact = (DeployArtifactStub) project.getArtifact();
150 
151         String packaging = project.getPackaging();
152 
153         assertEquals("jar", packaging);
154 
155         artifact.setFile(file);
156 
157         ArtifactRepositoryStub repo = getRepoStub(mojo);
158 
159         assertNotNull(repo);
160 
161         repo.setAppendToUrl("basic-deploy-test");
162 
163         assertEquals("deploy-test", repo.getId());
164         assertEquals("deploy-test", repo.getKey());
165         assertEquals("file", repo.getProtocol());
166         assertEquals("file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl());
167 
168         mojo.execute();
169 
170         // check the artifact in local repository
171         List<String> expectedFiles = new ArrayList<>();
172         List<String> fileList = new ArrayList<>();
173 
174         expectedFiles.add("org");
175         expectedFiles.add("apache");
176         expectedFiles.add("maven");
177         expectedFiles.add("test");
178         expectedFiles.add("maven-deploy-test");
179         expectedFiles.add("1.0-SNAPSHOT");
180         expectedFiles.add("maven-metadata-deploy-test.xml");
181         // expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.jar" );
182         // expectedFiles.add( "maven-deploy-test-1.0-SNAPSHOT.pom" );
183         // as we are in SNAPSHOT the file is here twice
184         expectedFiles.add("maven-metadata-deploy-test.xml");
185         // extra Aether files
186         expectedFiles.add("resolver-status.properties");
187         expectedFiles.add("resolver-status.properties");
188 
189         File localRepo = new File(LOCAL_REPO, "");
190 
191         File[] files = localRepo.listFiles();
192 
193         for (File file2 : Objects.requireNonNull(files)) {
194             addFileToList(file2, fileList);
195         }
196 
197         assertEquals(expectedFiles.size(), fileList.size());
198 
199         assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
200 
201         // check the artifact in remote repository
202         expectedFiles = new ArrayList<>();
203         fileList = new ArrayList<>();
204 
205         expectedFiles.add("org");
206         expectedFiles.add("apache");
207         expectedFiles.add("maven");
208         expectedFiles.add("test");
209         expectedFiles.add("maven-deploy-test");
210         expectedFiles.add("1.0-SNAPSHOT");
211         expectedFiles.add("maven-metadata.xml");
212         expectedFiles.add("maven-metadata.xml.md5");
213         expectedFiles.add("maven-metadata.xml.sha1");
214         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar");
215         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar.md5");
216         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar.sha1");
217         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom");
218         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.md5");
219         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.sha1");
220         // as we are in SNAPSHOT the file is here twice
221         expectedFiles.add("maven-metadata.xml");
222         expectedFiles.add("maven-metadata.xml.md5");
223         expectedFiles.add("maven-metadata.xml.sha1");
224 
225         remoteRepo = new File(remoteRepo, "basic-deploy-test");
226 
227         files = remoteRepo.listFiles();
228 
229         for (File file1 : Objects.requireNonNull(files)) {
230             addFileToList(file1, fileList);
231         }
232 
233         assertEquals(expectedFiles.size(), fileList.size());
234 
235         assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
236     }
237 
238     public void testSkippingDeploy() throws Exception {
239         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml");
240 
241         DeployMojo mojo = (DeployMojo) lookupMojo("deploy", testPom);
242 
243         assertNotNull(mojo);
244 
245         File file = new File(
246                 getBasedir(),
247                 "target/test-classes/unit/basic-deploy-test/target/" + "deploy-test-file-1.0-SNAPSHOT.jar");
248 
249         assertTrue(file.exists());
250 
251         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
252 
253         setVariableValueToObject(mojo, "pluginDescriptor", new PluginDescriptor());
254         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
255         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
256         setVariableValueToObject(mojo, "session", session);
257 
258         artifact = (DeployArtifactStub) project.getArtifact();
259 
260         String packaging = project.getPackaging();
261 
262         assertEquals("jar", packaging);
263 
264         artifact.setFile(file);
265 
266         ArtifactRepositoryStub repo = getRepoStub(mojo);
267 
268         assertNotNull(repo);
269 
270         repo.setAppendToUrl("basic-deploy-test");
271 
272         assertEquals("deploy-test", repo.getId());
273         assertEquals("deploy-test", repo.getKey());
274         assertEquals("file", repo.getProtocol());
275         assertEquals("file://" + getBasedir() + "/target/remote-repo/basic-deploy-test", repo.getUrl());
276 
277         setVariableValueToObject(mojo, "skip", Boolean.TRUE.toString());
278 
279         mojo.execute();
280 
281         File localRepo = new File(LOCAL_REPO, "");
282 
283         File[] files = localRepo.listFiles();
284 
285         assertNull(files);
286 
287         remoteRepo = new File(remoteRepo, "basic-deploy-test");
288 
289         files = remoteRepo.listFiles();
290 
291         assertNull(files);
292     }
293 
294     public void testBasicDeployWithPackagingAsPom() throws Exception {
295         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-pom/plugin-config.xml");
296 
297         mojo = (DeployMojo) lookupMojo("deploy", testPom);
298 
299         MockitoAnnotations.initMocks(this);
300 
301         assertNotNull(mojo);
302 
303         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
304         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
305         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
306         repositorySession.setLocalRepositoryManager(
307                 new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
308                         .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
309         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
310         when(session.getRepositorySession()).thenReturn(repositorySession);
311 
312         File pomFile = new File(
313                 getBasedir(),
314                 "target/test-classes/unit/basic-deploy-pom/target/" + "deploy-test-file-1.0-SNAPSHOT.pom");
315 
316         assertTrue(pomFile.exists());
317 
318         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
319         project.setGroupId("org.apache.maven.test");
320         project.setArtifactId("maven-deploy-test");
321         project.setVersion("1.0-SNAPSHOT");
322 
323         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
324         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
325 
326         artifact = (DeployArtifactStub) project.getArtifact();
327 
328         artifact.setArtifactHandlerExtension(project.getPackaging());
329 
330         artifact.setFile(pomFile);
331 
332         ArtifactRepositoryStub repo = getRepoStub(mojo);
333 
334         repo.setAppendToUrl("basic-deploy-pom");
335 
336         mojo.execute();
337 
338         List<String> expectedFiles = new ArrayList<>();
339         List<String> fileList = new ArrayList<>();
340 
341         expectedFiles.add("org");
342         expectedFiles.add("apache");
343         expectedFiles.add("maven");
344         expectedFiles.add("test");
345         expectedFiles.add("maven-deploy-test");
346         expectedFiles.add("1.0-SNAPSHOT");
347         expectedFiles.add("maven-metadata.xml");
348         expectedFiles.add("maven-metadata.xml.md5");
349         expectedFiles.add("maven-metadata.xml.sha1");
350         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom");
351         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.md5");
352         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.sha1");
353         // as we are in SNAPSHOT the file is here twice
354         expectedFiles.add("maven-metadata.xml");
355         expectedFiles.add("maven-metadata.xml.md5");
356         expectedFiles.add("maven-metadata.xml.sha1");
357         remoteRepo = new File(remoteRepo, "basic-deploy-pom");
358 
359         File[] files = remoteRepo.listFiles();
360 
361         for (File file : Objects.requireNonNull(files)) {
362             addFileToList(file, fileList);
363         }
364 
365         assertEquals(expectedFiles.size(), fileList.size());
366 
367         assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
368     }
369 
370     public void testBasicDeployWithPackagingAsBom() throws Exception {
371         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-bom/plugin-config.xml");
372 
373         mojo = (DeployMojo) lookupMojo("deploy", testPom);
374 
375         MockitoAnnotations.initMocks(this);
376 
377         assertNotNull(mojo);
378 
379         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
380         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
381         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
382         repositorySession.setLocalRepositoryManager(
383                 new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
384                         .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
385         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
386         when(session.getRepositorySession()).thenReturn(repositorySession);
387 
388         File pomFile = new File(
389                 getBasedir(),
390                 "target/test-classes/unit/basic-deploy-bom/target/" + "deploy-test-file-1.0-SNAPSHOT.pom");
391 
392         assertTrue(pomFile.exists());
393 
394         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
395         project.setGroupId("org.apache.maven.test");
396         project.setArtifactId("maven-deploy-test");
397         project.setVersion("1.0-SNAPSHOT");
398 
399         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
400         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
401 
402         artifact = (DeployArtifactStub) project.getArtifact();
403 
404         artifact.setArtifactHandlerExtension(project.getPackaging());
405 
406         artifact.setFile(pomFile);
407 
408         ArtifactRepositoryStub repo = getRepoStub(mojo);
409 
410         repo.setAppendToUrl("basic-deploy-bom");
411 
412         mojo.execute();
413 
414         List<String> expectedFiles = new ArrayList<>();
415         List<String> fileList = new ArrayList<>();
416 
417         expectedFiles.add("org");
418         expectedFiles.add("apache");
419         expectedFiles.add("maven");
420         expectedFiles.add("test");
421         expectedFiles.add("maven-deploy-test");
422         expectedFiles.add("1.0-SNAPSHOT");
423         expectedFiles.add("maven-metadata.xml");
424         expectedFiles.add("maven-metadata.xml.md5");
425         expectedFiles.add("maven-metadata.xml.sha1");
426         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom");
427         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.md5");
428         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.sha1");
429         // as we are in SNAPSHOT the file is here twice
430         expectedFiles.add("maven-metadata.xml");
431         expectedFiles.add("maven-metadata.xml.md5");
432         expectedFiles.add("maven-metadata.xml.sha1");
433         remoteRepo = new File(remoteRepo, "basic-deploy-bom");
434 
435         File[] files = remoteRepo.listFiles();
436 
437         for (File file : Objects.requireNonNull(files)) {
438             addFileToList(file, fileList);
439         }
440 
441         assertEquals(expectedFiles.size(), fileList.size());
442 
443         assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
444     }
445 
446     public void testDeployIfArtifactFileIsNull() throws Exception {
447         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml");
448 
449         DeployMojo mojo = (DeployMojo) lookupMojo("deploy", testPom);
450 
451         MockitoAnnotations.initMocks(this);
452 
453         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
454         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
455 
456         setVariableValueToObject(mojo, "session", session);
457 
458         assertNotNull(mojo);
459 
460         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
461         project.setGroupId("org.apache.maven.test");
462         project.setArtifactId("maven-deploy-test");
463         project.setVersion("1.0-SNAPSHOT");
464 
465         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
466         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
467 
468         artifact = (DeployArtifactStub) project.getArtifact();
469 
470         artifact.setFile(null);
471 
472         assertNull(artifact.getFile());
473 
474         try {
475             mojo.execute();
476             fail("Did not throw mojo execution exception");
477         } catch (MojoExecutionException e) {
478             // expected, message should include artifactId
479             assertEquals(
480                     "The packaging plugin for project maven-deploy-test did not assign a file to the build artifact",
481                     e.getMessage());
482         }
483     }
484 
485     public void testDeployIfProjectFileIsNull() throws Exception {
486         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-test/plugin-config.xml");
487 
488         DeployMojo mojo = (DeployMojo) lookupMojo("deploy", testPom);
489 
490         MockitoAnnotations.initMocks(this);
491 
492         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
493         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
494 
495         setVariableValueToObject(mojo, "session", session);
496 
497         assertNotNull(mojo);
498 
499         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
500         project.setGroupId("org.apache.maven.test");
501         project.setArtifactId("maven-deploy-test");
502         project.setVersion("1.0-SNAPSHOT");
503 
504         project.setFile(null);
505         assertNull(project.getFile());
506 
507         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
508         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
509 
510         try {
511             mojo.execute();
512             fail("Did not throw mojo execution exception");
513         } catch (MojoExecutionException e) {
514             // expected, message should include artifactId
515             assertEquals("The POM for project maven-deploy-test could not be attached", e.getMessage());
516         }
517     }
518 
519     public void testDeployWithAttachedArtifacts() throws Exception {
520         File testPom = new File(
521                 getBasedir(), "target/test-classes/unit/basic-deploy-with-attached-artifacts/" + "plugin-config.xml");
522 
523         mojo = (DeployMojo) lookupMojo("deploy", testPom);
524 
525         MockitoAnnotations.initMocks(this);
526 
527         assertNotNull(mojo);
528 
529         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
530         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
531         DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
532         repositorySession.setLocalRepositoryManager(
533                 new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
534                         .newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
535         when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
536         when(session.getRepositorySession()).thenReturn(repositorySession);
537 
538         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
539         project.setGroupId("org.apache.maven.test");
540         project.setArtifactId("maven-deploy-test");
541         project.setVersion("1.0-SNAPSHOT");
542 
543         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
544         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
545 
546         artifact = (DeployArtifactStub) project.getArtifact();
547 
548         File file = new File(
549                 getBasedir(),
550                 "target/test-classes/unit/basic-deploy-with-attached-artifacts/target/"
551                         + "deploy-test-file-1.0-SNAPSHOT.jar");
552 
553         artifact.setFile(file);
554 
555         ArtifactRepositoryStub repo = getRepoStub(mojo);
556 
557         repo.setAppendToUrl("basic-deploy-with-attached-artifacts");
558 
559         mojo.execute();
560 
561         // check the artifacts in remote repository
562         List<String> expectedFiles = new ArrayList<>();
563         List<String> fileList = new ArrayList<>();
564 
565         expectedFiles.add("org");
566         expectedFiles.add("apache");
567         expectedFiles.add("maven");
568         expectedFiles.add("test");
569         expectedFiles.add("maven-deploy-test");
570         expectedFiles.add("1.0-SNAPSHOT");
571         expectedFiles.add("maven-metadata.xml");
572         expectedFiles.add("maven-metadata.xml.md5");
573         expectedFiles.add("maven-metadata.xml.sha1");
574         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar");
575         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar.md5");
576         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.jar.sha1");
577         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom");
578         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.md5");
579         expectedFiles.add("maven-deploy-test-1.0-SNAPSHOT.pom.sha1");
580         // as we are in SNAPSHOT the file is here twice
581         expectedFiles.add("maven-metadata.xml");
582         expectedFiles.add("maven-metadata.xml.md5");
583         expectedFiles.add("maven-metadata.xml.sha1");
584         expectedFiles.add("attached-artifact-test-0");
585         expectedFiles.add("1.0-SNAPSHOT");
586         expectedFiles.add("maven-metadata.xml");
587         expectedFiles.add("maven-metadata.xml.md5");
588         expectedFiles.add("maven-metadata.xml.sha1");
589         expectedFiles.add("attached-artifact-test-0-1.0-SNAPSHOT.jar");
590         expectedFiles.add("attached-artifact-test-0-1.0-SNAPSHOT.jar.md5");
591         expectedFiles.add("attached-artifact-test-0-1.0-SNAPSHOT.jar.sha1");
592         // as we are in SNAPSHOT the file is here twice
593         expectedFiles.add("maven-metadata.xml");
594         expectedFiles.add("maven-metadata.xml.md5");
595         expectedFiles.add("maven-metadata.xml.sha1");
596 
597         remoteRepo = new File(remoteRepo, "basic-deploy-with-attached-artifacts");
598 
599         File[] files = remoteRepo.listFiles();
600 
601         for (File file1 : Objects.requireNonNull(files)) {
602             addFileToList(file1, fileList);
603         }
604 
605         assertEquals(expectedFiles.size(), fileList.size());
606 
607         assertEquals(0, getSizeOfExpectedFiles(fileList, expectedFiles));
608     }
609 
610     public void testNonPomDeployWithAttachedArtifactsOnly() throws Exception {
611         File testPom = new File(
612                 getBasedir(), "target/test-classes/unit/basic-deploy-with-attached-artifacts/" + "plugin-config.xml");
613 
614         mojo = (DeployMojo) lookupMojo("deploy", testPom);
615 
616         MockitoAnnotations.initMocks(this);
617 
618         assertNotNull(mojo);
619 
620         ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
621         when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
622 
623         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
624         project.setGroupId("org.apache.maven.test");
625         project.setArtifactId("maven-deploy-test");
626         project.setVersion("1.0-SNAPSHOT");
627 
628         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
629         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
630 
631         artifact = (DeployArtifactStub) project.getArtifact();
632         artifact.setFile(null);
633 
634         try {
635             mojo.execute();
636             fail("Did not throw mojo execution exception");
637         } catch (MojoExecutionException e) {
638             // expected, message should include artifactId
639             assertEquals(
640                     "The packaging plugin for project maven-deploy-test did not assign a main file to the project "
641                             + "but it has attachments. Change packaging to 'pom'.",
642                     e.getMessage());
643         }
644     }
645 
646     @Ignore("SCP is not part of Maven3 distribution. Aether handles transport extensions.")
647     public void _testBasicDeployWithScpAsProtocol() throws Exception {
648         String originalUserHome = System.getProperty("user.home");
649 
650         // FIX THE DAMN user.home BEFORE YOU DELETE IT!!!
651         File altHome = new File(getBasedir(), "target/ssh-user-home");
652         altHome.mkdirs();
653 
654         System.out.println("Testing user.home value for .ssh dir: " + altHome.getCanonicalPath());
655 
656         Properties props = System.getProperties();
657         props.setProperty("user.home", altHome.getCanonicalPath());
658 
659         System.setProperties(props);
660 
661         File testPom = new File(getBasedir(), "target/test-classes/unit/basic-deploy-scp/plugin-config.xml");
662 
663         mojo = (DeployMojo) lookupMojo("deploy", testPom);
664 
665         assertNotNull(mojo);
666 
667         RepositorySystem repositorySystem = mock(RepositorySystem.class);
668 
669         setVariableValueToObject(mojo, "repositorySystem", repositorySystem);
670 
671         File file = new File(
672                 getBasedir(),
673                 "target/test-classes/unit/basic-deploy-scp/target/" + "deploy-test-file-1.0-SNAPSHOT.jar");
674 
675         assertTrue(file.exists());
676 
677         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
678 
679         setVariableValueToObject(mojo, "pluginContext", new ConcurrentHashMap<>());
680         setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(project));
681 
682         artifact = (DeployArtifactStub) project.getArtifact();
683 
684         artifact.setFile(file);
685 
686         String altUserHome = System.getProperty("user.home");
687 
688         if (altUserHome.equals(originalUserHome)) {
689             // this is *very* bad!
690             throw new IllegalStateException(
691                     "Setting 'user.home' system property to alternate value did NOT work. Aborting test.");
692         }
693 
694         File sshFile = new File(altUserHome, ".ssh");
695 
696         System.out.println("Testing .ssh dir: " + sshFile.getCanonicalPath());
697 
698         // delete first the .ssh folder if existing before executing the mojo
699         if (sshFile.exists()) {
700             FileUtils.deleteDirectory(sshFile);
701         }
702 
703         mojo.execute();
704 
705         assertTrue(sshFile.exists());
706 
707         FileUtils.deleteDirectory(sshFile);
708     }
709 
710     public void testLegacyAltDeploymentRepositoryWithDefaultLayout() throws Exception {
711         DeployMojo mojo = new DeployMojo();
712 
713         setVariableValueToObject(mojo, "project", project);
714         setVariableValueToObject(mojo, "session", session);
715         setVariableValueToObject(mojo, "altDeploymentRepository", "altDeploymentRepository::default::http://localhost");
716 
717         project.setVersion("1.0-SNAPSHOT");
718 
719         assertEquals(
720                 new RemoteRepository.Builder("altDeploymentRepository", "default", "http://localhost").build(),
721                 mojo.getDeploymentRepository(
722                         project, null, null, "altDeploymentRepository::default::http://localhost"));
723     }
724 
725     public void testLegacyAltDeploymentRepositoryWithLegacyLayout() throws Exception {
726         DeployMojo mojo = new DeployMojo();
727 
728         setVariableValueToObject(mojo, "project", project);
729         setVariableValueToObject(mojo, "session", session);
730         setVariableValueToObject(mojo, "altDeploymentRepository", "altDeploymentRepository::legacy::http://localhost");
731 
732         project.setVersion("1.0-SNAPSHOT");
733         try {
734             mojo.getDeploymentRepository(project, null, null, "altDeploymentRepository::legacy::http://localhost");
735             fail("Should throw: Invalid legacy syntax and layout for repository.");
736         } catch (MojoExecutionException e) {
737             assertEquals(e.getMessage(), "Invalid legacy syntax and layout for repository.");
738             assertEquals(
739                     e.getLongMessage(),
740                     "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::http://localhost\" instead, and only default layout is supported.");
741         }
742     }
743 
744     public void testInsaneAltDeploymentRepository() throws Exception {
745         DeployMojo mojo = new DeployMojo();
746 
747         setVariableValueToObject(mojo, "project", project);
748         setVariableValueToObject(mojo, "session", session);
749         setVariableValueToObject(
750                 mojo, "altDeploymentRepository", "altDeploymentRepository::hey::wow::foo::http://localhost");
751 
752         project.setVersion("1.0-SNAPSHOT");
753         try {
754             mojo.getDeploymentRepository(
755                     project, null, null, "altDeploymentRepository::hey::wow::foo::http://localhost");
756             fail("Should throw: Invalid legacy syntax and layout for repository.");
757         } catch (MojoExecutionException e) {
758             assertEquals(e.getMessage(), "Invalid legacy syntax and layout for repository.");
759             assertEquals(
760                     e.getLongMessage(),
761                     "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::wow::foo::http://localhost\" instead, and only default layout is supported.");
762         }
763     }
764 
765     public void testDefaultScmSvnAltDeploymentRepository() throws Exception {
766         DeployMojo mojo = new DeployMojo();
767 
768         setVariableValueToObject(mojo, "project", project);
769         setVariableValueToObject(mojo, "session", session);
770         setVariableValueToObject(
771                 mojo, "altDeploymentRepository", "altDeploymentRepository::default::scm:svn:http://localhost");
772 
773         project.setVersion("1.0-SNAPSHOT");
774 
775         assertEquals(
776                 new RemoteRepository.Builder("altDeploymentRepository", "default", "scm:svn:http://localhost").build(),
777                 mojo.getDeploymentRepository(
778                         project, null, null, "altDeploymentRepository::default::scm:svn:http://localhost"));
779     }
780 
781     public void testLegacyScmSvnAltDeploymentRepository() throws Exception {
782         DeployMojo mojo = new DeployMojo();
783 
784         setVariableValueToObject(mojo, "project", project);
785         setVariableValueToObject(
786                 mojo, "altDeploymentRepository", "altDeploymentRepository::legacy::scm:svn:http://localhost");
787 
788         project.setVersion("1.0-SNAPSHOT");
789         try {
790             mojo.getDeploymentRepository(
791                     project, null, null, "altDeploymentRepository::legacy::scm:svn:http://localhost");
792             fail("Should throw: Invalid legacy syntax and layout for repository.");
793         } catch (MojoExecutionException e) {
794             assertEquals(e.getMessage(), "Invalid legacy syntax and layout for repository.");
795             assertEquals(
796                     e.getLongMessage(),
797                     "Invalid legacy syntax and layout for alternative repository. Use \"altDeploymentRepository::scm:svn:http://localhost\" instead, and only default layout is supported.");
798         }
799     }
800 
801     public void testAltSnapshotDeploymentRepository() throws Exception {
802         DeployMojo mojo = new DeployMojo();
803 
804         setVariableValueToObject(mojo, "project", project);
805         setVariableValueToObject(mojo, "session", session);
806         setVariableValueToObject(
807                 mojo, "altSnapshotDeploymentRepository", "altSnapshotDeploymentRepository::http://localhost");
808 
809         project.setVersion("1.0-SNAPSHOT");
810 
811         assertEquals(
812                 new RemoteRepository.Builder("altSnapshotDeploymentRepository", "default", "http://localhost").build(),
813                 mojo.getDeploymentRepository(project, "altSnapshotDeploymentRepository::http://localhost", null, null));
814     }
815 
816     public void testAltReleaseDeploymentRepository() throws Exception {
817         DeployMojo mojo = new DeployMojo();
818 
819         setVariableValueToObject(mojo, "project", project);
820         setVariableValueToObject(mojo, "session", session);
821         setVariableValueToObject(
822                 mojo, "altReleaseDeploymentRepository", "altReleaseDeploymentRepository::http://localhost");
823 
824         project.setVersion("1.0");
825 
826         assertEquals(
827                 new RemoteRepository.Builder("altReleaseDeploymentRepository", "default", "http://localhost").build(),
828                 mojo.getDeploymentRepository(project, null, "altReleaseDeploymentRepository::http://localhost", null));
829     }
830 
831     private void addFileToList(File file, List<String> fileList) {
832         if (!file.isDirectory()) {
833             fileList.add(file.getName());
834         } else {
835             fileList.add(file.getName());
836 
837             File[] files = file.listFiles();
838 
839             for (File file1 : Objects.requireNonNull(files)) {
840                 addFileToList(file1, fileList);
841             }
842         }
843     }
844 
845     private int getSizeOfExpectedFiles(List<String> fileList, List<String> expectedFiles) {
846         for (String fileName : fileList) {
847             // translate uniqueVersion to -SNAPSHOT
848             fileName = fileName.replaceFirst("-\\d{8}\\.\\d{6}-\\d+", "-SNAPSHOT");
849 
850             if (!expectedFiles.remove(fileName)) {
851                 fail(fileName + " is not included in the expected files");
852             }
853         }
854         return expectedFiles.size();
855     }
856 
857     private ArtifactRepositoryStub getRepoStub(Object mojo) throws Exception {
858         MavenProject project = (MavenProject) getVariableValueFromObject(mojo, "project");
859         return (ArtifactRepositoryStub) project.getDistributionManagementArtifactRepository();
860     }
861 }