Skip to content

Commit 2cbd11f

Browse files
committed
ReporterFactory is no longer a singleton
Made purpose of several classes more clear, updated some methods to match with core
1 parent facc543 commit 2cbd11f

File tree

11 files changed

+91
-45
lines changed

11 files changed

+91
-45
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
88
import org.utplsql.api.reporter.DocumentationReporter;
99
import org.utplsql.api.reporter.Reporter;
10+
import org.utplsql.api.reporter.ReporterFactory;
1011
import org.utplsql.api.testRunner.TestRunnerStatement;
1112

1213
import java.sql.CallableStatement;
@@ -23,6 +24,8 @@
2324
public class TestRunner {
2425

2526
private TestRunnerOptions options = new TestRunnerOptions();
27+
private CompatibilityProxy compatibilityProxy;
28+
private ReporterFactory reporterFactory = new ReporterFactory();
2629

2730
public TestRunner addPath(String path) {
2831
options.pathList.add(path);
@@ -39,6 +42,11 @@ public TestRunner addReporter(Reporter reporter) {
3942
return this;
4043
}
4144

45+
public TestRunner addReporter( String reporterName ) {
46+
options.reporterList.add(reporterFactory.createReporter(reporterName));
47+
return this;
48+
}
49+
4250
public TestRunner colorConsole(boolean colorConsole) {
4351
options.colorConsole = colorConsole;
4452
return this;
@@ -95,9 +103,16 @@ public TestRunner skipCompatibilityCheck( boolean skipCompatibilityCheck )
95103
return this;
96104
}
97105

106+
public TestRunner setReporterFactory( ReporterFactory reporterFactory ) {
107+
this.reporterFactory = reporterFactory;
108+
return this;
109+
}
110+
98111
public void run(Connection conn) throws SomeTestsFailedException, SQLException, DatabaseNotCompatibleException, UtPLSQLNotInstalledException {
99112

100-
CompatibilityProxy compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
113+
compatibilityProxy = new CompatibilityProxy(conn, options.skipCompatibilityCheck);
114+
if ( reporterFactory == null )
115+
reporterFactory = new ReporterFactory();
101116

102117
// First of all check version compatibility
103118
compatibilityProxy.failOnNotCompatible();
@@ -148,7 +163,7 @@ else if (e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
148163
*/
149164
private void validateReporter(Connection conn, Reporter reporter) throws SQLException {
150165
if (!reporter.isInit() || reporter.getId() == null || reporter.getId().isEmpty())
151-
reporter.init(conn);
166+
reporter.init(conn, compatibilityProxy, reporterFactory);
152167
}
153168

154169
}

src/main/java/org/utplsql/api/compatibility/OptionalFeatures.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
public enum OptionalFeatures {
77

88
FAIL_ON_ERROR("3.0.3", null),
9-
FRAMEWORK_COMPATIBILITY_CHECK("3.0.3", null);
9+
FRAMEWORK_COMPATIBILITY_CHECK("3.0.3", null),
10+
CUSTOM_REPORTERS("3.1.0", null);
1011

1112
private Version minVersion;
1213
private Version maxVersion;

src/main/java/org/utplsql/api/outputBuffer/AbstractOutputBuffer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public void fetchAvailable(Connection conn, Consumer<String> onLineFetched) thro
7676

7777
try (PreparedStatement pstmt = getLinesStatement(conn)) {
7878

79-
pstmt.setString(1, getReporter().getId());
8079
try (ResultSet resultSet = pstmt.executeQuery() ) {
8180
while (resultSet.next())
8281
onLineFetched.accept(resultSet.getString(1));

src/main/java/org/utplsql/api/outputBuffer/OutputBufferProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static OutputBuffer getCompatibleOutputBuffer(Version databaseVersion, Re
2525
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
2626

2727
try {
28-
if (new Version("3.0.4.1610").isGreaterOrEqualThan(databaseVersion)) {
28+
if (databaseVersion.isGreaterOrEqualThan(new Version("3.1.0"))) {
2929
if ( hasOutput(reporter, oraConn) ) {
3030
return new DefaultOutputBuffer(reporter);
3131
}
@@ -42,8 +42,9 @@ public static OutputBuffer getCompatibleOutputBuffer(Version databaseVersion, Re
4242

4343
private static boolean hasOutput( Reporter reporter, OracleConnection oraConn ) throws SQLException {
4444

45-
try ( PreparedStatement stmt = oraConn.prepareStatement("select ut_runner.is_output_reporter(?) from dual")) {
46-
stmt.setString(1, reporter.getTypeName());
45+
String sql = "select is_output_reporter from table(ut_runner.get_reporters_list) where regexp_like(reporter_object_name, ?)";
46+
try ( PreparedStatement stmt = oraConn.prepareStatement(sql)) {
47+
stmt.setString(1, "[a-zA-Z0-9_]*\\.?" + reporter.getTypeName());
4748

4849
try ( ResultSet rs = stmt.executeQuery() ) {
4950
if ( rs.next() ) {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ public Reporter( String typeName, Object[] attributes ) {
3030
}
3131

3232
protected void setTypeName( String typeName ) {
33-
this.selfType = typeName.replaceAll("[^0-9a-zA-Z_]", "");
33+
this.selfType = typeName.replaceAll("[^0-9a-zA-Z_\\.]", "");
3434
}
3535

3636

37-
public Reporter init(Connection con, CompatibilityProxy compatibilityProxy ) throws SQLException {
37+
public Reporter init(Connection con, CompatibilityProxy compatibilityProxy, ReporterFactory reporterFactory ) throws SQLException {
3838

3939
if ( compatibilityProxy == null )
4040
compatibilityProxy = new CompatibilityProxy(con);
41+
if ( reporterFactory == null )
42+
reporterFactory = new ReporterFactory();
4143

4244
OracleConnection oraConn = con.unwrap(OracleConnection.class);
4345

44-
initDbReporter( oraConn );
46+
initDbReporter( oraConn, reporterFactory );
4547

4648
init = true;
4749

@@ -51,7 +53,7 @@ public Reporter init(Connection con, CompatibilityProxy compatibilityProxy ) thr
5153
}
5254

5355
public Reporter init(Connection con) throws SQLException {
54-
return init(con, null);
56+
return init(con, null, null);
5557
}
5658

5759
protected abstract void initOutputBuffer( OracleConnection oraConn, CompatibilityProxy compatibilityProxy ) throws SQLException;
@@ -63,12 +65,12 @@ public Reporter init(Connection con) throws SQLException {
6365
* @param oraConn
6466
* @throws SQLException
6567
*/
66-
private void initDbReporter( OracleConnection oraConn ) throws SQLException {
68+
private void initDbReporter( OracleConnection oraConn, ReporterFactory reporterFactory ) throws SQLException {
6769
OracleCallableStatement callableStatement = (OracleCallableStatement) oraConn.prepareCall("{? = call " + selfType + "()}");
6870
callableStatement.registerOutParameter(1, OracleTypes.STRUCT, "UT_REPORTER_BASE");
6971
callableStatement.execute();
7072

71-
Reporter obj = (Reporter) callableStatement.getORAData(1, ReporterFactory.getInstance());
73+
Reporter obj = (Reporter) callableStatement.getORAData(1, reporterFactory);
7274

7375
setAttributes(obj.getAttributes());
7476
}

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Map;
1212
import java.util.function.BiFunction;
1313

14-
/** This singleton-class manages the instantiation of reporters.
14+
/** This class manages the instantiation of reporters.
1515
* One can register a supplier method for a specific name which will then be callable via createReporter(name)
1616
*
1717
* @author pesse
@@ -28,10 +28,8 @@ public ReporterInfo(BiFunction<String, Object[], ? extends Reporter> factoryMeth
2828
}
2929

3030
private Map<String, ReporterInfo> reportFactoryMethodMap = new HashMap<>();
31-
32-
private static ReporterFactory instance;
3331

34-
private ReporterFactory() {
32+
public ReporterFactory() {
3533
registerDefaultReporters();
3634
}
3735

@@ -42,24 +40,6 @@ private void registerDefaultReporters() {
4240
.forEach(r -> registerReporterFactoryMethod(r.name(), r.getFactoryMethod(), r.getDescription()));
4341
}
4442

45-
/** Returns the global instance of the ReporterFactory
46-
*
47-
* @return ReporterFactory
48-
*/
49-
public static ReporterFactory getInstance() {
50-
if ( instance == null )
51-
instance = new ReporterFactory();
52-
return instance;
53-
}
54-
55-
public static Reporter create( CoreReporters reporter ) {
56-
return getInstance().createReporter(reporter);
57-
}
58-
59-
public static Reporter create( String reporter ) {
60-
return getInstance().createReporter(reporter);
61-
}
62-
6343
/** Registers a creation method for a specified reporter name. Overrides eventually existing creation method
6444
*
6545
* @param reporterName the reporter's name to register
@@ -68,7 +48,7 @@ public static Reporter create( String reporter ) {
6848
* @return Object with information about the registered reporter
6949
*/
7050
public synchronized ReporterInfo registerReporterFactoryMethod( String reporterName, BiFunction<String, Object[], ? extends Reporter> factoryMethod, String description) {
71-
return reportFactoryMethodMap.put(reporterName, new ReporterInfo(factoryMethod, description));
51+
return reportFactoryMethodMap.put(reporterName.toUpperCase(), new ReporterInfo(factoryMethod, description));
7252
}
7353

7454
/** Unregisters a specified reporter name.
@@ -77,7 +57,7 @@ public synchronized ReporterInfo registerReporterFactoryMethod( String reporterN
7757
* @return information about the reporter which was previously registered or null
7858
*/
7959
public synchronized ReporterInfo unregisterReporterFactoryMethod( String reporterName ) {
80-
return reportFactoryMethodMap.remove(reporterName);
60+
return reportFactoryMethodMap.remove(reporterName.toUpperCase());
8161
}
8262

8363
/** Returns a new reporter of the given name.
@@ -89,6 +69,7 @@ public synchronized ReporterInfo unregisterReporterFactoryMethod( String reporte
8969
*/
9070
public Reporter createReporter(String reporterName, Object[] attributes) {
9171

72+
reporterName = reporterName.toUpperCase();
9273
BiFunction<String, Object[], ? extends Reporter> supplier = DefaultReporter::new;
9374

9475
if ( reportFactoryMethodMap.containsKey(reporterName)) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.utplsql.api.reporter;
2+
3+
/** Gives information about available reporters
4+
*
5+
* @author pesse
6+
*/
7+
public class ReporterInspector {
8+
9+
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.utplsql.api.reporter.core;
2+
3+
public class CoreReporterProvider {
4+
5+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.utplsql.api;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.utplsql.api.reporter.DefaultReporter;
5+
import org.utplsql.api.reporter.Reporter;
6+
import org.utplsql.api.reporter.ReporterFactory;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class RegisterCustomReporterTest {
11+
12+
@Test
13+
public void addCustomReporter() {
14+
15+
ReporterFactory reporterFactory = new ReporterFactory();
16+
reporterFactory.registerReporterFactoryMethod("ut_custom_reporter",
17+
(type, attr) -> new DefaultReporter("UT_EXISTING_REPORTER", attr),
18+
"My custom Reporter");
19+
20+
Reporter r = reporterFactory.createReporter("ut_custom_reporter");
21+
22+
assertEquals(r.getTypeName(), "UT_EXISTING_REPORTER");
23+
}
24+
25+
@Test
26+
public void createCustomDefaultReporter() {
27+
ReporterFactory reporterFactory = new ReporterFactory();
28+
Reporter r = reporterFactory.createReporter("ut_custom_reporter");
29+
30+
assertEquals(r.getTypeName(), "UT_CUSTOM_REPORTER");
31+
}
32+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ReporterNameTest {
1212

1313
@Test
1414
public void defaultReporterFactoryNamesList() {
15-
Map<String, String> reporterDescriptions = ReporterFactory.getInstance().getRegisteredReporterInfo();
15+
Map<String, String> reporterDescriptions = new ReporterFactory().getRegisteredReporterInfo();
1616

1717
for ( CoreReporters r : CoreReporters.values() ) {
1818
assertTrue(reporterDescriptions.containsKey(r.name()));

0 commit comments

Comments
 (0)