Skip to content

Commit bdb8c10

Browse files
* Modified Reporters Configuration
- Parametrized Name, OutputFile, Console * Removed default values for connections and use the same as the Java API * Added fully maven test in order to accomplish maven testing - The only drawback is that we have the parameter connection inside the pom test. * Updated the Readme of project
1 parent 6b179c0 commit bdb8c10

13 files changed

Lines changed: 669 additions & 219 deletions

File tree

‎README.md‎

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
# utPLSQL-maven-plugin
22
A maven plugin for running Unit Tests with utPLSQL v3+
33

4+
### Compatibility
45

5-
### Configuration
6+
This plugin is compatible with the Java-API 3.0.4.
7+
8+
### Prerequisites
9+
You have to be a fully utPLSQL environment available compatible with the Java API.
10+
11+
12+
### Plugin Parameters
613

714
* `url`
815
* URL of the Connection to the database
9-
* Default: `jdbc:oracle:thin:@localhost:1521:ut3`
16+
* Default: `${dbURL}`
1017
* `user`
1118
* Credential of the connection to the database
12-
* Default: `ut3`
19+
* Default: `${dbUser}`
1320
* `password`
1421
* Password of the connection to the database
15-
* Default: `XNtxj8eEgA6X6b6f`
22+
* Default: `${dbPass}`
1623
* `ignoreFailure`
1724
* Ignore or continue when a test fail
1825
* Default: `${maven.test.failure.ignore}`
19-
* `colorConsole`
20-
* Colors in the console
21-
* `true` | `false` (default: `false`)
2226
* `skipCompatibilityCheck`
2327
* Skip the Compatibility Checks
2428
* `true` | `false` (default: `false`)
2529
* `reporters`
26-
* List of the Reporters
27-
* Enumeration : `UT_DOCUMENTATION_REPORTER`, `UT_COVERAGE_HTML_REPORTER`, `UT_TEAMCITY_REPORTER`, `UT_XUNIT_REPORTER`, `UT_COVERALLS_REPORTER`, `UT_COVERAGE_SONAR_REPORTER`, `UT_SONAR_TEST_REPORTER`
30+
* List of the Reporters (Check the example below).
31+
* You can pass the name of the reporter and/or the output file of the reporter and/or if the report is logged to the console
32+
* This is a current list of the names of the reporters (See UtPLSQL documentation in order to check the final list): `UT_DOCUMENTATION_REPORTER`, `UT_COVERAGE_HTML_REPORTER`, `UT_TEAMCITY_REPORTER`, `UT_XUNIT_REPORTER`, `UT_COVERALLS_REPORTER`, `UT_COVERAGE_SONAR_REPORTER`, `UT_SONAR_TEST_REPORTER`
33+
* Check the example below
2834

2935
* `paths`
3036
* Paths of the resources
@@ -42,53 +48,54 @@ A maven plugin for running Unit Tests with utPLSQL v3+
4248

4349

4450

45-
### Example
51+
### Sample of use
52+
The next snippet is a sample of declaration of the pom
4653
```xml
47-
<plugin>
48-
<groupId>${pom.groupId}</groupId>
54+
<plugin>
55+
<groupId>org.utplsql</groupId>
4956
<artifactId>utplsql-maven-plugin</artifactId>
50-
<version>${pom.version}</version>
57+
<version>1.0.0-SNAPSHOT</version>
5158
<goals>
5259
<goal>test</goal>
5360
</goals>
5461
<configuration>
55-
<!-- Mandatory Attributes -->
5662
<url>url_of_connection</url>
5763
<user>user</user>
5864
<password>password</password>
59-
6065
<failOnErrors>false</failOnErrors>
61-
<colorConsole>true</colorConsole>
62-
6366
<reporters>
64-
<reporter>UT_COVERAGE_SONAR_REPORTER</reporter>
65-
<reporter>UT_SONAR_TEST_REPORTER</reporter>
67+
<reporter>
68+
<name>UT_COVERAGE_SONAR_REPORTER</name>
69+
<fileOutput>utplsql/coverage-sonar-reporter.xml</fileOutput>
70+
<consoleOutput>true</consoleOutput>
71+
</reporter>
72+
<reporter>
73+
<name>UT_SONAR_TEST_REPORTER</name>
74+
<fileOutput>utplsql/sonar-test-reporter.xml</fileOutput>
75+
<consoleOutput>false</consoleOutput>
76+
</reporter>
77+
<reporter>
78+
<name>UT_TEAMCITY_REPORTER</name>
79+
</reporter>
6680
</reporters>
6781
<sources>
6882
<source>
69-
<directory>SampleDirectory</directory>
83+
<directory>src/test/resources/scripts/sources</directory>
7084
<includes>
7185
<include>**/*pkg</include>
7286
<include>**/*pkb</include>
7387
</includes>
74-
</source>
75-
<source>
76-
<directory>SampleDirectory2</directory>
77-
<includes>
78-
<include>**/*pkb</include>
79-
</includes>
8088
</source>
8189
</sources>
82-
8390
<tests>
8491
<test>
85-
<directory>src/main/test/test1</directory>
92+
<directory>src/test/resources/scripts/test</directory>
8693
<includes>
8794
<include>**/*pkg</include>
95+
<include>**/*pkb</include>
8896
</includes>
8997
</test>
90-
</tests>
91-
98+
</tests>
9299
</configuration>
93100
</plugin>
94101
```

