#102 Run tests with code coverage by PhilippSalvisberg · Pull Request #105 · utPLSQL/utPLSQL-SQLDeveloper
import org.utplsql.sqldev.dal.RealtimeReporterDao; 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.model.preference.PreferenceModel; import org.utplsql.sqldev.runner.UtplsqlRunner; import org.utplsql.sqldev.ui.coverage.CodeCoverageReporterDialog;
import oracle.ide.config.Preferences;
public class CodeCoverageReporter { private static final Logger logger = Logger.getLogger(CodeCoverageReporter.class.getName());
private String connectionName; private Connection conn; private List<String> pathList; private List<String> includeObjectList;
// constructor for testing purposes only public CodeCoverageReporter(final List<String> pathList, final List<String> includeObjectList, final Connection conn) { this.pathList = pathList; this.includeObjectList = includeObjectList; this.conn = conn; setDefaultSchema(); }
private void setConnection(final String connectionName) {
private void setDefaultSchema() { if (includeObjectList != null && !includeObjectList.isEmpty()) { // use the owner with the most hits in includeObjectList HashMap<String, Integer> owners = new HashMap<>(); for (String entry : includeObjectList) { String[] obj = entry.toUpperCase().split("\\."); if (obj.length == 2) { // only if objectOwner and objectName are available Integer count = owners.get(obj[0]); if (count == null) { count = 1; } else { count++; } owners.put(obj[0], count); } } List<String> sortedOwners = owners.entrySet().stream() .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).map(Map.Entry::getKey) .collect(Collectors.toList()); schemas = String.join(", ", sortedOwners); } }
private void runCodeCoverageWithRealtimeReporter() { final UtplsqlRunner runner = new UtplsqlRunner(pathList, toStringList(schemas), toStringList(includeObjects), toStringList(excludeObjects), connectionName); runner.runTestAsync(); }
private void runCodeCoverageStandalone() { Connection coverageConn = null; try { coverageConn = conn != null ? conn : DatabaseTools.cloneConnection(connectionName); final UtplsqlDao dao = new UtplsqlDao(coverageConn); final String html = dao.htmlCodeCoverage(pathList, toStringList(schemas), toStringList(includeObjects), toStringList(excludeObjects)); openInBrowser(html); } finally { try { if (coverageConn != null && conn == null) { // close only if connection has been cloned DatabaseTools.closeConnection(coverageConn); } } catch (GenericDatabaseAccessException e) { // ignore } } }
public static void openInBrowser(String html) { try { 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); FileTools.writeFile(file.toPath(), Arrays.asList(html.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;
public String getSchemas() { return schemas; }
public void setIncludeObjects(final String includeObjects) { this.includeObjects = includeObjects; }