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.shared.release.phase;
20  
21  import java.io.File;
22  import java.util.List;
23  
24  import org.apache.maven.project.MavenProject;
25  import org.apache.maven.shared.release.PlexusJUnit4TestCase;
26  import org.apache.maven.shared.release.ReleaseExecutionException;
27  import org.apache.maven.shared.release.ReleaseFailureException;
28  import org.apache.maven.shared.release.ReleaseResult;
29  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
30  import org.apache.maven.shared.release.config.ReleaseUtils;
31  import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
32  import org.apache.maven.shared.release.env.ReleaseEnvironment;
33  import org.apache.maven.shared.release.exec.MavenExecutor;
34  import org.apache.maven.shared.release.exec.MavenExecutorException;
35  import org.apache.maven.shared.release.stubs.MavenExecutorWrapper;
36  import org.junit.Test;
37  
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.fail;
40  import static org.mockito.Matchers.eq;
41  import static org.mockito.Matchers.isA;
42  import static org.mockito.Matchers.isNull;
43  import static org.mockito.Mockito.doThrow;
44  import static org.mockito.Mockito.mock;
45  import static org.mockito.Mockito.verify;
46  import static org.mockito.Mockito.verifyNoMoreInteractions;
47  
48  /**
49   * Test the simple test running phase.
50   *
51   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
52   */
53  public class RunCompleteGoalsPhaseTest extends PlexusJUnit4TestCase {
54      private RunCompleteGoalsPhase phase;
55  
56      private MavenExecutorWrapper mavenExecutorWrapper;
57  
58      private DefaultReleaseEnvironment releaseEnvironment;
59  
60      @Override
61      public void setUp() throws Exception {
62          super.setUp();
63  
64          phase = (RunCompleteGoalsPhase) lookup(ReleasePhase.class, "run-completion-goals");
65  
66          mavenExecutorWrapper =
67                  (MavenExecutorWrapper) lookup("org.apache.maven.shared.release.exec.MavenExecutor", "wrapper");
68  
69          releaseEnvironment = new DefaultReleaseEnvironment();
70          releaseEnvironment.setMavenExecutorId("wrapper");
71      }
72  
73      @Test
74      public void testExecute() throws ReleaseExecutionException, ReleaseFailureException, MavenExecutorException {
75          // prepare
76          File testFile = getTestFile("target/working-directory");
77  
78          ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
79          builder.setCompletionGoals("clean integration-test");
80          builder.setWorkingDirectory(testFile.getAbsolutePath());
81  
82          MavenExecutor mock = mock(MavenExecutor.class);
83  
84          mavenExecutorWrapper.setMavenExecutor(mock);
85  
86          // execute
87          phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), releaseEnvironment, (List<MavenProject>) null);
88  
89          // verify
90          verify(mock)
91                  .executeGoals(
92                          eq(testFile),
93                          eq("clean integration-test"),
94                          isA(ReleaseEnvironment.class),
95                          eq(true),
96                          isNull(),
97                          isNull(),
98                          isA(ReleaseResult.class));
99          verifyNoMoreInteractions(mock);
100     }
101 
102     @Test
103     public void testSimulate() throws ReleaseExecutionException, MavenExecutorException {
104         // prepare
105         File testFile = getTestFile("target/working-directory");
106 
107         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
108         builder.setCompletionGoals("clean integration-test");
109         builder.setWorkingDirectory(testFile.getAbsolutePath());
110 
111         MavenExecutor mock = mock(MavenExecutor.class);
112 
113         mavenExecutorWrapper.setMavenExecutor(mock);
114 
115         // execute
116         phase.simulate(ReleaseUtils.buildReleaseDescriptor(builder), releaseEnvironment, null);
117 
118         // verify
119         verify(mock)
120                 .executeGoals(
121                         eq(testFile),
122                         eq("clean integration-test"),
123                         isA(ReleaseEnvironment.class),
124                         eq(true),
125                         isNull(),
126                         isNull(),
127                         isA(ReleaseResult.class));
128         verifyNoMoreInteractions(mock);
129     }
130 
131     @Test
132     public void testExecuteException() throws ReleaseFailureException, MavenExecutorException {
133         // prepare
134         File testFile = getTestFile("target/working-directory");
135 
136         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
137         builder.setCompletionGoals("clean integration-test");
138         builder.setWorkingDirectory(testFile.getAbsolutePath());
139 
140         MavenExecutor mock = mock(MavenExecutor.class);
141         doThrow(new MavenExecutorException("...", new Exception()))
142                 .when(mock)
143                 .executeGoals(
144                         eq(testFile),
145                         eq("clean integration-test"),
146                         isA(ReleaseEnvironment.class),
147                         eq(true),
148                         isNull(),
149                         isNull(),
150                         isA(ReleaseResult.class));
151 
152         mavenExecutorWrapper.setMavenExecutor(mock);
153 
154         // execute
155         try {
156             phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), releaseEnvironment, (List<MavenProject>) null);
157 
158             fail("Should have thrown an exception");
159         } catch (ReleaseExecutionException e) {
160             assertEquals(
161                     "Check cause", MavenExecutorException.class, e.getCause().getClass());
162         }
163 
164         // verify
165         verify(mock)
166                 .executeGoals(
167                         eq(testFile),
168                         eq("clean integration-test"),
169                         isA(ReleaseEnvironment.class),
170                         eq(true),
171                         isNull(),
172                         isNull(),
173                         isA(ReleaseResult.class));
174         verifyNoMoreInteractions(mock);
175     }
176 
177     @Test
178     public void testSimulateException() throws MavenExecutorException {
179         // prepare
180         File testFile = getTestFile("target/working-directory");
181 
182         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
183         builder.setCompletionGoals("clean integration-test");
184         builder.setWorkingDirectory(testFile.getAbsolutePath());
185 
186         MavenExecutor mock = mock(MavenExecutor.class);
187         doThrow(new MavenExecutorException("...", new Exception()))
188                 .when(mock)
189                 .executeGoals(
190                         eq(testFile),
191                         eq("clean integration-test"),
192                         isA(ReleaseEnvironment.class),
193                         eq(true),
194                         isNull(),
195                         isNull(),
196                         isA(ReleaseResult.class));
197 
198         mavenExecutorWrapper.setMavenExecutor(mock);
199 
200         // execute
201         try {
202             phase.simulate(ReleaseUtils.buildReleaseDescriptor(builder), releaseEnvironment, null);
203 
204             fail("Should have thrown an exception");
205         } catch (ReleaseExecutionException e) {
206             assertEquals(
207                     "Check cause", MavenExecutorException.class, e.getCause().getClass());
208         }
209 
210         // verify
211         verify(mock)
212                 .executeGoals(
213                         eq(testFile),
214                         eq("clean integration-test"),
215                         isA(ReleaseEnvironment.class),
216                         eq(true),
217                         isNull(),
218                         isNull(),
219                         isA(ReleaseResult.class));
220         verifyNoMoreInteractions(mock);
221     }
222 
223     @Test
224     public void testEmptyGoals() throws Exception {
225         // prepare
226         File testFile = getTestFile("target/working-directory");
227 
228         ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder();
229         builder.setCompletionGoals("");
230         builder.setWorkingDirectory(testFile.getAbsolutePath());
231 
232         MavenExecutor mock = mock(MavenExecutor.class);
233 
234         mavenExecutorWrapper.setMavenExecutor(mock);
235 
236         // execute
237         phase.execute(ReleaseUtils.buildReleaseDescriptor(builder), releaseEnvironment, (List<MavenProject>) null);
238 
239         // verify
240         // never invoke mock
241         verifyNoMoreInteractions(mock);
242     }
243 }