11package org .utplsql .maven .plugin .test ;
22
33import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertFalse ;
45import static org .junit .Assert .assertTrue ;
6+ import static org .mockito .ArgumentMatchers .anyString ;
7+ import static org .mockito .Mockito .mock ;
8+ import static org .mockito .Mockito .times ;
9+ import static org .mockito .Mockito .verify ;
10+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
11+ import static org .powermock .api .mockito .PowerMockito .mockStatic ;
12+ import static org .powermock .api .mockito .PowerMockito .when ;
513
614import java .io .File ;
15+ import java .sql .Connection ;
16+ import java .util .ArrayList ;
717import java .util .List ;
818
19+ import org .apache .commons .lang3 .tuple .Pair ;
920import org .apache .maven .plugin .MojoExecutionException ;
1021import org .apache .maven .plugin .testing .MojoRule ;
1122import org .junit .Assert ;
23+ import org .junit .Before ;
1224import org .junit .Rule ;
1325import org .junit .Test ;
1426import org .junit .rules .ExpectedException ;
27+ import org .junit .runner .RunWith ;
28+ import org .mockito .Mock ;
29+ import org .powermock .core .classloader .annotations .PrepareForTest ;
30+ import org .powermock .modules .junit4 .PowerMockRunner ;
1531import org .powermock .reflect .Whitebox ;
32+ import org .utplsql .api .DBHelper ;
1633import org .utplsql .api .FileMapperOptions ;
34+ import org .utplsql .api .Version ;
35+ import org .utplsql .api .reporter .Reporter ;
36+ import org .utplsql .api .reporter .ReporterFactory ;
1737import org .utplsql .maven .plugin .UtPLSQLMojo ;
38+ import org .utplsql .maven .plugin .model .ReporterParameter ;
39+ import org .utplsql .maven .plugin .reporter .ReporterWriter ;
1840
41+ @ RunWith (PowerMockRunner .class )
42+ @ PrepareForTest ({
43+ DBHelper .class ,
44+ ReporterFactory .class })
1945public class UtPLSQLMojoTest {
2046
2147 @ Rule
@@ -24,6 +50,24 @@ public class UtPLSQLMojoTest {
2450 @ Rule
2551 public ExpectedException thrown = ExpectedException .none ();
2652
53+ @ Mock
54+ public Connection mockConnection ;
55+
56+ @ Mock
57+ public Version mockVersion ;
58+
59+ @ Mock
60+ public ReporterFactory mockReporterFactory ;
61+
62+ @ Before
63+ public void setUp () throws Exception {
64+ mockStatic (DBHelper .class );
65+ when (DBHelper .getDatabaseFrameworkVersion (mockConnection )).thenReturn (mockVersion );
66+
67+ mockStatic (ReporterFactory .class );
68+ when (ReporterFactory .createEmpty ()).thenReturn (mockReporterFactory );
69+ }
70+
2771 /**
2872 * testInvalidSourcesDirectory.
2973 *
@@ -210,5 +254,48 @@ public void testSourcesAndTestsParameterHaveNotIncludesTag() throws Exception {
210254 assertTrue (tests .getFilePaths ().contains ("src/test/bar/f2.pkg" ));
211255 }
212256
257+ @ Test
258+ public void testDefaultConsoleBehaviour () throws Exception {
259+ UtPLSQLMojo utplsqlMojo = (UtPLSQLMojo ) rule .lookupConfiguredMojo (new File ("src/test/resources/defaultConsoleOutputBehaviour/" ), "test" );
260+ Assert .assertNotNull (utplsqlMojo );
261+
262+ List <Reporter > reporterList = new ArrayList <>();
263+ when (mockReporterFactory .createReporter (anyString ())).thenAnswer (invocation -> {
264+ Reporter mockReporter = mock (Reporter .class );
265+ reporterList .add (mockReporter );
266+ return mockReporter ;
267+ });
268+
269+ Whitebox .invokeMethod (utplsqlMojo , "initReporters" , mockConnection );
270+
271+ // Assert that we called the create reporter with the correct parameters.
272+ verify (mockReporterFactory , times (2 )).createReporter ("UT_DOCUMENTATION_REPORTER" );
273+ verify (mockReporterFactory ).createReporter ("UT_COVERAGE_SONAR_REPORTER" );
274+ verify (mockReporterFactory ).createReporter ("UT_SONAR_TEST_REPORTER" );
275+ verifyNoMoreInteractions (mockReporterFactory );
276+
277+ // Assert that all reporters have been initialized.
278+ for (Reporter mockReporter : reporterList ) {
279+ verify (mockReporter ).init (mockConnection );
280+ verifyNoMoreInteractions (mockReporter );
281+ }
282+
283+ // Assert that we added only the necessary reporters to the writer.
284+ ReporterWriter reporterWritter = Whitebox .getInternalState (utplsqlMojo , "reporterWriter" );
285+ List <Pair <Reporter , ReporterParameter >> listReporters = Whitebox .getInternalState (reporterWritter , "listReporters" );
286+ assertEquals (3 , listReporters .size ());
287+
288+ ReporterParameter reporterParameter1 = listReporters .get (0 ).getRight ();
289+ assertTrue (reporterParameter1 .isConsoleOutput ());
290+ assertFalse (reporterParameter1 .isFileOutput ());
291+
292+ ReporterParameter reporterParameter2 = listReporters .get (1 ).getRight ();
293+ assertFalse (reporterParameter2 .isConsoleOutput ());
294+ assertTrue (reporterParameter2 .isFileOutput ());
295+
296+ ReporterParameter reporterParameter3 = listReporters .get (2 ).getRight ();
297+ assertTrue (reporterParameter3 .isConsoleOutput ());
298+ assertTrue (reporterParameter3 .isFileOutput ());
299+ }
213300
214301}
0 commit comments