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.release;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.util.Collections;
25  
26  import org.apache.maven.api.di.Provides;
27  import org.apache.maven.api.plugin.testing.Basedir;
28  import org.apache.maven.api.plugin.testing.InjectMojo;
29  import org.apache.maven.api.plugin.testing.MojoTest;
30  import org.apache.maven.execution.DefaultMavenExecutionRequest;
31  import org.apache.maven.execution.MavenSession;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.shared.release.ReleaseManager;
34  import org.apache.maven.shared.release.ReleasePerformRequest;
35  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.api.extension.ExtendWith;
39  import org.mockito.ArgumentCaptor;
40  import org.mockito.Mock;
41  import org.mockito.junit.jupiter.MockitoExtension;
42  
43  import static org.junit.jupiter.api.Assertions.assertEquals;
44  import static org.junit.jupiter.api.AssertionsKt.assertNotNull;
45  import static org.mockito.Mockito.verify;
46  import static org.mockito.Mockito.verifyNoMoreInteractions;
47  import static org.mockito.Mockito.when;
48  
49  /**
50   * Test release:stage.
51   *
52   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
53   */
54  @ExtendWith(MockitoExtension.class)
55  @MojoTest
56  class StageReleaseMojoTest {
57      @Mock
58      private ReleaseManager releaseManagerMock;
59  
60      @Inject
61      private MavenProject mavenProject;
62  
63      @Inject
64      private MavenSession mavenSession;
65  
66      @Provides
67      private ReleaseManager releaseManager() {
68          return releaseManagerMock;
69      }
70  
71      @BeforeEach
72      void setup() {
73          when(mavenProject.getFile()).thenReturn(new File("pom.xml"));
74          when(mavenSession.getProjects()).thenReturn(Collections.singletonList(mavenProject));
75      }
76  
77      private void prepareMocks() {
78          when(mavenSession.getRequest()).thenReturn(new DefaultMavenExecutionRequest());
79  
80          when(mavenProject.getGroupId()).thenReturn("groupId");
81          when(mavenProject.getArtifactId()).thenReturn("artifactId");
82          when(mavenProject.getVersion()).thenReturn("1.0.0-SNAPSHOT");
83      }
84  
85      @Test
86      @Basedir("/mojos/stage")
87      @InjectMojo(goal = "stage", pom = "stage.xml")
88      void testStage(StageReleaseMojo mojo) throws Exception {
89          prepareMocks();
90  
91          mojo.execute();
92  
93          // verify
94          ArgumentCaptor<ReleasePerformRequest> argument = ArgumentCaptor.forClass(ReleasePerformRequest.class);
95          verify(releaseManagerMock).perform(argument.capture());
96          assertNotNull(argument.getValue().getReleaseDescriptorBuilder());
97          assertNotNull(argument.getValue().getReleaseEnvironment());
98          assertNotNull(argument.getValue().getReactorProjects());
99          assertEquals(Boolean.FALSE, argument.getValue().getDryRun());
100 
101         ReleaseDescriptorBuilder.BuilderReleaseDescriptor releaseDescriptor =
102                 argument.getValue().getReleaseDescriptorBuilder().build();
103         assertEquals("deploy site:stage-deploy", releaseDescriptor.getPerformGoals());
104         assertEquals("-DskipTests -DaltDeploymentRepository=\"staging\"", releaseDescriptor.getAdditionalArguments());
105 
106         verifyNoMoreInteractions(releaseManagerMock);
107     }
108 
109     @Test
110     @Basedir("/mojos/stage")
111     @InjectMojo(goal = "stage", pom = "stage.xml")
112     void testCreateGoals(StageReleaseMojo mojo) {
113         mojo.createGoals();
114         assertEquals("deploy site:stage-deploy", mojo.goals);
115         mojo.goals = "deploy site:deploy";
116         mojo.createGoals();
117         assertEquals("deploy site:stage-deploy", mojo.goals);
118     }
119 
120     @Test
121     @Basedir("/mojos/stage")
122     @InjectMojo(goal = "stage", pom = "stage.xml")
123     void testCreateArguments(StageReleaseMojo mojo) {
124         prepareMocks();
125 
126         mojo.setDeploymentRepository();
127         ReleaseDescriptorBuilder.BuilderReleaseDescriptor releaseDescriptor =
128                 mojo.createReleaseDescriptor().build();
129         assertEquals("-DskipTests -DaltDeploymentRepository=\"staging\"", releaseDescriptor.getAdditionalArguments());
130     }
131 }