11package org .utplsql ;
22
3+ import static java .lang .String .format ;
4+
5+ import java .io .File ;
6+ import java .io .FileOutputStream ;
7+ import java .io .IOException ;
8+ import java .io .PrintStream ;
39import java .sql .Connection ;
410import java .sql .DriverManager ;
511import java .sql .SQLException ;
612import java .util .ArrayList ;
13+ import java .util .HashMap ;
714import java .util .List ;
15+ import java .util .Map ;
816import java .util .function .Consumer ;
917
18+ import org .apache .commons .lang3 .StringUtils ;
1019import org .apache .maven .model .Resource ;
1120import org .apache .maven .plugin .AbstractMojo ;
1221import org .apache .maven .plugin .MojoExecutionException ;
1625import org .utplsql .api .FileMapperOptions ;
1726import org .utplsql .api .OutputBuffer ;
1827import org .utplsql .api .TestRunner ;
28+ import org .utplsql .api .exception .SomeTestsFailedException ;
1929import org .utplsql .api .reporter .Reporter ;
2030import org .utplsql .api .reporter .ReporterFactory ;
31+ import org .utplsql .helper .PluginDefault ;
2132import org .utplsql .helper .SQLScannerHelper ;
33+ import org .utplsql .model .ReporterConfiguration ;
34+ import org .utplsql .model .ReporterParameter ;
35+
2236
2337@ Mojo (name = "test" , defaultPhase = LifecyclePhase .TEST )
24- public class UtPLSQLMojo extends AbstractMojo
25- {
38+ public class UtPLSQLMojo extends AbstractMojo {
2639 @ Parameter (defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3" )
27- private String url ;
40+ private String url ;
2841
2942 @ Parameter (defaultValue = "ut3" )
30- private String user ;
43+ private String user ;
3144
3245 @ Parameter (defaultValue = "XNtxj8eEgA6X6b6f" )
33- private String password ;
46+ private String password ;
47+
48+ @ Parameter
49+ private String includeObject ;
50+
51+ @ Parameter
52+ private String excludeObject ;
53+
54+ @ Parameter (defaultValue = "${maven.test.failure.ignore}" )
55+ private boolean ignoreFailure ;
3456
35- // This parameter should honor the -Dmaven.test.failure.ignore=false
3657 @ Parameter (defaultValue = "false" )
37- private boolean failOnErrors ;
58+ private boolean colorConsole ;
3859
3960 @ Parameter (defaultValue = "false" )
40- private boolean colorConsole ;
61+ private boolean skipCompatibilityCheck ;
4162
42- @ Parameter (defaultValue = "UT_DOCUMENTATION_REPORTER" )
43- private List <String > reporters ;
63+ @ Parameter
64+ private List <ReporterParameter > reporters ;
65+ private Map <String , ReporterParameter > mapReporters = new HashMap <>();
4466
4567 @ Parameter
4668 private List <String > paths ;
4769
4870 @ Parameter
49- private List <Resource > sources = new ArrayList <>();
71+ private List <Resource > sources = new ArrayList <>();
5072
5173 @ Parameter
52- private List <Resource > tests = new ArrayList <>();
53-
54- /**
55- *
56- * Execute the plugin
57- *
58- */
59- @ Override
60- public void execute () throws MojoExecutionException
61- {
62- // List<String> testPaths = Arrays.asList(new String[] { "tests" });
63-
64- FileMapperOptions sourceMappingOptions = new FileMapperOptions (new ArrayList <>());
65- FileMapperOptions testMappingOptions = new FileMapperOptions (new ArrayList <>()); // buildOptions(tests,
66- // PluginDefault.buildDefaultTest());
67-
68- Connection connection = null ;
69- List <Reporter > reporterList = null ;
70-
71- try
72- {
73- connection = DriverManager .getConnection (url , user , password );
74-
75- // Init Reporters
76- reporterList = initReporters (connection );
74+ private List <Resource > tests = new ArrayList <>();
75+
76+ @ Parameter (defaultValue = "${project.build.directory}" , readonly = true )
77+ private String targetDir ;
78+
79+ /**
80+ *
81+ * Execute the plugin
82+ *
83+ */
84+ @ Override
85+ public void execute () throws MojoExecutionException {
86+ FileMapperOptions sourceMappingOptions = buildOptions (sources , PluginDefault .buildDefaultSource ());
87+ FileMapperOptions testMappingOptions = buildOptions (tests , PluginDefault .buildDefaultTest ());
88+
89+ Connection connection = null ;
90+ List <Reporter > reporterList = null ;
91+ try {
92+ connection = DriverManager .getConnection (url , user , password );
93+ reporterList = initReporters (connection );
94+
7795 if (getLog ().isDebugEnabled ()) {
78- StringBuilder msg = new StringBuilder ();
79- msg .append ("Invoking TestRunner with: " ).append ('\n' );
80- msg .append ("reporters=" );
81- reporterList .forEach (new Consumer <Reporter >() {
82- @ Override
83- public void accept (Reporter t ) {
84- try {
85- msg .append (t .getSQLTypeName ()).append (", " );
86- } catch (Exception e ) {
87- // NA
88- }
89- }
90-
91- });
92- msg .append ('\n' );
93- msg .append ("sources=" );
94- sourceMappingOptions .getFilePaths ().forEach (new Consumer <String >() {
95- @ Override
96- public void accept (String t ) {
97- msg .append (t ).append (", " );
98- }
99- });
100- msg .append ('\n' );
101- msg .append ("tests=" );
102- testMappingOptions .getFilePaths ().forEach (new Consumer <String >() {
103- @ Override
104- public void accept (String t ) {
105- msg .append (t ).append (", " );
106- }
107- });
108- msg .append ('\n' );
109- getLog ().debug (msg .toString ());
96+ dumpParameters (sourceMappingOptions , testMappingOptions , reporterList );
11097 }
11198
112- TestRunner runner = new TestRunner ()
99+ TestRunner runner = new TestRunner ()
113100 .addPathList (paths )
114101 .addReporterList (reporterList )
115- .sourceMappingOptions (sourceMappingOptions )
116- .testMappingOptions (testMappingOptions )
117- .colorConsole (colorConsole )
118- .failOnErrors (failOnErrors );
119-
120- runner .run (connection );
121-
122- }
123- catch (Exception e )
124- {
125- getLog ().error (e );
126- throw new MojoExecutionException ("Unexpected error executing plugin " + e .getMessage ());
127- }
128- finally
129- {
130- try
131- {
102+ .sourceMappingOptions (sourceMappingOptions )
103+ .testMappingOptions (testMappingOptions )
104+ // .excludeObject(excludeObject)
105+ .includeObject (includeObject )
106+ .skipCompatibilityCheck (skipCompatibilityCheck )
107+ .colorConsole (colorConsole )
108+ .failOnErrors (!ignoreFailure );
109+
110+ runner .run (connection );
111+
112+ } catch (SomeTestsFailedException e ) {
113+ getLog ().error (e );
114+ throw new MojoExecutionException (e .getMessage ());
115+ } catch (SQLException e ) {
116+ getLog ().error (e );
117+ throw new MojoExecutionException (e .getMessage (), e );
118+ } finally {
119+ try {
132120 for (Reporter reporter : reporterList ) {
133- new OutputBuffer ( reporter ). printAvailable ( connection , System . out );
121+ writeReporter ( connection , reporter );
134122 }
135123 if (connection != null ) {
136- connection .close ();
124+ connection .close ();
137125 }
138- }
139- catch (Exception e )
140- {
141- getLog ().error ("Error" , e );
142- }
143- }
144- }
145-
146- /**
147- *
148- * @param resources
149- * @return
150- */
151- private FileMapperOptions buildOptions (List <Resource > resources , Resource defaultResource )
152- {
153- // Check if this element is empty
154- if (resources .isEmpty ())
155- {
156- resources .add (defaultResource );
157- }
158-
159- List <String > scripts = SQLScannerHelper .findSQLs (resources );
126+ } catch (Exception e ) {
127+ getLog ().error (e .getMessage (), e );
128+ }
129+ }
130+ }
131+
132+ private void writeReporter (Connection connection , Reporter reporter ) throws SQLException , IOException {
133+ String outputFile = mapReporters .get (reporter .getReporterId ()).getConfiguration ().getOutputFile ();
134+ if (StringUtils .isNotBlank (outputFile )) {
135+ File file = new File (outputFile );
136+ if (!file .isAbsolute ()) {
137+ file = new File (targetDir , outputFile );
138+ }
139+ if (!file .getParentFile ().exists ()) {
140+ file .getParentFile ().mkdirs ();
141+ }
142+ try (FileOutputStream fout = new FileOutputStream (file )) {
143+ getLog ().info (format ("Writing report to %s" , file .getAbsolutePath ()));
144+ new OutputBuffer (reporter ).printAvailable (connection , new PrintStream (fout ));
145+ }
146+ } else {
147+ new OutputBuffer (reporter ).printAvailable (connection , System .out );
148+ }
149+ }
150+
151+ /**
152+ *
153+ * @param resources
154+ * @return
155+ */
156+ private FileMapperOptions buildOptions (List <Resource > resources , Resource defaultResource ) {
157+ // Check if this element is empty
158+ if (resources .isEmpty ()) {
159+ resources .add (defaultResource );
160+ }
161+
162+ List <String > scripts = SQLScannerHelper .findSQLs (resources );
160163 return new FileMapperOptions (scripts );
161- }
162-
163- /**
164- * Init all the reports
165- *
166- * @param connection
167- * @return
168- * @throws SQLException
169- */
170- private List <Reporter > initReporters (Connection connection ) throws SQLException
171- {
172- List <Reporter > reporterList = new ArrayList <>();
173-
174- for (String reporterId : reporters )
175- {
176- Reporter reporter = ReporterFactory .createReporter (reporterId );
177- reporter .init (connection );
178-
179- reporterList .add (reporter );
180- }
181-
182- return reporterList ;
183- }
164+ }
165+
166+ /**
167+ * Init all the reports
168+ *
169+ * @param connection
170+ * @return
171+ * @throws SQLException
172+ */
173+ private List <Reporter > initReporters (Connection connection ) throws SQLException {
174+ List <Reporter > reporterList = new ArrayList <>();
175+
176+ for (ReporterParameter reporterParameter : reporters ) {
177+ String reporterName = reporterParameter .getId ().name ();
178+ Reporter reporter = ReporterFactory .createReporter (reporterName );
179+ reporter .init (connection );
180+ reporterList .add (reporter );
181+ if (reporterParameter .getConfiguration () == null
182+ || StringUtils .isEmpty (reporterParameter .getConfiguration ().getOutputFile ())) {
183+ reporterParameter
184+ .setConfiguration (new ReporterConfiguration (reporterParameter .getId ().getOutputFile ()));
185+ }
186+ mapReporters .put (reporter .getReporterId (), reporterParameter );
187+ }
188+
189+ return reporterList ;
190+ }
191+
192+ private void dumpParameters (FileMapperOptions sourceMappingOptions , FileMapperOptions testMappingOptions ,
193+ List <Reporter > reporterList ) {
194+ StringBuilder msg = new StringBuilder ();
195+ msg .append ("Invoking TestRunner with: " ).append ('\n' );
196+ msg .append ("reporters=" );
197+ reporterList .forEach (new Consumer <Reporter >() {
198+ @ Override
199+ public void accept (Reporter t ) {
200+ try {
201+ msg .append (t .getSQLTypeName ()).append (", " );
202+ } catch (Exception e ) {
203+ // NA
204+ }
205+ }
206+
207+ });
208+ msg .append ('\n' );
209+ msg .append ("sources=" );
210+ sourceMappingOptions .getFilePaths ().forEach (new Consumer <String >() {
211+ @ Override
212+ public void accept (String t ) {
213+ msg .append (t ).append (", " );
214+ }
215+ });
216+ msg .append ('\n' );
217+ msg .append ("tests=" );
218+ testMappingOptions .getFilePaths ().forEach (new Consumer <String >() {
219+ @ Override
220+ public void accept (String t ) {
221+ msg .append (t ).append (", " );
222+ }
223+ });
224+ msg .append ('\n' );
225+ getLog ().debug (msg .toString ());
226+ }
184227}
0 commit comments