1 package org.apache.maven.shared.release.phase;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Matchers.isA;
24 import static org.mockito.Matchers.isNull;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyNoMoreInteractions;
29
30 import java.io.File;
31 import java.util.List;
32
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.settings.Settings;
35 import org.apache.maven.shared.release.ReleaseExecutionException;
36 import org.apache.maven.shared.release.ReleaseFailureException;
37 import org.apache.maven.shared.release.ReleaseResult;
38 import org.apache.maven.shared.release.config.ReleaseDescriptor;
39 import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
40 import org.apache.maven.shared.release.env.ReleaseEnvironment;
41 import org.apache.maven.shared.release.exec.MavenExecutor;
42 import org.apache.maven.shared.release.exec.MavenExecutorException;
43 import org.codehaus.plexus.PlexusTestCase;
44
45
46
47
48
49
50 public class RunPrepareGoalsPhaseTest
51 extends PlexusTestCase
52 {
53 private RunPrepareGoalsPhase phase;
54
55 protected void setUp()
56 throws Exception
57 {
58 super.setUp();
59
60 phase = (RunPrepareGoalsPhase) lookup( ReleasePhase.ROLE, "run-preparation-goals" );
61 }
62
63 public void testExecute()
64 throws ReleaseExecutionException, ReleaseFailureException, MavenExecutorException
65 {
66
67 File testFile = getTestFile( "target/working-directory" );
68
69 ReleaseDescriptor config = new ReleaseDescriptor();
70 config.setPreparationGoals( "clean integration-test" );
71 config.setWorkingDirectory( testFile.getAbsolutePath() );
72
73 MavenExecutor mock = mock( MavenExecutor.class );
74
75 phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
76
77
78 phase.execute( config, (Settings) null, (List<MavenProject>) null );
79
80
81 verify( mock ).executeGoals( eq( testFile ),
82 eq( "clean integration-test" ),
83 isA( ReleaseEnvironment.class),
84 eq( true),
85 isNull( String.class ),
86 isNull( String.class ),
87 isA( ReleaseResult.class ) );
88 verifyNoMoreInteractions( mock );
89 }
90
91 public void testSimulate()
92 throws ReleaseExecutionException, MavenExecutorException
93 {
94
95 File testFile = getTestFile( "target/working-directory" );
96
97 ReleaseDescriptor config = new ReleaseDescriptor();
98 config.setPreparationGoals( "clean integration-test" );
99 config.setWorkingDirectory( testFile.getAbsolutePath() );
100
101 MavenExecutor mock = mock( MavenExecutor.class );
102
103 phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, (MavenExecutor) mock );
104
105
106 phase.simulate( config, new DefaultReleaseEnvironment(), null );
107
108
109 verify( mock ).executeGoals( eq( testFile ),
110 eq( "clean integration-test" ),
111 isA( ReleaseEnvironment.class),
112 eq( true),
113 isNull( String.class ),
114 isNull( String.class ),
115 isA( ReleaseResult.class ) );
116 verifyNoMoreInteractions( mock );
117 }
118
119 public void testExecuteException()
120 throws ReleaseFailureException, MavenExecutorException
121 {
122
123 File testFile = getTestFile( "target/working-directory" );
124
125 ReleaseDescriptor config = new ReleaseDescriptor();
126 config.setPreparationGoals( "clean integration-test" );
127 config.setWorkingDirectory( testFile.getAbsolutePath() );
128
129 MavenExecutor mock = mock( MavenExecutor.class );
130 doThrow( new MavenExecutorException( "...", new Exception() ) ).when( mock ).executeGoals( eq( testFile ),
131 eq( "clean integration-test" ),
132 isA( ReleaseEnvironment.class ),
133 eq( true ),
134 isNull( String.class ),
135 isNull( String.class ),
136 isA( ReleaseResult.class ) );
137
138 phase.setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
139
140
141 try
142 {
143 phase.execute( config, (Settings) null, (List<MavenProject>) null );
144
145 fail( "Should have thrown an exception" );
146 }
147 catch ( ReleaseExecutionException e )
148 {
149 assertEquals( "Check cause", MavenExecutorException.class, e.getCause().getClass() );
150 }
151
152
153 verify( mock ).executeGoals( eq( testFile ),
154 eq( "clean integration-test" ),
155 isA( ReleaseEnvironment.class ),
156 eq( true ),
157 isNull( String.class ),
158 isNull( String.class ),
159 isA( ReleaseResult.class ) );
160 verifyNoMoreInteractions( mock );
161 }
162
163 public void testSimulateException() throws MavenExecutorException
164 {
165
166 File testFile = getTestFile( "target/working-directory" );
167
168 ReleaseDescriptor config = new ReleaseDescriptor();
169 config.setPreparationGoals( "clean integration-test" );
170 config.setWorkingDirectory( testFile.getAbsolutePath() );
171
172 MavenExecutor mock = mock( MavenExecutor.class );
173 doThrow( new MavenExecutorException( "...", new Exception() ) ).when( mock ).executeGoals( eq( testFile ),
174 eq( "clean integration-test" ),
175 isA( ReleaseEnvironment.class ),
176 eq( true ),
177 isNull( String.class ),
178 isNull( String.class ),
179 isA( ReleaseResult.class ) );
180
181 phase.setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
182
183
184 try
185 {
186 phase.simulate( config, new DefaultReleaseEnvironment(), null );
187
188 fail( "Should have thrown an exception" );
189 }
190 catch ( ReleaseExecutionException e )
191 {
192 assertEquals( "Check cause", MavenExecutorException.class, e.getCause().getClass() );
193 }
194
195
196 verify( mock ).executeGoals( eq( testFile ),
197 eq( "clean integration-test" ),
198 isA( ReleaseEnvironment.class ),
199 eq( true ),
200 isNull( String.class ),
201 isNull( String.class ),
202 isA( ReleaseResult.class ) );
203 verifyNoMoreInteractions( mock );
204
205 }
206
207 public void testEmptyGoals()
208 throws Exception
209 {
210
211 File testFile = getTestFile( "target/working-directory" );
212
213 ReleaseDescriptor config = new ReleaseDescriptor();
214 config.setPreparationGoals( "" );
215 config.setWorkingDirectory( testFile.getAbsolutePath() );
216
217 MavenExecutor mock = mock( MavenExecutor.class );
218
219 phase.setMavenExecutor(ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mock );
220
221
222 phase.execute( config, (Settings) null, (List<MavenProject>) null );
223
224
225
226 verifyNoMoreInteractions( mock );
227 }
228 }