Skip to content

Commit 19782c3

Browse files
committed
First attempt to refactor and improve Reporter API
ReporterFactory is now a singleton. One can register its own reporters with a given name. The name is independent from the Oracle Object-Name. That way it is very easy to add a slightly different behaviour to an existing base reporter in java
1 parent 249fff4 commit 19782c3

11 files changed

+135
-53
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ public final class CustomTypes {
1010
public static final String UT_VARCHAR2_LIST = "UT_VARCHAR2_LIST";
1111

1212
public static final String UT_REPORTERS = "UT_REPORTERS";
13-
public static final String UT_DOCUMENTATION_REPORTER = "UT_DOCUMENTATION_REPORTER";
14-
public static final String UT_COVERAGE_HTML_REPORTER = "UT_COVERAGE_HTML_REPORTER";
15-
public static final String UT_TEAMCITY_REPORTER = "UT_TEAMCITY_REPORTER";
16-
public static final String UT_XUNIT_REPORTER = "UT_XUNIT_REPORTER";
17-
public static final String UT_COVERALLS_REPORTER = "UT_COVERALLS_REPORTER";
18-
public static final String UT_COVERAGE_SONAR_REPORTER = "UT_COVERAGE_SONAR_REPORTER";
19-
public static final String UT_SONAR_TEST_REPORTER = "UT_SONAR_TEST_REPORTER";
2013

2114
public static final String UT_FILE_MAPPING = "UT_FILE_MAPPING";
2215
public static final String UT_FILE_MAPPINGS = "UT_FILE_MAPPINGS";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public CoverageHTMLReporter(String projectName, String assetsPath) {
3535

3636
@Override
3737
public String getSQLTypeName() throws SQLException {
38-
return CustomTypes.UT_COVERAGE_HTML_REPORTER;
38+
return DefaultReporters.UT_COVERAGE_HTML_REPORTER.name();
3939
}
4040

4141
public String getProjectName() {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.utplsql.api.reporter;
22

3-
import org.utplsql.api.CustomTypes;
4-
53
import java.sql.SQLException;
64

75
public class CoverageSonarReporter extends Reporter {
86

97
@Override
108
public String getSQLTypeName() throws SQLException {
11-
return CustomTypes.UT_COVERAGE_SONAR_REPORTER;
9+
return DefaultReporters.UT_COVERAGE_SONAR_REPORTER.name();
1210
}
1311

1412
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.utplsql.api.reporter;
22

3-
import org.utplsql.api.CustomTypes;
4-
53
import java.sql.SQLException;
64

75
public class CoverallsReporter extends Reporter {
86

97
@Override
108
public String getSQLTypeName() throws SQLException {
11-
return CustomTypes.UT_COVERALLS_REPORTER;
9+
return DefaultReporters.UT_COVERALLS_REPORTER.name();
1210
}
1311

1412
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.utplsql.api.reporter;
2+
3+
import java.util.function.Supplier;
4+
5+
/** This enum defines default reporters, added and maintained by the utPLSQL team, and their (default) factory method
6+
*
7+
* @author pesse
8+
*/
9+
public enum DefaultReporters {
10+
11+
UT_COVERAGE_HTML_REPORTER(CoverageHTMLReporter::new),
12+
UT_DOCUMENTATION_REPORTER(DocumentationReporter::new),
13+
UT_TEAMCITY_REPORTER(TeamCityReporter::new),
14+
UT_XUNIT_REPORTER(XUnitReporter::new),
15+
UT_COVERALLS_REPORTER(CoverallsReporter::new),
16+
UT_COVERAGE_SONAR_REPORTER(CoverageSonarReporter::new),
17+
UT_SONAR_TEST_REPORTER(SonarTestReporter::new);
18+
19+
private Supplier<? extends Reporter> factoryMethod;
20+
21+
DefaultReporters(Supplier<? extends Reporter> factoryMethod ) {
22+
this.factoryMethod = factoryMethod;
23+
}
24+
25+
public Supplier<? extends Reporter> getFactoryMethod() {
26+
return factoryMethod;
27+
}
28+
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.utplsql.api.reporter;
22

3-
import org.utplsql.api.CustomTypes;
4-
53
import java.sql.SQLException;
64
import java.sql.SQLInput;
75
import java.sql.SQLOutput;
@@ -34,7 +32,7 @@ public void setFailed(int failed) {
3432

3533
@Override
3634
public String getSQLTypeName() throws SQLException {
37-
return CustomTypes.UT_DOCUMENTATION_REPORTER;
35+
return DefaultReporters.UT_DOCUMENTATION_REPORTER.name();
3836
}
3937

4038
@Override

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

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,83 @@
22

33
import org.utplsql.api.CustomTypes;
44

5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Set;
9+
import java.util.function.Supplier;
10+
11+
/** This singleton-class manages the instantiation of reporters.
12+
* One can register a supplier method for a specific name which will then be callable via createReporter(name)
13+
*
14+
* @author pesse
15+
*/
516
public final class ReporterFactory {
617

7-
private ReporterFactory() {}
8-
9-
public static Reporter createReporter(String reporterName) {
10-
switch (reporterName.toUpperCase()) {
11-
case CustomTypes.UT_DOCUMENTATION_REPORTER: return new DocumentationReporter();
12-
case CustomTypes.UT_COVERAGE_HTML_REPORTER: return new CoverageHTMLReporter();
13-
case CustomTypes.UT_TEAMCITY_REPORTER: return new TeamCityReporter();
14-
case CustomTypes.UT_XUNIT_REPORTER: return new XUnitReporter();
15-
case CustomTypes.UT_COVERALLS_REPORTER: return new CoverallsReporter();
16-
case CustomTypes.UT_COVERAGE_SONAR_REPORTER: return new CoverageSonarReporter();
17-
case CustomTypes.UT_SONAR_TEST_REPORTER: return new SonarTestReporter();
18-
default: throw new RuntimeException("Reporter " + reporterName + " not implemented.");
19-
}
18+
private Map<String, Supplier<? extends Reporter>> reportFactoryMethodMap = new HashMap<>();
19+
20+
private static ReporterFactory instance;
21+
22+
private ReporterFactory() {
23+
registerDefaultReporters();
24+
}
25+
26+
/** Registers the default reporters, provided with utPLSQL core
27+
*/
28+
private void registerDefaultReporters() {
29+
Arrays.stream(DefaultReporters.values())
30+
.forEach(r -> registerReporterFactoryMethod(r.name(), r.getFactoryMethod()));
2031
}
2132

33+
/** Returns the global instance of the ReporterFactory
34+
*
35+
* @return ReporterFactory
36+
*/
37+
public static ReporterFactory getInstance() {
38+
if ( instance == null )
39+
instance = new ReporterFactory();
40+
return instance;
41+
}
42+
43+
/** Registers a creation method for a specified reporter name. Overrides eventually existing creation method
44+
*
45+
* @param reporterName the reporter's name to register
46+
* @param factoryMethod the method which will return the reporter
47+
* @return the method stored for the registered name
48+
*/
49+
public synchronized Supplier<? extends Reporter> registerReporterFactoryMethod( String reporterName, Supplier<? extends Reporter> factoryMethod) {
50+
return reportFactoryMethodMap.put(reporterName, factoryMethod);
51+
}
52+
53+
/** Unregisters a specified reporter name.
54+
*
55+
* @param reporterName the reporter's name to unregister
56+
* @return the method which was previously registered or null
57+
*/
58+
public synchronized Supplier<? extends Reporter> unregisterReporterFactoryMethod( String reporterName ) {
59+
return reportFactoryMethodMap.remove(reporterName);
60+
}
61+
62+
/** Returns a new reporter of the given name (or should do so). In reality it just calls the registered method
63+
* for the given reporterName and returns its value (which should be a subclass of Reporter).
64+
* Usually you should expect a new instance of a reporter, but who knows what evil forces register themselves nowadays...
65+
*
66+
* @param reporterName the reporter's name to create a new instance of
67+
* @return A reporter
68+
*/
69+
public Reporter createReporter(String reporterName) {
70+
Supplier<? extends Reporter> supplier = reportFactoryMethodMap.get(reporterName);
71+
if ( supplier == null )
72+
throw new RuntimeException("Reporter " + reporterName + " not implemented.");
73+
else
74+
return supplier.get();
75+
}
76+
77+
/** Returns a set of all registered reporter's names
78+
*
79+
* @return Set of reporter names
80+
*/
81+
public Set<String> getRegisteredReporterNames() {
82+
return reportFactoryMethodMap.keySet();
83+
}
2284
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.utplsql.api.reporter;
22

3-
import org.utplsql.api.CustomTypes;
4-
53
import java.sql.SQLException;
64

75
public class SonarTestReporter extends Reporter {
86

97
@Override
108
public String getSQLTypeName() throws SQLException {
11-
return CustomTypes.UT_SONAR_TEST_REPORTER;
9+
return DefaultReporters.UT_SONAR_TEST_REPORTER.name();
1210
}
1311

1412
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.utplsql.api.reporter;
22

3-
import org.utplsql.api.CustomTypes;
4-
53
import java.sql.SQLException;
64

75
public class TeamCityReporter extends Reporter {
86

97
@Override
108
public String getSQLTypeName() throws SQLException {
11-
return CustomTypes.UT_TEAMCITY_REPORTER;
9+
return DefaultReporters.UT_TEAMCITY_REPORTER.name();
1210
}
1311

1412
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package org.utplsql.api.reporter;
22

3-
import org.utplsql.api.CustomTypes;
4-
53
import java.sql.SQLException;
64

75
public class XUnitReporter extends Reporter {
86

97
@Override
108
public String getSQLTypeName() throws SQLException {
11-
return CustomTypes.UT_XUNIT_REPORTER;
9+
return DefaultReporters.UT_XUNIT_REPORTER.name();
1210
}
1311

1412
}

0 commit comments

Comments
 (0)