‎utplsql-maven-plugin/src/main/java/org/utplsql/UtPLSQLMojo.java‎

Lines changed: 41 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22

33
import static java.lang.String.format;
44

5-
import java.io.File;
6-
import java.io.FileOutputStream;
7-
import java.io.IOException;
8-
import java.io.PrintStream;
95
import java.sql.Connection;
106
import java.sql.DriverManager;
117
import java.sql.SQLException;
128
import java.util.ArrayList;
13-
import java.util.HashMap;
149
import java.util.List;
15-
import java.util.Map;
1610

1711
import org.apache.commons.lang3.StringUtils;
1812
import org.apache.maven.model.Resource;
@@ -23,15 +17,14 @@
2317
import org.apache.maven.plugins.annotations.Mojo;
2418
import org.apache.maven.plugins.annotations.Parameter;
2519
import org.utplsql.api.FileMapperOptions;
26-
import org.utplsql.api.OutputBuffer;
2720
import org.utplsql.api.TestRunner;
2821
import org.utplsql.api.exception.SomeTestsFailedException;
2922
import org.utplsql.api.reporter.Reporter;
3023
import org.utplsql.api.reporter.ReporterFactory;
3124
import org.utplsql.helper.PluginDefault;
3225
import org.utplsql.helper.SQLScannerHelper;
33-
import org.utplsql.model.ReporterConfiguration;
3426
import org.utplsql.model.ReporterParameter;
27+
import org.utplsql.reporter.ReporterWriter;
3528

