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