1818 */
1919package org .apache .maven .plugins .help ;
2020
21+ import java .lang .reflect .Field ;
2122import java .lang .reflect .InvocationTargetException ;
23+ import java .lang .reflect .Method ;
2224import java .util .Collections ;
2325
2426import org .apache .maven .execution .MavenSession ;
3739import org .junit .Test ;
3840import org .mockito .ArgumentCaptor ;
3941
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 ;
4342import static org .junit .Assert .*;
4443import static org .mockito .Mockito .*;
4544
@@ -55,7 +54,10 @@ public class DescribeMojoTest {
5554 public void testGetExpressionsRoot () {
5655 try {
5756 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 );
5961 } catch (Throwable e ) {
6062 fail ("The API changes" );
6163 }
@@ -73,7 +75,10 @@ public void testValidExpression() throws Exception {
7375 String ls = System .getProperty ("line.separator" );
7476
7577 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 );
7782
7883 assertEquals (
7984 " Available parameters:" + ls + ls + " name" + ls + " User property: valid.expression" + ls
@@ -96,7 +101,10 @@ public void testInvalidExpression() throws Exception {
96101 String ls = System .getProperty ("line.separator" );
97102
98103 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 );
100108
101109 assertEquals (
102110 " Available parameters:" + ls + ls
@@ -113,11 +121,12 @@ public void testInvalidExpression() throws Exception {
113121 @ Test
114122 public void testParsePluginInfoGAV () throws Throwable {
115123 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" );
119127
120- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
128+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
129+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
121130
122131 assertEquals ("org.test" , pi .getGroupId ());
123132 assertEquals ("test" , pi .getArtifactId ());
@@ -128,28 +137,30 @@ public void testParsePluginInfoGAV() throws Throwable {
128137 @ Test
129138 public void testParsePluginInfoPluginPrefix () throws Throwable {
130139 DescribeMojo mojo = new DescribeMojo ();
131- writeDeclaredField (mojo , "plugin" , "help" , true );
140+ setFieldWithReflection (mojo , "plugin" , "help" );
132141
133- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
142+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
143+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
134144
135145 assertNull (pi .getGroupId ());
136146 assertNull (pi .getArtifactId ());
137147 assertNull (pi .getVersion ());
138148 assertEquals ("help" , pi .getPrefix ());
139149
140- writeDeclaredField (mojo , "plugin" , "help2:::" , true );
150+ setFieldWithReflection (mojo , "plugin" , "help2:::" );
141151
142- pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
152+ pi = (PluginInfo ) parsePluginLookupInfo . invoke (mojo );
143153
144154 assertEquals ("help2" , pi .getPrefix ());
145155 }
146156
147157 @ Test
148158 public void testParsePluginInfoPluginGA () throws Throwable {
149159 DescribeMojo mojo = new DescribeMojo ();
150- writeDeclaredField (mojo , "plugin" , "org.test:test" , true );
160+ setFieldWithReflection (mojo , "plugin" , "org.test:test" );
151161
152- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
162+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
163+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
153164
154165 assertEquals ("org.test" , pi .getGroupId ());
155166 assertEquals ("test" , pi .getArtifactId ());
@@ -160,9 +171,10 @@ public void testParsePluginInfoPluginGA() throws Throwable {
160171 @ Test
161172 public void testParsePluginInfoPluginGAV () throws Throwable {
162173 DescribeMojo mojo = new DescribeMojo ();
163- writeDeclaredField (mojo , "plugin" , "org.test:test:1.0" , true );
174+ setFieldWithReflection (mojo , "plugin" , "org.test:test:1.0" );
164175
165- PluginInfo pi = (PluginInfo ) invokeMethod (mojo , true , "parsePluginLookupInfo" );
176+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
177+ PluginInfo pi = (PluginInfo ) parsePluginLookupInfo .invoke (mojo );
166178
167179 assertEquals ("org.test" , pi .getGroupId ());
168180 assertEquals ("test" , pi .getArtifactId ());
@@ -173,9 +185,10 @@ public void testParsePluginInfoPluginGAV() throws Throwable {
173185 @ Test
174186 public void testParsePluginInfoPluginIncorrect () throws Throwable {
175187 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" );
177189 try {
178- invokeMethod (mojo , "parsePluginLookupInfo" );
190+ Method parsePluginLookupInfo = setParsePluginLookupInfoAccessibility ();
191+ parsePluginLookupInfo .invoke (mojo );
179192 fail ();
180193 } catch (Exception e ) {
181194 // expected
@@ -201,18 +214,16 @@ public void testLookupPluginDescriptorPrefixWithVersion() throws Throwable {
201214 MavenPluginManager pluginManager = mock (MavenPluginManager .class );
202215 MavenSession session = mock (MavenSession .class );
203216 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 );
208218 MavenProject mavenProject = new MavenProject ();
209219 mavenProject .setPluginArtifactRepositories (Collections .emptyList ());
210- writeField (mojo , "project" , mavenProject , true );
220+ setParentFieldWithReflection (mojo , "project" , mavenProject );
211221 when (mojoDescriptorCreator .findPluginForPrefix ("help" , session )).thenReturn (plugin );
212222 when (pluginManager .getPluginDescriptor (any (Plugin .class ), anyList (), any ()))
213223 .thenReturn (pd );
214224
215- PluginDescriptor returned = (PluginDescriptor ) invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
225+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
226+ PluginDescriptor returned = (PluginDescriptor ) lookupPluginDescriptor .invoke (mojo , pi );
216227
217228 assertEquals (pd , returned );
218229
@@ -245,20 +256,18 @@ public void testLookupPluginDescriptorPrefixWithoutVersion() throws Throwable {
245256 PluginVersionResult versionResult = mock (PluginVersionResult .class );
246257 MavenSession session = mock (MavenSession .class );
247258 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 );
252260 MavenProject mavenProject = new MavenProject ();
253261 mavenProject .setPluginArtifactRepositories (Collections .emptyList ());
254- writeField (mojo , "project" , mavenProject , true );
262+ setParentFieldWithReflection (mojo , "project" , mavenProject );
255263 when (mojoDescriptorCreator .findPluginForPrefix ("help" , session )).thenReturn (plugin );
256264 when (pluginVersionResolver .resolve (any (PluginVersionRequest .class ))).thenReturn (versionResult );
257265 when (versionResult .getVersion ()).thenReturn ("1.0" );
258266 when (pluginManager .getPluginDescriptor (any (Plugin .class ), anyList (), any ()))
259267 .thenReturn (pd );
260268
261- PluginDescriptor returned = (PluginDescriptor ) invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
269+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
270+ PluginDescriptor returned = (PluginDescriptor ) lookupPluginDescriptor .invoke (mojo , pi );
262271 assertEquals (pd , returned );
263272
264273 verify (mojoDescriptorCreator ).findPluginForPrefix ("help" , session );
@@ -290,17 +299,15 @@ public void testLookupPluginDescriptorGAV() throws Throwable {
290299 MavenPluginManager pluginManager = mock (MavenPluginManager .class );
291300 MavenSession session = mock (MavenSession .class );
292301 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 );
297303 MavenProject mavenProject = new MavenProject ();
298304 mavenProject .setPluginArtifactRepositories (Collections .emptyList ());
299- writeField (mojo , "project" , mavenProject , true );
305+ setParentFieldWithReflection (mojo , "project" , mavenProject );
300306 when (pluginManager .getPluginDescriptor (any (Plugin .class ), anyList (), any ()))
301307 .thenReturn (pd );
302308
303- PluginDescriptor returned = (PluginDescriptor ) invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
309+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
310+ PluginDescriptor returned = (PluginDescriptor ) lookupPluginDescriptor .invoke (mojo , pi );
304311
305312 assertEquals (pd , returned );
306313
@@ -320,7 +327,8 @@ public void testLookupPluginDescriptorGMissingA() {
320327 PluginInfo pi = new PluginInfo ();
321328 pi .setGroupId ("org.test" );
322329 try {
323- invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
330+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
331+ lookupPluginDescriptor .invoke (mojo , pi );
324332 fail ();
325333 } catch (InvocationTargetException e ) {
326334 assertTrue (e .getTargetException ().getMessage ().startsWith ("You must specify either" ));
@@ -335,12 +343,56 @@ public void testLookupPluginDescriptorAMissingG() {
335343 PluginInfo pi = new PluginInfo ();
336344 pi .setArtifactId ("test" );
337345 try {
338- invokeMethod (mojo , true , "lookupPluginDescriptor" , pi );
346+ Method lookupPluginDescriptor = setLookupPluginDescriptorAccessibility ();
347+ lookupPluginDescriptor .invoke (mojo , pi );
339348 fail ();
340349 } catch (InvocationTargetException e ) {
341350 assertTrue (e .getTargetException ().getMessage ().startsWith ("You must specify either" ));
342351 } catch (Exception e ) {
343352 fail (e .getMessage ());
344353 }
345354 }
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+ }
346398}
0 commit comments