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