Skip to content

Commit 59b223d

Browse files
extend controller to support utplsql.coverage action
1 parent 17ce4c0 commit 59b223d

File tree

1 file changed

+84
-10
lines changed

1 file changed

+84
-10
lines changed

sqldev/src/main/java/org/utplsql/sqldev/menu/UtplsqlController.xtend

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ package org.utplsql.sqldev.menu
1616

1717
import java.net.URL
1818
import java.util.ArrayList
19+
import java.util.HashSet
20+
import java.util.List
1921
import java.util.logging.Logger
22+
import java.util.regex.Pattern
2023
import javax.swing.JEditorPane
2124
import oracle.dbtools.raptor.navigator.db.DBNavigatorWindow
2225
import oracle.dbtools.raptor.navigator.db.DatabaseConnection
@@ -33,6 +36,7 @@ import oracle.ide.config.Preferences
3336
import oracle.ide.controller.Controller
3437
import oracle.ide.controller.IdeAction
3538
import oracle.ide.editor.Editor
39+
import org.utplsql.sqldev.CodeCoverageReporter
3640
import org.utplsql.sqldev.UtplsqlWorksheet
3741
import org.utplsql.sqldev.dal.UtplsqlDao
3842
import org.utplsql.sqldev.model.URLTools
@@ -45,24 +49,29 @@ class UtplsqlController implements Controller {
4549
static final Logger logger = Logger.getLogger(UtplsqlController.name);
4650
val extension URLTools urlTools = new URLTools
4751

48-
public static int UTLPLSQL_TEST_CMD_ID = Ide.findCmdID("utplsql.test")
49-
public static int UTLPLSQL_GENERATE_CMD_ID = Ide.findCmdID("utplsql.generate")
50-
public static final IdeAction UTLPLSQL_TEST_ACTION = IdeAction.get(UtplsqlController.UTLPLSQL_TEST_CMD_ID)
51-
public static final IdeAction UTLPLSQL_GENERATE_ACTION = IdeAction.get(UtplsqlController.UTLPLSQL_GENERATE_CMD_ID)
52+
public static int UTPLSQL_TEST_CMD_ID = Ide.findCmdID("utplsql.test")
53+
public static int UTPLSQL_COVERAGE_CMD_ID = Ide.findCmdID("utplsql.coverage")
54+
public static int UTPLSQL_GENERATE_CMD_ID = Ide.findCmdID("utplsql.generate")
55+
public static final IdeAction UTPLSQL_TEST_ACTION = IdeAction.get(UtplsqlController.UTPLSQL_TEST_CMD_ID)
56+
public static final IdeAction UTPLSQL_COVERAGE_ACTION = IdeAction.get(UtplsqlController.UTPLSQL_COVERAGE_CMD_ID)
57+
public static final IdeAction UTPLSQL_GENERATE_ACTION = IdeAction.get(UtplsqlController.UTPLSQL_GENERATE_CMD_ID)
5258

5359
override handleEvent(IdeAction action, Context context) {
54-
if (action.commandId === UtplsqlController.UTLPLSQL_TEST_CMD_ID) {
60+
if (action.commandId === UTPLSQL_TEST_CMD_ID) {
5561
runTest(context)
5662
return true
57-
} else if (action.commandId === UtplsqlController.UTLPLSQL_GENERATE_CMD_ID) {
63+
} else if (action.commandId === UTPLSQL_COVERAGE_CMD_ID) {
64+
codeCoverage(context)
65+
return true
66+
} else if (action.commandId === UTPLSQL_GENERATE_CMD_ID) {
5867
generateTest(context)
5968
return true
6069
}
6170
return false
6271
}
6372

6473
override update(IdeAction action, Context context) {
65-
if (action.commandId === UTLPLSQL_TEST_CMD_ID) {
74+
if (action.commandId === UTPLSQL_TEST_CMD_ID || action.commandId === UTPLSQL_COVERAGE_CMD_ID) {
6675
val preferences = PreferenceModel.getInstance(Preferences.preferences)
6776
action.enabled = false
6877
val view = context.view
@@ -117,7 +126,7 @@ class UtplsqlController implements Controller {
117126
}
118127
}
119128
return true
120-
} else if (action.commandId === UTLPLSQL_GENERATE_CMD_ID) {
129+
} else if (action.commandId === UTPLSQL_GENERATE_CMD_ID) {
121130
action.enabled = false
122131
// enable if generation is possible
123132
val view = context.view
@@ -175,6 +184,36 @@ class UtplsqlController implements Controller {
175184
}
176185
return pathList
177186
}
187+
188+
private def getPathList(String path) {
189+
val pathList = new ArrayList<String>
190+
pathList.add(path)
191+
return pathList
192+
}
193+
194+
private def dedupPathList(List<String> pathList) {
195+
val set = new HashSet<String>
196+
for (path : pathList) {
197+
set.add(path)
198+
}
199+
val ret = new ArrayList<String>
200+
val p = Pattern.compile("((((\\w+)\\.)?\\w+)\\.)?\\w+")
201+
for (path : set) {
202+
val m = p.matcher(path)
203+
if (m.matches()) {
204+
val parent1 = m.group(4) // user
205+
val parent2 = m.group(2) // user.package
206+
if (parent1 === null || !set.contains(parent1)) {
207+
if (parent2 === null || !set.contains(parent2)) {
208+
ret.add(path)
209+
}
210+
}
211+
} else {
212+
logger.severe('''path: «path» did not match «p.toString», this is unexected!''')
213+
}
214+
}
215+
return ret
216+
}
178217

179218
private def getURL(Context context) {
180219
var URL url
@@ -242,20 +281,55 @@ class UtplsqlController implements Controller {
242281
val parser = new UtplsqlParser(component.text, if (preferences.checkRunUtplsqlTest) {Connections.instance.getConnection(connectionName)} else {null}, owner)
243282
val position = component.caretPosition
244283
val path = parser.getPathAt(position)
245-
val utPlsqlWorksheet = new UtplsqlWorksheet(path, connectionName)
284+
val utPlsqlWorksheet = new UtplsqlWorksheet(path.pathList, connectionName)
246285
utPlsqlWorksheet.runTestAsync
247286
}
248287
} else if (view instanceof DBNavigatorWindow) {
249288
val url=context.URL
250289
if (url !== null) {
251290
val connectionName = url.connectionName
252291
logger.fine('''connectionName: «connectionName»''')
253-
val pathList=context.pathList
292+
val pathList=context.pathList.dedupPathList
254293
val utPlsqlWorksheet = new UtplsqlWorksheet(pathList, connectionName)
255294
utPlsqlWorksheet.runTestAsync
256295
}
257296
}
258297
}
298+
299+
def codeCoverage(Context context) {
300+
val view = context.view
301+
val node = context.node
302+
logger.finer('''Code coverage from view «view?.class?.name» and node «node?.class?.name».''')
303+
if (view instanceof Editor) {
304+
val component = view.defaultFocusComponent
305+
if (component instanceof JEditorPane) {
306+
var String connectionName = null;
307+
var String owner = null;
308+
if (node instanceof DatabaseSourceNode) {
309+
connectionName = node.connectionName
310+
owner = node.owner
311+
} else if (view instanceof Worksheet) {
312+
connectionName = view.connectionName
313+
}
314+
logger.fine('''connectionName: «connectionName»''')
315+
val preferences = PreferenceModel.getInstance(Preferences.preferences)
316+
val parser = new UtplsqlParser(component.text, if (preferences.checkRunUtplsqlTest) {Connections.instance.getConnection(connectionName)} else {null}, owner)
317+
val position = component.caretPosition
318+
val path = parser.getPathAt(position)
319+
val reporter = new CodeCoverageReporter(path.pathList, connectionName)
320+
reporter.runAsync
321+
}
322+
} else if (view instanceof DBNavigatorWindow) {
323+
val url=context.URL
324+
if (url !== null) {
325+
val connectionName = url.connectionName
326+
logger.fine('''connectionName: «connectionName»''')
327+
val pathList=context.pathList.dedupPathList
328+
val reporter = new CodeCoverageReporter(pathList, connectionName)
329+
reporter.runAsync
330+
}
331+
}
332+
}
259333

260334
def generateTest(Context context) {
261335
val view = context.view

0 commit comments

Comments
 (0)