18
18
*/
19
19
package org .apache .maven .plugins .help ;
20
20
21
+ import java .lang .reflect .Field ;
21
22
import java .lang .reflect .InvocationTargetException ;
23
+ import java .lang .reflect .Method ;
22
24
import java .util .Collections ;
23
25
24
26
import org .apache .maven .execution .MavenSession ;
37
39
import org .junit .Test ;
38
40
import org .mockito .ArgumentCaptor ;
39
41
40
- import static org .apache .commons .lang3 .reflect .FieldUtils .writeDeclaredField ;
41
- import static org .apache .commons .lang3 .reflect .FieldUtils .writeField ;
42
- import static org .apache .commons .lang3 .reflect .MethodUtils .invokeMethod ;
43
42
import static org .junit .Assert .*;
44
43
import static org .mockito .Mockito .*;
45
44
@@ -55,7 +54,10 @@ public class DescribeMojoTest {
55
54
public void testGetExpressionsRoot () {
56
55
try {
57
56
DescribeMojo describeMojo = new DescribeMojo ();
58
- invokeMethod (describeMojo , true , "toLines" , "" , 2 , 2 , 80 );
57
+ Method toLines =
58
+ describeMojo .getClass ().getDeclaredMethod ("toLines" , String .class , int .class , int .class , int .class );
59
+ toLines .setAccessible (true );
60
+ toLines .invoke (null , "" , 2 , 2 , 80 );
59
61
} catch (Throwable e ) {
60
62
fail ("The API changes" );
61
63
}
@@ -73,7 +75,10 @@ public void testValidExpression() throws Exception {
73
75
String ls = System .getProperty ("line.separator" );
74
76
75
77
try {
76
- invokeMethod (new DescribeMojo (), true , "describeMojoParameters" , md , sb );
78
+ Method describeMojoParameters = DescribeMojo .class .getDeclaredMethod (
79
+ "describeMojoParameters" , MojoDescriptor .class , StringBuilder .class );
80
+ describeMojoParameters .setAccessible (true );
81
+ describeMojoParameters .invoke (new DescribeMojo (), md , sb );
77
82
78
83
assertEquals (
79
84
" Available parameters:" + ls + ls + " name" + ls + " User property: valid.expression" + ls
@@ -96,7 +101,10 @@ public void testInvalidExpression() throws Exception {
96
101
String ls = System .getProperty ("line.separator" );
97
102
98
103
try {
99
- invokeMethod (new DescribeMojo (), true , "describeMojoParameters" , md , sb );
104
+ Method describeMojoParameters = DescribeMojo .class .getDeclaredMethod (
105
+ "describeMojoParameters" , MojoDescriptor .class , StringBuilder .class );
106
+ describeMojoParameters .setAccessible (true );
107
+ describeMojoParameters .invoke (new DescribeMojo (), md , sb );
100
108
101
109
assertEquals (
102
110
" Available parameters:" + ls + ls
@@ -113,11 +121,12 @@ public void testInvalidExpression() throws Exception {
113
121
@ Test
114
122
public void testParsePluginInfoGAV () throws Throwable {
115
123
DescribeMojo mojo = new DescribeMojo ();
116
- writeDeclaredField (mojo , "groupId" , "org.test" , true );
117
- writeDeclaredField (mojo , "artifactId" , "test" , true );
118
- writeDeclaredField (mojo , "version" , "1.0" , true );
124
+ setFieldWithReflection (mojo , "groupId" , "org.test" );
125
+ setFieldWithReflection (mojo , "artifactId" , "test" );
126
+ setFieldWithReflection (mojo , "version" , "1.0" );
119
127
120
- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
128
+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
129
+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
121
130
122
131
assertEquals ("org.test" , pi .getGroupId ());
123
132
assertEquals ("test" , pi .getArtifactId ());
@@ -128,28 +137,30 @@ public void testParsePluginInfoGAV() throws Throwable {
128
137
@ Test
129
138
public void testParsePluginInfoPluginPrefix () throws Throwable {
130
139
DescribeMojo mojo = new DescribeMojo ();
131
- writeDeclaredField (mojo , "plugin" , "help" , true );
140
+ setFieldWithReflection (mojo , "plugin" , "help" );
132
141
133
- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
142
+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
143
+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
134
144
135
145
assertNull (pi .getGroupId ());
136
146
assertNull (pi .getArtifactId ());
137
147
assertNull (pi .getVersion ());
138
148
assertEquals ("help" , pi .getPrefix ());
139
149
140
- writeDeclaredField (mojo , "plugin" , "help2:::" , true );
150
+ setFieldWithReflection (mojo , "plugin" , "help2:::" );
141
151
142
- pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
152
+ pi = (PluginInfo ) parsePluginLookupInfo . invoke (mojo );
143
153
144
154
assertEquals ("help2" , pi .getPrefix ());
145
155
}
146
156
147
157
@ Test
148
158
public void testParsePluginInfoPluginGA () throws Throwable {
149
159
DescribeMojo mojo = new DescribeMojo ();
150
- writeDeclaredField (mojo , "plugin" , "org.test:test" , true );
160
+ setFieldWithReflection (mojo , "plugin" , "org.test:test" );
151
161
152
- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
162
+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
163
+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
153
164
154
165
assertEquals ("org.test" , pi .getGroupId ());
155
166
assertEquals ("test" , pi .getArtifactId ());
@@ -160,9 +171,10 @@ public void testParsePluginInfoPluginGA() throws Throwable {
160
171
@ Test
161
172
public void testParsePluginInfoPluginGAV () throws Throwable {
162
173
DescribeMojo mojo = new DescribeMojo ();
163
- writeDeclaredField (mojo , "plugin" , "org.test:test:1.0" , true );
174
+ setFieldWithReflection (mojo , "plugin" , "org.test:test:1.0" );
164
175
165
- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
176
+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
177
+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
166
178
167
179
assertEquals ("org.test" , pi .getGroupId ());
168
180
assertEquals ("test" , pi .getArtifactId ());
@@ -173,9 +185,10 @@ public void testParsePluginInfoPluginGAV() throws Throwable {
173
185
@ Test
174
186
public void testParsePluginInfoPluginIncorrect () throws Throwable {
175
187
DescribeMojo mojo = new DescribeMojo ();
176
- writeDeclaredField (mojo , "plugin" , "org.test:test:1.0:invalid" , true );
188
+ setFieldWithReflection (mojo , "plugin" , "org.test:test:1.0:invalid" );
177
189
try {
178
- invokeMethod (mojo , "parsePluginLookupInfo" );
190
+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
191
+ parsePluginLookupInfo .invoke (mojo );
179
192
fail ();
180
193
} catch (Exception e ) {
181
194
// expected
@@ -201,18 +214,16 @@ public void testLookupPluginDescriptorPrefixWithVersion() throws Throwable {
201
214
MavenPluginManager pluginManager = mock (MavenPluginManager .class );
202
215
MavenSession session = mock (MavenSession .class );
203
216
when (session .getRepositorySession ()).thenReturn (mock (RepositorySystemSession .class ));
204
- writeDeclaredField (mojo , "mojoDescriptorCreator" , mojoDescriptorCreator , true );
205
- writeDeclaredField (mojo , "pluginVersionResolver" , pluginVersionResolver , true );
206
- writeDeclaredField (mojo , "pluginManager" , pluginManager , true );
207
- writeField (mojo , "session" , session , true );
217
+ setFieldsOnMojo (mojo , mojoDescriptorCreator , pluginVersionResolver , pluginManager , session );
208
218
MavenProject mavenProject = new MavenProject ();
209
219
mavenProject .setPluginArtifactRepositories (Collections .emptyList ());
210
- writeField (mojo , "project" , mavenProject , true );
220
+ setParentFieldWithReflection (mojo , "project" , mavenProject );
211
221
when (mojoDescriptorCreator .findPluginForPrefix ("help" , session )).thenReturn (plugin );
212
222
when (pluginManager .getPluginDescriptor (any (Plugin .class ), anyList (), any ()))
213
223
.thenReturn (pd );
214
224
215
- PluginDescriptor returned = (PluginDescriptor ) invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
225
+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
226
+ PluginDescriptor returned = (PluginDescriptor ) lookupPluginDescriptor .invoke (mojo , pi );
216
227
217
228
assertEquals (pd , returned );
218
229
@@ -245,20 +256,18 @@ public void testLookupPluginDescriptorPrefixWithoutVersion() throws Throwable {
245
256
PluginVersionResult versionResult = mock (PluginVersionResult .class );
246
257
MavenSession session = mock (MavenSession .class );
247
258
when (session .getRepositorySession ()).thenReturn (mock (RepositorySystemSession .class ));
248
- writeDeclaredField (mojo , "mojoDescriptorCreator" , mojoDescriptorCreator , true );
249
- writeDeclaredField (mojo , "pluginVersionResolver" , pluginVersionResolver , true );
250
- writeDeclaredField (mojo , "pluginManager" , pluginManager , true );
251
- writeField (mojo , "session" , session , true );
259
+ setFieldsOnMojo (mojo , mojoDescriptorCreator , pluginVersionResolver , pluginManager , session );
252
260
MavenProject mavenProject = new MavenProject ();
253
261
mavenProject .setPluginArtifactRepositories (Collections .emptyList ());
254
- writeField (mojo , "project" , mavenProject , true );
262
+ setParentFieldWithReflection (mojo , "project" , mavenProject );
255
263
when (mojoDescriptorCreator .findPluginForPrefix ("help" , session )).thenReturn (plugin );
256
264
when (pluginVersionResolver .resolve (any (PluginVersionRequest .class ))).thenReturn (versionResult );
257
265
when (versionResult .getVersion ()).thenReturn ("1.0" );
258
266
when (pluginManager .getPluginDescriptor (any (Plugin .class ), anyList (), any ()))
259
267
.thenReturn (pd );
260
268
261
- PluginDescriptor returned = (PluginDescriptor ) invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
269
+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
270
+ PluginDescriptor returned = (PluginDescriptor ) lookupPluginDescriptor .invoke (mojo , pi );
262
271
assertEquals (pd , returned );
263
272
264
273
verify (mojoDescriptorCreator ).findPluginForPrefix ("help" , session );
@@ -290,17 +299,15 @@ public void testLookupPluginDescriptorGAV() throws Throwable {
290
299
MavenPluginManager pluginManager = mock (MavenPluginManager .class );
291
300
MavenSession session = mock (MavenSession .class );
292
301
when (session .getRepositorySession ()).thenReturn (mock (RepositorySystemSession .class ));
293
- writeDeclaredField (mojo , "mojoDescriptorCreator" , mojoDescriptorCreator , true );
294
- writeDeclaredField (mojo , "pluginVersionResolver" , pluginVersionResolver , true );
295
- writeDeclaredField (mojo , "pluginManager" , pluginManager , true );
296
- writeField (mojo , "session" , session , true );
302
+ setFieldsOnMojo (mojo , mojoDescriptorCreator , pluginVersionResolver , pluginManager , session );
297
303
MavenProject mavenProject = new MavenProject ();
298
304
mavenProject .setPluginArtifactRepositories (Collections .emptyList ());
299
- writeField (mojo , "project" , mavenProject , true );
305
+ setParentFieldWithReflection (mojo , "project" , mavenProject );
300
306
when (pluginManager .getPluginDescriptor (any (Plugin .class ), anyList (), any ()))
301
307
.thenReturn (pd );
302
308
303
- PluginDescriptor returned = (PluginDescriptor ) invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
309
+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
310
+ PluginDescriptor returned = (PluginDescriptor ) lookupPluginDescriptor .invoke (mojo , pi );
304
311
305
312
assertEquals (pd , returned );
306
313
@@ -320,7 +327,8 @@ public void testLookupPluginDescriptorGMissingA() {
320
327
PluginInfo pi = new PluginInfo ();
321
328
pi .setGroupId ("org.test" );
322
329
try {
323
- invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
330
+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
331
+ lookupPluginDescriptor .invoke (mojo , pi );
324
332
fail ();
325
333
} catch (InvocationTargetException e ) {
326
334
assertTrue (e .getTargetException ().getMessage ().startsWith ("You must specify either" ));
@@ -335,12 +343,56 @@ public void testLookupPluginDescriptorAMissingG() {
335
343
PluginInfo pi = new PluginInfo ();
336
344
pi .setArtifactId ("test" );
337
345
try {
338
- invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
346
+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
347
+ lookupPluginDescriptor .invoke (mojo , pi );
339
348
fail ();
340
349
} catch (InvocationTargetException e ) {
341
350
assertTrue (e .getTargetException ().getMessage ().startsWith ("You must specify either" ));
342
351
} catch (Exception e ) {
343
352
fail (e .getMessage ());
344
353
}
345
354
}
355
+
356
+ private static void setParentFieldWithReflection (
357
+ final DescribeMojo mojo , final String fieldName , final Object value )
358
+ throws NoSuchFieldException , IllegalAccessException {
359
+ final Field field = mojo .getClass ().getSuperclass ().getDeclaredField (fieldName );
360
+ field .setAccessible (true );
361
+ field .set (mojo , value );
362
+ field .setAccessible (false );
363
+ }
364
+
365
+ private static void setFieldWithReflection (final Object mojo , final String fieldName , final Object value )
366
+ throws NoSuchFieldException , IllegalAccessException {
367
+ final Field field = mojo .getClass ().getDeclaredField (fieldName );
368
+ field .setAccessible (true );
369
+ field .set (mojo , value );
370
+ field .setAccessible (false );
371
+ }
372
+
373
+ private static void setFieldsOnMojo (
374
+ final DescribeMojo mojo ,
375
+ final MojoDescriptorCreator mojoDescriptorCreator ,
376
+ final PluginVersionResolver pluginVersionResolver ,
377
+ final MavenPluginManager pluginManager ,
378
+ final MavenSession session )
379
+ throws NoSuchFieldException , IllegalAccessException {
380
+ setFieldWithReflection (mojo , "mojoDescriptorCreator" , mojoDescriptorCreator );
381
+ setFieldWithReflection (mojo , "pluginVersionResolver" , pluginVersionResolver );
382
+ setFieldWithReflection (mojo , "pluginManager" , pluginManager );
383
+ setParentFieldWithReflection (mojo , "session" , session );
384
+ }
385
+
386
+ private static Method setLookupPluginDescriptorAccessibility () throws NoSuchMethodException {
387
+ Method lookupPluginDescriptor =
388
+ DescribeMojo .class .getDeclaredMethod ("lookupPluginDescriptor" , PluginInfo .class );
389
+ lookupPluginDescriptor .setAccessible (true );
390
+ return lookupPluginDescriptor ;
391
+ }
392
+
393
+ private static Method setParsePluginLookupInfoAccessibility () throws NoSuchMethodException {
394
+ Method parsePluginLookupInfo = DescribeMojo .class .getDeclaredMethod ("parsePluginLookupInfo" );
395
+ parsePluginLookupInfo .setAccessible (true );
396
+ return parsePluginLookupInfo ;
397
+ }
346
398
}
0 commit comments