3629
/**
3730
* This class expose the {@link TestRunner} interface to Maven.
@@ -42,13 +35,13 @@
4235
@Mojo(name = "test", defaultPhase = LifecyclePhase.TEST)
4336
public class UtPLSQLMojo extends AbstractMojo
4437
{
45-
@Parameter(defaultValue = "jdbc:oracle:thin:@localhost:1521:ut3")
38+
@Parameter(defaultValue = "${dbUrl}")
4639
private String url;
4740

48-
@Parameter(defaultValue = "ut3")
41+
@Parameter(defaultValue = "${dbUser}")
4942
private String user;
5043

51-
@Parameter(defaultValue = "XNtxj8eEgA6X6b6f")
44+
@Parameter(defaultValue = "${dbPass}")
5245
private String password;
5346

5447
@Parameter
@@ -57,18 +50,11 @@ public class UtPLSQLMojo extends AbstractMojo
5750
@Parameter
5851
private String excludeObject;
5952

60-
@Parameter(defaultValue = "${maven.test.failure.ignore}")
61-
private boolean ignoreFailure;
62-
63-
@Parameter(defaultValue = "false")
64-
private boolean colorConsole;
65-
6653
@Parameter(defaultValue = "false")
6754
private boolean skipCompatibilityCheck;
6855

6956
@Parameter
7057
private List<ReporterParameter> reporters;
71-
private Map<String, ReporterParameter> mapReporters = new HashMap<>();
7258

7359
@Parameter(defaultValue = "")
7460
private List<String> paths;
@@ -82,6 +68,15 @@ public class UtPLSQLMojo extends AbstractMojo
8268
@Parameter(defaultValue = "${project.build.directory}", readonly = true)
8369
private String targetDir;
8470

71+
@Parameter(defaultValue = "${maven.test.failure.ignore}")
72+
private boolean ignoreFailure;
73+
74+
// Color in the console, loaded by environment variables
75+
private boolean colorConsole = PluginDefault.resolveColor();
76+
77+
// Reporter Writer
78+
private ReporterWriter reporterWriter;
79+
8580
/**
8681
*
8782
* Execute the plugin
@@ -90,21 +85,17 @@ public class UtPLSQLMojo extends AbstractMojo
9085
@Override
9186
public void execute() throws MojoExecutionException
9287
{
93-
FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource(), "sources");
94-
;
95-
FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest(), "test");
96-
9788
Connection connection = null;
98-
List<Reporter> reporterList = null;
9989
try
10090
{
91+
FileMapperOptions sourceMappingOptions = buildOptions(sources, PluginDefault.buildDefaultSource(), "sources");
92+
FileMapperOptions testMappingOptions = buildOptions(tests, PluginDefault.buildDefaultTest(), "test");
93+
94+
// Create the Connection to the Database
10195
connection = DriverManager.getConnection(url, user, password);
102-
reporterList = initReporters(connection);
96+
List<Reporter> reporterList = initReporters(connection);
10397

104-
if (getLog().isDebugEnabled())
105-
{
106-
dumpParameters(sourceMappingOptions, testMappingOptions, reporterList);
107-
}
98+
logParameters(sourceMappingOptions, testMappingOptions, reporterList);
10899

109100
TestRunner runner = new TestRunner()
110101
.addPathList(paths)
@@ -116,11 +107,11 @@ public void execute() throws MojoExecutionException
116107
.failOnErrors(!ignoreFailure);
117108

118109
// Setting Optional Parameters
119-
if (StringUtils.isNotEmpty(excludeObject))
110+
if (StringUtils.isNotBlank(excludeObject))
120111
{
121112
runner.excludeObject(excludeObject);
122113
}
123-
if (StringUtils.isNotEmpty(includeObject))
114+
if (StringUtils.isNotBlank(includeObject))
124115
{
125116
runner.includeObject(includeObject);
126117
}
@@ -143,10 +134,8 @@ public void execute() throws MojoExecutionException
143134
try
144135
{
145136
// Write Reporters
146-
for (Reporter reporter : reporterList)
147-
{
148-
writeReporter(connection, reporter);
149-
}
137+
reporterWriter.writeReporters(connection);
138+
150139
if (connection != null)
151140
{
152141
connection.close();
@@ -159,41 +148,6 @@ public void execute() throws MojoExecutionException
159148
}
160149
}
161150

162-
/**
163-
*
164-
* @param connection
165-
* @param reporter
166-
* @throws SQLException
167-
* @throws IOException
168-
*/
169-
private void writeReporter(Connection connection, Reporter reporter) throws SQLException, IOException
170-
{
171-
String outputFile = mapReporters.get(reporter.getReporterId()).getConfiguration().getOutputFile();
172-
if (StringUtils.isNotBlank(outputFile) && !StringUtils.equals("-", outputFile))
173-
{
174-
File file = new File(outputFile);
175-
if (!file.isAbsolute())
176-
{
177-
file = new File(targetDir, outputFile);
178-
}
179-
if (!file.getParentFile().exists())
180-
{
181-
file.getParentFile().mkdirs();
182-
}
183-
try (FileOutputStream fout = new FileOutputStream(file))
184-
{
185-
186-
getLog().info(format("Writing report %s to %s", reporter.getSelfType(), file.getAbsolutePath()));
187-
new OutputBuffer(reporter).printAvailable(connection, new PrintStream(fout));
188-
}
189-
}
190-
else
191-
{
192-
getLog().info(format("Reporter %s:", reporter.getSelfType()));
193-
new OutputBuffer(reporter).printAvailable(connection, System.out);
194-
}
195-
}
196-
197151
/**
198152
*
199153
* @param resources
@@ -217,12 +171,11 @@ private FileMapperOptions buildOptions(List<Resource> resources, Resource defaul
217171
catch (Exception e)
218172
{
219173
throw new MojoExecutionException(format("Invalid <%s> in your pom.xml: %s", msg, e.getMessage()));
220-
221174
}
222175
}
223176

224177
/**
225-
* Init all the reports
178+
* Init all the reporters
226179
*
227180
* @param connection
228181
* @return
@@ -232,19 +185,20 @@ private List<Reporter> initReporters(Connection connection) throws SQLException
232185
{
233186
List<Reporter> reporterList = new ArrayList<>();
234187

188+
// Initializate Reporters
189+
reporterWriter = new ReporterWriter(targetDir);
190+
235191
for (ReporterParameter reporterParameter : reporters)
236192
{
237-
String reporterName = reporterParameter.getId().name();
238-
Reporter reporter = ReporterFactory.createReporter(reporterName);
193+
Reporter reporter = ReporterFactory.createReporter(reporterParameter.getName());
239194
reporter.init(connection);
240195
reporterList.add(reporter);
241-
if (reporterParameter.getConfiguration() == null
242-
|| StringUtils.isEmpty(reporterParameter.getConfiguration().getOutputFile()))
196+
197+
// Only added the reporter if at least one of the output is required
198+
if (StringUtils.isNotBlank(reporterParameter.getFileOutput()) || reporterParameter.isConsoleOutput())
243199
{
244-
reporterParameter
245-
.setConfiguration(new ReporterConfiguration(reporterParameter.getId().getOutputFile()));
200+
reporterWriter.addReporter(reporterParameter, reporter);
246201
}
247-
mapReporters.put(reporter.getReporterId(), reporterParameter);
248202
}
249203

250204
return reporterList;
@@ -256,10 +210,18 @@ private List<Reporter> initReporters(Connection connection) throws SQLException
256210
* @param testMappingOptions
257211
* @param reporterList
258212
*/
259-
private void dumpParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions,
213+
private void logParameters(FileMapperOptions sourceMappingOptions, FileMapperOptions testMappingOptions,
260214
List<Reporter> reporterList)
261215
{
262216
Log log = getLog();
217+
log.info("Invoking TestRunner with: " + targetDir);
218+
219+
// Do nothing when the debug is disabled
220+
if (!log.isDebugEnabled())
221+
{
222+
return;
223+
}
224+
263225
log.debug("Invoking TestRunner with: ");
264226
log.debug("reporters=");
265227
reporterList.forEach((Reporter r) -> log.debug(r.getSelfType()));

0 commit comments

Comments
 (0)