1
+ package org.utplsql.sqldev
2
+
3
+ import java.awt.Desktop
4
+ import java.io.File
5
+ import java.net.URL
6
+ import java.nio.charset.StandardCharsets
7
+ import java.nio.file.Files
8
+ import java.nio.file.Paths
9
+ import java.sql.Connection
10
+ import java.util.List
11
+ import java.util.logging.Logger
12
+ import oracle.dbtools.raptor.utils.Connections
13
+ import org.utplsql.sqldev.dal.UtplsqlDao
14
+
15
+ class CodeCoverageReporter {
16
+ static val Logger logger = Logger . getLogger(CodeCoverageReporter . name);
17
+
18
+ var Connection conn
19
+ var List<String > pathList
20
+
21
+ new (List<String > pathList, String connectionName) {
22
+ this . pathList = pathList
23
+ setConnection(connectionName)
24
+ }
25
+
26
+ private def setConnection (String connectionName ) {
27
+ if (connectionName == = null ) {
28
+ throw new RuntimeException (" Cannot initialize a CodeCoverageReporter without a ConnectionName" )
29
+ } else {
30
+ // must be closed manually
31
+ this . conn = Connections . instance. cloneConnection(Connections . instance. getConnection(connectionName))
32
+ }
33
+ }
34
+
35
+ private def run () {
36
+ try {
37
+ logger. fine(' ' ' Running code coverage reporter for «pathList»...' ' ' )
38
+ val dal = new UtplsqlDao (conn)
39
+ val content = dal. htmlCodeCoverage(pathList)
40
+ val file = File . createTempFile(" utplsql_" , " html" )
41
+ logger. fine(' ' ' Writing result to «file.absolutePath»...' ' ' )
42
+ Files . write(Paths . get(file. absolutePath), content. split(System . lineSeparator), StandardCharsets . UTF_8 );
43
+ val url = file. toURI(). toURL(). toExternalForm()
44
+ logger. fine(' ' ' Opening «url» in browser...' ' ' )
45
+ val Desktop desktop = if (Desktop . isDesktopSupported()) {Desktop . getDesktop()} else {null }
46
+ if (desktop !== null && desktop. isSupported(Desktop . Action . BROWSE ) && url !== null ) {
47
+ desktop. browse((new URL (url)). toURI)
48
+ logger. fine(url + " opened in browser." );
49
+ } else {
50
+ logger. severe(' ' ' Could not launch «file» in browser. No default browser defined on this system.' ' ' )
51
+ }
52
+ } catch (Exception e) {
53
+ logger. severe(' ' ' Error when running code coverage: «e?.message»' ' ' )
54
+ }
55
+ finally {
56
+ conn. close
57
+ }
58
+ }
59
+
60
+ def runAsync () {
61
+ val Runnable runnable = [|run]
62
+ val thread = new Thread (runnable)
63
+ thread. name = " code coverage reporter"
64
+ thread. start
65
+ }
66
+
67
+ }
0 commit comments