@@ -37,6 +37,8 @@ import org.utplsql.sqldev.UtplsqlWorksheet
3737import org.utplsql.sqldev.dal.UtplsqlDao
3838import org.utplsql.sqldev.model.URLTools
3939import org.utplsql.sqldev.model.preference.PreferenceModel
40+ import org.utplsql.sqldev.oddgen.TestTemplate
41+ import org.utplsql.sqldev.oddgen.model.GenContext
4042import org.utplsql.sqldev.parser.UtplsqlParser
4143
4244class UtplsqlController implements Controller {
@@ -52,6 +54,9 @@ class UtplsqlController implements Controller {
5254 if (action. commandId == = UtplsqlController . UTLPLSQL_TEST_CMD_ID ) {
5355 runTest(context)
5456 return true
57+ } else if (action. commandId == = UtplsqlController . UTLPLSQL_GENERATE_CMD_ID ) {
58+ generateTest(context)
59+ return true
5560 }
5661 return false
5762 }
@@ -188,6 +193,36 @@ class UtplsqlController implements Controller {
188193 logger. fine(' ' ' url: «url»' ' ' )
189194 return url
190195 }
196+
197+ private def void populateGenContext (GenContext genContext , PreferenceModel preferences ) {
198+ genContext. generateFiles = preferences. generateFiles
199+ genContext. outputDirectory = preferences. outputDirectory
200+ genContext. testPackagePrefix = preferences. testPackagePrefix. toLowerCase
201+ genContext. testPackageSuffix = preferences. testPackageSuffix. toLowerCase
202+ genContext. testUnitPrefix = preferences. testUnitPrefix. toLowerCase
203+ genContext. testUnitSuffix = preferences. testUnitSuffix. toLowerCase
204+ genContext. numberOfTestsPerUnit = preferences. numberOfTestsPerUnit
205+ genContext. generateComments = preferences. generateComments
206+ genContext. disableTests = preferences. disableTests
207+ genContext. suitePath = preferences. suitePath. toLowerCase
208+ genContext. indentSpaces = preferences. indentSpaces
209+ }
210+
211+ private def getGenContext (Context context ) {
212+ val connectionName = context. URL . connectionName
213+ val genContext = new GenContext
214+ if (Connections . instance. isConnectionOpen(connectionName)) {
215+ genContext. conn = Connections . instance. getConnection(connectionName)
216+ val element = context. selection. get(0 )
217+ if (element instanceof PlSqlNode ) {
218+ genContext. objectType = element. objectType. replace(" BODY" , " " )
219+ genContext. objectName = element. objectName
220+ val preferences = PreferenceModel . getInstance(Preferences . preferences)
221+ populateGenContext(genContext, preferences)
222+ }
223+ }
224+ return genContext
225+ }
191226
192227 def runTest (Context context ) {
193228 val view = context. view
@@ -223,4 +258,48 @@ class UtplsqlController implements Controller {
223258 }
224259 }
225260 }
261+
262+ def generateTest (Context context ) {
263+ val view = context. view
264+ val node = context. node
265+ logger. finer(' ' ' Generate utPLSQL test from view «view?.class?.name» and node «node?.class?.name».' ' ' )
266+ if (view instanceof Editor ) {
267+ val component = view. defaultFocusComponent
268+ if (component instanceof JEditorPane ) {
269+ var String connectionName = null ;
270+ if (node instanceof DatabaseSourceNode ) {
271+ connectionName = node. connectionName
272+ } else if (view instanceof Worksheet ) {
273+ connectionName = view. connectionName
274+ }
275+ if (connectionName !== null ) {
276+ if (Connections . instance. isConnectionOpen(connectionName)) {
277+ val genContext = new GenContext
278+ genContext. conn = Connections . instance. getConnection(connectionName)
279+ val parser = new UtplsqlParser (component. text)
280+ val position = component. caretPosition
281+ val obj = parser. getObjectAt(position)
282+ if (obj !== null ) {
283+ genContext. objectType = obj. type. toUpperCase
284+ genContext. objectName = obj. name. toUpperCase
285+ val preferences = PreferenceModel . getInstance(Preferences . preferences)
286+ populateGenContext(genContext, preferences)
287+ val testTemplate = new TestTemplate (genContext)
288+ val code = testTemplate. generate. toString
289+ UtplsqlWorksheet . openWithCode(code, connectionName)
290+ }
291+ }
292+ }
293+ }
294+
295+ } else if (view instanceof DBNavigatorWindow ) {
296+ val url= context. URL
297+ if (url !== null ) {
298+ val connectionName = url. connectionName
299+ val testTemplate = new TestTemplate (context. genContext)
300+ val code = testTemplate. generate. toString
301+ UtplsqlWorksheet . openWithCode(code, connectionName)
302+ }
303+ }
304+ }
226305}
0 commit comments