Skip to content

Commit fef81ef

Browse files
committed
Refactor logic to copy CoverageHTMLReporter-assets
Introduced ReporterOptionsAware-Interface which might be used in future improvements
1 parent f860599 commit fef81ef

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.utplsql.cli;
2+
3+
import org.utplsql.api.compatibility.CompatibilityProxy;
4+
import org.utplsql.api.reporter.CoreReporters;
5+
import org.utplsql.api.reporter.ReporterFactory;
6+
import org.utplsql.cli.reporters.LocalAssetsCoverageHTMLReporter;
7+
8+
/** A simple class to provide a ReporterFactory for the RunCommand
9+
*
10+
* @author pesse
11+
*/
12+
public class ReporterFactoryProvider {
13+
14+
public static ReporterFactory createReporterFactory(CompatibilityProxy proxy ) {
15+
ReporterFactory reporterFactory = ReporterFactory.createDefault(proxy);
16+
reporterFactory.registerReporterFactoryMethod(CoreReporters.UT_COVERAGE_HTML_REPORTER.name(), LocalAssetsCoverageHTMLReporter::new, "Will copy all necessary assets to a folder named after the Output-File");
17+
18+
return reporterFactory;
19+
}
20+
}

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
import org.utplsql.api.compatibility.CompatibilityProxy;
1010
import org.utplsql.api.exception.SomeTestsFailedException;
1111
import org.utplsql.api.reporter.CoreReporters;
12-
import org.utplsql.api.reporter.CoverageHTMLReporter;
1312
import org.utplsql.api.reporter.Reporter;
1413
import org.utplsql.api.reporter.ReporterFactory;
1514
import org.utplsql.cli.exception.DatabaseConnectionFailed;
15+
import org.utplsql.cli.reporters.ReporterOptionsAware;
1616

1717
import java.io.File;
1818
import java.io.FileNotFoundException;
1919
import java.io.FileOutputStream;
2020
import java.io.PrintStream;
21-
import java.nio.file.Paths;
2221
import java.sql.Connection;
2322
import java.sql.SQLException;
2423
import java.util.ArrayList;
@@ -159,7 +158,7 @@ public int run() throws Exception {
159158

160159
// First of all do a compatibility check and fail-fast
161160
compatibilityProxy = checkFrameworkCompatibility(conn);
162-
reporterFactory = ReporterFactory.createDefault(compatibilityProxy);
161+
reporterFactory = ReporterFactoryProvider.createReporterFactory(compatibilityProxy);
163162

164163
reporterList = initReporters(conn, reporterOptionsList);
165164

@@ -226,14 +225,11 @@ private List<Reporter> initReporters( Connection conn, List<ReporterOptions> rep
226225
for (ReporterOptions ro : reporterOptionsList) {
227226
Reporter reporter = reporterFactory.createReporter(ro.getReporterName());
228227

228+
if ( reporter instanceof ReporterOptionsAware )
229+
((ReporterOptionsAware) reporter).setReporterOptions(ro);
229230

230231
reporter.init(conn, compatibilityProxy, reporterFactory);
231232

232-
// Quick-hack for CoverageHTML Reporter
233-
if ( reporter instanceof CoverageHTMLReporter && ro.outputToFile() ) {
234-
((CoverageHTMLReporter)reporter).setAssetsPath(ro.getOutputFileName()+"_assets/");
235-
CoverageHTMLReporter.writeReportAssetsTo(Paths.get(ro.getOutputFileName()+"_assets/"));
236-
}
237233
ro.setReporterObj(reporter);
238234
reporterList.add(reporter);
239235
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.utplsql.cli.reporters;
2+
3+
import org.utplsql.api.compatibility.CompatibilityProxy;
4+
import org.utplsql.api.reporter.CoverageHTMLReporter;
5+
import org.utplsql.api.reporter.Reporter;
6+
import org.utplsql.api.reporter.ReporterFactory;
7+
import org.utplsql.cli.ReporterOptions;
8+
9+
import java.nio.file.Paths;
10+
import java.sql.Connection;
11+
import java.sql.SQLException;
12+
13+
/** Simple replacement of the CoverageHTMLReporter which writes the necessary assets to a folder
14+
* named after the Output File's name.
15+
*
16+
* @author pesse
17+
*/
18+
public class LocalAssetsCoverageHTMLReporter extends CoverageHTMLReporter implements ReporterOptionsAware {
19+
20+
private ReporterOptions options;
21+
22+
public LocalAssetsCoverageHTMLReporter(String selfType, Object[] attributes) {
23+
super(selfType, attributes);
24+
}
25+
26+
@Override
27+
public Reporter init(Connection con, CompatibilityProxy compatibilityProxy, ReporterFactory reporterFactory) throws SQLException {
28+
super.init(con, compatibilityProxy, reporterFactory);
29+
30+
if ( options != null && options.outputToFile() )
31+
writeReportAssetsTo(Paths.get(getAssetsPath()));
32+
33+
return this;
34+
}
35+
36+
private void setAssetsPathFromOptions() {
37+
if ( options != null && options.outputToFile() )
38+
setAssetsPath(options.getOutputFileName()+"_assets/");
39+
}
40+
41+
@Override
42+
public void setReporterOptions(ReporterOptions options) {
43+
this.options = options;
44+
setAssetsPathFromOptions();
45+
}
46+
47+
@Override
48+
protected void setAttributes(Object[] attributes) {
49+
super.setAttributes(attributes);
50+
setAssetsPathFromOptions();
51+
}
52+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.utplsql.cli.reporters;
2+
3+
import org.utplsql.cli.ReporterOptions;
4+
5+
/** Reporters implementing this interface will get their specific ReporterOptions before initialization
6+
*
7+
* @author pesse
8+
*/
9+
public interface ReporterOptionsAware {
10+
void setReporterOptions(ReporterOptions options);
11+
}

0 commit comments

Comments
 (0)