1 package org.apache.maven.plugins.release;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.Properties;
24
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29 import org.apache.maven.shared.release.ReleaseExecutionException;
30 import org.apache.maven.shared.release.ReleaseFailureException;
31 import org.apache.maven.shared.release.ReleaseManager;
32 import org.apache.maven.shared.release.config.ReleaseDescriptor;
33 import org.apache.maven.shared.release.env.ReleaseEnvironment;
34 import org.jmock.Mock;
35 import org.jmock.core.Constraint;
36 import org.jmock.core.constraint.IsEqual;
37 import org.jmock.core.constraint.IsInstanceOf;
38 import org.jmock.core.constraint.IsNull;
39 import org.jmock.core.matcher.InvokeOnceMatcher;
40 import org.jmock.core.stub.ThrowStub;
41
42
43
44
45
46
47 public class PrepareReleaseMojoTest
48 extends AbstractMojoTestCase
49 {
50 private void setDefaults( PrepareReleaseMojo mojo )
51 throws IllegalAccessException
52 {
53 setVariableValueToObject( mojo, "updateWorkingCopyVersions", Boolean.TRUE );
54 }
55
56 public void testPrepare()
57 throws Exception
58 {
59 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
60 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
61 setDefaults( mojo );
62 mojo.setBasedir( testFile.getParentFile() );
63 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
64 {
65 public Properties getExecutionProperties(){
66 return new Properties();
67 };
68 };
69
70 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
71 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
72 releaseDescriptor.setUpdateDependencies( false );
73
74 Mock mock = new Mock( ReleaseManager.class );
75
76 Constraint[] constraints = new Constraint[] {
77 new IsEqual( releaseDescriptor ),
78 new IsInstanceOf( ReleaseEnvironment.class ),
79 new IsNull(),
80 new IsEqual( Boolean.TRUE ),
81 new IsEqual( Boolean.FALSE )
82 };
83
84 mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints );
85 mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
86
87 mojo.execute();
88
89 assertTrue( true );
90 }
91
92 public void testPrepareWithExecutionException()
93 throws Exception
94 {
95 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
96 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
97 setDefaults( mojo );
98 mojo.setBasedir( testFile.getParentFile() );
99 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
100 {
101 public Properties getExecutionProperties(){
102 return new Properties();
103 };
104 };
105 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
106 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
107 releaseDescriptor.setUpdateDependencies( false );
108
109 Mock mock = new Mock( ReleaseManager.class );
110
111 Constraint[] constraints = new Constraint[] {
112 new IsEqual( releaseDescriptor ),
113 new IsInstanceOf( ReleaseEnvironment.class ),
114 new IsNull(),
115 new IsEqual( Boolean.TRUE ),
116 new IsEqual( Boolean.FALSE )
117 };
118
119 mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints ).will(
120 new ThrowStub( new ReleaseExecutionException( "..." ) ) );
121 mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
122
123 try
124 {
125 mojo.execute();
126
127 fail( "Should have thrown an exception" );
128 }
129 catch ( MojoExecutionException e )
130 {
131 assertEquals( "Check cause", ReleaseExecutionException.class, e.getCause().getClass() );
132 }
133 }
134
135 public void testPrepareWithExecutionFailure()
136 throws Exception
137 {
138 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
139 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
140 setDefaults( mojo );
141 mojo.setBasedir( testFile.getParentFile() );
142 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
143 {
144 public Properties getExecutionProperties(){
145 return new Properties();
146 };
147 };
148 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
149 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
150 releaseDescriptor.setUpdateDependencies( false );
151
152 Mock mock = new Mock( ReleaseManager.class );
153
154 Constraint[] constraints = new Constraint[] {
155 new IsEqual( releaseDescriptor ),
156 new IsInstanceOf( ReleaseEnvironment.class ),
157 new IsNull(),
158 new IsEqual( Boolean.TRUE ),
159 new IsEqual( Boolean.FALSE )
160 };
161
162 ReleaseFailureException cause = new ReleaseFailureException( "..." );
163 mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints ).will(
164 new ThrowStub( cause ) );
165 mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
166
167 try
168 {
169 mojo.execute();
170
171 fail( "Should have thrown an exception" );
172 }
173 catch ( MojoFailureException e )
174 {
175 assertEquals( "Check cause exists", cause, e.getCause() );
176 }
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 }