1818 */
1919package org .apache .maven .plugins .checkstyle ;
2020
21- import java .io .File ;
22- import java .util .Arrays ;
23-
21+ import org .apache .maven .api .di .Provides ;
22+ import org .apache .maven .api .plugin .testing .InjectMojo ;
23+ import org .apache .maven .api .plugin .testing .MojoParameter ;
24+ import org .apache .maven .api .plugin .testing .MojoTest ;
2425import org .apache .maven .model .Build ;
25- import org .apache .maven .plugin .Mojo ;
2626import org .apache .maven .plugin .MojoExecutionException ;
2727import org .apache .maven .plugin .MojoFailureException ;
2828import org .apache .maven .plugin .descriptor .PluginDescriptor ;
29- import org .apache .maven .plugin .testing .AbstractMojoTestCase ;
3029import org .apache .maven .plugin .testing .stubs .MavenProjectStub ;
31- import org .junit .jupiter .api .Assertions ;
32- import org .junit .jupiter .api .BeforeEach ;
30+ import org .apache .maven .project .MavenProject ;
3331import org .junit .jupiter .api .Test ;
3432
33+ import javax .inject .Inject ;
34+ import java .io .File ;
35+
3536import static org .junit .jupiter .api .Assertions .fail ;
3637
3738/**
3839 * @author Edwin Punzalan
3940 *
4041 */
41- public class CheckstyleViolationCheckMojoTest extends AbstractMojoTestCase {
42-
43- @ BeforeEach
44- public void setUp () throws Exception {
45- super .setUp ();
46- }
47-
48- @ Test
49- public void testDefaultConfig () throws Exception {
50- File pluginXmlFile = new File (getBasedir (), "src/test/resources/plugin-configs/check-plugin-config.xml" );
51-
52- CheckstyleViolationCheckMojo mojo = (CheckstyleViolationCheckMojo ) lookupMojo ("check" , pluginXmlFile );
53-
54- // mojo setup
55-
56- setVariableValueToObject ((Mojo ) mojo , "project" , new MavenProjectStub () {
57-
58- public File getFile () {
59- return new File (getBasedir (), "target/classes" );
60- }
61-
62- public Build getBuild () {
63- return new Build () {
64- private static final long serialVersionUID = -743084937617131258L ;
42+ @ MojoTest
43+ public class CheckstyleViolationCheckMojoTest {
6544
66- public String getDirectory () {
67- return getBasedir () + "/target/classes" ;
68- }
69- };
70- }
71- });
45+ @ Inject
46+ private MavenProject project ;
47+ @ Inject
48+ private PluginDescriptor plugin ;
7249
73- setVariableValueToObject ((Mojo ) mojo , "configLocation" , "sun_checks.xml" );
74- setVariableValueToObject ((Mojo ) mojo , "cacheFile" , getBasedir () + "/target/classes/checkstyle-cachefile" );
75- setVariableValueToObject (
76- (Mojo ) mojo ,
77- "sourceDirectories" ,
78- Arrays .asList (
79- getBasedir () + "/src/test/plugin-configs/src" )); // new File( getBasedir() + "/target" ) );
80- setVariableValueToObject ((Mojo ) mojo , "inputEncoding" , "UTF-8" );
81- setVariableValueToObject ((Mojo ) mojo , "skipExec" , Boolean .TRUE );
82-
83- Assertions .assertNotNull (mojo , "Mojo not found." );
84-
85- Assertions .assertNotNull (mojo .project , "project null." );
8650
51+ @ InjectMojo (goal = "check" , pom = "src/test/resources/plugin-configs/check-plugin-config.xml" )
52+ @ MojoParameter (name = "configLocation" , value = "sun_checks.xml" )
53+ @ MojoParameter (name = "cacheFile" , value = "/target/classes/checkstyle-cachefile" )
54+ @ MojoParameter (name = "sourceDirectories" , value = "/src/test/plugin-configs/src" )
55+ @ MojoParameter (name = "inputEncoding" , value = "UTF-8" )
56+ @ MojoParameter (name = "skipExec" , value = "true" )
57+ @ Test
58+ public void testDefaultConfig (CheckstyleViolationCheckMojo mojo ) throws Exception {
8759 try {
8860 mojo .execute ();
8961
@@ -93,18 +65,9 @@ public String getDirectory() {
9365 }
9466 }
9567
96- @ Test
97- public void testInvalidFormatWithSkipExec () throws Exception {
98- File pluginXmlFile = new File (getBasedir (), "src/test/resources/plugin-configs/check-plugin-config.xml" );
99-
100- Mojo mojo = lookupMojo ("check" , pluginXmlFile );
101-
102- Assertions .assertNotNull (mojo , "Mojo not found." );
103-
104- // mojo setup
105-
106- setVariableValueToObject (mojo , "project" , new MavenProjectStub () {
107-
68+ @ Provides
69+ public MavenProjectStub projectStub () {
70+ return new MavenProjectStub () {
10871 public File getFile () {
10972 return new File (getBasedir (), "target/classes" );
11073 }
@@ -118,20 +81,18 @@ public String getDirectory() {
11881 }
11982 };
12083 }
121- });
122-
123- setVariableValueToObject (mojo , "configLocation" , "sun_checks.xml" );
124- setVariableValueToObject (mojo , "cacheFile" , getBasedir () + "/target/classes/checkstyle-cachefile" );
125- setVariableValueToObject (
126- mojo ,
127- "sourceDirectories" ,
128- Arrays .asList (
129- getBasedir () + "/src/test/plugin-configs/src" )); // new File( getBasedir() + "/target" ) );
130- setVariableValueToObject (mojo , "inputEncoding" , "UTF-8" );
131- setVariableValueToObject (mojo , "skipExec" , Boolean .TRUE );
132-
133- setVariableValueToObject (mojo , "outputFileFormat" , "plain" );
84+ };
85+ }
13486
87+ @ InjectMojo (goal = "check" , pom = "src/test/resources/plugin-configs/check-plugin-config.xml" )
88+ @ MojoParameter (name = "configLocation" , value = "sun_checks.xml" )
89+ @ MojoParameter (name = "cacheFile" , value = "/target/classes/checkstyle-cachefile" )
90+ @ MojoParameter (name = "sourceDirectories" , value = "/src/test/plugin-configs/src" )
91+ @ MojoParameter (name = "inputEncoding" , value = "UTF-8" )
92+ @ MojoParameter (name = "skipExec" , value = "true" )
93+ @ MojoParameter (name = "outputFileFormat" , value = "plain" )
94+ @ Test
95+ public void testInvalidFormatWithSkipExec (CheckstyleViolationCheckMojo mojo ) throws Exception {
13596 try {
13697 mojo .execute ();
13798
@@ -141,130 +102,59 @@ public String getDirectory() {
141102 }
142103 }
143104
105+ @ InjectMojo (goal = "check" , pom = "src/test/resources/plugin-configs/check-plugin-config.xml" )
106+ @ MojoParameter (name = "configLocation" , value = "sun_checks.xml" )
107+ @ MojoParameter (name = "cacheFile" , value = "/target/classes/checkstyle-cachefile" )
108+ @ MojoParameter (name = "sourceDirectories" , value = "/src/test/plugin-configs/src" )
109+ @ MojoParameter (name = "inputEncoding" , value = "UTF-8" )
110+ @ MojoParameter (name = "skipExec" , value = "true" )
111+ @ MojoParameter (name = "outputFile" , value = "target/NoSuchFile.xml" )
144112 @ Test
145- public void testNoOutputFile () throws Exception {
146- File pluginXmlFile = new File (getBasedir (), "src/test/resources/plugin-configs/check-plugin-config.xml" );
147-
148- Mojo mojo = lookupMojo ("check" , pluginXmlFile );
149-
150- Assertions .assertNotNull (mojo , "Mojo not found." );
151-
152- // mojo setup
153-
154- setVariableValueToObject (mojo , "project" , new MavenProjectStub () {
155-
156- public File getFile () {
157- return new File (getBasedir (), "target/classes" );
158- }
159-
160- public Build getBuild () {
161- return new Build () {
162- private static final long serialVersionUID = -743084937617131258L ;
163-
164- public String getDirectory () {
165- return getBasedir () + "/target/classes" ;
166- }
167- };
168- }
169- });
170-
171- setVariableValueToObject (mojo , "configLocation" , "sun_checks.xml" );
172- setVariableValueToObject (mojo , "cacheFile" , getBasedir () + "/target/classes/checkstyle-cachefile" );
173- setVariableValueToObject (
174- mojo ,
175- "sourceDirectories" ,
176- Arrays .asList (
177- getBasedir () + "/src/test/plugin-configs/src" )); // new File( getBasedir() + "/target" ) );
178- setVariableValueToObject (mojo , "inputEncoding" , "UTF-8" );
179- setVariableValueToObject (mojo , "skipExec" , Boolean .TRUE );
180-
181- setVariableValueToObject (mojo , "outputFile" , new File ("target/NoSuchFile.xml" ));
182-
113+ public void testNoOutputFile (CheckstyleViolationCheckMojo mojo ) throws Exception {
183114 mojo .execute ();
184115 }
185116
186- @ Test
187- public void testPlainOutputFileFailOnError () throws Exception {
188- try {
189- File pluginXmlFile =
190- new File (getBasedir (), "src/test/resources/plugin-configs/check-plugin-plain-output.xml" );
191-
192- Mojo mojo = lookupMojo ("check" , pluginXmlFile );
193-
194- Assertions .assertNotNull (mojo , "Mojo not found." );
195117
196- PluginDescriptor descriptorStub = new PluginDescriptor ();
197- descriptorStub .setGroupId ("org.apache.maven.plugins" );
198- descriptorStub .setArtifactId ("maven-checkstyle-plugin" );
199- setVariableValueToObject (mojo , "plugin" , descriptorStub );
200118
201- setVariableValueToObject (mojo , "failsOnError" , true );
119+ @ InjectMojo (goal = "check" , pom = "src/test/resources/plugin-configs/check-plugin-plain-output.xml" )
120+ @ MojoParameter (name = "failsOnError" , value = "true" )
121+ @ Test
122+ public void testPlainOutputFileFailOnError (CheckstyleViolationCheckMojo mojo ) throws Exception {
123+ try {
124+ mojo .execute ();
202125
203- mojo .execute ();
204-
205- fail ("Must fail on violations" );
206- } catch (MojoExecutionException e ) {
207- // expected
126+ fail ("Must fail on violations" );
127+ } catch (MojoExecutionException e ) {
128+ // expected
129+ }
208130 }
209- }
210131
211- @ Test
212- public void testPlainOutputFile () throws Exception {
213- File pluginXmlFile = new File (getBasedir (), "src/test/resources/plugin-configs/check-plugin-plain-output.xml" );
214-
215- Mojo mojo = lookupMojo ("check" , pluginXmlFile );
216132
217- Assertions .assertNotNull (mojo , "Mojo not found." );
218133
134+ @ Provides
135+ public PluginDescriptor getPluginDescriptor () {
219136 PluginDescriptor descriptorStub = new PluginDescriptor ();
220137 descriptorStub .setGroupId ("org.apache.maven.plugins" );
221138 descriptorStub .setArtifactId ("maven-checkstyle-plugin" );
222- setVariableValueToObject (mojo , "plugin" , descriptorStub );
223-
224- setVariableValueToObject (mojo , "failsOnError" , false );
139+ return descriptorStub ;
140+ }
225141
142+ @ InjectMojo (goal = "check" , pom = "src/test/resources/plugin-configs/check-plugin-plain-output.xml" )
143+ @ MojoParameter (name = "failsOnError" , value = "false" )
144+ @ Test
145+ public void testPlainOutputFile (CheckstyleViolationCheckMojo mojo ) throws Exception {
226146 mojo .execute ();
227147 }
228148
149+ @ InjectMojo (goal = "check" , pom = "src/test/resources/plugin-configs/check-plugin-config.xml" )
150+ @ MojoParameter (name = "configLocation" , value = "sun_checks.xml" )
151+ @ MojoParameter (name = "cacheFile" , value = "/target/classes/checkstyle-cachefile" )
152+ @ MojoParameter (name = "sourceDirectories" , value = "/src/test/plugin-configs/src" )
153+ @ MojoParameter (name = "inputEncoding" , value = "UTF-8" )
154+ @ MojoParameter (name = "skipExec" , value = "true" )
155+ @ MojoParameter (name = "failOnViolation" , value = "false" )
229156 @ Test
230- public void testNoFail () throws Exception {
231- File pluginXmlFile = new File (getBasedir (), "src/test/resources/plugin-configs/check-plugin-config.xml" );
232-
233- Mojo mojo = lookupMojo ("check" , pluginXmlFile );
234-
235- Assertions .assertNotNull (mojo , "Mojo not found." );
236-
237- // mojo setup
238-
239- setVariableValueToObject (mojo , "project" , new MavenProjectStub () {
240-
241- public File getFile () {
242- return new File (getBasedir (), "target/classes" );
243- }
244-
245- public Build getBuild () {
246- return new Build () {
247- private static final long serialVersionUID = -743084937617131258L ;
248-
249- public String getDirectory () {
250- return getBasedir () + "/target/classes" ;
251- }
252- };
253- }
254- });
255-
256- setVariableValueToObject (mojo , "configLocation" , "sun_checks.xml" );
257- setVariableValueToObject (mojo , "cacheFile" , getBasedir () + "/target/classes/checkstyle-cachefile" );
258- setVariableValueToObject (
259- mojo ,
260- "sourceDirectories" ,
261- Arrays .asList (
262- getBasedir () + "/src/test/plugin-configs/src" )); // new File( getBasedir() + "/target" ) );
263- setVariableValueToObject (mojo , "inputEncoding" , "UTF-8" );
264- setVariableValueToObject (mojo , "skipExec" , Boolean .TRUE );
265-
266- setVariableValueToObject (mojo , "failOnViolation" , Boolean .FALSE );
267-
157+ public void testNoFail (CheckstyleViolationCheckMojo mojo ) throws Exception {
268158 mojo .execute ();
269159 }
270160}
0 commit comments