11package org .utplsql .maven .plugin ;
22
3- import org .apache .commons .lang3 .tuple .Pair ;
43import org .apache .maven .plugin .MojoExecutionException ;
54import org .apache .maven .plugin .testing .MojoRule ;
6- import org .junit .Before ;
75import org .junit .Rule ;
86import org .junit .Test ;
9- import org .junit .runner .RunWith ;
10- import org .mockito .Mock ;
11- import org .powermock .core .classloader .annotations .PrepareForTest ;
12- import org .powermock .modules .junit4 .PowerMockRunner ;
13- import org .utplsql .api .DBHelper ;
147import org .utplsql .api .FileMapperOptions ;
15- import org .utplsql .api .Version ;
16- import org .utplsql .api .reporter .Reporter ;
17- import org .utplsql .api .reporter .ReporterFactory ;
18- import org .utplsql .maven .plugin .io .ReportWriter ;
198import org .utplsql .maven .plugin .model .ReporterParameter ;
209
2110import java .io .ByteArrayOutputStream ;
2211import java .io .File ;
2312import java .io .PrintStream ;
24- import java .sql .Connection ;
25- import java .util .ArrayList ;
2613import java .util .List ;
2714
2815import static org .junit .Assert .assertEquals ;
2916import static org .junit .Assert .assertFalse ;
3017import static org .junit .Assert .assertNotNull ;
3118import static org .junit .Assert .assertThrows ;
3219import static org .junit .Assert .assertTrue ;
33- import static org .mockito .ArgumentMatchers .anyString ;
34- import static org .mockito .Mockito .mock ;
35- import static org .mockito .Mockito .times ;
36- import static org .mockito .Mockito .verify ;
37- import static org .mockito .Mockito .verifyNoMoreInteractions ;
38- import static org .powermock .api .mockito .PowerMockito .mockStatic ;
39- import static org .powermock .api .mockito .PowerMockito .when ;
40-
41- @ RunWith (PowerMockRunner .class )
42- @ PrepareForTest ({DBHelper .class , ReporterFactory .class })
20+
4321public class MojoRuleTest {
4422
4523 @ Rule
4624 public final MojoRule rule = new MojoRule ();
4725
48- @ Mock
49- public Connection mockConnection ;
50-
51- @ Mock
52- public Version mockVersion ;
53-
54- @ Mock
55- public ReporterFactory mockReporterFactory ;
56-
57- @ Before
58- public void setUp () throws Exception {
59- mockStatic (DBHelper .class );
60- when (DBHelper .getDatabaseFrameworkVersion (mockConnection )).thenReturn (mockVersion );
61-
62- mockStatic (ReporterFactory .class );
63- when (ReporterFactory .createEmpty ()).thenReturn (mockReporterFactory );
64- }
65-
6626 /**
6727 * Invalid Sources Directory
6828 * <p>
@@ -72,7 +32,7 @@ public void setUp() throws Exception {
7232 */
7333 @ Test
7434 public void invalidSourcesDirectory () throws Exception {
75- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
35+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
7636 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/invalid-sources-directories/" ), "test" );
7737 assertNotNull (utplsqlMojo );
7838
@@ -90,7 +50,7 @@ public void invalidSourcesDirectory() throws Exception {
9050 */
9151 @ Test
9252 public void invalidTestsDirectory () throws Exception {
93- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
53+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
9454 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/invalid-tests-sources-directories/" ), "test" );
9555 assertNotNull (utplsqlMojo );
9656
@@ -108,7 +68,7 @@ public void invalidTestsDirectory() throws Exception {
10868 */
10969 @ Test
11070 public void sourcesTestsParameters () throws Exception {
111- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
71+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
11272 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/test-sources-tests-params/" ), "test" );
11373 assertNotNull (utplsqlMojo );
11474
@@ -151,7 +111,7 @@ public void sourcesTestsParameters() throws Exception {
151111 */
152112 @ Test
153113 public void sourcesAndTestsParameterDoesNotExist () throws Exception {
154- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule .lookupConfiguredMojo (
114+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule .lookupConfiguredMojo (
155115 new File ("src/test/resources/unit-tests/test-no-sources-tests-params/directory-does-not-exist/" ), "test" );
156116 assertNotNull (utplsqlMojo );
157117
@@ -173,7 +133,7 @@ public void sourcesAndTestsParameterDoesNotExist() throws Exception {
173133 */
174134 @ Test
175135 public void sourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists () throws Exception {
176- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
136+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
177137 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/test-no-sources-tests-params/directory-exists/" ), "test" );
178138 assertNotNull (utplsqlMojo );
179139
@@ -199,7 +159,7 @@ public void sourcesAndTestsParameterDoesNotExistButDefaultDirectoryExists() thro
199159 */
200160 @ Test
201161 public void sourcesAndTestsParameterHaveNotDirectoryTag () throws Exception {
202- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
162+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
203163 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/partial-source-and-test-tag/missing-directory/" ), "test" );
204164 assertNotNull (utplsqlMojo );
205165
@@ -226,7 +186,7 @@ public void sourcesAndTestsParameterHaveNotDirectoryTag() throws Exception {
226186 */
227187 @ Test
228188 public void sourcesAndTestsParameterHaveNotIncludesTag () throws Exception {
229- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
189+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
230190 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/partial-source-and-test-tag/missing-includes/" ), "test" );
231191 assertNotNull (utplsqlMojo );
232192
@@ -248,83 +208,50 @@ public void sourcesAndTestsParameterHaveNotIncludesTag() throws Exception {
248208 */
249209 @ Test
250210 public void defaultConsoleBehaviour () throws Exception {
251- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
211+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
252212 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/default-console-output-behaviour/" ), "test" );
253213 assertNotNull (utplsqlMojo );
254214
255- List <Reporter > reporterList = new ArrayList <>();
256- when (mockReporterFactory .createReporter (anyString ())).thenAnswer (invocation -> {
257- Reporter mockReporter = mock (Reporter .class );
258- reporterList .add (mockReporter );
259- return mockReporter ;
260- });
261-
262- ReportWriter reportWriter = new ReportWriter (utplsqlMojo .targetDir , mockVersion , utplsqlMojo .getLog ());
263- utplsqlMojo .initReporters (mockConnection , reportWriter , mockReporterFactory );
264-
265- // Assert that we called the creation reporter with the correct parameters.
266- verify (mockReporterFactory , times (2 )).createReporter ("UT_DOCUMENTATION_REPORTER" );
267- verify (mockReporterFactory ).createReporter ("UT_COVERAGE_SONAR_REPORTER" );
268- verify (mockReporterFactory ).createReporter ("UT_SONAR_TEST_REPORTER" );
269- verifyNoMoreInteractions (mockReporterFactory );
270-
271- // Assert that all reporters have been initialized.
272- for (Reporter mockReporter : reporterList ) {
273- verify (mockReporter ).init (mockConnection );
274- verifyNoMoreInteractions (mockReporter );
275- }
215+ utplsqlMojo .execute ();
276216
277217 // Assert that we added only the necessary reporters to the writer.
278- List <Pair < Reporter , ReporterParameter >> reporters = reportWriter . getReporters () ;
218+ List <ReporterParameter > reporters = utplsqlMojo . reporters ;
279219 assertEquals (3 , reporters .size ());
280220
281- ReporterParameter reporterParameter1 = reporters .get (0 ). getRight () ;
221+ ReporterParameter reporterParameter1 = reporters .get (0 );
282222 assertTrue (reporterParameter1 .isConsoleOutput ());
283223 assertFalse (reporterParameter1 .isFileOutput ());
284224
285- ReporterParameter reporterParameter2 = reporters .get (1 ). getRight () ;
225+ ReporterParameter reporterParameter2 = reporters .get (1 );
286226 assertFalse (reporterParameter2 .isConsoleOutput ());
287227 assertTrue (reporterParameter2 .isFileOutput ());
288228
289- ReporterParameter reporterParameter3 = reporters .get (2 ). getRight () ;
229+ ReporterParameter reporterParameter3 = reporters .get (2 );
290230 assertTrue (reporterParameter3 .isConsoleOutput ());
291231 assertTrue (reporterParameter3 .isFileOutput ());
292232 }
293233
294234 /**
295- * Add Default Reporter
235+ * Default Reporter
296236 */
297237 @ Test
298- public void addDefaultReporter () throws Exception {
299- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
300- .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/default-console-output-behaviour /" ), "test" );
238+ public void defaultReporter () throws Exception {
239+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
240+ .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/default-reporter /" ), "test" );
301241 assertNotNull (utplsqlMojo );
302242
303- List <Reporter > reporterList = new ArrayList <>();
304- when (mockReporterFactory .createReporter (anyString ())).thenAnswer (invocation -> {
305- Reporter mockReporter = mock (Reporter .class );
306- when (mockReporter .getTypeName ()).thenReturn (invocation .getArgument (0 ));
307- reporterList .add (mockReporter );
308- return mockReporter ;
309- });
310-
311- List <ReporterParameter > reporterParameters = utplsqlMojo .reporters ;
312- reporterParameters .clear ();
313-
314- ReportWriter reportWriter = new ReportWriter (utplsqlMojo .targetDir , mockVersion , utplsqlMojo .getLog ());
315- utplsqlMojo .initReporters (mockConnection , reportWriter , mockReporterFactory );
243+ utplsqlMojo .execute ();
316244
317- assertEquals (1 , reporterList .size ());
318- assertEquals ("UT_DOCUMENTATION_REPORTER" , reporterList .get (0 ).getTypeName ());
319- verify (reporterList .get (0 )).init (mockConnection );
245+ assertEquals (1 , utplsqlMojo .reporters .size ());
246+ assertEquals ("UT_DOCUMENTATION_REPORTER" , utplsqlMojo .reporters .get (0 ).getName ());
320247 }
321248
322249 /**
323250 * Skip utPLSQL Tests
324251 */
325252 @ Test
326253 public void skipUtplsqlTests () throws Exception {
327- UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule
254+ UtPlsqlMojo utplsqlMojo = (UtPlsqlMojo ) rule
328255 .lookupConfiguredMojo (new File ("src/test/resources/unit-tests/skip-utplsql-tests/" ), "test" );
329256 assertNotNull (utplsqlMojo );
330257
0 commit comments