Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe22a70

Browse files
committed
New public interface for ReporterFactory and CoreReporters
also allow delayed instantiation of Reporters in TestRunner
1 parent 6bd7fda commit fe22a70

9 files changed

Lines changed: 129 additions & 73 deletions

File tree

src/main/java/org/utplsql/api/TestRunner.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.sql.CallableStatement;
1414
import java.sql.Connection;
1515
import java.sql.SQLException;
16+
import java.util.ArrayList;
1617
import java.util.List;
1718

1819
/**
@@ -25,7 +26,8 @@ public class TestRunner {
2526

2627
private TestRunnerOptions options = new TestRunnerOptions();
2728
private CompatibilityProxy compatibilityProxy;
28-
private ReporterFactory reporterFactory = new ReporterFactory();
29+
private ReporterFactory reporterFactory;
30+
private List<String> reporterNames = new ArrayList<>();
2931

3032
public TestRunner addPath(String path) {
3133
options.pathList.add(path);
@@ -43,7 +45,10 @@ public TestRunner addReporter(Reporter reporter) {
4345
}
4446

4547
public TestRunner addReporter( String reporterName ) {
46-
options.reporterList.add(reporterFactory.createReporter(reporterName));
48+
if ( reporterFactory != null )
49+
options.reporterList.add(reporterFactory.createReporter(reporterName));
50+
else
51+
reporterNames.add(reporterName);
4752
return this;
4853
}
4954

@@ -108,11 +113,20 @@ public TestRunner setReporterFactory( ReporterFactory reporterFactory ) {
108113
return this;
109114
}
110115

116+
private void delayedAddReporters() {
117+
if ( reporterFactory != null )
118+
reporterNames.stream().forEach( this::addReporter );
119+
else
120+
throw new IllegalStateException("ReporterFactory must be set to add delayed Reporters!");
121+
}
122+
111123
public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException, UtPLSQLNotInstalledException {
112124

113125
compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
114126
if ( reporterFactory == null )
115-
reporterFactory = new ReporterFactory();
127+
reporterFactory = ReporterFactory.createDefault(compatibilityProxy);
128+
129+
delayedAddReporters();
116130

117131
// First of all check version compatibility
118132
compatibilityProxy.failOnNotCompatible();
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
package org.utplsql.api.reporter;
22

3-
import java.util.function.BiFunction;
3+
import org.utplsql.api.Version;
4+
import org.utplsql.api.exception.InvalidVersionException;
45

5-
/** This enum defines default reporters, added and maintained by the utPLSQL team, and their (default) factory method
6+
/** This enum defines default reporters, added and maintained by the utPLSQL team,
7+
* and information since (and maybe until) which version they exist
68
*
79
* @author pesse
810
*/
911
public enum CoreReporters {
1012

11-
UT_COVERAGE_HTML_REPORTER(CoverageHTMLReporter::new, "Generates a HTML coverage report with summary and line by line information on code coverage.\n" +
12-
"Based on open-source simplecov-html coverage reporter for Ruby.\n" +
13-
"Includes source code in the report."),
14-
UT_DOCUMENTATION_REPORTER(DocumentationReporter::new, "A textual pretty-print of unit test results (usually use for console output)"),
15-
UT_TEAMCITY_REPORTER(DefaultReporter::new, "For reporting live progress of test execution with Teamcity CI."),
16-
UT_XUNIT_REPORTER(DefaultReporter::new, "Used for reporting test results with CI servers like Jenkins/Hudson/Teamcity."),
17-
UT_COVERALLS_REPORTER(DefaultReporter::new, "Generates a JSON coverage report providing information on code coverage with line numbers.\n" +
18-
"Designed for [Coveralls](https://coveralls.io/)."),
19-
UT_COVERAGE_SONAR_REPORTER(DefaultReporter::new, "Generates a JSON coverage report providing information on code coverage with line numbers.\n" +
20-
"Designed for [SonarQube](https://about.sonarqube.com/) to report coverage."),
21-
UT_SONAR_TEST_REPORTER(DefaultReporter::new, "Generates a JSON report providing detailed information on test execution.\n" +
22-
"Designed for [SonarQube](https://about.sonarqube.com/) to report test execution.");
23-
24-
private BiFunction<String, Object[], ? extends Reporter> factoryMethod;
25-
private String description;
26-
27-
CoreReporters(BiFunction<String, Object[], ? extends Reporter> factoryMethod, String description ) {
28-
this.factoryMethod = factoryMethod;
29-
this.description = description;
13+
UT_COVERAGE_HTML_REPORTER(new Version("3.0.0"), null),
14+
UT_DOCUMENTATION_REPORTER(new Version("3.0.0"), null),
15+
UT_TEAMCITY_REPORTER(new Version("3.0.0"), null),
16+
UT_XUNIT_REPORTER(new Version("3.0.0"), null),
17+
UT_COVERALLS_REPORTER(new Version("3.0.0"), null),
18+
UT_COVERAGE_SONAR_REPORTER(new Version("3.0.0"), null),
19+
UT_SONAR_TEST_REPORTER(new Version("3.0.0"), null),
20+
UT_COVERAGE_COBERTURA_REPORTER(new Version("3.1.0"), null);
21+
22+
private Version since;
23+
private Version until;
24+
25+
CoreReporters(Version since, Version until ) {
26+
this.since = since;
27+
this.until = until;
3028
}
3129

32-
public BiFunction<String, Object[], ? extends Reporter> getFactoryMethod() {
33-
return factoryMethod;
30+
public Version getSince() {
31+
return since;
3432
}
3533

36-
public String getDescription() {
37-
return description;
34+
public Version getUntil() {
35+
return until;
36+
}
37+
38+
/** Checks whether a CoreReporter is valid for the given databaseVersion
39+
*
40+
* @param databaseVersion Database-Version
41+
* @return true or false
42+
*/
43+
public boolean isAvailableFor( Version databaseVersion ) {
44+
try {
45+
if ((since == null || databaseVersion.isGreaterOrEqualThan(since))
46+
&& (until == null || databaseVersion.isLessOrEqualThan(until)))
47+
return true;
48+
}
49+
catch ( InvalidVersionException e ) { }
50+
51+
return false;
3852
}
3953
}

src/main/java/org/utplsql/api/reporter/DefaultReporter.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package org.utplsql.api.reporter;
22

3-
import oracle.jdbc.OracleCallableStatement;
43
import oracle.jdbc.OracleConnection;
5-
import oracle.jdbc.OracleTypes;
6-
import oracle.sql.Datum;
7-
import oracle.sql.ORAData;
8-
import oracle.sql.STRUCT;
9-
import oracle.sql.StructDescriptor;
104
import org.utplsql.api.compatibility.CompatibilityProxy;
11-
import org.utplsql.api.outputBuffer.OutputBuffer;
125

13-
import java.sql.Connection;
146
import java.sql.SQLException;
157

168
/** This is a basic Reporter implementation, using ORAData interface
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.utplsql.api.reporter;
2+
3+
import org.utplsql.api.compatibility.CompatibilityProxy;
4+
5+
/** Helper-class which registers default ReporterFactoryMethods based on the given databaseVersion
6+
*
7+
* @author pesse
8+
*/
9+
class DefaultReporterFactoryMethodRegistrator {
10+
11+
public static void registerDefaultReporters(ReporterFactory reporterFactory, CompatibilityProxy compatibilityProxy ) {
12+
13+
// At the moment we don't have version-specific reporters which need a special MethodFactory
14+
reporterFactory.registerReporterFactoryMethod(CoreReporters.UT_DOCUMENTATION_REPORTER.name(), DocumentationReporter::new, "Provides additional properties lvl and failed");
15+
reporterFactory.registerReporterFactoryMethod(CoreReporters.UT_COVERAGE_HTML_REPORTER.name(), CoverageHTMLReporter::new, "Provides additional properties projectName and assetPath");
16+
}
17+
18+
19+
}

src/main/java/org/utplsql/api/reporter/ReporterFactory.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import oracle.sql.ORAData;
55
import oracle.sql.ORADataFactory;
66
import oracle.sql.STRUCT;
7+
import org.utplsql.api.compatibility.CompatibilityProxy;
78

9+
import java.sql.Connection;
810
import java.sql.SQLException;
911
import java.util.Arrays;
1012
import java.util.HashMap;
@@ -29,15 +31,8 @@ public ReporterInfo(BiFunction<String, Object[], ? extends Reporter> factoryMeth
2931

3032
private Map<String, ReporterInfo> reportFactoryMethodMap = new HashMap<>();
3133

32-
public ReporterFactory() {
33-
registerDefaultReporters();
34-
}
34+
ReporterFactory() {
3535

36-
/** Registers the default reporters, provided with utPLSQL core
37-
*/
38-
private void registerDefaultReporters() {
39-
Arrays.stream(CoreReporters.values())
40-
.forEach(r -> registerReporterFactoryMethod(r.name(), r.getFactoryMethod(), r.getDescription()));
4136
}
4237

4338
/** Registers a creation method for a specified reporter name. Overrides eventually existing creation method
@@ -126,4 +121,25 @@ public ORAData create(Datum d, int sqlType) throws SQLException {
126121

127122
return null;
128123
}
124+
125+
/** Returns a new instance of an empty ReporterFactory with no registered ReporterFactoryMethods
126+
* Normally, you should be using createDefault-method instead.
127+
*
128+
* @return a new ReporterFactory instance
129+
*/
130+
public static ReporterFactory createEmpty() {
131+
return new ReporterFactory();
132+
}
133+
134+
/** Returns a new instance of a ReporterFactory with the default ReporterFactoryMethods registered.
135+
* This can depend upon the version of utPLSQL, therefore you have to provide a CompatibilityProxy
136+
*
137+
* @param proxy Compatibility proxy
138+
* @return a new ReporterFactory instance with all default ReporterFactoryMethods registered
139+
*/
140+
public static ReporterFactory createDefault(CompatibilityProxy proxy) {
141+
ReporterFactory reporterFactory = new ReporterFactory();
142+
DefaultReporterFactoryMethodRegistrator.registerDefaultReporters(reporterFactory, proxy);
143+
return reporterFactory;
144+
}
129145
}

src/main/java/org/utplsql/api/reporter/core/CoreReporterProvider.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/test/java/org/utplsql/api/RegisterCustomReporterTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.utplsql.api;
22

33
import org.junit.jupiter.api.Test;
4+
import org.utplsql.api.compatibility.CompatibilityProxy;
45
import org.utplsql.api.reporter.DefaultReporter;
56
import org.utplsql.api.reporter.Reporter;
67
import org.utplsql.api.reporter.ReporterFactory;
@@ -12,7 +13,7 @@ public class RegisterCustomReporterTest {
1213
@Test
1314
public void addCustomReporter() {
1415

15-
ReporterFactory reporterFactory = new ReporterFactory();
16+
ReporterFactory reporterFactory = ReporterFactory.createEmpty();
1617
reporterFactory.registerReporterFactoryMethod("ut_custom_reporter",
1718
(type, attr) -> new DefaultReporter("UT_EXISTING_REPORTER", attr),
1819
"My custom Reporter");
@@ -24,9 +25,10 @@ public void addCustomReporter() {
2425

2526
@Test
2627
public void createCustomDefaultReporter() {
27-
ReporterFactory reporterFactory = new ReporterFactory();
28+
ReporterFactory reporterFactory = ReporterFactory.createEmpty();
2829
Reporter r = reporterFactory.createReporter("ut_custom_reporter");
2930

3031
assertEquals(r.getTypeName(), "UT_CUSTOM_REPORTER");
3132
}
33+
3234
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.utplsql.api.compatibility.CompatibilityProxy;
5+
import org.utplsql.api.reporter.CoreReporters;
6+
import org.utplsql.api.reporter.CoverageHTMLReporter;
7+
import org.utplsql.api.reporter.DocumentationReporter;
8+
import org.utplsql.api.reporter.ReporterFactory;
9+
10+
import java.sql.SQLException;
11+
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
public class ReporterFactoryIT extends AbstractDatabaseTest {
15+
16+
17+
@Test
18+
public void createDefaultReporterFactoryMethod() throws SQLException {
19+
CompatibilityProxy proxy = new CompatibilityProxy(getConnection());
20+
21+
ReporterFactory reporterFactory = ReporterFactory.createDefault(proxy);
22+
23+
assertTrue( reporterFactory.createReporter(CoreReporters.UT_DOCUMENTATION_REPORTER.name()) instanceof DocumentationReporter );
24+
assertTrue( reporterFactory.createReporter(CoreReporters.UT_COVERAGE_HTML_REPORTER.name()) instanceof CoverageHTMLReporter);
25+
}
26+
}

src/test/java/org/utplsql/api/ReporterNameTest.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)