◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

import org.utplsql.sqldev.dal.UtplsqlDao;
import org.utplsql.sqldev.exception.GenericDatabaseAccessException;
import org.utplsql.sqldev.exception.GenericRuntimeException;
import org.utplsql.sqldev.model.DatabaseTools;
import org.utplsql.sqldev.model.FileTools;
import org.utplsql.sqldev.ui.coverage.CodeCoverageReporterDialog;

public class CodeCoverageReporter {
private static final Logger logger = Logger.getLogger(CodeCoverageReporter.class.getName());

private Connection conn;
private List<String> pathList;
private List<String> includeObjectList;
Expand All @@ -47,14 +56,17 @@ public CodeCoverageReporter(final List<String> pathList, final List<String> incl
final String connectionName) {
this.pathList = pathList;
this.includeObjectList = includeObjectList;
setConnection(connectionName);
}

public CodeCoverageReporter(final List<String> pathList, final List<String> includeObjectList,
final Connection conn) {
this.pathList = pathList;
this.includeObjectList = includeObjectList;
this.conn = conn;
}

private void setConnection(final String connectionName) {
@@ -64,7 +76,32 @@ private void setConnection(final String connectionName) {
throw new NullPointerException();
} else {
// must be closed manually
conn = DatabaseTools.cloneConnection(connectionName);
}
}

Expand All @@ -83,12 +120,57 @@ private ArrayList<String> toStringList(final String s) {
private void run() {
logger.fine(() -> "Running code coverage reporter for " + pathList + "...");
try {
final UtplsqlDao dal = new UtplsqlDao(conn);
final String content = dal.htmlCodeCoverage(pathList, toStringList(schemas),
toStringList(includeObjects), toStringList(excludeObjects));
final File file = File.createTempFile("utplsql_", ".html");
logger.fine(() -> "Writing result to " + file + "...");
FileTools.writeFile(file.toPath(), Arrays.asList(content.split(System.lineSeparator())), StandardCharsets.UTF_8);
final URL url = file.toURI().toURL();
logger.fine(() -> "Opening " + url.toExternalForm() + " in browser...");
final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
Expand All @@ -97,21 +179,12 @@ private void run() {
logger.fine(() -> url.toExternalForm() + " opened in browser.");
} else {
logger.severe(
() -> "Could not launch " + file + "in browser. No default browser defined on this system.");
}
} catch (Exception e) {
final String msg = "Error while running code coverage for " + pathList + ".";
logger.severe(() -> msg);
throw new GenericRuntimeException(msg, e);
} finally {
try {
DatabaseTools.closeConnection(conn);
} catch (GenericDatabaseAccessException e) {
// ignore
}
if (frame != null) {
frame.exit();
}
}
}

Expand Down Expand Up @@ -143,6 +216,10 @@ public void setSchemas(final String schemas) {
this.schemas = schemas;
}

public void setIncludeObjects(final String includeObjects) {
this.includeObjects = includeObjects;
}
Expand Down
102 changes: 89 additions & 13 deletions sqldev/src/main/java/org/utplsql/sqldev/dal/RealtimeReporterDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class RealtimeReporterDao {
public RealtimeReporterDao(final Connection conn) {
this.conn = conn;
jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(conn, true));
jdbcTemplate.setFetchSize(1);
}

public boolean isSupported() {
Expand Down @@ -93,6 +93,47 @@ public void produceReport(final String reporterId, final List<String> pathList)
jdbcTemplate.update(plsql, binds);
}

public void consumeReport(final String reporterId, final RealtimeReporterEventConsumer consumer) {
StringBuilder sb = new StringBuilder();
sb.append("DECLARE\n");
Expand All @@ -102,28 +143,63 @@ public void consumeReport(final String reporterId, final RealtimeReporterEventCo
sb.append(" ? := l_reporter.get_lines_cursor();\n");
sb.append("END;");
final String plsql = sb.toString();
jdbcTemplate.execute(plsql, new CallableStatementCallback<Void>() {
@Override
public Void doInCallableStatement(final CallableStatement cs) throws SQLException {
cs.setString(1, reporterId);
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
final ResultSet rs = ((ResultSet) cs.getObject(2));
while (rs.next()) {
final String itemType = rs.getString("item_type");
final Clob textClob = rs.getClob("text");
final String textString = textClob.getSubString(1, ((int) textClob.length()));
final RealtimeReporterEvent event = convert(itemType, textString);
if (event != null) {
consumer.process(event);
}
}
rs.close();
return null;
}
});
}

private RealtimeReporterEvent convert(final String itemType, final String text) {
logger.fine(() -> "\n---- " + itemType + " ----\n" + text);
try {
Expand Down
8 changes: 5 additions & 3 deletions sqldev/src/main/java/org/utplsql/sqldev/dal/UtplsqlDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class UtplsqlDao {
public static final int FIRST_VERSION_WITH_ANNOTATION_API = 3001003;
public static final int FIRST_VERSION_WITHOUT_INTERNAL_API = 3001008;
public static final int FIRST_VERSION_WITH_HAS_SUITES_API = 3001008;
private JdbcTemplate jdbcTemplate;
// cache fields
private Boolean cachedDbaViewAccessible;
Expand All @@ -50,6 +51,7 @@ public class UtplsqlDao {

public UtplsqlDao(final Connection conn) {
jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(conn, true));
}

/**
Expand Down Expand Up @@ -918,17 +920,17 @@ public String htmlCodeCoverage(final List<String> pathList, final List<String> s
sb.append(" a_paths => ut_varchar2_list(\n");
sb.append(StringTools.getCSV(pathList, 16));
sb.append(" ),\n");
if (!schemaList.isEmpty()) {
sb.append(" a_coverage_schemes => ut_varchar2_list(\n");
sb.append(StringTools.getCSV(schemaList, 16));
sb.append(" ),\n");
}
if (!includeObjectList.isEmpty()) {
sb.append(" a_include_objects => ut_varchar2_list(\n");
sb.append(StringTools.getCSV(includeObjectList, 16));
sb.append(" ),\n");
}
if (!excludeObjectList.isEmpty()) {
sb.append(" a_exclude_objects => ut_varchar2_list(\n");
sb.append(StringTools.getCSV(excludeObjectList, 16));
sb.append(" ),\n");
Expand Down
Loading
Toggle all file notes Toggle all file annotations