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 public void testPrepare()
51 throws Exception
52 {
53 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
54 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
55 mojo.setBasedir( testFile.getParentFile() );
56 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
57 {
58 public Properties getExecutionProperties(){
59 return new Properties();
60 };
61 };
62
63 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
64 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
65 releaseDescriptor.setUpdateDependencies( false );
66
67 Mock mock = new Mock( ReleaseManager.class );
68
69 Constraint[] constraints = new Constraint[] {
70 new IsEqual( releaseDescriptor ),
71 new IsInstanceOf( ReleaseEnvironment.class ),
72 new IsNull(),
73 new IsEqual( Boolean.TRUE ),
74 new IsEqual( Boolean.FALSE )
75 };
76
77 mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints );
78 mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
79
80 mojo.execute();
81
82 assertTrue( true );
83 }
84
85 public void testPrepareWithExecutionException()
86 throws Exception
87 {
88 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
89 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
90 mojo.setBasedir( testFile.getParentFile() );
91 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
92 {
93 public Properties getExecutionProperties(){
94 return new Properties();
95 };
96 };
97 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
98 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
99 releaseDescriptor.setUpdateDependencies( false );
100
101 Mock mock = new Mock( ReleaseManager.class );
102
103 Constraint[] constraints = new Constraint[] {
104 new IsEqual( releaseDescriptor ),
105 new IsInstanceOf( ReleaseEnvironment.class ),
106 new IsNull(),
107 new IsEqual( Boolean.TRUE ),
108 new IsEqual( Boolean.FALSE )
109 };
110
111 mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints ).will(
112 new ThrowStub( new ReleaseExecutionException( "..." ) ) );
113 mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
114
115 try
116 {
117 mojo.execute();
118
119 fail( "Should have thrown an exception" );
120 }
121 catch ( MojoExecutionException e )
122 {
123 assertEquals( "Check cause", ReleaseExecutionException.class, e.getCause().getClass() );
124 }
125 }
126
127 public void testPrepareWithExecutionFailure()
128 throws Exception
129 {
130 File testFile = getTestFile( "target/test-classes/mojos/prepare/prepare.xml" );
131 PrepareReleaseMojo mojo = (PrepareReleaseMojo) lookupMojo( "prepare", testFile );
132 mojo.setBasedir( testFile.getParentFile() );
133 mojo.session = new MavenSession( null, null, null, null, null, null, null, null, null )
134 {
135 public Properties getExecutionProperties(){
136 return new Properties();
137 };
138 };
139 ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
140 releaseDescriptor.setWorkingDirectory( testFile.getParentFile().getAbsolutePath() );
141 releaseDescriptor.setUpdateDependencies( false );
142
143 Mock mock = new Mock( ReleaseManager.class );
144
145 Constraint[] constraints = new Constraint[] {
146 new IsEqual( releaseDescriptor ),
147 new IsInstanceOf( ReleaseEnvironment.class ),
148 new IsNull(),
149 new IsEqual( Boolean.TRUE ),
150 new IsEqual( Boolean.FALSE )
151 };
152
153 ReleaseFailureException cause = new ReleaseFailureException( "..." );
154 mock.expects( new InvokeOnceMatcher() ).method( "prepare" ).with( constraints ).will(
155 new ThrowStub( cause ) );
156 mojo.setReleaseManager( (ReleaseManager) mock.proxy() );
157
158 try
159 {
160 mojo.execute();
161
162 fail( "Should have thrown an exception" );
163 }
164 catch ( MojoFailureException e )
165 {
166 assertEquals( "Check cause exists", cause, e.getCause() );
167 }
168 }
169
170
171
172
173
174
175
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 }